samagotchi 0.181.0 → 0.183.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (200) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +93 -1
  3. data/bin/chi +4 -1
  4. data/docs/architecture.md +17 -7
  5. data/docs/bundle-authoring.md +1 -1
  6. data/docs/bundles.md +103 -4
  7. data/docs/cli.md +58 -10
  8. data/docs/configuration.md +3 -1
  9. data/docs/guardrails.md +16 -1
  10. data/docs/internals/block-renderers.md +172 -3
  11. data/docs/internals/bridge.md +23 -5
  12. data/docs/internals/bundles.md +21 -4
  13. data/docs/internals/engine.md +29 -8
  14. data/docs/internals/inline-widgets.md +3 -1
  15. data/docs/internals/prompt-caching.md +9 -1
  16. data/docs/internals/remote-sessions.md +38 -7
  17. data/docs/internals/web-frontend.md +116 -26
  18. data/docs/internals/working-set.md +1 -1
  19. data/docs/mcp-server.md +80 -0
  20. data/docs/plugins.md +308 -21
  21. data/docs/sessions.md +7 -4
  22. data/docs/sub-agent.md +9 -0
  23. data/docs/testing.md +3 -1
  24. data/docs/thin-clients.md +146 -0
  25. data/lib/samagotchi/answer_display.rb +3 -2
  26. data/lib/samagotchi/block_fix/stamp.rb +49 -0
  27. data/lib/samagotchi/block_fix.rb +220 -0
  28. data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
  29. data/lib/samagotchi/bridge/pending_card.rb +4 -3
  30. data/lib/samagotchi/bridge/sse_writer.rb +16 -15
  31. data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
  32. data/lib/samagotchi/bridge.rb +69 -69
  33. data/lib/samagotchi/bridge_client.rb +9 -0
  34. data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
  35. data/lib/samagotchi/bundle_command/check.rb +36 -19
  36. data/lib/samagotchi/bundle_command.rb +50 -21
  37. data/lib/samagotchi/bundle_manifest.rb +101 -4
  38. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  39. data/lib/samagotchi/bundles/mcp/client.rb +260 -0
  40. data/lib/samagotchi/bundles/mcp/content.rb +107 -0
  41. data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
  42. data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
  43. data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
  44. data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
  45. data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
  46. data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
  47. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  48. data/lib/samagotchi/cli/parent_wait.rb +5 -3
  49. data/lib/samagotchi/command_intake.rb +7 -0
  50. data/lib/samagotchi/commands/registry.rb +8 -3
  51. data/lib/samagotchi/config.rb +3 -0
  52. data/lib/samagotchi/continue_intent.rb +8 -0
  53. data/lib/samagotchi/continue_offer.rb +24 -15
  54. data/lib/samagotchi/engine/block_fix.rb +332 -0
  55. data/lib/samagotchi/engine/cards.rb +14 -5
  56. data/lib/samagotchi/engine/events.rb +10 -15
  57. data/lib/samagotchi/engine/models.rb +1 -1
  58. data/lib/samagotchi/engine/plugins.rb +3 -0
  59. data/lib/samagotchi/engine/recap.rb +1 -1
  60. data/lib/samagotchi/engine/side_requests.rb +8 -6
  61. data/lib/samagotchi/engine/sign_in.rb +142 -0
  62. data/lib/samagotchi/engine/turn_end.rb +3 -3
  63. data/lib/samagotchi/engine.rb +47 -27
  64. data/lib/samagotchi/engine_snapshots.rb +1 -1
  65. data/lib/samagotchi/events.rb +1 -0
  66. data/lib/samagotchi/guardrail_wiring.rb +3 -1
  67. data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
  68. data/lib/samagotchi/guardrails/verdict.rb +3 -2
  69. data/lib/samagotchi/hold_stop.rb +4 -4
  70. data/lib/samagotchi/idle_client.rb +10 -6
  71. data/lib/samagotchi/idle_recap.rb +19 -11
  72. data/lib/samagotchi/idle_reminders.rb +7 -5
  73. data/lib/samagotchi/idle_scheduler.rb +7 -10
  74. data/lib/samagotchi/inline_widgets.rb +12 -0
  75. data/lib/samagotchi/installed_bundles/dir.rb +37 -0
  76. data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
  77. data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
  78. data/lib/samagotchi/installed_bundles/record.rb +3 -2
  79. data/lib/samagotchi/installed_bundles/standing.rb +3 -1
  80. data/lib/samagotchi/kernel_loop.rb +7 -5
  81. data/lib/samagotchi/llm/chat_loop.rb +2 -0
  82. data/lib/samagotchi/llm/http.rb +96 -5
  83. data/lib/samagotchi/markdown_parser.rb +28 -0
  84. data/lib/samagotchi/mcp_command.rb +110 -0
  85. data/lib/samagotchi/mcp_server/http.rb +112 -0
  86. data/lib/samagotchi/mcp_server/protocol.rb +244 -0
  87. data/lib/samagotchi/mcp_server/stdio.rb +92 -0
  88. data/lib/samagotchi/mcp_server/text.rb +155 -0
  89. data/lib/samagotchi/mcp_server/tools.rb +245 -0
  90. data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
  91. data/lib/samagotchi/memory_bundle/builder.rb +20 -2
  92. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  93. data/lib/samagotchi/memory_bundle/installer.rb +16 -3
  94. data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
  95. data/lib/samagotchi/memory_bundle/shas.rb +6 -4
  96. data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
  97. data/lib/samagotchi/memory_bundle/status.rb +15 -6
  98. data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
  99. data/lib/samagotchi/plugin/api.rb +6 -1
  100. data/lib/samagotchi/plugin/card.rb +36 -11
  101. data/lib/samagotchi/plugin/context.rb +63 -6
  102. data/lib/samagotchi/plugin/http.rb +40 -15
  103. data/lib/samagotchi/plugin/http_errors.rb +15 -0
  104. data/lib/samagotchi/plugin/http_stream.rb +86 -0
  105. data/lib/samagotchi/plugin/http_transport.rb +58 -16
  106. data/lib/samagotchi/plugin/loader.rb +18 -11
  107. data/lib/samagotchi/plugin/secrets.rb +266 -0
  108. data/lib/samagotchi/plugin/sign_in.rb +392 -0
  109. data/lib/samagotchi/plugin/sse_reader.rb +103 -0
  110. data/lib/samagotchi/queued_command.rb +28 -4
  111. data/lib/samagotchi/recap_setup.rb +13 -4
  112. data/lib/samagotchi/ref.rb +3 -2
  113. data/lib/samagotchi/relay_verifier.rb +8 -7
  114. data/lib/samagotchi/relay_watcher.rb +4 -4
  115. data/lib/samagotchi/run_options.rb +2 -1
  116. data/lib/samagotchi/send_command.rb +54 -41
  117. data/lib/samagotchi/session_access/ask.rb +244 -0
  118. data/lib/samagotchi/session_access/contract.rb +8 -0
  119. data/lib/samagotchi/session_access/delete_plan.rb +77 -0
  120. data/lib/samagotchi/session_access/exchange/token.rb +101 -0
  121. data/lib/samagotchi/session_access/exchange.rb +173 -0
  122. data/lib/samagotchi/session_access/local.rb +38 -6
  123. data/lib/samagotchi/session_access/remote.rb +23 -2
  124. data/lib/samagotchi/session_access.rb +1 -0
  125. data/lib/samagotchi/session_chain/delete.rb +39 -81
  126. data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
  127. data/lib/samagotchi/session_command_catalog.rb +10 -0
  128. data/lib/samagotchi/session_commands.rb +17 -0
  129. data/lib/samagotchi/session_config.rb +4 -4
  130. data/lib/samagotchi/session_delete_command.rb +129 -3
  131. data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
  132. data/lib/samagotchi/session_manager/listing.rb +4 -4
  133. data/lib/samagotchi/session_manager/removal.rb +38 -12
  134. data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
  135. data/lib/samagotchi/session_manager.rb +1 -0
  136. data/lib/samagotchi/session_observer.rb +27 -0
  137. data/lib/samagotchi/steer_cut.rb +4 -3
  138. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
  139. data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
  140. data/lib/samagotchi/testing/boundary.rb +4 -0
  141. data/lib/samagotchi/testing/bundle.rb +48 -2
  142. data/lib/samagotchi/testing/fake_http.rb +60 -15
  143. data/lib/samagotchi/testing/host.rb +4 -1
  144. data/lib/samagotchi/testing/recorder.rb +45 -5
  145. data/lib/samagotchi/tools/tool_index.rb +14 -3
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +20 -6
  148. data/lib/samagotchi/web/ask_routes.rb +292 -0
  149. data/lib/samagotchi/web/block_fix_routes.rb +39 -0
  150. data/lib/samagotchi/web/markdown_renderer.rb +24 -9
  151. data/lib/samagotchi/web/mcp_route.rb +88 -0
  152. data/lib/samagotchi/web/message_display.rb +6 -1
  153. data/lib/samagotchi/web/participant_view.rb +1 -1
  154. data/lib/samagotchi/web/public/app.js +7 -8
  155. data/lib/samagotchi/web/public/block_fix.js +270 -0
  156. data/lib/samagotchi/web/public/block_fix_note.js +104 -0
  157. data/lib/samagotchi/web/public/block_frame.js +77 -13
  158. data/lib/samagotchi/web/public/block_host.js +45 -9
  159. data/lib/samagotchi/web/public/block_regions.js +126 -0
  160. data/lib/samagotchi/web/public/block_renderers.css +12 -0
  161. data/lib/samagotchi/web/public/block_watch.js +16 -2
  162. data/lib/samagotchi/web/public/card.js +22 -9
  163. data/lib/samagotchi/web/public/card_actions.js +21 -0
  164. data/lib/samagotchi/web/public/card_kinds.js +1 -1
  165. data/lib/samagotchi/web/public/card_registry.js +3 -2
  166. data/lib/samagotchi/web/public/data.js +15 -12
  167. data/lib/samagotchi/web/public/delete_sheet.js +222 -162
  168. data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
  169. data/lib/samagotchi/web/public/format.js +3 -0
  170. data/lib/samagotchi/web/public/index.html +52 -7
  171. data/lib/samagotchi/web/public/inline_widgets.js +1 -1
  172. data/lib/samagotchi/web/public/more_sheet.js +6 -4
  173. data/lib/samagotchi/web/public/picture_regions.js +243 -0
  174. data/lib/samagotchi/web/public/picture_viewer.js +113 -12
  175. data/lib/samagotchi/web/public/refs.js +5 -3
  176. data/lib/samagotchi/web/public/region_hit.js +144 -0
  177. data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
  178. data/lib/samagotchi/web/public/running_tasks.js +6 -4
  179. data/lib/samagotchi/web/public/session_actions.js +105 -100
  180. data/lib/samagotchi/web/public/stream_handlers.js +5 -1
  181. data/lib/samagotchi/web/public/turn_block.js +5 -2
  182. data/lib/samagotchi/web/server.rb +6 -4
  183. data/lib/samagotchi/web/session_access_routes.rb +14 -11
  184. data/lib/samagotchi/web/session_control.rb +84 -30
  185. data/lib/samagotchi/web/session_create.rb +2 -2
  186. data/lib/samagotchi/worker/block_fixes.rb +22 -0
  187. data/lib/samagotchi/worker/child_side.rb +3 -2
  188. data/lib/samagotchi/worker/command_queue.rb +13 -13
  189. data/lib/samagotchi/worker/leave.rb +3 -3
  190. data/lib/samagotchi/worker/scratch.rb +6 -6
  191. data/lib/samagotchi/worker/signal_grace.rb +6 -5
  192. data/lib/samagotchi/worker/startup.rb +3 -3
  193. data/lib/samagotchi/worker/wake_turns.rb +4 -4
  194. data/lib/samagotchi/worker.rb +42 -24
  195. data/lib/samagotchi/worker_exit_claim.rb +1 -1
  196. data/lib/samagotchi/worker_idle_exit.rb +18 -9
  197. data/lib/samagotchi/worker_inbound.rb +12 -7
  198. data/lib/samagotchi/worker_participant_gate.rb +9 -7
  199. data/lib/samagotchi/worker_task_wakes.rb +7 -5
  200. metadata +42 -1
@@ -199,6 +199,7 @@ module Samagotchi
199
199
  case key
200
200
  when "files.description" then "a files: entry given as a mapping"
201
201
  when "session_settings.types", "settings.types" then "#{key.split(".").first}: as a mapping of keys to types"
202
+ when "plugin.parts" then "plugin: parts:"
202
203
  else "#{key}:"
203
204
  end
204
205
  end
@@ -207,7 +208,7 @@ module Samagotchi
207
208
 
208
209
  # @return [Boolean] whether a listed file is missing
209
210
  def check_files(manifest)
210
- listed = manifest.files.keys + Array(manifest.plugin && manifest.plugin[:file]) +
211
+ listed = manifest.files.keys + manifest.plugin_files.map(&:first) +
211
212
  manifest.scripts.keys.map { |s| File.join("scripts", s) }
212
213
  missing = listed.reject { |rel| File.file?(path(rel)) }
213
214
  missing.each do |rel|
@@ -225,8 +226,14 @@ module Samagotchi
225
226
  memories.include?(name) || MemoryBundle::MemoryFiles.repo_meta?(name)
226
227
  end
227
228
  loose.each { |name| unlisted(name, "files:") }
228
- plugin = manifest.plugin && manifest.plugin[:file]
229
- unlisted("plugin.rb", "plugin:") if File.file?(path("plugin.rb")) && plugin != "plugin.rb"
229
+ if manifest.plugin
230
+ plugin_files = manifest.plugin_files.map(&:first)
231
+ Dir.glob(path("*.rb")).map { |f| File.basename(f) }.sort.each do |name|
232
+ unlisted(name, "plugin: parts:") unless plugin_files.include?(name)
233
+ end
234
+ elsif File.file?(path("plugin.rb"))
235
+ unlisted("plugin.rb", "plugin:")
236
+ end
230
237
  Dir.glob(path("scripts/*")).sort.each do |file|
231
238
  unlisted("scripts/#{File.basename(file)}", "scripts:") unless manifest.scripts.key?(File.basename(file))
232
239
  end
@@ -278,20 +285,24 @@ module Samagotchi
278
285
  check_floor(manifest, line) if manifest.plugin && manifest.requires_chi && !@too_new && !@installed
279
286
  end
280
287
 
281
- # The plugin calls an API newer than the oldest chi requires_chi admits.
288
+ # The plugin calls an API newer than the oldest chi requires_chi admits:
289
+ # the newest call across all of the plugin's files.
282
290
  def check_floor(manifest, line)
283
291
  floor = lower_bound(manifest.requires_chi)
284
- return unless floor && File.file?(path(manifest.plugin[:file]))
292
+ return unless floor
285
293
 
286
- source = File.read(path(manifest.plugin[:file]))
287
- calls = source.scan(API_CALL).flat_map { |call| [call, call.split(".").first(2).join(".")] }
288
- calls = (calls + ApiCalls.new(source).keywords).uniq.select { |call| Plugin::Api::SINCE.key?(call) }
289
- call = calls.max_by { |c| Gem::Version.new(Plugin::Api::SINCE[c]) } or return
294
+ found = plugin_sources(manifest).flat_map do |file, source|
295
+ calls = source.scan(API_CALL).flat_map { |call| [call, call.split(".").first(2).join(".")] }
296
+ (calls + ApiCalls.new(source).keywords).uniq.select { |call| Plugin::Api::SINCE.key?(call) }.map { |call| [file, call] }
297
+ end
298
+ return if found.empty?
299
+
300
+ file, call = found.max_by { |_, c| Gem::Version.new(Plugin::Api::SINCE[c]) }
290
301
  version = Plugin::Api::SINCE[call]
291
302
  return unless Gem::Version.new(version) > floor
292
303
 
293
304
  add(:warn, :"requires-chi-floor", "manifest.yml",
294
- "requires_chi #{manifest.requires_chi.inspect} admits chi #{floor}, but #{manifest.plugin[:file]} uses " \
305
+ "requires_chi #{manifest.requires_chi.inspect} admits chi #{floor}, but #{file} uses " \
295
306
  "#{call}, which needs chi #{version}", line: line,
296
307
  fix: "requires_chi: \">= #{version}\"")
297
308
  end
@@ -445,25 +456,31 @@ module Samagotchi
445
456
  # A warning, as a name past the boundary is: the plugin may be written
446
457
  # for a newer chi, and a call on a path that never runs breaks nothing.
447
458
  def check_api(manifest)
448
- file = manifest.plugin && manifest.plugin[:file]
449
- return unless file && File.file?(path(file))
450
-
451
- ApiCalls.new(File.read(path(file))).findings.each do |finding|
452
- add(:warn, :api, file, finding.message, line: finding.line, fix: finding.fix)
459
+ plugin_sources(manifest).each do |file, source|
460
+ ApiCalls.new(source).findings.each do |finding|
461
+ add(:warn, :api, file, finding.message, line: finding.line, fix: finding.fix)
462
+ end
453
463
  end
454
464
  end
455
465
 
466
+ # [[file, text]] for each of the plugin's files on disk, parts first
467
+ # (BundleManifest#plugin_files' order); [] without a plugin.
468
+ def plugin_sources(manifest)
469
+ manifest.plugin_files.filter_map { |file, _| [file, File.read(path(file))] if File.file?(path(file)) }
470
+ end
471
+
456
472
  # A chi.on(..., on_error: :fail_closed) guard denies the call it raises
457
473
  # on; only the manifest's plugin: required: true makes its failing to
458
474
  # load deny every call (chi knows it before the plugin registers).
459
475
  FAIL_CLOSED = /on_error:\s*(?::fail_closed\b|["']fail_closed["'])/
460
476
 
461
477
  def check_fail_closed(manifest)
462
- file = manifest.plugin && manifest.plugin[:file]
463
- return if !file || manifest.plugin[:required] || !File.file?(path(file))
478
+ return if manifest.plugin.nil? || manifest.plugin[:required]
479
+
480
+ file, source = plugin_sources(manifest).find { |_, text| text.match?(FAIL_CLOSED) }
481
+ return unless source
464
482
 
465
- source = File.read(path(file))
466
- return unless (index = source =~ FAIL_CLOSED)
483
+ index = source =~ FAIL_CLOSED
467
484
 
468
485
  add(:warn, :"fail-closed", file, "a fail-closed chi.on hook, but the manifest's plugin: has no required: true: " \
469
486
  "if the plugin fails to load, tool calls still run",
@@ -112,7 +112,8 @@ module Samagotchi
112
112
  # What `chi bundle status NAME` says after a plugin's state
113
113
  # (MemoryBundle::Status.plugin_status) that keeps it from loading.
114
114
  PLUGIN_NOTES = { "modified" => " (edited after install: not loaded; reinstall the bundle)",
115
- "unrecorded" => " (no sha256 recorded: not loaded; reinstall the bundle)" }.freeze
115
+ "unrecorded" => " (no sha256 recorded: not loaded; reinstall the bundle)",
116
+ "parts missing" => " (its manifest names plugin parts the install has none of: not loaded; reinstall the bundle)" }.freeze
116
117
 
117
118
  # The subcommands' flags: --help only; a word not starting with -- is
118
119
  # a positional (install's source, -h included). --scope takes whatever
@@ -213,7 +214,7 @@ module Samagotchi
213
214
  return 1 if installed_under_another_name?(expanded_source, bundle_name, prepared, "Install")
214
215
 
215
216
  installer = installer_for(expanded_source, bundle_name, prepared: prepared, scope: scope, force: force, dev: dev)
216
- run_installer(installer, failed: "Install") do |manifest|
217
+ run_installer(installer, bundle_name, failed: "Install") do |manifest|
217
218
  print_plugin(manifest)
218
219
  print_provenance(bundle_name, manifest)
219
220
  end
@@ -252,7 +253,7 @@ module Samagotchi
252
253
  @stderr.puts "Bundle '#{bundle_name}' not installed — falling back to install"
253
254
  # (no Plugin line here)
254
255
  installer = installer_for(expanded_source, bundle_name, prepared: prepared, scope: scope, force: force, dry_run: dry_run)
255
- return run_installer(installer, failed: "Install") do |manifest|
256
+ return run_installer(installer, bundle_name, failed: "Install") do |manifest|
256
257
  next dry_run_done if dry_run
257
258
 
258
259
  print_provenance(bundle_name, manifest)
@@ -263,7 +264,7 @@ module Samagotchi
263
264
  # reads which plugin the run touched.
264
265
  before = Samagotchi::MemoryBundle::Provenance.new(name: bundle_name).record
265
266
  manifest = nil
266
- status = run_installer(installer, failed: "Upgrade") do |run_manifest|
267
+ status = run_installer(installer, bundle_name, failed: "Upgrade") do |run_manifest|
267
268
  manifest = run_manifest
268
269
  print_plugin(manifest, plugin: !dry_run)
269
270
  next dry_run_done if dry_run
@@ -326,15 +327,23 @@ module Samagotchi
326
327
  refuse_mismatch: true, **)
327
328
  end
328
329
 
329
- # Runs +installer+, prints its summary, then yields the manifest.
330
- # Ensures the extracted source dir is cleaned up even if an exception
331
- # occurs between installer.run and the block (e.g. summary,
330
+ # Runs +installer+, prints the version line and its summary, then yields
331
+ # the manifest. Ensures the extracted source dir is cleaned up even if an
332
+ # exception occurs between installer.run and the block (e.g. summary,
332
333
  # print_plugin). The block (upgrade_conflicts) still sees
333
334
  # the files because cleanup happens after the block returns or raises.
334
335
  # @return [Integer] the block's exit status, or 1 after "<failed> failed: …"
335
- def run_installer(installer, failed:)
336
+ def run_installer(installer, bundle_name, failed:)
337
+ # The version installed before, read before the run rewrites the record.
338
+ old = Samagotchi::MemoryBundle::Provenance.new(name: bundle_name).record&.version.to_s
336
339
  _nd, manifest = installer.run
337
- @stdout.puts installer.summary
340
+ version = manifest&.version.to_s
341
+ # Current: the same version, and the run wrote nothing.
342
+ current = !version.empty? && old == version && installer.current?
343
+ line = version_line(bundle_name, old, version, current: current)
344
+ @stdout.puts line if line
345
+ summary = installer.summary(current: current)
346
+ @stdout.puts summary unless summary.empty?
338
347
  yield manifest
339
348
  rescue Samagotchi::MemoryBundle::Installer::InstallError => e
340
349
  @stderr.puts "#{failed} failed: #{e.message}"
@@ -343,12 +352,28 @@ module Samagotchi
343
352
  installer.cleanup_source!
344
353
  end
345
354
 
355
+ # The bundle and its version, first in an install's or upgrade's output
356
+ # (a dry run's too: what would be): "mcp 0.8.0 → 0.9.0" when it changes,
357
+ # "mcp 0.9.0" when it doesn't (or for a new install), "mcp 0.9.0 (already
358
+ # current)" when the run wrote nothing either (the summary leaves out its
359
+ # "Up to date" line then). Nil without a manifest version.
360
+ # @param old [String] the installed record's version, "" for none
361
+ # @return [String, nil]
362
+ def version_line(bundle_name, old, version, current:)
363
+ return if version.empty?
364
+ return "#{bundle_name} #{old} → #{version}" unless old.empty? || old == version
365
+
366
+ "#{bundle_name} #{version}#{" (already current)" if current}"
367
+ end
368
+
346
369
  def print_plugin(manifest, plugin: true)
347
370
  return unless plugin && manifest&.plugin
348
371
 
349
372
  # Installed with --force past its requires_chi: the Engine won't load it.
350
373
  failure = Samagotchi::BundleManifest.requires_chi_failure(manifest.requires_chi, Samagotchi::VERSION)
351
- @stdout.puts "Plugin: #{manifest.plugin[:file]} (#{failure ? "won't load on this chi: #{failure}" : "loads at the next chi start"})"
374
+ parts = manifest.plugin_parts.size
375
+ @stdout.puts "Plugin: #{manifest.plugin[:file]}#{" + #{parts} part#{"s" unless parts == 1}" if parts.positive?} " \
376
+ "(#{failure ? "won't load on this chi: #{failure}" : "loads at the next chi start"})"
352
377
  end
353
378
 
354
379
  # @return [Integer] 0
@@ -382,7 +407,7 @@ module Samagotchi
382
407
  def restart_hint(before, manifest = nil, installer = nil)
383
408
  return if before.nil? || before.error?
384
409
 
385
- plugin_files = [before.plugin&.file, manifest&.plugin&.dig(:file)].compact.uniq
410
+ plugin_files = ([before.plugin&.file, *before.plugin&.parts&.keys] + (manifest&.plugin_files || []).map(&:first)).compact.uniq
386
411
  changed = if installer
387
412
  plugin_files.any? { |file| CHANGED_WORDS.include?(installer.results[file]&.word) }
388
413
  else
@@ -590,6 +615,9 @@ module Samagotchi
590
615
  if (plugin = st.plugin)
591
616
  note = PLUGIN_NOTES.fetch(plugin[:state], "")
592
617
  @stdout.puts " Plugin: #{plugin[:file]} [#{plugin[:state]}]#{note}"
618
+ plugin[:parts].reject { |part| part[:state] == "ok" }.each do |part|
619
+ @stdout.puts " part #{part[:file]} [#{part[:state]}]#{PLUGIN_NOTES.fetch(part[:state], "")}"
620
+ end
593
621
  @stdout.puts " requires_chi: #{plugin[:requires_chi]}" if plugin[:requires_chi]
594
622
  @stdout.puts " not loaded: #{plugin[:requires_failure]}" if plugin[:requires_failure]
595
623
  end
@@ -607,7 +635,7 @@ module Samagotchi
607
635
  st = Samagotchi::MemoryBundle::Status.bundle_status(bname)
608
636
  mods = st.files.values.count { |v| v[:behind] || v[:missing] || v[:unreadable] }
609
637
  plugin = st.plugin
610
- mods += 1 if plugin && (plugin[:state] != "ok" || plugin[:requires_failure])
638
+ mods += 1 if plugin && (plugin[:state] != "ok" || plugin[:requires_failure] || plugin[:parts].any? { |part| part[:state] != "ok" })
611
639
  # A rule that is missing or edited doesn't load; a script runs edited.
612
640
  mods += st.rules.values.count { |state| state != "ok" } + st.scripts.values.count("missing")
613
641
  mods += 1 if st.scope_error
@@ -672,13 +700,14 @@ module Samagotchi
672
700
  @stderr.puts "Bundle '#{bname}': #{e.message}"; return 1
673
701
  end
674
702
  plugin_path = prov.plugin_path(record)
675
- plugin_file = plugin_path && File.basename(plugin_path)
703
+ # The plugin's files, its parts first (the load order).
704
+ plugin_files = plugin_path ? [*record.plugin.parts.keys, File.basename(plugin_path)] : []
676
705
  if file_arg
677
706
  # A memory first, then the plugin (an older chi's hook is no file of the bundle's)
678
707
  if record.owns?(file_arg)
679
708
  diff_memory(prov, target_dir, file_arg)
680
- elsif plugin_file && [plugin_file, "plugin/#{plugin_file}"].include?(file_arg)
681
- diff_plugin(prov, record, plugin_path)
709
+ elsif (plugin_file = plugin_files.find { |file| [file, "plugin/#{file}"].include?(file_arg) })
710
+ diff_plugin(prov, record, plugin_file)
682
711
  elsif (asset = asset_states(prov, record).find { |a| a.label == file_arg })
683
712
  diff_asset(asset)
684
713
  else
@@ -691,8 +720,8 @@ module Samagotchi
691
720
  diff_memory(prov, target_dir, key)
692
721
  @stdout.puts ""
693
722
  end
694
- if plugin_file
695
- diff_plugin(prov, record, plugin_path)
723
+ plugin_files.each do |file|
724
+ diff_plugin(prov, record, file)
696
725
  @stdout.puts ""
697
726
  end
698
727
  asset_states(prov, record).select(&:edited?).each do |a|
@@ -747,11 +776,11 @@ module Samagotchi
747
776
  end
748
777
  end
749
778
 
750
- def diff_plugin(prov, record, plugin_path)
751
- file = File.basename(plugin_path)
752
- print_base_and_current("plugin/#{file}", prov.plugin_base_path(file), plugin_path)
779
+ # One file of the plugin (+file+: the plugin file or a part, by name).
780
+ def diff_plugin(prov, record, file)
781
+ print_base_and_current("plugin/#{file}", prov.plugin_base_path(file), File.join(prov.plugin_dir, file))
753
782
  requires_chi = record.manifest.requires_chi
754
- @stdout.puts "--- metadata: sha256=#{record.plugin.sha256}#{" requires_chi=#{requires_chi}" if requires_chi}"
783
+ @stdout.puts "--- metadata: sha256=#{record.plugin_shas[file]}#{" requires_chi=#{requires_chi}" if requires_chi}"
755
784
  end
756
785
 
757
786
  def print_base_and_current(heading, base, current)
@@ -26,6 +26,9 @@ module Samagotchi
26
26
  # sha256: sha256:0a1b...
27
27
  # required: true # optional: a guardrail; when it fails to load
28
28
  # # chi denies every tool call (docs/hooks.md)
29
+ # parts: # optional — more .rb files of the plugin, each
30
+ # client.rb: sha256:… # evaluated in this order before file:, into
31
+ # # the same namespace (#plugin_files)
29
32
  # requires_chi: ">= 0.1.28" # optional — a Gem::Requirement for chi's VERSION
30
33
  # requires_chi_next: true # optional, shipped bundles — ignored here:
31
34
  # # `rake release:bump` raises requires_chi to
@@ -92,6 +95,8 @@ module Samagotchi
92
95
 
93
96
  # A plugin file is a plain .rb name in the bundle's top directory.
94
97
  PLUGIN_FILE = /\A[A-Za-z0-9_][A-Za-z0-9_.-]*\.rb\z/
98
+ # The most plugin parts (plugin: parts:) a manifest may list.
99
+ MAX_PLUGIN_PARTS = 16
95
100
  # A script is a plain file name in the bundle's scripts/.
96
101
  SCRIPT_FILE = /\A[A-Za-z0-9_][A-Za-z0-9_.-]*\z/
97
102
  # A need is a plain executable name: no path, no spaces.
@@ -126,7 +131,8 @@ module Samagotchi
126
131
  "settings.types" => "0.92.0",
127
132
  "settings" => "0.143.0",
128
133
  "block_renderers" => "0.151.0",
129
- "plugin.required" => "0.157.0"
134
+ "plugin.required" => "0.157.0",
135
+ "plugin.parts" => "0.183.0"
130
136
  }.freeze
131
137
 
132
138
  # files: name → "sha256:<hex>" (both forms); file_descriptions: name →
@@ -239,6 +245,32 @@ module Samagotchi
239
245
  sha.delete_prefix("sha256:")
240
246
  end
241
247
 
248
+ # @return [String, nil] a plugin part's sha256 hex, "sha256:" stripped,
249
+ # nil when the manifest gives none (or names no such part)
250
+ def checksum_for_part(name)
251
+ sha = plugin_parts[name.to_s].to_s
252
+ sha.empty? ? nil : sha.delete_prefix("sha256:")
253
+ end
254
+
255
+ # The plugin's parts (plugin: parts:), name => "sha256:<hex>" ("" when
256
+ # not given), in the manifest's order; {} without any.
257
+ def plugin_parts = @plugin&.dig(:parts) || {}
258
+
259
+ # The one wording of a part the manifest lists that the bundle's dir
260
+ # lacks (the install refuses it, chi bundle test/try raise it; callers
261
+ # add their context).
262
+ def self.missing_part(part) = "the manifest names plugin part #{part}, which the bundle doesn't have"
263
+
264
+ # Every file of the plugin with its sha, in load order: the parts as
265
+ # listed, then the plugin file; [] without a plugin. The one list the
266
+ # loader, the installer and chi bundle check walk.
267
+ # @return [Array(Array(String, String))]
268
+ def plugin_files
269
+ return [] unless @plugin
270
+
271
+ plugin_parts.to_a + [[@plugin[:file], @plugin[:sha256].to_s]]
272
+ end
273
+
242
274
  # Why chi +version+ can't load this bundle's plugin, or nil when it can
243
275
  # (or the bundle names no requirement).
244
276
  def self.requires_chi_failure(requirement, version)
@@ -273,6 +305,7 @@ module Samagotchi
273
305
  found["context_providers"] = SINCE["context_providers"] unless @context_providers.empty?
274
306
  found["block_renderers"] = SINCE["block_renderers"] unless @block_renderers.empty?
275
307
  found["plugin.required"] = SINCE["plugin.required"] if @plugin&.dig(:required) && SINCE.key?("plugin.required")
308
+ found["plugin.parts"] = SINCE["plugin.parts"] if @plugin&.dig(:parts) && SINCE.key?("plugin.parts")
276
309
  found["session_settings"] = SINCE["session_settings"] unless @session_settings.empty?
277
310
  found["settings"] = SINCE["settings"] unless @settings.nil?
278
311
  found["files.description"] = SINCE["files.description"] if raw["files"].is_a?(Hash) && raw["files"].values.any?(Hash)
@@ -302,6 +335,8 @@ module Samagotchi
302
335
  sha = plugin[:sha256].to_s
303
336
  manifest["plugin"] = { "file" => plugin[:file].to_s, "sha256" => sha.start_with?("sha256:") ? sha : "sha256:#{sha}" }
304
337
  manifest["plugin"]["required"] = true if plugin[:required]
338
+ parts = plugin[:parts] || {}
339
+ manifest["plugin"]["parts"] = parts.to_h { |part, part_sha| [part.to_s, prefixed_sha(part_sha)] } unless parts.empty?
305
340
  end
306
341
  manifest["requires_chi"] = requires_chi.to_s if requires_chi && !requires_chi.to_s.empty?
307
342
  needs = parse_needs(needs)
@@ -384,6 +419,13 @@ module Samagotchi
384
419
  end
385
420
  end
386
421
 
422
+ # +sha+ with the "sha256:" prefix (as the plugin's is written).
423
+ def self.prefixed_sha(sha)
424
+ sha = sha.to_s
425
+ sha.start_with?("sha256:") ? sha : "sha256:#{sha}"
426
+ end
427
+ private_class_method :prefixed_sha
428
+
387
429
  def self.optional_text(value)
388
430
  text = value.to_s.strip
389
431
  text.empty? ? nil : text
@@ -492,8 +534,9 @@ module Samagotchi
492
534
  end
493
535
  end
494
536
 
495
- # plugin: {file:, sha256:, required:} → {file:, sha256:} (sha "" when
496
- # not given) with required: true when it says so, or nil; a file that
537
+ # plugin: {file:, sha256:, required:, parts:} → {file:, sha256:} (sha ""
538
+ # when not given) with required: true when it says so and parts: {name
539
+ # => sha} (#parse_plugin_parts) when it lists any, or nil; a file that
497
540
  # isn't a plain .rb name, or a required: that isn't true or false, is a
498
541
  # validation error.
499
542
  def parse_plugin(raw)
@@ -510,7 +553,61 @@ module Samagotchi
510
553
  required = raw.key?("required") ? raw["required"] : raw[:required]
511
554
  raise ValidationError, "plugin required: must be true or false, not #{required.inspect}" unless [nil, true, false].include?(required)
512
555
 
513
- required ? { file: file, sha256: sha, required: true } : { file: file, sha256: sha }
556
+ parsed = required ? { file: file, sha256: sha, required: true } : { file: file, sha256: sha }
557
+ parts = parse_plugin_parts(raw.key?("parts") ? raw["parts"] : raw[:parts], file)
558
+ parts.empty? ? parsed : parsed.merge(parts: parts)
559
+ end
560
+
561
+ # plugin: parts: {name => sha256} → the same, in the order listed (never
562
+ # sorted: it is the load order), each sha "sha256:" prefixed ("" kept);
563
+ # {} when absent. A name that isn't a plain .rb file name in the top
564
+ # directory, the plugin file's own name, a name the plugin file or an
565
+ # earlier part has in another case (one file on a case-insensitive disk:
566
+ # Plugin.rb beside plugin.rb), a name listed twice (#duplicate_part) or
567
+ # more than MAX_PLUGIN_PARTS entries is a ValidationError naming it.
568
+ def parse_plugin_parts(raw, plugin_file)
569
+ return {} if raw.nil?
570
+ raise ValidationError, "plugin parts: must be a mapping of file names to sha256" unless raw.is_a?(Hash)
571
+ if raw.size > MAX_PLUGIN_PARTS
572
+ raise ValidationError, "plugin parts: lists #{raw.size} files, at most #{MAX_PLUGIN_PARTS}"
573
+ end
574
+
575
+ twice = duplicate_part
576
+ raise ValidationError, "plugin parts: #{twice} is listed twice" if twice
577
+
578
+ raw.each_with_object({}) do |(name, sha), parts|
579
+ name = name.to_s.strip
580
+ unless name.match?(PLUGIN_FILE)
581
+ raise ValidationError, "plugin parts: #{name.inspect} isn't a .rb file name in the bundle's top directory"
582
+ end
583
+ raise ValidationError, "plugin parts: #{name} is the plugin file itself" if name == plugin_file
584
+
585
+ twin = [plugin_file, *parts.keys].find { |other| other.casecmp?(name) }
586
+ raise ValidationError, "plugin parts: #{name} and #{twin} differ only in case (one file on a case-insensitive disk)" if twin
587
+
588
+ sha = sha.to_s.strip
589
+ parts[name] = sha.empty? || sha.start_with?("sha256:") ? sha : "sha256:#{sha}"
590
+ end
591
+ end
592
+
593
+ # The first part name manifest.yml's plugin: parts: mapping lists twice,
594
+ # or nil. YAML.load keeps the last of two equal keys silently, so this
595
+ # reads the file's node tree (only for a manifest that has parts).
596
+ def duplicate_part
597
+ return nil unless @path && File.file?(@path)
598
+
599
+ parts = yaml_value(yaml_value(Psych.parse_file(@path)&.root, "plugin"), "parts")
600
+ return nil unless parts.is_a?(Psych::Nodes::Mapping)
601
+
602
+ keys = parts.children.each_slice(2).map { |key, _| key.respond_to?(:value) ? key.value.to_s.strip : nil }.compact
603
+ keys.find { |key| keys.count(key) > 1 }
604
+ end
605
+
606
+ # A YAML mapping node's value node under the scalar +key+, or nil.
607
+ def yaml_value(node, key)
608
+ return nil unless node.is_a?(Psych::Nodes::Mapping)
609
+
610
+ node.children.each_slice(2).find { |k, _| k.is_a?(Psych::Nodes::Scalar) && k.value == key }&.last
514
611
  end
515
612
 
516
613
  # scripts: {file => sha256} → the same, "sha256:" prefixed; {} when absent.
@@ -1,10 +1,10 @@
1
1
  ---
2
2
  name: dev
3
- version: 0.7.8
3
+ version: 0.7.9
4
4
  scope: system
5
5
  description: "Developer tools"
6
6
  trust_level: reviewed
7
- requires_chi: ">= 0.155.0"
7
+ requires_chi: ">= 0.183.0"
8
8
  includes:
9
9
  - known-names
10
10
  - mcp