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
@@ -179,17 +179,21 @@ module Samagotchi
179
179
  # mode GuardrailWiring also reads rules, threshold and min_record for the auto-allow (docs/guardrails.md).
180
180
  # off (the default) | audit (judge every ask, log it, never decide) | on. An unknown value warns and is off.
181
181
  Entry.new(key: "guardrails.system_one.mode", yaml_path: %w[guardrails system_one mode], type: :enum, default: "off", expose: %i[config], enum_values: %w[off audit on], lifecycle: :next_tool_or_approval),
182
- # The hosts: entry serving /v1/systemone (Splash, or the hosted Jev API); unset: no judgment.
183
- Entry.new(key: "guardrails.system_one.host", yaml_path: %w[guardrails system_one host], type: :string, default: nil, expose: %i[config], lifecycle: :next_tool_or_approval),
184
- # The judgment model; unset: the host's first model, and no judgment if it names none.
185
- Entry.new(key: "guardrails.system_one.model", yaml_path: %w[guardrails system_one model], type: :string, default: nil, expose: %i[config], lifecycle: :next_tool_or_approval),
186
182
  # The rule ids System 1 may answer for ("a|b", or a YAML list, parsed like guardrails.small_models).
187
183
  Entry.new(key: "guardrails.system_one.rules", yaml_path: %w[guardrails system_one rules], type: :string, default: nil, expose: %i[config], lifecycle: :next_tool_or_approval),
188
184
  # The probability an auto-allow needs, and how many clean records a rule needs before it is trusted.
189
185
  Entry.new(key: "guardrails.system_one.threshold", yaml_path: %w[guardrails system_one threshold], type: :float, default: 0.8, expose: %i[config], lifecycle: :next_tool_or_approval),
190
186
  Entry.new(key: "guardrails.system_one.min_record", yaml_path: %w[guardrails system_one min_record], type: :integer, default: 5, expose: %i[config], lifecycle: :next_tool_or_approval),
191
- # How long one judgment may take, in milliseconds; a judgment that doesn't arrive asks the user.
192
- Entry.new(key: "guardrails.system_one.timeout_ms", yaml_path: %w[guardrails system_one timeout_ms], type: :integer, default: 2000, expose: %i[config], lifecycle: :next_tool_or_approval),
187
+
188
+ # What every use of System 1 shares: approvals, toolkit suggestions, context candidates and bundles' judgments.
189
+ # The hosts: entry serving /v1/systemone (Splash, or the hosted Jev API); unset: no judgment, so nothing auto-allows.
190
+ Entry.new(key: "system_one.host", yaml_path: %w[system_one host], type: :string, default: nil, expose: %i[config], lifecycle: :next_tool_or_approval),
191
+ # The judgment model; unset: the host's first model, and no judgment if it names none.
192
+ Entry.new(key: "system_one.model", yaml_path: %w[system_one model], type: :string, default: nil, expose: %i[config], lifecycle: :next_tool_or_approval),
193
+ # How long one judgment may take, in milliseconds; a judgment that doesn't arrive is no judgment.
194
+ Entry.new(key: "system_one.timeout_ms", yaml_path: %w[system_one timeout_ms], type: :integer, default: 2000, expose: %i[config], lifecycle: :next_tool_or_approval),
195
+ # Save each System 1 ask and what came of it (docs/system-one.md): on (the default) | off.
196
+ Entry.new(key: "system_one.runs", yaml_path: %w[system_one runs], type: :enum, default: "on", expose: %i[config], enum_values: %w[on off], lifecycle: :next_tool_or_approval),
193
197
 
194
198
  Entry.new(key: "log.file", yaml_path: %w[log file], type: :string, default: nil, expose: %i[env config cli], lifecycle: :process_start),
195
199
  Entry.new(key: "log.disable", yaml_path: %w[log disable], type: :bool, default: false, expose: %i[env config cli], lifecycle: :process_start),
@@ -204,6 +208,12 @@ module Samagotchi
204
208
  # The tokens one scope's memory index (sent with every prompt) may hold before a write that crosses it gets a note
205
209
  # (MemoryBundle::IndexSize); 0: no note. `memory:` is its own section, not the `memories:` preload list.
206
210
  Entry.new(key: "memory.index_warn_tokens", yaml_path: %w[memory index_warn_tokens], type: :integer, default: 2500, expose: %i[env config cli], lifecycle: :next_matching_turn_display_poll_write),
211
+ # The most characters one memory_read result holds (ToolOutputCut cuts it with a line naming the memory file); 0: no
212
+ # fixed cap. memory_read is not cut by max_tool_output_chars (ToolRunner).
213
+ Entry.new(key: "memory.read_max_chars", yaml_path: %w[memory read_max_chars], type: :integer, default: 0, expose: %i[env config cli], lifecycle: :next_matching_use),
214
+ # The tokens one memory may hold (each read of it pays them) before a write that crosses it gets a note
215
+ # (MemoryBundle::EntrySize); 0: no note.
216
+ Entry.new(key: "memory.entry_warn_tokens", yaml_path: %w[memory entry_warn_tokens], type: :integer, default: 5000, expose: %i[env config cli], lifecycle: :next_matching_turn_display_poll_write),
207
217
  Entry.new(key: "context.status_thresholds", yaml_path: %w[context status_thresholds], type: :string, default: "20,40,60,80", expose: %i[env config cli], lifecycle: :next_matching_turn_display_poll_write),
208
218
  # Attached context (ContextSources): how often a source's command runs when it names no --every (seconds; less
209
219
  # than 30 counts as 30).
@@ -219,7 +229,7 @@ module Samagotchi
219
229
  Entry.new(key: "context.peer_turns", yaml_path: %w[context peer_turns], type: :integer, default: 3, expose: %i[env config cli], lifecycle: :next_matching_turn_display_poll_write),
220
230
  # The candidate judge (ContextJudge): off (no conversation text goes to a System 1 host); audit (asks and logs
221
231
  # context_candidates.jsonl, attaches nothing); on (ContextCandidateChoice: p ≥ threshold attaches quietly,
222
- # offer_threshold ≤ p < threshold shows one card). Its host and model are guardrails.system_one.host/model.
232
+ # offer_threshold ≤ p < threshold shows one card). Its host and model are system_one.host and .model.
223
233
  # reask_after_turns: the owner prompts after a judged ask before its one re-ask.
224
234
  Entry.new(key: "context.candidates.mode", yaml_path: %w[context candidates mode], type: :enum, default: "off", expose: %i[config], enum_values: %w[off audit on], lifecycle: :next_matching_turn_display_poll_write),
225
235
  Entry.new(key: "context.candidates.threshold", yaml_path: %w[context candidates threshold], type: :float, default: 0.8, expose: %i[config], lifecycle: :next_matching_turn_display_poll_write),
@@ -260,7 +270,7 @@ module Samagotchi
260
270
  Entry.new(key: "toolkit.default", yaml_path: %w[toolkit default], type: :string, default: nil, expose: %i[env config], lifecycle: :session_creation),
261
271
  Entry.new(key: "toolkit.child", yaml_path: %w[toolkit child], type: :string, default: nil, expose: %i[env config], lifecycle: :session_creation),
262
272
  # Toolkit suggestion (ToolkitSuggest): off; audit (logs, never blocks or changes a session); on (the first eligible
263
- # turn waits up to guardrails.system_one.timeout_ms and may start on the suggested kit).
273
+ # turn waits up to system_one.timeout_ms and may start on the suggested kit).
264
274
  Entry.new(key: "toolkit.suggest.mode", yaml_path: %w[toolkit suggest mode], type: :enum, default: "off", expose: %i[config], enum_values: %w[off audit on], lifecycle: :worker_start),
265
275
  Entry.new(key: "toolkit.suggest.threshold", yaml_path: %w[toolkit suggest threshold], type: :float, default: 0.5, expose: %i[config], lifecycle: :worker_start),
266
276
  Entry.new(key: "toolkit.suggest.defer_turns", yaml_path: %w[toolkit suggest defer_turns], type: :integer, default: 3, expose: %i[config], lifecycle: :worker_start),
@@ -390,8 +400,18 @@ module Samagotchi
390
400
  # The keys a hosts.<name>.models entry may hold (HostModel.parse_map).
391
401
  HOST_MODEL_KEYS = %w[price served].freeze
392
402
  # Section keys beyond the registry's: guardrails' YAML rules (Engine#guardrail_rules).
393
- # And toolkit's kits (Toolkit.kits).
394
- SECTION_EXTRA_KEYS = { "guardrails" => %w[rules disable].freeze, "toolkit" => %w[kits].freeze }.freeze
403
+ # And toolkit's kits (Toolkit.kits), and bundles' judgments' settings (SystemOne::JudgmentSettings).
404
+ SECTION_EXTRA_KEYS = { "guardrails" => %w[rules disable].freeze, "toolkit" => %w[kits].freeze, "system_one" => %w[judgments].freeze }.freeze
405
+
406
+ # guardrails.system_one's keys that moved to system_one (SystemOne::Client.from_config).
407
+ SYSTEM_ONE_MOVED_KEYS = %w[host model timeout_ms].freeze
408
+ # What a system_one.judgments entry may hold. Core's judgments take none (RESERVED_JUDGMENTS).
409
+ JUDGMENT_ENTRY_KEYS = %w[mode host model].freeze
410
+ JUDGMENT_MODES = %w[off audit on].freeze
411
+ # Names core keeps, as a judgment's first word: a bundle can't declare one and
412
+ # system_one.judgments can't set one (Record and Similar are model-blind, so a
413
+ # swapped model would inherit a clean record and could auto-allow).
414
+ RESERVED_JUDGMENTS = %w[approvals context guardrails toolkit].freeze
395
415
 
396
416
  # What a :bool value reads as (Config.coerce!), YAML 1.1's words and 1/0.
397
417
  BOOL_WORDS = { "1" => true, "true" => true, "yes" => true, "on" => true,
@@ -464,6 +484,14 @@ module Samagotchi
464
484
  word && entry.enum_values.include?(word) ? word : nil
465
485
  end
466
486
 
487
+ # A system_one.judgments entry's mode as written, nil for one it can't
488
+ # be: an unquoted on/off is a boolean there too.
489
+ # @return [String, nil]
490
+ def judgment_mode(raw)
491
+ word = BOOL_ENUM_WORDS.fetch(raw) { raw.to_s.strip.downcase }
492
+ word if JUDGMENT_MODES.include?(word)
493
+ end
494
+
467
495
  # What +entry+'s type takes, as a refusal names it.
468
496
  def takes(entry)
469
497
  case entry.type
@@ -692,6 +720,8 @@ module Samagotchi
692
720
  # A section's leaf: a key the registry knows, or a mapping of a
693
721
  # third level (web.participants.cap) whose keys it knows.
694
722
  def leaf_problems(path, value)
723
+ return judgment_problems(value) if path == "system_one.judgments"
724
+
695
725
  if value.is_a?(Hash) && ENTRIES.any? { |e| e.key.start_with?("#{path}.") }
696
726
  value.keys.filter_map { |leaf| key_problem("#{path}.#{leaf}") }
697
727
  else
@@ -699,6 +729,29 @@ module Samagotchi
699
729
  end
700
730
  end
701
731
 
732
+ # system_one.judgments' entries: a map entry bypasses the registry, so
733
+ # its keys and mode are checked here (an unquoted on/off is a boolean).
734
+ # An undeclared name can only be told once the plugins have loaded
735
+ # (SystemOne::Judgments#warn_undeclared).
736
+ def judgment_problems(value)
737
+ return value.nil? ? [] : ["config: 'system_one.judgments' must be a mapping of judgments; ignored"] unless value.is_a?(Hash)
738
+
739
+ value.flat_map do |name, entry|
740
+ where = "system_one.judgments.#{name}"
741
+ if RESERVED_JUDGMENTS.include?(name.to_s.split(".").first)
742
+ next ["config: #{where}: core's judgments take no settings of their own; ignored (see docs/system-one.md)"]
743
+ end
744
+ next ["config: #{where} must be a mapping of #{JUDGMENT_ENTRY_KEYS.join(", ")}; ignored"] unless entry.is_a?(Hash)
745
+
746
+ unknown = (entry.keys.map(&:to_s) - JUDGMENT_ENTRY_KEYS).map do |k|
747
+ unknown_key_message("#{where}.#{k}", k, JUDGMENT_ENTRY_KEYS, prefix: "#{where}.")
748
+ end
749
+ mode = entry.key?("mode") && judgment_mode(entry["mode"]).nil? ? ["config: #{where}.mode must be off, audit or on; it is off"] : []
750
+ text = %w[host model].reject { |k| entry[k].nil? || entry[k].is_a?(String) }.map { |k| "config: #{where}.#{k} must be text" }
751
+ unknown + mode + text
752
+ end
753
+ end
754
+
702
755
  # What a model ref in config.yml won't do as written: an alias whose
703
756
  # target is another alias (aliases apply once, ModelRef), a ref that
704
757
  # starts with "<host>/" (only ':' names a host).
@@ -752,10 +805,17 @@ module Samagotchi
752
805
  @known_sections ||= known_config_keys.filter_map { |k| k.split(".").first if k.include?(".") }.to_set.freeze
753
806
  end
754
807
 
755
- # Keys replaced by another: the warning names the new one (no alias).
808
+ # Keys replaced by another, and what the warning adds: it names the new
809
+ # one (no alias).
810
+ PARENT_ANSWERS_HINT = "hard, standard or soft; see docs/guardrails.md"
811
+ SYSTEM_ONE_MOVED_HINT = "it has moved: until all of host, model and timeout_ms have, nothing is judged or " \
812
+ "auto-allowed, and every ask goes to you; see docs/configuration.md"
756
813
  REPLACED_KEYS = {
757
- "guardrails.parent_approvals" => "guardrails.parent_answers",
758
- "turn.parent_continue" => "guardrails.parent_answers"
814
+ "guardrails.parent_approvals" => ["guardrails.parent_answers", PARENT_ANSWERS_HINT],
815
+ "turn.parent_continue" => ["guardrails.parent_answers", PARENT_ANSWERS_HINT],
816
+ "guardrails.system_one.host" => ["system_one.host", SYSTEM_ONE_MOVED_HINT],
817
+ "guardrails.system_one.model" => ["system_one.model", SYSTEM_ONE_MOVED_HINT],
818
+ "guardrails.system_one.timeout_ms" => ["system_one.timeout_ms", SYSTEM_ONE_MOVED_HINT]
759
819
  }.freeze
760
820
 
761
821
  # nil when +dotted+ is a key the file may hold.
@@ -763,7 +823,7 @@ module Samagotchi
763
823
  return nil if known_config_keys.include?(dotted)
764
824
  return "config: #{ConfigFile.removed_transport_problem("'#{dotted}'")}" if dotted == "server.transport"
765
825
  if (replaced = REPLACED_KEYS[dotted])
766
- return "config: unknown key '#{dotted}': replaced by '#{replaced}' (hard, standard or soft; see docs/guardrails.md)"
826
+ return "config: unknown key '#{dotted}': replaced by '#{replaced.first}' (#{replaced.last})"
767
827
  end
768
828
 
769
829
  entry = find_by_key(dotted)
@@ -82,13 +82,22 @@ module Samagotchi
82
82
  ContextCandidatesLog.records_for(log, session_id).any? { |line| line["kind"] == "carded" && line["name"] == name }
83
83
  end
84
84
 
85
+ # Replace an open offer card with what became of it. Typed in off or audit
86
+ # mode no card is open: #choose answers without one rather than raising a
87
+ # "closed" card out of nowhere.
88
+ def close_open_card(candidate, session_id, state_dir, show_card, body)
89
+ return unless carded?(ContextCandidatesLog.log_for(state_dir), session_id, candidate.name)
90
+
91
+ show_card.call(**closed_card(candidate, body))
92
+ end
93
+
85
94
  def judge_attach(candidate, job:, p:, state_dir:, log:, show_card:)
86
95
  own = ContextSources.session_location(job.session_id, state_dir: state_dir)
87
96
  own.add(ContextCandidates.source_for(candidate, why: JUDGE_WHY))
88
97
  Log.info(:context, "candidate_attached", name: candidate.name, trigger: "judge")
89
98
  ContextCandidatesLog.event("judge_attached", session_id: job.session_id, name: candidate.name, log: log, p: p)
90
99
  # A card from an earlier ask loses its buttons.
91
- show_card.call(**closed_card(candidate, "Attached: #{JUDGE_WHY}.")) if carded?(log, job.session_id, candidate.name)
100
+ close_open_card(candidate, job.session_id, state_dir, show_card, "Attached: #{JUDGE_WHY}.")
92
101
  :attached
93
102
  rescue ContextSources::Invalid, ContextProviders::Invalid => e
94
103
  Log.warn(:context, "candidate_not_attached", name: candidate.name, error: e.message.to_s[0, 200])
@@ -133,12 +142,19 @@ module Samagotchi
133
142
  own.add(ContextCandidates.source_for(candidate, why: CHOSEN_WHY))
134
143
  Log.info(:context, "candidate_attached", name: candidate.name, trigger: "command")
135
144
  ContextCandidatesLog.event("attached", session_id: session.id, name: candidate.name, state_dir: state_dir, by: "command")
136
- show_card.call(**closed_card(candidate, "Attached."))
145
+ label_run(session, candidate, "attached", state_dir)
146
+ close_open_card(candidate, session.id, state_dir, show_card, "Attached.")
137
147
  [:ok, "attached #{candidate.name}: its text comes with your next message"]
138
148
  rescue ContextSources::Invalid, ContextProviders::Invalid => e
139
149
  [:error, "#{candidate.name} not attached: #{e.message}"]
140
150
  end
141
151
 
152
+ # /context attach|decline (the card's buttons), on the latest ask's run.
153
+ def label_run(session, candidate, value, state_dir)
154
+ records = ContextCandidatesLog.records_for(ContextCandidatesLog.log_for(state_dir), session.id)
155
+ ContextCandidatesLog.label_run(records, candidate.name, value, by: "command", sessions_dir: state_dir)
156
+ end
157
+
142
158
  def decline(candidate, status, session, state_dir, show_card)
143
159
  if status == "attached"
144
160
  return [:error, "#{candidate.name} is attached; chi context rm #{candidate.name} removes it"]
@@ -147,7 +163,8 @@ module Samagotchi
147
163
 
148
164
  ContextSources.session_location(session.id, state_dir: state_dir).decline(candidate.name, candidate.url.to_s)
149
165
  ContextCandidatesLog.event("declined", session_id: session.id, name: candidate.name, state_dir: state_dir, by: "command")
150
- show_card.call(**closed_card(candidate, "Not attached here."))
166
+ label_run(session, candidate, "declined", state_dir)
167
+ close_open_card(candidate, session.id, state_dir, show_card, "Not attached here.")
151
168
  [:ok, "declined #{candidate.name}: not proposed again in this session"]
152
169
  end
153
170
  end
@@ -33,7 +33,10 @@ module Samagotchi
33
33
  BRANCH_MIN_CHARS = 6
34
34
  NUMBER_MIN = 10
35
35
  # A fenced code block's opening line (up to 3 spaces, then ``` or ~~~).
36
- FENCE_OPEN = /\A {0,3}(`{3,}|~{3,})/
36
+ # A backtick fence's info string carries no backtick, so nothing after the
37
+ # marker on that line may be one: "```foo``` first" is inline code, and
38
+ # reading it as a fence leaves it unclosed and blanks the rest of the text.
39
+ FENCE_OPEN = /\A {0,3}(`{3,}(?![^\n]*`)|~{3,})/
37
40
  # What may sit around a word-like mention without joining it.
38
41
  WORD_BEFORE = %r{(?<![\w./#-])}
39
42
  WORD_AFTER = %r{(?![\w/-])(?!\.[\w/-])}
@@ -3,6 +3,7 @@
3
3
  require_relative "log"
4
4
  require_relative "session"
5
5
  require_relative "side_judgments"
6
+ require_relative "system_one/runs"
6
7
 
7
8
  module Samagotchi
8
9
  # The candidates' record, `context_candidates.jsonl` in the state dir
@@ -15,7 +16,8 @@ module Samagotchi
15
16
  # Kinds (every line has session_id, name and at):
16
17
  # - proposed: a plugin's ctx.context.propose recorded it (title, by)
17
18
  # - mentioned: an owner prompt named it and it was attached (mention)
18
- # - judged: System 1's ask (ContextJudge.ask: p, task_p, stated, would, …)
19
+ # - judged: System 1's ask (ContextJudge.ask: run_id, p, task_p, stated,
20
+ # would, …)
19
21
  # - judge_attached: on mode attached it (p ≥ threshold; p)
20
22
  # - carded: on mode showed its card (the middle band; p): once per
21
23
  # candidate, read back by ContextCandidateChoice.carded?
@@ -34,6 +36,10 @@ module Samagotchi
34
36
  STATUS_OF = { "proposed" => "proposed", "mentioned" => "attached", "judge_attached" => "attached", "attached" => "attached",
35
37
  "declined" => "declined", "detached" => "proposed", "withdrawn" => nil }.freeze
36
38
 
39
+ # The kinds #look sees that are a removal: a chip's ×, chi context rm. One
40
+ # removed after the session's last turn is never seen.
41
+ REMOVED = %w[declined detached].freeze
42
+
37
43
  module_function
38
44
 
39
45
  # @param state_dir [String, nil] the sessions folder (Session.default_state_dir)
@@ -54,6 +60,17 @@ module Samagotchi
54
60
  # +session_id+'s lines, oldest first.
55
61
  def records_for(log, session_id) = log.records.select { |line| line["session_id"] == session_id.to_s }
56
62
 
63
+ # Explicit `chi sessions delete`: everything the session left in the log
64
+ # goes with it, including the `judged` lines that keep 200 characters of
65
+ # its prompts. Never raises.
66
+ # @return [Integer] how many lines were dropped
67
+ def purge_session(session_id, state_dir: nil)
68
+ log_for(state_dir).prune { |line| line["session_id"] != session_id.to_s }
69
+ rescue StandardError => e
70
+ Log.warn(:context, "context_candidates_log_failed", error: e.class.name, msg: e.message.to_s[0, 200])
71
+ 0
72
+ end
73
+
57
74
  # The status the lines last left each candidate in, by name (a
58
75
  # withdrawn one is gone).
59
76
  # @return [Hash{String => String}]
@@ -77,10 +94,28 @@ module Samagotchi
77
94
 
78
95
  kind = status == "proposed" ? "detached" : status
79
96
  event(kind, session_id: session_id, name: candidate.name, log: log, by: "user")
97
+ label_run(records, candidate.name, kind, by: "user", root: log.dir) if REMOVED.include?(kind)
80
98
  kind
81
99
  end
82
100
  end
83
101
 
102
+ # The user's say on a candidate, as a label on the latest ask about it
103
+ # (the judged line's run, question "about"). No ask with a run: nothing.
104
+ # Never raises.
105
+ # @param records [Array<Hash>] the session's lines (#records_for)
106
+ # @param root [String, nil] the state dir; nil: from +sessions_dir+
107
+ # @return [Boolean] whether a label was written
108
+ def label_run(records, name, value, by:, sessions_dir: nil, root: nil)
109
+ judged = records.reverse_each.find { |line| line["kind"] == "judged" && line["name"] == name && line["run_id"] }
110
+ return false if judged.nil?
111
+
112
+ runs = SystemOne::Runs.in(root || File.dirname(sessions_dir || Session.default_state_dir))
113
+ runs.label(judged["run_id"], value, by: by, question: "about")
114
+ rescue StandardError => e
115
+ Log.warn(:context, "context_candidates_log_failed", error: e.class.name, msg: e.message.to_s[0, 200])
116
+ false
117
+ end
118
+
84
119
  # +text+ as a log line keeps it: one line, at most PROMPT_MAX chars.
85
120
  def short(text)
86
121
  line = text.to_s.gsub(/\s+/, " ").strip
@@ -4,6 +4,7 @@ require_relative "config"
4
4
  require_relative "context_candidates_log"
5
5
  require_relative "log"
6
6
  require_relative "system_one/question"
7
+ require_relative "system_one/runs"
7
8
 
8
9
  module Samagotchi
9
10
  # System 1's judgment of a session's candidate context
@@ -26,6 +27,8 @@ module Samagotchi
26
27
  # (attach, card, or nothing); audit only logs.
27
28
  module ContextJudge
28
29
  MODES = %w[off audit on].freeze
30
+ # Its saved asks' name (SystemOne::Runs).
31
+ JUDGMENT = "context.candidate"
29
32
  ABOUT_ID = "about"
30
33
  TASK_ID = "task"
31
34
  # The bound on each prompt in the state.
@@ -168,14 +171,15 @@ module Samagotchi
168
171
  # candidate is asked again at the next owner prompt.
169
172
  # @return [Hash, nil] the line's fields
170
173
  def ask(client:, job:, log:, settings:, cancel_controller: nil)
174
+ run_id = SystemOne::Runs.new_id(JUDGMENT)
171
175
  answers = client.ask(state: state(job), questions: questions(child: !job.parent_attached.nil?),
172
- cancel_controller: cancel_controller)
176
+ cancel_controller: cancel_controller, run_id: run_id)
173
177
  about = answers&.dig(ABOUT_ID)&.noul
174
178
  task = answers&.dig(TASK_ID)&.noul
175
179
  return nil if about.nil? || task.nil?
176
180
 
177
181
  stated = task >= TASK_MIN
178
- fields = { "title" => job.title, "phase" => job.phase, "owner_prompts" => job.owner_prompts,
182
+ fields = { "run_id" => run_id, "title" => job.title, "phase" => job.phase, "owner_prompts" => job.owner_prompts,
179
183
  "p" => about.round(4), "task_p" => task.round(4), "stated" => stated,
180
184
  "would" => stated ? would(about, settings) : nil, "mode" => settings.mode,
181
185
  "threshold" => settings.threshold, "offer_threshold" => settings.offer_threshold,
@@ -13,8 +13,7 @@ module Samagotchi
13
13
  # - the status line's value (#display), every request and again from the
14
14
  # server's final counts after each generation;
15
15
  # - a :context_status stream event (#observe's return), on every request,
16
- # so the web and the attached TUI get a fresh estimate each time (the
17
- # context.status_cadence setting no longer throttles this event);
16
+ # so the web and the attached TUI get a fresh estimate each time;
18
17
  # - the model's own short line (#take_guidance), once per rise into a
19
18
  # bucket whose guidance asks for a change (the second threshold up).
20
19
  #