samagotchi 0.141.0 → 0.143.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 (165) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +111 -7
  3. data/README.md +2 -1
  4. data/bin/chi +2 -0
  5. data/docs/architecture.md +25 -289
  6. data/docs/bundle-authoring.md +7 -6
  7. data/docs/bundles.md +1232 -0
  8. data/docs/cli.md +51 -13
  9. data/docs/configuration.md +68 -30
  10. data/docs/context.md +12 -12
  11. data/docs/guardrails.md +27 -20
  12. data/docs/hooks.md +28 -9
  13. data/docs/internals/attached-context.md +225 -0
  14. data/docs/internals/background-tasks.md +1 -1
  15. data/docs/internals/bridge.md +380 -0
  16. data/docs/internals/bundles.md +6 -3
  17. data/docs/internals/context-telemetry.md +1 -1
  18. data/docs/internals/engine.md +31 -19
  19. data/docs/internals/participants.md +13 -12
  20. data/docs/internals/prompt-caching.md +12 -12
  21. data/docs/internals/remote-sessions.md +1 -1
  22. data/docs/internals/{tool-guardrails.md → tool-output-limits.md} +21 -2
  23. data/docs/internals/web-push.md +1 -1
  24. data/docs/memory.md +33 -3
  25. data/docs/plugins.md +127 -1231
  26. data/docs/sessions.md +51 -27
  27. data/docs/sub-agent.md +6 -6
  28. data/docs/system-one.md +122 -0
  29. data/docs/testing.md +3 -1
  30. data/lib/samagotchi/bridge.rb +2 -0
  31. data/lib/samagotchi/bundle_command/api_calls.rb +132 -0
  32. data/lib/samagotchi/bundle_command/check.rb +50 -34
  33. data/lib/samagotchi/bundle_command/installed_check.rb +86 -0
  34. data/lib/samagotchi/bundle_command/scaffold.rb +1 -0
  35. data/lib/samagotchi/bundle_settings_check.rb +58 -0
  36. data/lib/samagotchi/bundles/agents-md/manifest.yml +4 -2
  37. data/lib/samagotchi/bundles/agents-md/plugin.rb +1 -1
  38. data/lib/samagotchi/bundles/btw/manifest.yml +4 -2
  39. data/lib/samagotchi/bundles/btw/plugin.rb +1 -1
  40. data/lib/samagotchi/bundles/check-in/manifest.yml +7 -3
  41. data/lib/samagotchi/bundles/check-in/plugin.rb +101 -11
  42. data/lib/samagotchi/bundles/claude-peer/manifest.yml +3 -3
  43. data/lib/samagotchi/bundles/claude-peer/plugin.rb +1 -1
  44. data/lib/samagotchi/bundles/claude-peer/scripts/pair +1 -1
  45. data/lib/samagotchi/bundles/codex-peer/manifest.yml +2 -2
  46. data/lib/samagotchi/bundles/codex-peer/plugin.rb +1 -1
  47. data/lib/samagotchi/bundles/coordinator/manifest.yml +2 -2
  48. data/lib/samagotchi/bundles/coordinator/plugin.rb +1 -1
  49. data/lib/samagotchi/bundles/core/manifest.yml +2 -2
  50. data/lib/samagotchi/bundles/cost-cap/manifest.yml +6 -3
  51. data/lib/samagotchi/bundles/cost-cap/plugin.rb +5 -7
  52. data/lib/samagotchi/bundles/github-pr/manifest.yml +3 -1
  53. data/lib/samagotchi/bundles/known-names/manifest.yml +3 -1
  54. data/lib/samagotchi/bundles/loop-guard/manifest.yml +4 -2
  55. data/lib/samagotchi/bundles/loop-guard/plugin.rb +1 -1
  56. data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
  57. data/lib/samagotchi/bundles/mcp/plugin.rb +2 -2
  58. data/lib/samagotchi/bundles/ruby/manifest.yml +2 -2
  59. data/lib/samagotchi/bundles/ruby/plugin.rb +1 -1
  60. data/lib/samagotchi/bundles/skills/manifest.yml +4 -2
  61. data/lib/samagotchi/bundles/skills/plugin.rb +1 -1
  62. data/lib/samagotchi/bundles/slow-commands/manifest.yml +4 -2
  63. data/lib/samagotchi/bundles/slow-commands/plugin.rb +1 -1
  64. data/lib/samagotchi/bundles/source-links/manifest.yml +3 -1
  65. data/lib/samagotchi/bundles/system/config_modification_protocol.md +3 -3
  66. data/lib/samagotchi/bundles/system/manifest.yml +4 -4
  67. data/lib/samagotchi/bundles/system/memory_guide.md +1 -1
  68. data/lib/samagotchi/bundles/system/self_map.md +3 -3
  69. data/lib/samagotchi/bundles/work-cap/manifest.yml +6 -3
  70. data/lib/samagotchi/bundles/work-cap/plugin.rb +11 -13
  71. data/lib/samagotchi/config.rb +74 -14
  72. data/lib/samagotchi/context_candidate_choice.rb +20 -3
  73. data/lib/samagotchi/context_candidates.rb +4 -1
  74. data/lib/samagotchi/context_candidates_log.rb +36 -1
  75. data/lib/samagotchi/context_judge.rb +6 -2
  76. data/lib/samagotchi/context_status.rb +1 -2
  77. data/lib/samagotchi/engine.rb +96 -70
  78. data/lib/samagotchi/extension_load.rb +11 -4
  79. data/lib/samagotchi/guardrail_wiring.rb +36 -31
  80. data/lib/samagotchi/guardrails/chi_dirs.rb +7 -4
  81. data/lib/samagotchi/guardrails/path_resolve.rb +1 -1
  82. data/lib/samagotchi/guardrails/protected_paths.rb +7 -4
  83. data/lib/samagotchi/guardrails_command.rb +3 -3
  84. data/lib/samagotchi/hook_routes.rb +37 -2
  85. data/lib/samagotchi/hooks/bundle_loader.rb +20 -4
  86. data/lib/samagotchi/hooks/registry.rb +23 -5
  87. data/lib/samagotchi/host_model.rb +3 -3
  88. data/lib/samagotchi/host_registry.rb +14 -7
  89. data/lib/samagotchi/judgments_command.rb +155 -0
  90. data/lib/samagotchi/kernel_loop.rb +20 -3
  91. data/lib/samagotchi/llm/chat_loop.rb +79 -10
  92. data/lib/samagotchi/llm/errors.rb +7 -5
  93. data/lib/samagotchi/llm/stray_tool_call.rb +68 -0
  94. data/lib/samagotchi/log_line.rb +1 -1
  95. data/lib/samagotchi/memory_bundle/builder.rb +4 -2
  96. data/lib/samagotchi/memory_bundle/entry_size.rb +100 -0
  97. data/lib/samagotchi/memory_bundle/installed_bundle.rb +9 -2
  98. data/lib/samagotchi/memory_bundle/installed_copy.rb +92 -0
  99. data/lib/samagotchi/memory_bundle/installer.rb +2 -0
  100. data/lib/samagotchi/memory_bundle/manifest.rb +23 -13
  101. data/lib/samagotchi/memory_bundle/provenance.rb +4 -1
  102. data/lib/samagotchi/memory_bundle/shipped_update.rb +13 -1
  103. data/lib/samagotchi/model_catalog.rb +3 -2
  104. data/lib/samagotchi/models_command.rb +1 -1
  105. data/lib/samagotchi/plugin/api.rb +41 -7
  106. data/lib/samagotchi/plugin/context.rb +59 -1
  107. data/lib/samagotchi/plugin/judgments.rb +72 -0
  108. data/lib/samagotchi/plugin/loader.rb +6 -3
  109. data/lib/samagotchi/plugin/peer.rb +17 -22
  110. data/lib/samagotchi/plugin_session_state.rb +32 -5
  111. data/lib/samagotchi/prompt_warmup.rb +1 -9
  112. data/lib/samagotchi/run_options.rb +1 -0
  113. data/lib/samagotchi/self_report.rb +36 -0
  114. data/lib/samagotchi/session_access/contract.rb +1 -1
  115. data/lib/samagotchi/session_access/local.rb +1 -1
  116. data/lib/samagotchi/session_access/remote.rb +3 -3
  117. data/lib/samagotchi/session_archive_command.rb +3 -3
  118. data/lib/samagotchi/session_commands.rb +7 -4
  119. data/lib/samagotchi/session_config.rb +9 -3
  120. data/lib/samagotchi/session_continue.rb +1 -1
  121. data/lib/samagotchi/session_manager.rb +25 -5
  122. data/lib/samagotchi/session_retention.rb +10 -0
  123. data/lib/samagotchi/side_judgments.rb +12 -1
  124. data/lib/samagotchi/system_one/answer.rb +63 -11
  125. data/lib/samagotchi/system_one/approval_label.rb +56 -0
  126. data/lib/samagotchi/system_one/approval_question.rb +2 -0
  127. data/lib/samagotchi/system_one/client.rb +43 -17
  128. data/lib/samagotchi/system_one/fake_backend.rb +1 -1
  129. data/lib/samagotchi/system_one/http_backend.rb +12 -4
  130. data/lib/samagotchi/system_one/judgment_settings.rb +61 -0
  131. data/lib/samagotchi/system_one/judgments.rb +243 -0
  132. data/lib/samagotchi/system_one/log.rb +25 -2
  133. data/lib/samagotchi/system_one/recording.rb +117 -0
  134. data/lib/samagotchi/system_one/runs.rb +179 -0
  135. data/lib/samagotchi/system_one/runs_store.rb +230 -0
  136. data/lib/samagotchi/system_one.rb +9 -0
  137. data/lib/samagotchi/terminal_ui/attach_link.rb +2 -2
  138. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -1
  139. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  140. data/lib/samagotchi/terminal_ui/local_link.rb +1 -1
  141. data/lib/samagotchi/terminal_ui/remote_link.rb +1 -2
  142. data/lib/samagotchi/testing/bundle.rb +9 -2
  143. data/lib/samagotchi/testing/host.rb +2 -1
  144. data/lib/samagotchi/testing/judgments.rb +176 -0
  145. data/lib/samagotchi/testing/recorder.rb +5 -0
  146. data/lib/samagotchi/testing/recording_api.rb +1 -1
  147. data/lib/samagotchi/tool_output_cut.rb +26 -6
  148. data/lib/samagotchi/tool_response.rb +1 -1
  149. data/lib/samagotchi/tool_runner.rb +63 -4
  150. data/lib/samagotchi/toolkit_suggest.rb +11 -2
  151. data/lib/samagotchi/tools/memory.rb +28 -0
  152. data/lib/samagotchi/turn_note.rb +9 -0
  153. data/lib/samagotchi/update_command.rb +49 -16
  154. data/lib/samagotchi/version.rb +1 -1
  155. data/lib/samagotchi/web/app.rb +1 -0
  156. data/lib/samagotchi/web/auto_update.rb +79 -49
  157. data/lib/samagotchi/web/auto_update_record.rb +59 -0
  158. data/lib/samagotchi/web/public/app.js +8 -4
  159. data/lib/samagotchi/web/public/hash_intent.js +24 -0
  160. data/lib/samagotchi/web/public/index.html +3 -1
  161. data/lib/samagotchi/web/public/revise_stack.js +14 -5
  162. data/lib/samagotchi/web/session_control.rb +3 -3
  163. data/lib/samagotchi/web/update_check.rb +7 -6
  164. data/lib/samagotchi/web/update_ops.rb +80 -25
  165. metadata +23 -2
@@ -34,6 +34,7 @@ require_relative "peer_sources"
34
34
  require_relative "session_commands"
35
35
  require_relative "plugin/loader"
36
36
  require_relative "plugin/card"
37
+ require_relative "bundle_settings_check"
37
38
  require_relative "extension_load"
38
39
  require_relative "log"
39
40
  require_relative "log_subscriber"
@@ -169,7 +170,7 @@ module Samagotchi
169
170
  @chat_backend = nil
170
171
  @chat_backend_mutex = Mutex.new
171
172
  @side_judgments = SideJudgments.new
172
- @context_judge_after_turn = nil
173
+ @judgments = build_judgments
173
174
  # On mode's cards an answer brought while a turn ran: shown once it
174
175
  # has ended (#show_context_cards_after_turn).
175
176
  @context_cards_after_turn = []
@@ -295,7 +296,12 @@ module Samagotchi
295
296
  # toolkit, the tool index, peer sources), then the plugins themselves
296
297
  # unless +load+ is false (see #initialize for when).
297
298
  def build_tools_and_plugins(load:, task_finish_notes:)
298
- @extension_load = ExtensionLoad.new(hook_failures: @guardrail_failures)
299
+ # The routes are built already (#initialize), so a bundle's hook file
300
+ # that raises at fire time is told the same way a chi.on handler is:
301
+ # once per hook in this worker. The guard covers a hook that raises
302
+ # before the routes are there (nothing in the Engine's path does).
303
+ @extension_load = ExtensionLoad.new(hook_failures: @guardrail_failures,
304
+ hook_failed: ->(hook, failed, effect: nil) { @hook_routes&.plugin_hook_failed(hook, failed, effect: effect) })
299
305
  @hooks = @extension_load.hooks
300
306
  # The tools this session offers (the prompts' declarations and the
301
307
  # kernel's dispatch): the built-ins, per Engine. execute.description
@@ -431,7 +437,11 @@ module Samagotchi
431
437
  # System 1 (experimental, audit phase): the judgment client is read
432
438
  # from config on every ask, and the running turn's cancel controller
433
439
  # closes a judgment that is no longer wanted.
434
- system_one: -> { SystemOne::Client.from_config(host_registry: @host_registry) },
440
+ system_one: lambda {
441
+ system_one_recording(SystemOne::Client.from_config(host_registry: @host_registry, mode: Config.get("guardrails.system_one.mode")),
442
+ SystemOne::ApprovalQuestion::JUDGMENT, @session)
443
+ },
444
+ system_one_runs: -> { system_one_runs },
435
445
  cancel_controller: -> { active_cancel_controller },
436
446
  # A participant's turn: their name on the owner's card, their
437
447
  # grant's mode for the turn's (GuardrailWiring#turn_mode).
@@ -1544,9 +1554,11 @@ module Samagotchi
1544
1554
  @guardrail_failures.message if @guardrail_failures_announced
1545
1555
  end
1546
1556
 
1547
- # The plugins' load warning the first turn announced, likewise.
1557
+ # The plugins' load warning the first turn announced, likewise, and what
1558
+ # their failed chi.on handlers told (HookRoutes#plugin_hook_failed).
1548
1559
  def plugin_warning
1549
- @plugin_failures.message if @guardrail_failures_announced
1560
+ loaded = @plugin_failures.message if @guardrail_failures_announced
1561
+ [loaded, @hook_routes.hook_failures_message].compact.join("; ").then { |text| text unless text.empty? }
1550
1562
  end
1551
1563
 
1552
1564
  # Ask the user to approve a call the gate voted `ask` on
@@ -2316,10 +2328,7 @@ module Samagotchi
2316
2328
  pending_input: pending_input)
2317
2329
  publish_used_memories(session, on_event)
2318
2330
  complete_turn(turn, result)
2319
- # The candidate judge's asks a shared local server kept for the
2320
- # turn's end go first; the warm-up follows them and re-warms the
2321
- # prefix they evicted.
2322
- warm_up_next_turn(turn, result, after: run_context_judge_after_turn)
2331
+ warm_up_next_turn(turn, result)
2323
2332
  result
2324
2333
  rescue Interrupt => e
2325
2334
  # After the end event (in the after_turn/session_end hooks) the
@@ -2347,7 +2356,6 @@ module Samagotchi
2347
2356
  ensure
2348
2357
  release_turn(probe_cancel_before)
2349
2358
  show_context_cards_after_turn
2350
- run_context_judge_after_turn
2351
2359
  end
2352
2360
  end
2353
2361
 
@@ -2448,8 +2456,7 @@ module Samagotchi
2448
2456
  # On mode: the suggested kit, before anything is declared (a Stop
2449
2457
  # during the wait is caught by the check below).
2450
2458
  settle_toolkit_suggestion(turn, toolkit_suggestion)
2451
- # The candidate judge: its asks run beside the turn on a remote
2452
- # System 1 host, after it on the model's own local one; on mode's
2459
+ # The candidate judge: its asks run beside the turn; on mode's
2453
2460
  # attaches reach the model at the next between-turn delivery.
2454
2461
  start_context_judge(turn)
2455
2462
  apply_toolkit!(session, turn.on_event)
@@ -2529,7 +2536,7 @@ module Samagotchi
2529
2536
  # start differs anyway or starts at once: a reminder due, a continue
2530
2537
  # offer (the turn ran out of steps), a carried steer or queued input.
2531
2538
  # Never fails the turn.
2532
- def warm_up_next_turn(turn, result, after: nil)
2539
+ def warm_up_next_turn(turn, result)
2533
2540
  return if result.canceled? || result.resumable?
2534
2541
 
2535
2542
  model = effective_model
@@ -2544,7 +2551,7 @@ module Samagotchi
2544
2551
  return unless prompt
2545
2552
 
2546
2553
  started = @prompt_warmup.start(client: client, prompt: prompt, model: @kernel.turn_settings.model_name,
2547
- slot: @kernel.last_slot, images: images, after: after)
2554
+ slot: @kernel.last_slot, images: images)
2548
2555
  @kernel.warmed_up! if started
2549
2556
  rescue StandardError => e
2550
2557
  Log.warn(:model, "warmup_skipped", error: e.class.name, msg: e.message.to_s[0, 200])
@@ -2924,13 +2931,38 @@ module Samagotchi
2924
2931
  tool_index: @tool_index,
2925
2932
  peer_sources: @peer_sources,
2926
2933
  prompt_sections: @prompt_sections,
2927
- prompt_changed: -> { prompt_changed! }
2934
+ prompt_changed: -> { prompt_changed! },
2935
+ judgments: @judgments,
2936
+ hook_failed: ->(hook, failed) { @hook_routes.plugin_hook_failed(hook, failed) }
2928
2937
  )
2929
2938
  # What plugins show while they load (an MCP server that didn't
2930
2939
  # start) waits for the first turn, beside the load warnings: no UI
2931
2940
  # is there yet, and the Engine isn't built.
2932
- @extension_load.load_plugins(registries, failures: @plugin_failures)
2941
+ @extension_load.load_plugins(registries, failures: @plugin_failures) do
2942
+ warn_undeclared_judgments
2943
+ warn_bundle_settings
2944
+ end
2945
+ end
2946
+
2947
+ # config.yml bundles: names and keys that do nothing (BundleSettingsCheck),
2948
+ # held like the judgments' line, for the same reason.
2949
+ def warn_bundle_settings
2950
+ BundleSettingsCheck.warnings(@extension_load.bundle_settings).each do |line|
2951
+ Log.warn(:config, "bundle_setting_unknown", msg: line)
2952
+ hook_notify(line, :warn, "config")
2953
+ end
2954
+ end
2955
+ private :warn_bundle_settings
2956
+
2957
+ # Held with the plugins' own notices, so every UI gets it: a worker's
2958
+ # stderr goes nowhere.
2959
+ def warn_undeclared_judgments
2960
+ @judgments.undeclared.each do |name|
2961
+ Log.warn(:config, "judgment_undeclared", judgment: name)
2962
+ hook_notify("system_one.judgments.#{name}: no loaded bundle declares it, so it does nothing", :warn, "config")
2963
+ end
2933
2964
  end
2965
+ private :warn_undeclared_judgments
2934
2966
 
2935
2967
  # Keep a plugin's new tool set (chi.replace_tools, from any thread)
2936
2968
  # for the turn thread (PluginTasks#stage_tools).
@@ -3055,18 +3087,10 @@ module Samagotchi
3055
3087
  return
3056
3088
  end
3057
3089
 
3058
- client = SystemOne::Client.from_config(host_registry: @host_registry, mode: "on", tag: :toolkit)
3090
+ client = system_one_recording(SystemOne::Client.from_config(host_registry: @host_registry, mode: "on", tag: :toolkit),
3091
+ ToolkitSuggest::JUDGMENT, session, mode: Config.get("toolkit.suggest.mode").to_s)
3059
3092
  return unless client
3060
3093
 
3061
- # D7: on a shared single-slot server audit's free ask would compete
3062
- # with the turn's prefill, and any ask evicts the session's cached
3063
- # prefix: on mode asks only on a first turn there (nothing is cached
3064
- # yet worth keeping), never on the deferred path.
3065
- if (!joined || ToolkitSuggest.model_reply?(session.messages)) && system_one_shares_local_host?(client.host)
3066
- Log.info(:toolkit, "toolkit_suggest_shared_host_skipped", session_id: session.id, host: client.host)
3067
- return
3068
- end
3069
-
3070
3094
  # The warm-up's only value is the cache: the real ask never queues
3071
3095
  # behind it.
3072
3096
  stop_toolkit_warmup
@@ -3091,7 +3115,9 @@ module Samagotchi
3091
3115
  state: { project: state[:project].dup.freeze, prompt: state[:prompt].dup.freeze }.freeze
3092
3116
  )
3093
3117
  threshold = Config.get("toolkit.suggest.threshold")
3118
+ runs = session.scratch ? nil : system_one_runs
3094
3119
  pending = ToolkitSuggest::Pending.new(log: log, joined: joined,
3120
+ acted: runs && ->(judged, did) { runs.acted(judged.fields[:run_id], did) },
3095
3121
  timeout_seconds: SystemOne::Client.configured_timeout_ms / 1000.0)
3096
3122
  # On mode: cut by a Stop while the turn waits (Pending#wait), not
3097
3123
  # by one after it went on; audit's ask is never cut.
@@ -3124,7 +3150,7 @@ module Samagotchi
3124
3150
  # slow System 1 host (a local one) is the usual cause.
3125
3151
  timeout_ms = SystemOne::Client.configured_timeout_ms
3126
3152
  Log.warn(:toolkit, "toolkit_suggest_timeout", session_id: turn.session.id, timeout_ms: timeout_ms,
3127
- reason: "System 1 gave no answer within guardrails.system_one.timeout_ms")
3153
+ reason: "System 1 gave no answer within system_one.timeout_ms")
3128
3154
  @toolkit_binding.fell_back(turn.session, "System 1 took over #{timeout_ms} ms")
3129
3155
  return
3130
3156
  end
@@ -3183,9 +3209,6 @@ module Samagotchi
3183
3209
 
3184
3210
  client = SystemOne::Client.from_config(host_registry: @host_registry, mode: "on", tag: :toolkit)
3185
3211
  return unless client
3186
- # D7: on the model's own single-slot server it would evict the
3187
- # session's cached prefix for a question that may never come.
3188
- return if system_one_shares_local_host?(client.host)
3189
3212
 
3190
3213
  project = File.basename(session.project_root.to_s)
3191
3214
  @toolkit_warmup_thread = Thread.new do
@@ -3210,18 +3233,44 @@ module Samagotchi
3210
3233
  end
3211
3234
  private :toolkit_suggestion_log
3212
3235
 
3213
- # D7: System 1's host is the session's model server, and a local one
3214
- # (a single-slot Splash or llama.cpp): the same name, or two hosts:
3215
- # entries for one server root (scheme, host, port).
3216
- def system_one_shares_local_host?(judge_name)
3217
- model_host, = @host_registry.host_for_model(effective_model_name)
3218
- return false if model_host.nil? || model_host.remote?
3219
- return true if judge_name.to_s.strip.casecmp?(model_host.name.to_s)
3220
-
3221
- judge = @host_registry.find_entry(judge_name)
3222
- !judge.nil? && judge.root_url.downcase == model_host.root_url.downcase
3236
+ # +client+ (nil for none) saving its asks as +judgment+'s runs. Anything
3237
+ # going wrong here leaves the client as it was: a run is never worth a
3238
+ # judgment.
3239
+ #
3240
+ # A scratch session's asks aren't saved; a participant's turn is never
3241
+ # judged, so it never comes here.
3242
+ # @param mode [String, nil] the use's mode, for its runs (Recording.wrap)
3243
+ def system_one_recording(client, judgment, session, mode: nil)
3244
+ return client if client.nil? || session&.scratch
3245
+
3246
+ context = SystemOne::Recording::Context.of(session, session_model: session && effective_model_name)
3247
+ SystemOne::Recording.wrap(client, judgment: judgment, runs: system_one_runs, context: context, recorded_mode: mode)
3248
+ rescue StandardError => e
3249
+ Log.warn(:system_one, "system_one_recording_failed", error: e.class.name, msg: e.message.to_s[0, 200])
3250
+ client
3251
+ end
3252
+ private :system_one_recording
3253
+
3254
+ # Bundles' judgments (chi.judgment), on this Engine's Runs and side asks.
3255
+ # A judgment's own host and model are for bundles only: core's clients
3256
+ # above never pass them (D2, JudgmentSettings).
3257
+ def build_judgments
3258
+ SystemOne::Judgments.new(
3259
+ client_for: lambda { |_name, settings|
3260
+ SystemOne::Client.from_config(host_registry: @host_registry, mode: settings.mode, host: settings.host,
3261
+ model: settings.client_model, tag: :plugins)
3262
+ },
3263
+ runs: -> { @session&.scratch ? SystemOne::Runs.new : system_one_runs },
3264
+ where: -> { @session && SystemOne::Recording::Context.of(@session, session_model: effective_model_name) },
3265
+ side: @side_judgments,
3266
+ participant: -> { !turn_author.nil? }
3267
+ )
3223
3268
  end
3224
- private :system_one_shares_local_host?
3269
+ private :build_judgments
3270
+
3271
+ # Where this Engine's runs are saved; read each time, as the settings are.
3272
+ def system_one_runs = SystemOne::Runs.for_sessions_dir(session_state_dir)
3273
+ private :system_one_runs
3225
3274
 
3226
3275
  # The candidate judge (ContextJudge) at an owner turn's start, after
3227
3276
  # the toolkit's ask settled: the session's candidates' status changes
@@ -3250,10 +3299,7 @@ module Samagotchi
3250
3299
  private :judge_late_candidates
3251
3300
 
3252
3301
  # One look at +session+'s candidates; the asks go to SideJudgments'
3253
- # :context kind, one thread asking each due candidate in turn. On the
3254
- # model's own local server (#system_one_shares_local_host?) the asks
3255
- # wait for the running turn's end (#run_context_judge_after_turn),
3256
- # never competing with its prefill; any other host is asked now.
3302
+ # :context kind, one thread asking each due candidate in turn.
3257
3303
  def context_judge_pass(session, texts, phase:)
3258
3304
  return if session.id.nil? || session.scratch
3259
3305
 
@@ -3270,17 +3316,11 @@ module Samagotchi
3270
3316
  parent_attached: context_parent_attached(session))
3271
3317
  return if jobs.empty?
3272
3318
 
3273
- client = SystemOne::Client.from_config(host_registry: @host_registry, mode: "on", tag: :context)
3319
+ client = system_one_recording(SystemOne::Client.from_config(host_registry: @host_registry, mode: "on", tag: :context),
3320
+ ContextJudge::JUDGMENT, session, mode: settings.mode)
3274
3321
  return unless client
3275
3322
 
3276
- choice = context_choice(session, log, settings)
3277
- ask = -> { start_context_asks(client, jobs, log, settings, choice) }
3278
- if turn_running? && system_one_shares_local_host?(client.host)
3279
- Log.info(:context, "context_judge_after_turn", session_id: session.id, host: client.host)
3280
- @context_judge_after_turn = ask
3281
- else
3282
- ask.call
3283
- end
3323
+ start_context_asks(client, jobs, log, settings, context_choice(session, log, settings))
3284
3324
  rescue StandardError => e
3285
3325
  ContextJudge.warn(e)
3286
3326
  end
@@ -3338,21 +3378,6 @@ module Samagotchi
3338
3378
  end
3339
3379
  private :show_context_cards_after_turn
3340
3380
 
3341
- # The asks a shared local host kept for the turn's end: a completed
3342
- # turn starts them before its warm-up (which waits for them,
3343
- # PromptWarmup#start's after:); a turn that ended otherwise, from
3344
- # #run_turn's ensure.
3345
- # @return [Thread, nil] the asks' thread
3346
- def run_context_judge_after_turn
3347
- ask = @context_judge_after_turn or return nil
3348
- @context_judge_after_turn = nil
3349
- ask.call
3350
- rescue StandardError => e
3351
- ContextJudge.warn(e)
3352
- nil
3353
- end
3354
- private :run_context_judge_after_turn
3355
-
3356
3381
  # A delegate child's parent's sources, by title (else name): the
3357
3382
  # judge's started_from. nil for any other session (a chi send --new
3358
3383
  # session has no parent; a fork isn't a delegate).
@@ -3451,7 +3476,8 @@ module Samagotchi
3451
3476
  turn_author: -> { turn_author },
3452
3477
  # A plugin's prompt section is in the stable part: a full re-read.
3453
3478
  cache_confirm: ->(line, changed) { CacheConfirm.ask(line: line, cost: :full, changed: changed, engine: self) },
3454
- candidate_proposed: ->(_candidate) { judge_late_candidates }
3479
+ candidate_proposed: ->(_candidate) { judge_late_candidates },
3480
+ judgments: -> { @judgments }
3455
3481
  )
3456
3482
  end
3457
3483
 
@@ -13,8 +13,14 @@ module Samagotchi
13
13
  # turn (Engine#announce_load_events!).
14
14
  class ExtensionLoad
15
15
  # @param hook_failures [Guardrails::LoadFailures] where hooks that fail to load go
16
- def initialize(hook_failures:)
16
+ # @param hook_failed [#call, nil] (label, "Class: message", effect:) when
17
+ # an installed bundle's hook file raises at fire time: the Engine's
18
+ # HookRoutes, so the user is told once per hook. nil where there is no
19
+ # UI to tell (the test kit's sandbox): a hook that raises then says
20
+ # nothing, and raises nothing.
21
+ def initialize(hook_failures:, hook_failed: nil)
17
22
  @hook_failures = hook_failures
23
+ @hook_failed = hook_failed
18
24
  @loading = false
19
25
  @held_events = []
20
26
  end
@@ -43,11 +49,12 @@ module Samagotchi
43
49
  end
44
50
 
45
51
  # Load the installed bundles' plugins into +registries+; one that fails
46
- # goes to +failures+, and the rest still load.
52
+ # goes to +failures+, and the rest still load. The block runs after them,
53
+ # still loading: what it shows is held with theirs.
47
54
  def load_plugins(registries, failures:)
48
55
  @held_events = []
49
56
  @loading = true
50
- Plugin::Loader.load_installed(registries, failures: failures, settings: bundle_settings)
57
+ Plugin::Loader.load_installed(registries, failures: failures, settings: bundle_settings).tap { yield if block_given? }
51
58
  ensure
52
59
  @loading = false
53
60
  end
@@ -86,7 +93,7 @@ module Samagotchi
86
93
  begin
87
94
  Hooks::BundleLoader.load(bundle_name: bundle_name, hooks_dir: hooks_dir, metadata: bundle.hooks, registry: registry,
88
95
  failures: @hook_failures, settings: settings[bundle_name.to_s] || {},
89
- requires_chi: bundle.requires_chi)
96
+ requires_chi: bundle.requires_chi, hook_failed: @hook_failed)
90
97
  rescue Exception => e # rubocop:disable Lint/RescueException -- a hook's SyntaxError or exit must not stop chi
91
98
  raise if e.is_a?(SignalException) # Ctrl-C and kill signals are the user's
92
99
 
@@ -44,12 +44,15 @@ module Samagotchi
44
44
  # (a bundle's rules kept because it needs a newer chi)
45
45
  # @param system_one [#call] → SystemOne::Client or nil: the judgment client
46
46
  # (nil while `guardrails.system_one.mode` is off or no host is set)
47
+ # @param system_one_runs [#call] → SystemOne::Runs or nil: where labels
48
+ # and what an ask led to are saved
47
49
  # @param cancel_controller [#call] → the running turn's CancellationController, nil
48
50
  # @param participant [#call] (author) → the participant's
49
51
  # Participants::Entry (participants.json: their name, their grant's
50
52
  # mode), nil for none
51
53
  def initialize(scratch:, hooks:, tools:, session:, model_key:, cancelled:, ask:, model_name: -> {},
52
- notify: ->(_message) {}, system_one: -> {}, cancel_controller: -> {}, participant: ->(_author) {})
54
+ notify: ->(_message) {}, system_one: -> {}, system_one_runs: -> {}, cancel_controller: -> {},
55
+ participant: ->(_author) {})
53
56
  @scratch = scratch
54
57
  @hooks_lookup = hooks
55
58
  @tools_lookup = tools
@@ -60,6 +63,7 @@ module Samagotchi
60
63
  @ask = ask
61
64
  @notify = notify
62
65
  @system_one = system_one
66
+ @system_one_runs = system_one_runs
63
67
  @cancel_controller = cancel_controller
64
68
  @participant = participant
65
69
  @turn_mode = nil
@@ -273,6 +277,7 @@ module Samagotchi
273
277
  config_path: Samagotchi::ConfigFile.global_path,
274
278
  hooks_dir: Hooks::Loader.hooks_dir,
275
279
  context_dir: ContextSources.root,
280
+ runs_dir: File.join(File.dirname(@approvals.path, 2), "system_one"),
276
281
  home_path: ChiHome.path,
277
282
  shim_path: ChiShim.path
278
283
  )
@@ -375,11 +380,13 @@ module Samagotchi
375
380
  # judged is written after it, so the examples are always earlier asks.
376
381
  entries = log.entries
377
382
  started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
383
+ # A scratch session saves no run, so nothing hangs a later line on one.
384
+ run_id = SystemOne::Runs.new_id(SystemOne::ApprovalQuestion::JUDGMENT) unless @session_lookup.call&.scratch
378
385
  answers = begin
379
386
  client.ask(state: SystemOne::ApprovalQuestion.state(approval, session_title: @session_lookup.call&.title,
380
387
  entries: entries),
381
388
  questions: { SystemOne::ApprovalQuestion::ID => SystemOne::ApprovalQuestion.question },
382
- cancel_controller: @cancel_controller.call)
389
+ cancel_controller: @cancel_controller.call, run_id: run_id)
383
390
  rescue LLM::RequestCancelled
384
391
  raise
385
392
  rescue StandardError => e
@@ -393,13 +400,14 @@ module Samagotchi
393
400
  auto = system_one_auto_allow?(client, approval, p, entries, targets: targets)
394
401
  decision = system_one_decision(client, auto)
395
402
 
396
- id = log.judgment(session_id: @session_lookup.call&.id,
403
+ id = log.judgment(id: run_id, session_id: @session_lookup.call&.id,
397
404
  rule: approval.rule, source: approval.source, tool: approval.tool,
398
405
  **SystemOne::ApprovalQuestion.call_fields(approval),
399
406
  repo_name: approval.repo_name,
400
407
  p: p, threshold: Config.get("guardrails.system_one.threshold"), model: client.model,
401
408
  latency_ms: latency_ms, decision: decision, origin: "live")
402
409
  # A judgment the log cannot hold is no judgment: the user is asked.
410
+ system_one_run_facts(run_id, approval, targets, id.nil? && auto ? "asked" : decision)
403
411
  { id: id, p: p, auto: auto && !id.nil? }
404
412
  rescue LLM::RequestCancelled
405
413
  raise
@@ -409,6 +417,22 @@ module Samagotchi
409
417
  nil
410
418
  end
411
419
 
420
+ # The saved run's later lines: what the ask led to, and what the gates
421
+ # say of it now, which can't be recomputed later (a rule list changes):
422
+ # a later comparison must tell a sweep of gated asks from ungated ones.
423
+ def system_one_run_facts(run_id, approval, targets, what)
424
+ runs = @system_one_runs.call
425
+ return unless run_id && runs&.on?
426
+
427
+ rule = approval.rule.to_s
428
+ runs.gates(run_id, rule: rule, rule_listed: system_one_rules.include?(rule),
429
+ self_protect_refused: SystemOne::SelfProtect.refuse?(approval, targets: targets),
430
+ threshold: Config.get("guardrails.system_one.threshold"))
431
+ runs.acted(run_id, what)
432
+ rescue StandardError => e
433
+ Samagotchi::Log.warn(:guardrails, "system_one_run_facts_failed", error: "#{e.class}: #{e.message}")
434
+ end
435
+
412
436
  # What the judgment line says it did: "auto" (System 1 answered for the
413
437
  # user), "asked" (on mode, the user decides), "audit" (nothing decided).
414
438
  def system_one_decision(client, auto)
@@ -450,43 +474,24 @@ module Samagotchi
450
474
  []
451
475
  end
452
476
 
453
- # The user's answer to that ask, as a label on the judgment: the scope
454
- # they took, a denial, or none for an ask that was cancelled. A parent
455
- # agent's answer is not the user's: its allow labels nothing, and its
456
- # deny is a label only where the parent could have allowed — a parent
457
- # that was not allowed to permit says nothing about what the user would
458
- # have done (docs/guardrails.md, "System 1").
477
+ # The answer to that ask, as a label on the judgment and on its saved run
478
+ # (SystemOne::ApprovalLabel: which answers are labels, how strong).
459
479
  def system_one_label(judgment, answer, payload)
460
480
  id = judgment && judgment[:id]
461
481
  return nil if id.nil?
462
482
 
463
- scopes = Array(payload[:approval].scopes)
464
- if !answer.is_a?(Samagotchi::QuestionAnswer)
465
- label = "none"
466
- elsif answer.parent_agent?
467
- return nil if parent_allowed?(answer, scopes) || !parent_may_permit?(payload)
468
-
469
- label = "deny"
470
- else
471
- index = answer.selected_indices.first
472
- label = if index && index < scopes.size
473
- scopes[index] == "once" ? "allow_once" : "allow_wide"
474
- else
475
- "deny"
476
- end
477
- end
478
- system_one_log.label(id, label, question_id: (answer.id if answer.is_a?(Samagotchi::QuestionAnswer)))
483
+ label = SystemOne::ApprovalLabel.of(answer, scopes: Array(payload[:approval].scopes),
484
+ parent_may_permit: -> { parent_may_permit?(payload) })
485
+ return nil if label.nil?
486
+
487
+ written = system_one_log.label(id, label.value, question_id: (answer.id if answer.is_a?(Samagotchi::QuestionAnswer)))
488
+ SystemOne::ApprovalLabel.save(@system_one_runs.call, id, label)
489
+ written
479
490
  rescue StandardError => e
480
491
  Samagotchi::Log.warn(:guardrails, "system_one_label_failed", error: "#{e.class}: #{e.message}")
481
492
  nil
482
493
  end
483
494
 
484
- # Whether a parent's answer picked one of the offered scopes.
485
- def parent_allowed?(answer, scopes)
486
- index = answer.selected_indices.first
487
- !index.nil? && index < scopes.size
488
- end
489
-
490
495
  # Whether this session's parent level lets a parent allow a call at all,
491
496
  # and the ask is not one only the user may answer.
492
497
  def parent_may_permit?(payload)
@@ -12,8 +12,9 @@ module Samagotchi
12
12
  # words against, as this process's environment and config.yml name them
13
13
  # (read each call). Each surface keeps its set:
14
14
  # .protected the config dir, the hooks dir, the approval store's
15
- # dir, the installed bundles' dir, attached context: an
16
- # ask only the user may answer (ParentApprovals.chi_files?)
15
+ # dir, the installed bundles' dir, attached context,
16
+ # System 1's saved runs: an ask only the user may answer
17
+ # (ParentApprovals.chi_files?)
17
18
  # .touches .protected and the session repo's git hooks dir: a
18
19
  # rule's touches: chi_dirs (Targets#touches_chi?)
19
20
  # .self_protect .protected and the whole state dir, resolved: what
@@ -27,11 +28,13 @@ module Samagotchi
27
28
  module_function
28
29
 
29
30
  # chi's config dir, the hooks dir, the approval store's dir, the
30
- # installed bundles' dir and attached context.
31
+ # installed bundles' dir, attached context and System 1's saved runs
32
+ # (labels there may train a model: no agent writes them).
31
33
  def protected
32
34
  hooks = begin Hooks::Loader.hooks_dir rescue nil end
33
35
  bundles = begin MemoryPaths.bundles_dir rescue nil end
34
- [ConfigFile.config_dir, hooks, File.join(Paths.state_dir, "guardrails"), bundles, File.join(Paths.state_dir, "context")]
36
+ [ConfigFile.config_dir, hooks, File.join(Paths.state_dir, "guardrails"), bundles, File.join(Paths.state_dir, "context"),
37
+ File.join(Paths.state_dir, "system_one")]
35
38
  .compact.map { |dir| dir.to_s.chomp("/") }.reject(&:empty?).uniq
36
39
  end
37
40
 
@@ -9,7 +9,7 @@ module Samagotchi
9
9
  # Outside, ShellPaths, RmTargets, ReviewPaths, PairCommand, Targets,
10
10
  # ParentApprovals, System 1's SelfProtect). Each surface keeps its own
11
11
  # predicate; this only says where a path lands and whether it is in a
12
- # dir. docs/internals/tool-guardrails.md maps which surface uses which.
12
+ # dir. docs/internals/tool-output-limits.md maps which surface uses which.
13
13
  module PathResolve
14
14
  # How many symlinks .real follows on a path that doesn't exist
15
15
  # (about the kernel's own ELOOP limit).
@@ -11,9 +11,10 @@ module Samagotchi
11
11
  # lexical answer and where the write lands, a vote when either is
12
12
  # protected).
13
13
  # deny: the approval store's dir, installed bundles (.bundles/:
14
- # hooks, rules, manifests) and attached context (a source's
14
+ # hooks, rules, manifests), attached context (a source's
15
15
  # command runs later, outside the gate; chi context writes
16
- # it); nothing legitimate writes there through the file tools.
16
+ # it) and System 1's saved runs (a label there may train a
17
+ # model); nothing legitimate writes there through the file tools.
17
18
  # ask (once / session): config.yml, home.json (the chi home: the
18
19
  # Ruby every chi launch boots), the chi shim (ChiShim: the
19
20
  # `chi` every tool runs) and the plain hooks dir. The
@@ -31,13 +32,15 @@ module Samagotchi
31
32
  # @param config_path [String] config.yml
32
33
  # @param hooks_dir [String] the plain hooks dir
33
34
  # @param context_dir [String, nil] <state dir>/context (ContextSources.root)
35
+ # @param runs_dir [String, nil] <state dir>/system_one (SystemOne::Runs)
34
36
  # @param home_path [String, nil] home.json (ChiHome.path)
35
37
  # @param shim_path [String, nil] the chi shim (ChiShim.path)
36
- def initialize(store_dir:, bundles_dir:, config_path:, hooks_dir:, context_dir: nil, home_path: nil, shim_path: nil)
38
+ def initialize(store_dir:, bundles_dir:, config_path:, hooks_dir:, context_dir: nil, runs_dir: nil, home_path: nil, shim_path: nil)
37
39
  @deny = [
38
40
  ["guardrail-store", store_dir, "the approval store belongs to the user"],
39
41
  ["installed-bundles", bundles_dir, "installed bundles (hooks, rules, manifests) are changed by chi bundle install"],
40
- ["chi-context", context_dir, "attached context sources run commands; change them with chi context"]
42
+ ["chi-context", context_dir, "attached context sources run commands; change them with chi context"],
43
+ ["system-one-runs", runs_dir, "System 1's saved runs are chi's own record"]
41
44
  ]
42
45
  @ask = [
43
46
  ["chi-config", config_path, "changes chi's config.yml"],
@@ -39,7 +39,7 @@ module Samagotchi
39
39
  user-only). --threshold overrides guardrails.system_one.threshold
40
40
  for the table; --model overrides the configured model for this run.
41
41
  --dry-run judges and prints the table without writing a line. Needs
42
- guardrails.system_one.mode audit or on and a guardrails.system_one.host
42
+ guardrails.system_one.mode audit or on and a system_one.host
43
43
  (exit 2 without one).
44
44
  TEXT
45
45
 
@@ -146,10 +146,10 @@ module Samagotchi
146
146
  threshold = threshold(options[:threshold])
147
147
  return usage_error(threshold) if threshold.is_a?(String)
148
148
 
149
- client = @client || SystemOne::Client.from_config(host_registry: HostRegistry.new, model: options[:model])
149
+ client = @client || SystemOne::Client.from_config(host_registry: HostRegistry.new, mode: Config.get("guardrails.system_one.mode"), model: options[:model])
150
150
  if client.nil?
151
151
  error_line("chi guardrails replay: no System 1 client — set guardrails.system_one.mode to audit or on " \
152
- "and guardrails.system_one.host in config.yml")
152
+ "and system_one.host in config.yml")
153
153
  return CLI::Exit::USAGE
154
154
  end
155
155
 
@@ -18,6 +18,15 @@ module Samagotchi
18
18
  class HookRoutes
19
19
  # What a notice may stand in for (#notify's fallback_for:).
20
20
  NOTICE_FALLBACKS = %i[display editor].freeze
21
+ # How much of a failed hook's error #plugin_hook_failed shows.
22
+ HOOK_FAILURE_CHARS = 300
23
+ # What the notice says the hook now does, by the policy Hooks.wrap gave it
24
+ # (a chi.on handler is always :log; a bundle's hook file any of the three).
25
+ HOOK_FAILURE_EFFECTS = {
26
+ log: "it's skipped, and only the log has its next failures",
27
+ skip: "it's skipped, and its next failures are silent",
28
+ deny: "it denies every tool call it guards, and its next failures are in each denial's reason"
29
+ }.freeze
21
30
 
22
31
  # @param event_routes [EventRoutes] where a notice goes
23
32
  # @param question_desk [#call] → QuestionDesk
@@ -31,6 +40,8 @@ module Samagotchi
31
40
  @steer = steer
32
41
  @turn_author = turn_author
33
42
  @unknown_fallbacks = Set.new
43
+ @hook_failures = {}
44
+ @hook_failures_lock = Mutex.new
34
45
  end
35
46
 
36
47
  # The things a hook can do beyond reading its event. Each gets the
@@ -81,11 +92,35 @@ module Samagotchi
81
92
  # load failures show): a running turn's event, else announced or held for
82
93
  # the caller (EventRoutes#route_by_turn: never held while the plugins
83
94
  # load, never an anytime command's, unmarked).
84
- def guardrail_notify(message)
85
- @event_routes.route_by_turn({ type: :guardrail_warning, message: message.to_s })
95
+ # @param label [String, nil] whose line ("plugins"); nil for the guardrails
96
+ def guardrail_notify(message, label: nil)
97
+ @event_routes.route_by_turn({ type: :guardrail_warning, message: message.to_s, label: label }.compact)
86
98
  nil
87
99
  end
88
100
 
101
+ # A bundle hook file or a chi.on handler raised (Hooks.wrap; the log has
102
+ # every firing of a :log one). Its first failure on this Engine, so in
103
+ # this worker, tells the user, as a plugin that failed to load does; then
104
+ # only the log. One line per label — file, bundle and event — so a
105
+ # handler on a stream can't nag.
106
+ # @param hook [String] "<file> (bundle <name>) <event> hook"
107
+ # @param failed [String] "Class: message"
108
+ # @param effect [Symbol] the hook's policy, for what the line says it does
109
+ def plugin_hook_failed(hook, failed, effect: :log)
110
+ line = "#{hook} failed (#{failed.lines.first.to_s.strip[0, HOOK_FAILURE_CHARS]}): " \
111
+ "#{HOOK_FAILURE_EFFECTS[effect] || HOOK_FAILURE_EFFECTS[:log]}"
112
+ first = @hook_failures_lock.synchronize { @hook_failures.key?(hook) ? false : (@hook_failures[hook] = line) }
113
+ guardrail_notify(line, label: "plugins") if first
114
+ nil
115
+ end
116
+
117
+ # What #plugin_hook_failed told, for a UI that joins later; nil for none.
118
+ # @return [String, nil]
119
+ def hook_failures_message
120
+ lines = @hook_failures_lock.synchronize { @hook_failures.values }
121
+ lines.empty? ? nil : lines.join("; ")
122
+ end
123
+
89
124
  # A question through the question flow (REPL sync handler, attached TUI,
90
125
  # web), single-select, kind "hook". A --non-interactive run has no one
91
126
  # to ask: nil at once. Anything but an answer (a sync handler's text, no