samagotchi 0.178.0 → 0.180.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 (141) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +58 -1
  3. data/docs/architecture.md +4 -2
  4. data/docs/bundles.md +48 -8
  5. data/docs/cli.md +3 -1
  6. data/docs/configuration.md +1 -1
  7. data/docs/context.md +32 -1
  8. data/docs/guardrails.md +2 -1
  9. data/docs/internals/attached-context.md +26 -4
  10. data/docs/internals/background-tasks.md +5 -1
  11. data/docs/internals/block-renderers.md +4 -3
  12. data/docs/internals/bridge.md +11 -2
  13. data/docs/internals/bundles.md +65 -34
  14. data/docs/internals/engine.md +1 -1
  15. data/docs/internals/web-frontend.md +81 -3
  16. data/docs/plugins.md +20 -5
  17. data/docs/releasing.md +3 -3
  18. data/docs/scheduling.md +4 -0
  19. data/docs/sessions.md +8 -2
  20. data/docs/testing.md +8 -1
  21. data/lib/samagotchi/block_renderers.rb +33 -16
  22. data/lib/samagotchi/bootstrap/bundles.rb +2 -2
  23. data/lib/samagotchi/bootstrap_command.rb +1 -1
  24. data/lib/samagotchi/bridge/card_wait.rb +34 -0
  25. data/lib/samagotchi/bridge/turn_accumulator.rb +22 -4
  26. data/lib/samagotchi/bundle_command/check.rb +10 -10
  27. data/lib/samagotchi/bundle_command/installed_check.rb +2 -2
  28. data/lib/samagotchi/bundle_command/try/sandbox.rb +3 -3
  29. data/lib/samagotchi/bundle_command.rb +22 -18
  30. data/lib/samagotchi/bundle_manifest.rb +579 -0
  31. data/lib/samagotchi/bundle_needs.rb +9 -8
  32. data/lib/samagotchi/bundle_settings_check.rb +13 -9
  33. data/lib/samagotchi/bundles/claude-peer/manifest.yml +5 -3
  34. data/lib/samagotchi/bundles/claude-peer/plugin.rb +4 -4
  35. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +11 -6
  36. data/lib/samagotchi/bundles/codex-peer/manifest.yml +5 -3
  37. data/lib/samagotchi/bundles/codex-peer/plugin.rb +4 -3
  38. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +11 -6
  39. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  40. data/lib/samagotchi/bundles/system/memory_guide.md +1 -1
  41. data/lib/samagotchi/config.rb +2 -2
  42. data/lib/samagotchi/context_absorber.rb +36 -6
  43. data/lib/samagotchi/context_note.rb +8 -5
  44. data/lib/samagotchi/context_providers.rb +30 -29
  45. data/lib/samagotchi/context_sources.rb +42 -12
  46. data/lib/samagotchi/engine/messages.rb +1 -1
  47. data/lib/samagotchi/events.rb +6 -5
  48. data/lib/samagotchi/guardrail_wiring.rb +20 -21
  49. data/lib/samagotchi/guardrails/approval.rb +3 -2
  50. data/lib/samagotchi/guardrails/outside.rb +3 -3
  51. data/lib/samagotchi/{memory_bundle → installed_bundles}/bundle_hook.rb +1 -1
  52. data/lib/samagotchi/installed_bundles/dir.rb +91 -0
  53. data/lib/samagotchi/{memory_bundle → installed_bundles}/file_digest.rb +1 -1
  54. data/lib/samagotchi/{memory_bundle → installed_bundles}/file_standing.rb +2 -2
  55. data/lib/samagotchi/{memory_bundle/installed_manifest.rb → installed_bundles/manifest.rb} +21 -21
  56. data/lib/samagotchi/{memory_bundle → installed_bundles}/plugin_ref.rb +2 -2
  57. data/lib/samagotchi/{memory_bundle/installed_bundle.rb → installed_bundles/record.rb} +17 -17
  58. data/lib/samagotchi/installed_bundles/sha.rb +41 -0
  59. data/lib/samagotchi/installed_bundles/standing.rb +65 -0
  60. data/lib/samagotchi/installed_bundles.rb +105 -0
  61. data/lib/samagotchi/memory_bundle/asset_installer.rb +7 -7
  62. data/lib/samagotchi/memory_bundle/builder.rb +13 -13
  63. data/lib/samagotchi/memory_bundle/file_state.rb +9 -9
  64. data/lib/samagotchi/memory_bundle/installed_copy.rb +6 -6
  65. data/lib/samagotchi/memory_bundle/installer.rb +33 -33
  66. data/lib/samagotchi/memory_bundle/listing.rb +5 -5
  67. data/lib/samagotchi/memory_bundle/manifest_standing.rb +7 -7
  68. data/lib/samagotchi/memory_bundle/merger.rb +15 -15
  69. data/lib/samagotchi/memory_bundle/profile.rb +8 -8
  70. data/lib/samagotchi/memory_bundle/provenance.rb +59 -192
  71. data/lib/samagotchi/memory_bundle/remerge.rb +16 -16
  72. data/lib/samagotchi/memory_bundle/shas.rb +2 -2
  73. data/lib/samagotchi/memory_bundle/shipped_history.rb +1 -1
  74. data/lib/samagotchi/memory_bundle/shipped_update.rb +17 -17
  75. data/lib/samagotchi/memory_bundle/status.rb +19 -16
  76. data/lib/samagotchi/memory_bundle/system_bundle.rb +12 -12
  77. data/lib/samagotchi/memory_bundle/trash.rb +2 -2
  78. data/lib/samagotchi/memory_bundle/uninstaller.rb +8 -8
  79. data/lib/samagotchi/memory_bundle.rb +2 -2
  80. data/lib/samagotchi/memory_command.rb +4 -4
  81. data/lib/samagotchi/{memory_bundle → memory_index}/entry_size.rb +8 -8
  82. data/lib/samagotchi/{memory_bundle/index_size.rb → memory_index/size.rb} +6 -6
  83. data/lib/samagotchi/{memory_bundle/index_sync.rb → memory_index/sync.rb} +5 -5
  84. data/lib/samagotchi/{memory_bundle/index_updater.rb → memory_index/updater.rb} +2 -2
  85. data/lib/samagotchi/memory_usage.rb +5 -5
  86. data/lib/samagotchi/muted_memories.rb +1 -1
  87. data/lib/samagotchi/peer_exchanges.rb +76 -0
  88. data/lib/samagotchi/plugin/context.rb +4 -0
  89. data/lib/samagotchi/plugin/loader.rb +35 -33
  90. data/lib/samagotchi/plugin/peer.rb +42 -11
  91. data/lib/samagotchi/project_subfolders.rb +9 -9
  92. data/lib/samagotchi/reply_wait/target.rb +11 -4
  93. data/lib/samagotchi/self_report.rb +14 -14
  94. data/lib/samagotchi/session_access/local.rb +16 -1
  95. data/lib/samagotchi/session_access/remote.rb +2 -1
  96. data/lib/samagotchi/session_config.rb +29 -19
  97. data/lib/samagotchi/session_manager/running_tasks.rb +15 -2
  98. data/lib/samagotchi/session_retention.rb +13 -4
  99. data/lib/samagotchi/session_stores.rb +3 -0
  100. data/lib/samagotchi/system_prompt.rb +4 -4
  101. data/lib/samagotchi/terminal_ui/attach_launcher.rb +5 -1
  102. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  103. data/lib/samagotchi/terminal_ui/formatting.rb +15 -5
  104. data/lib/samagotchi/terminal_ui/join_replay.rb +1 -1
  105. data/lib/samagotchi/terminal_ui/model_segments_text.rb +5 -5
  106. data/lib/samagotchi/terminal_ui.rb +4 -1
  107. data/lib/samagotchi/testing/bundle.rb +3 -3
  108. data/lib/samagotchi/testing/install.rb +3 -3
  109. data/lib/samagotchi/testing/sandbox.rb +2 -2
  110. data/lib/samagotchi/tool_runner.rb +14 -14
  111. data/lib/samagotchi/tools/memory.rb +22 -22
  112. data/lib/samagotchi/tools/task_runtime.rb +12 -3
  113. data/lib/samagotchi/update_command.rb +4 -4
  114. data/lib/samagotchi/version.rb +1 -1
  115. data/lib/samagotchi/web/app.rb +14 -1
  116. data/lib/samagotchi/web/message_display.rb +6 -0
  117. data/lib/samagotchi/web/participant_view.rb +1 -1
  118. data/lib/samagotchi/web/proxy_gate.rb +1 -0
  119. data/lib/samagotchi/web/public/app.js +13 -1
  120. data/lib/samagotchi/web/public/card_kinds.js +8 -3
  121. data/lib/samagotchi/web/public/cards_place.js +15 -1
  122. data/lib/samagotchi/web/public/context_chips.js +184 -49
  123. data/lib/samagotchi/web/public/ctx.js +1 -1
  124. data/lib/samagotchi/web/public/history_shape.js +8 -3
  125. data/lib/samagotchi/web/public/index.html +66 -0
  126. data/lib/samagotchi/web/public/peer_chip.js +171 -0
  127. data/lib/samagotchi/web/public/peer_data.js +119 -0
  128. data/lib/samagotchi/web/public/peer_exchange.js +83 -0
  129. data/lib/samagotchi/web/public/peer_links.js +161 -0
  130. data/lib/samagotchi/web/public/peer_note.js +49 -0
  131. data/lib/samagotchi/web/public/session_view.js +6 -1
  132. data/lib/samagotchi/web/public/stream_handlers.js +17 -4
  133. data/lib/samagotchi/web/public/turn_block.js +46 -8
  134. data/lib/samagotchi/web/public/turn_html.js +11 -2
  135. data/lib/samagotchi/web/public/turn_settle.js +117 -0
  136. data/lib/samagotchi/web/push/notifier.rb +2 -2
  137. data/lib/samagotchi/web/session_access_routes.rb +21 -1
  138. data/lib/samagotchi/web/session_hub.rb +20 -4
  139. data/lib/samagotchi/worker_boot.rb +4 -1
  140. metadata +24 -12
  141. data/lib/samagotchi/memory_bundle/manifest.rb +0 -581
@@ -8,7 +8,7 @@ require_relative "guardrails"
8
8
  require_relative "guardrails/parent_approvals"
9
9
  require_relative "guardrails/parent_policy"
10
10
  require_relative "model_match"
11
- require_relative "memory_bundle/manifest"
11
+ require_relative "bundle_manifest"
12
12
  require_relative "version"
13
13
  require_relative "hooks"
14
14
  require_relative "edit_preview"
@@ -220,31 +220,30 @@ module Samagotchi
220
220
  # chi doesn't meet isn't read (its rules may use keys this chi doesn't
221
221
  # know): the rules last loaded from it stay (#too_new_bundle_rules).
222
222
  def bundle_rules
223
- require_relative "memory_bundle/provenance"
223
+ require_relative "installed_bundles"
224
224
  rules = []
225
- MemoryBundle::Provenance.each_installed(holding: :guardrails) do |bundle_name, bundle|
226
- if bundle.error?
227
- Log.warn(:guardrails, "bundle_rules_invalid", echo: "[samagotchi:guardrails] bundle #{bundle_name}: #{bundle.error}", bundle: bundle_name)
228
- @failures.add("rules (bundle #{bundle_name})", bundle.error, required: true, group: :rules)
225
+ InstalledBundles.standings(holding: :guardrails) do |standing|
226
+ bundle_name = standing.name
227
+ bundle = standing.record
228
+ if standing.unreadable?
229
+ why = standing.refusal.text
230
+ Log.warn(:guardrails, "bundle_rules_invalid", echo: "[samagotchi:guardrails] bundle #{bundle_name}: #{why}", bundle: bundle_name)
231
+ @failures.add("rules (bundle #{bundle_name})", why, required: true, group: :rules)
229
232
  next
230
233
  end
231
- manifest = bundle.manifest
232
- if (too_new = manifest.requires_chi_failure)
233
- rules.concat(too_new_bundle_rules(bundle_name, manifest, too_new))
234
+ if standing.too_new?
235
+ rules.concat(too_new_bundle_rules(bundle_name, bundle.manifest, standing.refusal.text))
234
236
  next
235
237
  end
236
- dir = MemoryBundle::Provenance.new(name: bundle_name).guardrails_dir
238
+ installed = InstalledBundles::Dir.new(name: bundle_name)
237
239
  loaded = []
238
240
  whole = true
239
241
  bundle.guardrails.sort.each do |basename, sha|
240
242
  what = "rules #{basename} (bundle #{bundle_name})"
241
- path = File.join(dir, basename)
243
+ path = File.join(installed.guardrails_dir, basename)
242
244
  begin
243
- raise Guardrails::Rules::ParseError, "the file is missing" unless File.file?(path)
244
-
245
- unless MemoryBundle::Provenance.sha_matches?(path, sha)
246
- raise Guardrails::Rules::ParseError, "its sha256 differs from the installed one (edited after install? reinstall the bundle)"
247
- end
245
+ refusal = installed.file_refusal(:guardrails, basename, sha)
246
+ raise Guardrails::Rules::ParseError, refusal.text if refusal
248
247
 
249
248
  doc = YAML.safe_load_file(path)
250
249
  raise Guardrails::Rules::ParseError, "expected a mapping with rules:" unless doc.is_a?(Hash)
@@ -268,13 +267,13 @@ module Samagotchi
268
267
 
269
268
  def protected_paths
270
269
  @protected ||= begin
271
- require_relative "memory_bundle/provenance"
270
+ require_relative "installed_bundles"
272
271
  require_relative "context_sources"
273
272
  require_relative "chi_home"
274
273
  require_relative "chi_shim"
275
274
  Guardrails::ProtectedPaths.new(
276
275
  store_dir: File.dirname(@approvals.path),
277
- bundles_dir: MemoryBundle::Provenance.bundles_dir,
276
+ bundles_dir: InstalledBundles.dir,
278
277
  config_path: Samagotchi::ConfigFile.global_path,
279
278
  hooks_dir: Hooks::Loader.hooks_dir,
280
279
  context_dir: ContextSources.root,
@@ -533,7 +532,7 @@ module Samagotchi
533
532
  # load the new ones). Never loaded (a chi older than the bundle it
534
533
  # starts with): none, and a required load failure, as the hook loader
535
534
  # does for a guardrail hook, so the calls aren't quietly unguarded.
536
- # @param manifest [MemoryBundle::InstalledManifest]
535
+ # @param manifest [InstalledBundles::Manifest]
537
536
  def too_new_bundle_rules(bundle_name, manifest, reason)
538
537
  kept = @loaded_bundle_rules[bundle_name]
539
538
  unless kept
@@ -565,9 +564,9 @@ module Samagotchi
565
564
  # manifest.json, kept manifest.yml (its requires_chi is read from there)
566
565
  # and guardrails/ file.
567
566
  def rules_stamp
568
- require_relative "memory_bundle/provenance"
567
+ require_relative "installed_bundles"
569
568
  paths = [Samagotchi::ConfigFile.global_path] +
570
- Dir[File.join(MemoryBundle::Provenance.bundles_dir, "*", "{manifest.json,manifest.yml,guardrails/*}")]
569
+ Dir[File.join(InstalledBundles.dir, "*", "{manifest.json,manifest.yml,guardrails/*}")]
571
570
  paths.compact.sort.map do |path|
572
571
  stat = File.stat(path)
573
572
  [path, stat.mtime.to_r, stat.size]
@@ -3,6 +3,7 @@
3
3
  require "json"
4
4
 
5
5
  require_relative "../question_answer"
6
+ require_relative "verdict"
6
7
 
7
8
  module Samagotchi
8
9
  module Guardrails
@@ -165,8 +166,8 @@ module Samagotchi
165
166
 
166
167
  facts = { name: entry[:name], scope: entry[:scope] }
167
168
  if targets.memory_remove?
168
- require_relative "../memory_bundle/index_updater"
169
- description = MemoryBundle::IndexUpdater.description_for(entry[:scope], entry[:name])
169
+ require_relative "../memory_index/updater"
170
+ description = MemoryIndex::Updater.description_for(entry[:scope], entry[:name])
170
171
  facts[:description] = description unless description.to_s.strip.empty?
171
172
  end
172
173
  facts
@@ -2,14 +2,14 @@
2
2
 
3
3
  require "pathname"
4
4
  require_relative "path_resolve"
5
- require_relative "../memory_bundle/index_sync"
5
+ require_relative "../memory_index/sync"
6
6
 
7
7
  module Samagotchi
8
8
  module Guardrails
9
9
  # What `outside_repo` means (a rule's path: and git:): a path is outside
10
10
  # when it isn't in the session's repo root (the cwd outside a repo),
11
11
  # with symlinks resolved on both sides. Never outside:
12
- # - a memory's file (MemoryBundle::IndexSync.memory_scope): write/edit
12
+ # - a memory's file (MemoryIndex::Sync.memory_scope): write/edit
13
13
  # there is what memory_write does; index.md, the bundles dir and the
14
14
  # rest of chi's config dir still count;
15
15
  # - a tmp dir (Dir.tmpdir, /tmp, /var/tmp), unless the session's root
@@ -29,7 +29,7 @@ module Samagotchi
29
29
  tmp = nil
30
30
  reals.any? do |real|
31
31
  next false if under_folding_existing?(real, session)
32
- next false if MemoryBundle::IndexSync.memory_scope(path)
32
+ next false if MemoryIndex::Sync.memory_scope(path)
33
33
  next false if (tmp ||= session_tmp_roots(session)).any? { |dir| under_folding_existing?(real, dir) }
34
34
 
35
35
  true
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- module MemoryBundle
4
+ module InstalledBundles
5
5
  # One class-file hook's sha256 in an older chi's installed record; its
6
6
  # other fields are ignored now that class-file hooks are retired.
7
7
  BundleHook = Data.define(:sha256) do
@@ -0,0 +1,91 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../memory_paths"
4
+ require_relative "record"
5
+ require_relative "sha"
6
+ require_relative "standing"
7
+
8
+ module Samagotchi
9
+ module InstalledBundles
10
+ # One installed bundle's folder, <bundles dir>/<name>/, as the runtime
11
+ # reads it: its record and where its plugin, scripts, rules and kept
12
+ # manifest.yml are. Its only state is the name and the folder; writing
13
+ # the record is MemoryBundle::Provenance's (a Dir too).
14
+ class Dir
15
+ # Each kind of file Dir#file_refusal checks, and its folder.
16
+ FOLDERS = { plugin: "plugin", scripts: "scripts", guardrails: "guardrails" }.freeze
17
+
18
+ attr_reader :name, :bundle_dir
19
+
20
+ # @param dir [String] the bundles dir (InstalledBundles.dir by default)
21
+ def initialize(name:, dir: MemoryPaths.bundles_dir)
22
+ @name = name
23
+ @bundle_dir = File.join(dir, name)
24
+ end
25
+
26
+ # Installed guardrail rule files (guardrails/*.yml).
27
+ def guardrails_dir
28
+ File.join(@bundle_dir, "guardrails")
29
+ end
30
+
31
+ # The installed plugin file's dir (plugin/<file>).
32
+ def plugin_dir
33
+ File.join(@bundle_dir, "plugin")
34
+ end
35
+
36
+ # The installed scripts' dir (scripts/<file>): what a context
37
+ # provider's command finds as {bundle_dir}/scripts.
38
+ def scripts_dir
39
+ File.join(@bundle_dir, "scripts")
40
+ end
41
+
42
+ # The bundle's manifest.yml as installed (Record#kept_manifest_path).
43
+ def kept_manifest_path
44
+ File.join(@bundle_dir, "manifest.yml")
45
+ end
46
+
47
+ # The plugin file as installed, or nil when the bundle has none.
48
+ # @param bundle [Record, nil] its record (read when not given)
49
+ def plugin_path(bundle = record)
50
+ file = bundle&.plugin&.file
51
+ file ? File.join(plugin_dir, file) : nil
52
+ end
53
+
54
+ # Why chi won't load or run this installed file, nil when it is as
55
+ # installed: the one "is this file as installed" check of what chi loads
56
+ # (the plugin), runs (a provider's scripts), serves (a renderer's
57
+ # scripts) or reads (the rules). A --dev install's links pass. Hashes
58
+ # only the file asked about. One order and one wording for every kind:
59
+ # no sha recorded, then missing, then changed, each "bundle X's
60
+ # <folder>/<file> … (reinstall the bundle)".
61
+ # @param kind [Symbol] :plugin, :scripts or :guardrails
62
+ # @param file [String, nil] its name in that folder (nil: a plugin the
63
+ # record names no file of, which is missing)
64
+ # @param recorded [String, nil] the record's sha256 for it
65
+ # @return [Refusal, nil]
66
+ def file_refusal(kind, file, recorded)
67
+ folder = FOLDERS.fetch(kind) { raise ArgumentError, "no installed files of kind #{kind.inspect}" }
68
+ what = "bundle #{@name}'s #{folder}#{"/#{file}" if file}"
69
+ expected = Sha.recorded(recorded)
70
+ return Refusal.new(kind: :file_unrecorded, text: "#{what} isn't recorded (reinstall the bundle)") if expected.empty?
71
+
72
+ path = file && File.join(@bundle_dir, folder, file)
73
+ return Refusal.new(kind: :file_missing, text: "#{what} is missing (reinstall the bundle)") unless path && File.file?(path)
74
+ return nil if Sha.unchecked?(recorded)
75
+
76
+ actual = Sha.of_file(path)
77
+ return nil if actual == expected
78
+
79
+ Refusal.new(kind: :file_changed, actual: actual, expected: expected,
80
+ text: "#{what} differs from the installed one (reinstall the bundle)")
81
+ end
82
+
83
+ # The installed bundle's record, nil when it isn't installed.
84
+ # @return [Record, nil]
85
+ # @raise [JSON::ParserError] manifest.json doesn't parse
86
+ def record
87
+ Record.read(@name, File.join(@bundle_dir, "manifest.json"))
88
+ end
89
+ end
90
+ end
91
+ end
@@ -3,7 +3,7 @@
3
3
  require "digest"
4
4
 
5
5
  module Samagotchi
6
- module MemoryBundle
6
+ module InstalledBundles
7
7
  # The one digest of a file on disk, so the sha256 written by a record and
8
8
  # the one read back at load time come from the same code.
9
9
  #
@@ -4,7 +4,7 @@ require "time"
4
4
  require_relative "file_digest"
5
5
 
6
6
  module Samagotchi
7
- module MemoryBundle
7
+ module InstalledBundles
8
8
  # What chi says of an edited bundle file, everywhere it reports one: the one
9
9
  # owner of these words (chi update's row and --json, chi bundle list and
10
10
  # status, chi self, chi bootstrap, SystemBundle.ensure!'s echo, the Installer's
@@ -63,7 +63,7 @@ module Samagotchi
63
63
  end
64
64
 
65
65
  # A merge no report has shown yet, as the file's record keeps it
66
- # (Provenance#note_merged). Every merge leaves one: a worker's reaches nothing
66
+ # (MemoryBundle::Provenance#note_merged). Every merge leaves one: a worker's reaches nothing
67
67
  # but its log, an automatic update's rows no person. It shows in chi bundle
68
68
  # status while it exists. A report someone reads ends it: chi update's row, for
69
69
  # what it printed (this run's merges too), and chi bundle upgrade's output. No
@@ -2,21 +2,21 @@
2
2
 
3
3
  require_relative "../context_providers"
4
4
  require_relative "../version"
5
- require_relative "manifest"
6
- require_relative "provenance"
5
+ require_relative "../bundle_manifest"
6
+ require_relative "sha"
7
7
 
8
8
  module Samagotchi
9
9
  autoload :BlockRenderers, File.expand_path("../block_renderers", __dir__)
10
10
 
11
- module MemoryBundle
11
+ module InstalledBundles
12
12
  # The one answer to "this installed bundle's manifest": what its
13
13
  # manifest.yml says, as the readers of an installed bundle take it
14
- # (InstalledBundle#manifest). Identity and state stay on the record
15
- # (InstalledBundle: name, version, source, the plugin's file, every
14
+ # (Record#manifest). Identity and state stay on the record
15
+ # (Record: name, version, source, the plugin's file, every
16
16
  # sha, files, marks, a profile's members); manifest data is read here.
17
17
  #
18
18
  # Read from the bundle's kept manifest.yml (.bundles/<name>/manifest.yml,
19
- # Provenance#write's manifest_from:) when the record has its sha and the
19
+ # MemoryBundle::Provenance#write's manifest_from:) when the record has its sha and the
20
20
  # file still has it (a dev install's link is never checked): origin
21
21
  # :kept, so a key the record never mirrored (one an older chi didn't
22
22
  # know) is read. Otherwise from the record's frozen mirror of the
@@ -30,7 +30,7 @@ module Samagotchi
30
30
  # kept, another sha, or not a problems["manifest.yml"] = :edited
31
31
  # YAML mapping
32
32
  # kept, its sha origin :kept
33
- # Each key is parsed on its own (Manifest.tolerant for the kept file): one
33
+ # Each key is parsed on its own (BundleManifest.tolerant for the kept file): one
34
34
  # that doesn't parse reads as the mirror's value if that parses, else as
35
35
  # empty, and its message is in +problems+ (key => message), so one bad key
36
36
  # never costs the rest. A reader that logged such a key logs problems[key].
@@ -39,7 +39,7 @@ module Samagotchi
39
39
  #
40
40
  # requires_chi, trust_level the strings as recorded, nil when none
41
41
  # description the kept manifest's, nil from the record
42
- # needs Manifest.parse_needs'
42
+ # needs BundleManifest.parse_needs'
43
43
  # script_names the scripts/ files it lists
44
44
  # context_providers Array<ContextProviders::Provider>
45
45
  # block_renderers Array<BlockRenderers::Renderer>
@@ -55,13 +55,13 @@ module Samagotchi
55
55
  # includes a profile's recorded members (the record's
56
56
  # identity, never the kept file's), nil for a plain bundle
57
57
  # unknown_keys the kept manifest's keys this chi doesn't know
58
- # floor_keys the kept manifest's Manifest#floor_keys (key =>
58
+ # floor_keys the kept manifest's BundleManifest#floor_keys (key =>
59
59
  # the first chi that read it), {} from the record
60
60
  # origin :kept or :record
61
- class InstalledManifest < Data.define(:name, :version, :requires_chi, :trust_level, :description, :needs,
62
- :script_names, :context_providers, :block_renderers, :session_settings,
63
- :session_setting_types, :settings, :setting_types, :file_descriptions,
64
- :plugin_required, :includes, :unknown_keys, :floor_keys, :problems, :origin)
61
+ class Manifest < Data.define(:name, :version, :requires_chi, :trust_level, :description, :needs,
62
+ :script_names, :context_providers, :block_renderers, :session_settings,
63
+ :session_setting_types, :settings, :setting_types, :file_descriptions,
64
+ :plugin_required, :includes, :unknown_keys, :floor_keys, :problems, :origin)
65
65
  # The key problems names the kept file's standing under.
66
66
  KEPT_FILE = "manifest.yml"
67
67
  # How many manifests the cache holds before it starts again.
@@ -69,8 +69,8 @@ module Samagotchi
69
69
  @cache = {}
70
70
  @cache_lock = Mutex.new
71
71
 
72
- # @param bundle [InstalledBundle]
73
- # @return [InstalledManifest]
72
+ # @param bundle [Record]
73
+ # @return [Manifest]
74
74
  def self.of(bundle)
75
75
  key = [bundle, kept_stat(bundle.kept_manifest_path)]
76
76
  cached = @cache_lock.synchronize { @cache[key] }
@@ -109,11 +109,11 @@ module Samagotchi
109
109
  recorded = !bundle.manifest_sha.to_s.empty?
110
110
  return recorded ? standing(mirrored, :missing) : mirrored unless File.file?(path)
111
111
  return standing(mirrored, :stale) unless recorded
112
- return standing(mirrored, :edited) unless Provenance.sha_matches?(path, bundle.manifest_sha)
112
+ return standing(mirrored, :edited) unless Sha.matches?(path, bundle.manifest_sha)
113
113
 
114
- kept, problems = Manifest.tolerant(path: path, fallback_scripts: bundle.scripts.keys)
114
+ kept, problems = BundleManifest.tolerant(path: path, fallback_scripts: bundle.scripts.keys)
115
115
  from_kept(bundle, kept, problems, mirrored)
116
- rescue Manifest::ValidationError, SystemCallError, EncodingError
116
+ rescue BundleManifest::ValidationError, SystemCallError, EncodingError
117
117
  standing(mirrored, :edited)
118
118
  end
119
119
 
@@ -139,7 +139,7 @@ module Samagotchi
139
139
  unknown_keys: kept.unknown_keys.map(&:to_s), floor_keys: floors(kept), problems: problems.freeze, origin: :kept)
140
140
  end
141
141
 
142
- # Manifest#floor_keys of the tolerant read; a shape it can't tell is no floor.
142
+ # BundleManifest#floor_keys of the tolerant read; a shape it can't tell is no floor.
143
143
  def self.floors(kept)
144
144
  kept.floor_keys
145
145
  rescue StandardError
@@ -151,7 +151,7 @@ module Samagotchi
151
151
  problems = {}
152
152
  new(name: bundle.name, version: bundle.version,
153
153
  requires_chi: raw["requires_chi"]&.to_s, trust_level: raw["trust_level"]&.to_s, description: nil,
154
- needs: parsed(problems, "needs", [], Manifest::ValidationError) { Manifest.parse_needs(raw["needs"]) },
154
+ needs: parsed(problems, "needs", [], BundleManifest::ValidationError) { BundleManifest.parse_needs(raw["needs"]) },
155
155
  script_names: bundle.scripts.keys,
156
156
  context_providers: parsed(problems, "context_providers", []) { ContextProviders.parse_list(raw["context_providers"]) },
157
157
  block_renderers: parsed(problems, "block_renderers", []) { BlockRenderers.parse_list(raw["block_renderers"]) },
@@ -202,7 +202,7 @@ module Samagotchi
202
202
 
203
203
  # Why chi +version+ can't load this bundle, nil when it can (or it
204
204
  # names no requirement): the one requires_chi gate.
205
- def requires_chi_failure(version = Samagotchi::VERSION) = Manifest.requires_chi_failure(requires_chi, version)
205
+ def requires_chi_failure(version = Samagotchi::VERSION) = BundleManifest.requires_chi_failure(requires_chi, version)
206
206
 
207
207
  # A bundle with no trust_level counts as experimental.
208
208
  def experimental? = (trust_level || "experimental") == "experimental"
@@ -1,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- module MemoryBundle
4
+ module InstalledBundles
5
5
  # The plugin an installed bundle's record (manifest.json) names: its
6
6
  # file under plugin/, the sha256 recorded at install ("" when none
7
- # is; either "sha256:<hex>" or a bare hex, Provenance.recorded_sha
7
+ # is; either "sha256:<hex>" or a bare hex, Sha.recorded
8
8
  # reads both), and whether its manifest called it required (a guardrail
9
9
  # plugin: plugin: required: true; an older chi ignores the key).
10
10
  PluginRef = Data.define(:file, :sha256, :required) do
@@ -7,12 +7,12 @@ require_relative "bundle_hook"
7
7
  require_relative "plugin_ref"
8
8
 
9
9
  module Samagotchi
10
- module MemoryBundle
10
+ module InstalledBundles
11
11
  # Only what reports on an edited file needs it.
12
12
  autoload :FileStanding, File.expand_path("file_standing", __dir__)
13
- autoload :InstalledManifest, File.expand_path("installed_manifest", __dir__)
13
+ autoload :Manifest, File.expand_path("manifest", __dir__)
14
14
 
15
- # An installed bundle's record, as Provenance#write keeps it in
15
+ # An installed bundle's record, as MemoryBundle::Provenance#write keeps it in
16
16
  # <bundles>/<name>/manifest.json: identity, shas, files and marks.
17
17
  # Reading is tolerant: an older record lacks keys a newer chi writes,
18
18
  # and a key that is missing or of the wrong shape reads as empty (nil
@@ -25,7 +25,7 @@ module Samagotchi
25
25
  # incoming_sha: the bundle text a conflict mark speaks of, nil on an older mark;
26
26
  # legacy_base: true on an older mark that has since got its incoming_sha (.legacy_mark?);
27
27
  # why, missed, missed_places: FileStanding::Behind's, nil on an older mark;
28
- # merged_version, merged_at, merged_trash: a merge no report has shown (Provenance#note_merged)
28
+ # merged_version, merged_at, merged_trash: a merge no report has shown (MemoryBundle::Provenance#note_merged)
29
29
  # hooks basename => BundleHook (sha256; other legacy fields ignored)
30
30
  # guardrails basename => recorded sha256 ("" when none)
31
31
  # scripts file name => recorded sha256 ("" when none)
@@ -33,28 +33,28 @@ module Samagotchi
33
33
  # includes a profile's recorded members, nil for a plain bundle
34
34
  # mirror the record's frozen copy of the manifest keys (MIRROR_KEYS,
35
35
  # string keys, the values as recorded), which only
36
- # InstalledManifest reads: requires_chi, trust_level, needs,
36
+ # Manifest reads: requires_chi, trust_level, needs,
37
37
  # context_providers, block_renderers, session_settings,
38
38
  # session_setting_types, settings, setting_types. A key a
39
39
  # later manifest gains is never mirrored
40
40
  # dev a dev install (`chi bundle install --dev`): its source is
41
- # the folder it links to and its files carry Provenance::DEV_SHA
42
- # manifest_sha the kept manifest.yml's recorded sha256 (Provenance::DEV_SHA
41
+ # the folder it links to and its files carry Sha::DEV
42
+ # manifest_sha the kept manifest.yml's recorded sha256 (Sha::DEV
43
43
  # for a dev install's link), nil on a record written before
44
44
  # chi kept it, or rewritten since by a chi that doesn't
45
45
  # installed_by the chi VERSION that wrote the record with its kept
46
46
  # manifest, nil when manifest_sha is
47
- # dir its .bundles/<name> folder (.read, Provenance.each_installed),
47
+ # dir its .bundles/<name> folder (.read, InstalledBundles.each),
48
48
  # nil for a record parsed alone or one that doesn't read
49
49
  # error why manifest.json couldn't be read (the rest empty), else nil
50
- class InstalledBundle < Data.define(:name, :version, :scope, :source, :installed_at, :source_commit, :files, :hooks,
51
- :guardrails, :scripts, :plugin, :includes, :dev, :mirror, :manifest_sha,
52
- :installed_by, :dir, :error)
50
+ class Record < Data.define(:name, :version, :scope, :source, :installed_at, :source_commit, :files, :hooks,
51
+ :guardrails, :scripts, :plugin, :includes, :dev, :mirror, :manifest_sha,
52
+ :installed_by, :dir, :error)
53
53
  OPTIONAL = { version: nil, scope: nil, source: nil, installed_at: nil, source_commit: nil, files: {}, hooks: {},
54
54
  guardrails: {}, scripts: {}, plugin: nil, includes: nil, dev: false, mirror: {}, manifest_sha: nil,
55
55
  installed_by: nil, dir: nil, error: nil }.freeze
56
56
 
57
- # The manifest keys a record mirrors, as Provenance#write records them.
57
+ # The manifest keys a record mirrors, as MemoryBundle::Provenance#write records them.
58
58
  MIRROR_KEYS = %w[requires_chi trust_level needs context_providers block_renderers session_settings
59
59
  session_setting_types settings setting_types].freeze
60
60
 
@@ -110,20 +110,20 @@ module Samagotchi
110
110
 
111
111
  def error? = !error.nil?
112
112
 
113
- # Its kept manifest.yml (Provenance#kept_manifest_path), nil with no dir.
113
+ # Its kept manifest.yml (Dir#kept_manifest_path), nil with no dir.
114
114
  def kept_manifest_path = dir && File.join(dir, "manifest.yml")
115
115
 
116
- # Whether it lacks a kept manifest.yml chi can read (Provenance#adopt_manifest
116
+ # Whether it lacks a kept manifest.yml chi can read (MemoryBundle::Provenance#adopt_manifest
117
117
  # keeps one): an older chi's record has no manifest_sha (with or without a
118
- # file there, InstalledManifest's legacy and :stale), or the file it
118
+ # file there, Manifest's legacy and :stale), or the file it
119
119
  # recorded is gone (:missing). false with no dir.
120
120
  def manifest_unkept? = !dir.nil? && (manifest_sha.to_s.empty? || !File.file?(kept_manifest_path))
121
121
 
122
122
  # What its manifest says (requires_chi, needs, providers,
123
123
  # renderers, settings…): the one way to read manifest data of an
124
124
  # installed bundle.
125
- # @return [InstalledManifest]
126
- def manifest = InstalledManifest.of(self)
125
+ # @return [Manifest]
126
+ def manifest = Manifest.of(self)
127
127
 
128
128
  # The scope its files went to: "system" when none is recorded.
129
129
  def effective_scope = scope.to_s.strip.empty? ? "system" : scope
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "file_digest"
4
+
5
+ module Samagotchi
6
+ module InstalledBundles
7
+ # The sha256 a record keeps for an installed file, and whether the file
8
+ # on disk is still it.
9
+ module Sha
10
+ # What a dev install (`chi bundle install --dev`) records instead of
11
+ # a sha256: its files are links to the source, which change under it.
12
+ # Not a hex digest, so no file can be mistaken for it.
13
+ DEV = "sha256:dev"
14
+
15
+ # The hex digest a manifest records, in either of its formats:
16
+ # "sha256:<hex>" (hooks, rules, the plugin) or bare <hex> (a memory
17
+ # file's checksum:). "" when none is recorded.
18
+ def self.recorded(value)
19
+ value.to_s.delete_prefix("sha256:")
20
+ end
21
+
22
+ # The digest of a file, nil when it isn't there (FileDigest). Every
23
+ # caller checks existence first; .matches? reads a missing file as
24
+ # not the recorded one.
25
+ def self.of_file(path)
26
+ FileDigest.hex(path)
27
+ end
28
+
29
+ # Whether +value+ is DEV: the file isn't checked.
30
+ def self.unchecked?(value)
31
+ recorded(value) == recorded(DEV)
32
+ end
33
+
34
+ # Whether the file's content is the one recorded (either format); a
35
+ # dev install's (DEV) always is.
36
+ def self.matches?(path, value)
37
+ unchecked?(value) || of_file(path) == recorded(value)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Samagotchi
4
+ autoload :Log, File.expand_path("../log", __dir__)
5
+
6
+ module InstalledBundles
7
+ # Why chi won't use an installed bundle, or one of its files: the verdict's
8
+ # answer, which each reader reacts to in its own way (fail closed, a note,
9
+ # a log line once, a skip).
10
+ # kind :unreadable (the record: manifest.json doesn't read),
11
+ # :too_new (its requires_chi isn't met), or for one file
12
+ # (Dir#file_refusal) :file_missing, :file_unrecorded (no
13
+ # sha256 recorded for it), :file_changed
14
+ # text what the reader says of it today (one wording per kind of
15
+ # file: the plugin's, a script's, a rule's)
16
+ # actual, expected the file's sha256 and the recorded one, hex, for
17
+ # :file_changed; nil otherwise
18
+ Refusal = Data.define(:kind, :text, :actual, :expected) do
19
+ def initialize(kind:, text:, actual: nil, expected: nil) = super
20
+ end
21
+
22
+ # The [bundle, reason] pairs Standing.warn_unreadable has logged.
23
+ UNREADABLE_WARNED = Set.new
24
+ UNREADABLE_LOCK = Mutex.new
25
+
26
+ # One installed bundle as the verdict sees it, before any of its files is
27
+ # looked at: its record (one with error set when it doesn't read) and why
28
+ # chi won't use it, nil when it may.
29
+ Standing = Data.define(:name, :record, :refusal) do
30
+ # The bundle-level gate, on a record already read: an unreadable record
31
+ # (warned of once, .warn_unreadable), then requires_chi. Reads the
32
+ # record's manifest (cached); a cache miss hashes the kept manifest.yml
33
+ # (Manifest.of), and no other file.
34
+ # @param record [Record]
35
+ # @return [Standing]
36
+ def self.of(name, record)
37
+ refusal = if record.error?
38
+ warn_unreadable(name, record.error)
39
+ Refusal.new(kind: :unreadable, text: record.error)
40
+ elsif (failure = record.manifest.requires_chi_failure)
41
+ Refusal.new(kind: :too_new, text: failure)
42
+ end
43
+ new(name: name, record: record, refusal: refusal)
44
+ end
45
+
46
+ # A record that doesn't read, said in the log once per process per
47
+ # bundle (and reason), whichever reader came across it: most readers
48
+ # only skip it (providers, renderers, settings, needs, memory owners).
49
+ def self.warn_unreadable(name, text)
50
+ return unless UNREADABLE_LOCK.synchronize { UNREADABLE_WARNED.add?([name, text]) }
51
+
52
+ Log.warn(:memory, "bundle_unreadable", bundle: name, msg: text)
53
+ end
54
+
55
+ # Forget what was warned of (specs).
56
+ def self.reset_warnings! = UNREADABLE_LOCK.synchronize { UNREADABLE_WARNED.clear }
57
+
58
+ def usable? = refusal.nil?
59
+
60
+ def unreadable? = refusal&.kind == :unreadable
61
+
62
+ def too_new? = refusal&.kind == :too_new
63
+ end
64
+ end
65
+ end