samagotchi 0.178.0 → 0.179.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 (126) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +28 -1
  3. data/docs/architecture.md +4 -2
  4. data/docs/bundles.md +16 -7
  5. data/docs/cli.md +3 -1
  6. data/docs/context.md +25 -1
  7. data/docs/guardrails.md +2 -1
  8. data/docs/internals/attached-context.md +26 -4
  9. data/docs/internals/background-tasks.md +3 -0
  10. data/docs/internals/block-renderers.md +2 -2
  11. data/docs/internals/bridge.md +11 -2
  12. data/docs/internals/bundles.md +63 -34
  13. data/docs/internals/engine.md +1 -1
  14. data/docs/internals/web-frontend.md +18 -1
  15. data/docs/plugins.md +14 -3
  16. data/docs/releasing.md +3 -3
  17. data/docs/scheduling.md +4 -0
  18. data/docs/sessions.md +8 -2
  19. data/docs/testing.md +4 -1
  20. data/lib/samagotchi/block_renderers.rb +12 -14
  21. data/lib/samagotchi/bootstrap/bundles.rb +2 -2
  22. data/lib/samagotchi/bootstrap_command.rb +1 -1
  23. data/lib/samagotchi/bridge/card_wait.rb +34 -0
  24. data/lib/samagotchi/bridge/turn_accumulator.rb +22 -4
  25. data/lib/samagotchi/bundle_command/check.rb +10 -10
  26. data/lib/samagotchi/bundle_command/installed_check.rb +2 -2
  27. data/lib/samagotchi/bundle_command/try/sandbox.rb +3 -3
  28. data/lib/samagotchi/bundle_command.rb +17 -17
  29. data/lib/samagotchi/bundle_manifest.rb +579 -0
  30. data/lib/samagotchi/bundle_needs.rb +9 -8
  31. data/lib/samagotchi/bundle_settings_check.rb +9 -8
  32. data/lib/samagotchi/bundles/claude-peer/manifest.yml +5 -3
  33. data/lib/samagotchi/bundles/claude-peer/plugin.rb +4 -4
  34. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +11 -6
  35. data/lib/samagotchi/bundles/codex-peer/manifest.yml +5 -3
  36. data/lib/samagotchi/bundles/codex-peer/plugin.rb +4 -3
  37. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +11 -6
  38. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  39. data/lib/samagotchi/bundles/system/memory_guide.md +1 -1
  40. data/lib/samagotchi/config.rb +2 -2
  41. data/lib/samagotchi/context_absorber.rb +36 -6
  42. data/lib/samagotchi/context_note.rb +8 -5
  43. data/lib/samagotchi/context_providers.rb +30 -27
  44. data/lib/samagotchi/context_sources.rb +42 -12
  45. data/lib/samagotchi/engine/messages.rb +1 -1
  46. data/lib/samagotchi/events.rb +6 -5
  47. data/lib/samagotchi/guardrail_wiring.rb +20 -21
  48. data/lib/samagotchi/guardrails/approval.rb +3 -2
  49. data/lib/samagotchi/guardrails/outside.rb +3 -3
  50. data/lib/samagotchi/{memory_bundle → installed_bundles}/bundle_hook.rb +1 -1
  51. data/lib/samagotchi/installed_bundles/dir.rb +129 -0
  52. data/lib/samagotchi/{memory_bundle → installed_bundles}/file_digest.rb +1 -1
  53. data/lib/samagotchi/{memory_bundle → installed_bundles}/file_standing.rb +2 -2
  54. data/lib/samagotchi/{memory_bundle/installed_manifest.rb → installed_bundles/manifest.rb} +21 -21
  55. data/lib/samagotchi/{memory_bundle → installed_bundles}/plugin_ref.rb +2 -2
  56. data/lib/samagotchi/{memory_bundle/installed_bundle.rb → installed_bundles/record.rb} +17 -17
  57. data/lib/samagotchi/installed_bundles/sha.rb +41 -0
  58. data/lib/samagotchi/installed_bundles/standing.rb +45 -0
  59. data/lib/samagotchi/installed_bundles.rb +90 -0
  60. data/lib/samagotchi/memory_bundle/asset_installer.rb +7 -7
  61. data/lib/samagotchi/memory_bundle/builder.rb +13 -13
  62. data/lib/samagotchi/memory_bundle/file_state.rb +9 -9
  63. data/lib/samagotchi/memory_bundle/installed_copy.rb +6 -6
  64. data/lib/samagotchi/memory_bundle/installer.rb +33 -33
  65. data/lib/samagotchi/memory_bundle/listing.rb +5 -5
  66. data/lib/samagotchi/memory_bundle/manifest_standing.rb +7 -7
  67. data/lib/samagotchi/memory_bundle/merger.rb +15 -15
  68. data/lib/samagotchi/memory_bundle/profile.rb +8 -8
  69. data/lib/samagotchi/memory_bundle/provenance.rb +59 -192
  70. data/lib/samagotchi/memory_bundle/remerge.rb +16 -16
  71. data/lib/samagotchi/memory_bundle/shas.rb +2 -2
  72. data/lib/samagotchi/memory_bundle/shipped_history.rb +1 -1
  73. data/lib/samagotchi/memory_bundle/shipped_update.rb +17 -17
  74. data/lib/samagotchi/memory_bundle/status.rb +15 -15
  75. data/lib/samagotchi/memory_bundle/system_bundle.rb +12 -12
  76. data/lib/samagotchi/memory_bundle/trash.rb +2 -2
  77. data/lib/samagotchi/memory_bundle/uninstaller.rb +8 -8
  78. data/lib/samagotchi/memory_bundle.rb +2 -2
  79. data/lib/samagotchi/memory_command.rb +4 -4
  80. data/lib/samagotchi/{memory_bundle → memory_index}/entry_size.rb +8 -8
  81. data/lib/samagotchi/{memory_bundle/index_size.rb → memory_index/size.rb} +6 -6
  82. data/lib/samagotchi/{memory_bundle/index_sync.rb → memory_index/sync.rb} +5 -5
  83. data/lib/samagotchi/{memory_bundle/index_updater.rb → memory_index/updater.rb} +2 -2
  84. data/lib/samagotchi/memory_usage.rb +5 -5
  85. data/lib/samagotchi/muted_memories.rb +1 -1
  86. data/lib/samagotchi/peer_exchanges.rb +76 -0
  87. data/lib/samagotchi/plugin/context.rb +4 -0
  88. data/lib/samagotchi/plugin/loader.rb +35 -33
  89. data/lib/samagotchi/plugin/peer.rb +42 -11
  90. data/lib/samagotchi/project_subfolders.rb +9 -9
  91. data/lib/samagotchi/reply_wait/target.rb +11 -4
  92. data/lib/samagotchi/self_report.rb +14 -14
  93. data/lib/samagotchi/session_access/local.rb +16 -1
  94. data/lib/samagotchi/session_access/remote.rb +2 -1
  95. data/lib/samagotchi/session_config.rb +23 -19
  96. data/lib/samagotchi/session_manager/running_tasks.rb +15 -2
  97. data/lib/samagotchi/session_retention.rb +13 -4
  98. data/lib/samagotchi/session_stores.rb +3 -0
  99. data/lib/samagotchi/system_prompt.rb +4 -4
  100. data/lib/samagotchi/terminal_ui/attach_launcher.rb +5 -1
  101. data/lib/samagotchi/terminal_ui/formatting.rb +2 -2
  102. data/lib/samagotchi/terminal_ui/model_segments_text.rb +5 -5
  103. data/lib/samagotchi/terminal_ui.rb +4 -1
  104. data/lib/samagotchi/testing/bundle.rb +3 -3
  105. data/lib/samagotchi/testing/install.rb +3 -3
  106. data/lib/samagotchi/testing/sandbox.rb +2 -2
  107. data/lib/samagotchi/tool_runner.rb +14 -14
  108. data/lib/samagotchi/tools/memory.rb +22 -22
  109. data/lib/samagotchi/update_command.rb +4 -4
  110. data/lib/samagotchi/version.rb +1 -1
  111. data/lib/samagotchi/web/app.rb +14 -1
  112. data/lib/samagotchi/web/message_display.rb +6 -0
  113. data/lib/samagotchi/web/participant_view.rb +1 -1
  114. data/lib/samagotchi/web/proxy_gate.rb +1 -0
  115. data/lib/samagotchi/web/public/card_kinds.js +8 -3
  116. data/lib/samagotchi/web/public/cards_place.js +15 -1
  117. data/lib/samagotchi/web/public/ctx.js +1 -1
  118. data/lib/samagotchi/web/public/history_shape.js +3 -0
  119. data/lib/samagotchi/web/public/turn_block.js +46 -8
  120. data/lib/samagotchi/web/public/turn_settle.js +117 -0
  121. data/lib/samagotchi/web/push/notifier.rb +2 -2
  122. data/lib/samagotchi/web/session_access_routes.rb +21 -1
  123. data/lib/samagotchi/web/session_hub.rb +20 -4
  124. data/lib/samagotchi/worker_boot.rb +4 -1
  125. metadata +19 -12
  126. data/lib/samagotchi/memory_bundle/manifest.rb +0 -581
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "installed_bundle"
3
+ require_relative "../installed_bundles/record"
4
4
  require_relative "installer"
5
- require_relative "manifest"
5
+ require_relative "../bundle_manifest"
6
6
  require_relative "provenance"
7
7
  require_relative "source"
8
8
 
9
9
  module Samagotchi
10
10
  module MemoryBundle
11
11
  # The one owner of the words for an installed bundle's manifest.yml, as
12
- # InstalledManifest read it (chi bundle status's Manifest: line, chi
12
+ # InstalledBundles::Manifest read it (chi bundle status's Manifest: line, chi
13
13
  # update's row), and of keeping it for a record an older chi wrote
14
14
  # (.keep: Provenance#adopt_manifest from where it came from). The cases:
15
15
  # :kept read from the kept file; a line only when chi +installed_by+
16
- # predates a key it uses (Manifest::SINCE): newer than its installer
16
+ # predates a key it uses (BundleManifest::SINCE): newer than its installer
17
17
  # :legacy no kept file, no manifest_sha: a chi before the kept manifest installed it
18
18
  # :stale a kept file, no manifest_sha: such a chi rewrote the record since
19
19
  # :missing a manifest_sha, no kept file
@@ -31,7 +31,7 @@ module Samagotchi
31
31
 
32
32
  module_function
33
33
 
34
- # @param bundle [InstalledBundle]
34
+ # @param bundle [InstalledBundles::Record]
35
35
  # @return [Symbol, nil] the case, nil for a record with no dir or that doesn't read
36
36
  def state(bundle)
37
37
  return nil if bundle.error? || bundle.dir.nil?
@@ -80,8 +80,8 @@ module Samagotchi
80
80
  end
81
81
 
82
82
  dir = File.join(SourceNormalizer::SHIPPED_DIR, File.basename(source.chomp("/")))
83
- File.file?(File.join(dir, "manifest.yml")) && Manifest.read(dir: dir).version == bundle.version.to_s ? dir : nil
84
- rescue Manifest::ValidationError, Psych::Exception
83
+ File.file?(File.join(dir, "manifest.yml")) && BundleManifest.read(dir: dir).version == bundle.version.to_s ? dir : nil
84
+ rescue BundleManifest::ValidationError, Psych::Exception
85
85
  nil
86
86
  end
87
87
 
@@ -2,8 +2,8 @@
2
2
 
3
3
  require "digest"
4
4
  require_relative "../atomic_file"
5
- require_relative "file_digest"
6
- require_relative "installed_bundle"
5
+ require_relative "../installed_bundles/file_digest"
6
+ require_relative "../installed_bundles/record"
7
7
  require_relative "file_actions"
8
8
 
9
9
  module Samagotchi
@@ -59,7 +59,7 @@ module Samagotchi
59
59
  ALREADY_MERGED = Outcome.new(status: :keep, base: :incoming)
60
60
 
61
61
  # What an upgrade would do to the files asked about, by what callers say
62
- # of them. merged: file names; behind: FileStanding::Behind.
62
+ # of them. merged: file names; behind: InstalledBundles::FileStanding::Behind.
63
63
  Forecast = Data.define(:merged, :behind) do
64
64
  def conflicts = behind.map(&:file)
65
65
  end
@@ -115,20 +115,20 @@ module Samagotchi
115
115
  # possible, and what the bundle changed that the file doesn't get. A base
116
116
  # that may be the user's own text (an older mark) can't say what changed.
117
117
  # @param version [String, nil] the version the file stays behind
118
- # @return [FileStanding::Behind]
118
+ # @return [InstalledBundles::FileStanding::Behind]
119
119
  def behind(file_key, outcome, version)
120
- why = outcome.why && FileStanding.no_merge(outcome.why)
120
+ why = outcome.why && InstalledBundles::FileStanding.no_merge(outcome.why)
121
121
  base = paths(file_key)[:base]
122
- if InstalledBundle.legacy_mark?(provenance.record&.files&.fetch(file_key, nil))
123
- return FileStanding::Behind.new(file: file_key, version: version, why: why || FileStanding::LEGACY_CONFLICT)
122
+ if InstalledBundles::Record.legacy_mark?(provenance.record&.files&.fetch(file_key, nil))
123
+ return InstalledBundles::FileStanding::Behind.new(file: file_key, version: version, why: why || InstalledBundles::FileStanding::LEGACY_CONFLICT)
124
124
  end
125
- return FileStanding::Behind.new(file: file_key, version: version, why: why) unless File.exist?(base)
125
+ return InstalledBundles::FileStanding::Behind.new(file: file_key, version: version, why: why) unless File.exist?(base)
126
126
 
127
127
  sides = paths(file_key)
128
128
  headings, unnamed = TextMerge.changed_sections(File.binread(base), File.binread(sides[:incoming]), File.binread(sides[:current]))
129
129
  # Only the places under no heading: the others are named.
130
130
  places = unnamed ? [outcome.merge&.hunks&.count { |hunk| !hunk.union? && hunk.heading.nil? }.to_i, 1].max : 0
131
- FileStanding::Behind.new(file: file_key, version: version, why: why, missed: headings, places: places)
131
+ InstalledBundles::FileStanding::Behind.new(file: file_key, version: version, why: why, missed: headings, places: places)
132
132
  end
133
133
 
134
134
  # @return [Hash{Symbol => String}] the file's :base, :current, :incoming
@@ -154,7 +154,7 @@ module Samagotchi
154
154
  # A file the bundle doesn't own, the user's: there is no base to merge from.
155
155
  when :skip then no_base(state)
156
156
  # It can't be read: the read raises, as the merge would.
157
- when :refuse then FileDigest.hex(paths(file_key)[:current])
157
+ when :refuse then InstalledBundles::FileDigest.hex(paths(file_key)[:current])
158
158
  else raise ArgumentError, "no outcome for #{action.do.inspect} (#{file_key})"
159
159
  end
160
160
  end
@@ -162,9 +162,9 @@ module Samagotchi
162
162
  # No base to merge from: the file stays behind. With none on disk
163
163
  # TextMerge::NO_BASE says why; one the checksum contradicts (the file
164
164
  # equals it, yet is edited) may be the user's own text, as an older
165
- # chi's can (FileStanding::LEGACY_BASE).
165
+ # chi's can (InstalledBundles::FileStanding::LEGACY_BASE).
166
166
  def no_base(state)
167
- why = state.digests.base.nil? ? TextMerge::NO_BASE : FileStanding::LEGACY_BASE
167
+ why = state.digests.base.nil? ? TextMerge::NO_BASE : InstalledBundles::FileStanding::LEGACY_BASE
168
168
  CONFLICT.with(merge: TextMerge.failed(why))
169
169
  end
170
170
 
@@ -179,7 +179,7 @@ module Samagotchi
179
179
  sides = paths(file_key)
180
180
  return FAST_FORWARD unless Merger.removes_lines?(File.binread(sides[:current]), File.binread(sides[:incoming]))
181
181
 
182
- unproved.call ? CONFLICT.with(merge: TextMerge.failed(FileStanding::LEGACY_BASE)) : FAST_FORWARD
182
+ unproved.call ? CONFLICT.with(merge: TextMerge.failed(InstalledBundles::FileStanding::LEGACY_BASE)) : FAST_FORWARD
183
183
  end
184
184
  end
185
185
 
@@ -207,7 +207,7 @@ module Samagotchi
207
207
  result = TextMerge.merge(base: File.binread(sides[:base]), ours: ours, theirs: File.binread(sides[:incoming]))
208
208
  return CONFLICT.with(merge: result) unless result.clean?
209
209
  return ALREADY_MERGED if result.text.b == ours.b
210
- return CONFLICT.with(merge: TextMerge.failed(FileStanding::LEGACY_BASE)) if removes_lines?(ours, result.text) && unproved.call
210
+ return CONFLICT.with(merge: TextMerge.failed(InstalledBundles::FileStanding::LEGACY_BASE)) if removes_lines?(ours, result.text) && unproved.call
211
211
 
212
212
  Outcome.new(status: :merged, base: :incoming, merge: result, current_sha: Digest::SHA256.hexdigest(ours))
213
213
  end
@@ -251,7 +251,7 @@ module Samagotchi
251
251
 
252
252
  # The file's digest, nil when there is none.
253
253
  def self.digest(path)
254
- FileDigest.hex(path)
254
+ InstalledBundles::FileDigest.hex(path)
255
255
  end
256
256
  end
257
257
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "installer"
4
- require_relative "manifest"
4
+ require_relative "../bundle_manifest"
5
5
  require_relative "provenance"
6
6
  require_relative "source"
7
7
  require_relative "uninstaller"
@@ -48,13 +48,13 @@ module Samagotchi
48
48
  def shipped_meta?(dir, shipped_dir: SourceNormalizer::SHIPPED_DIR)
49
49
  return false unless File.expand_path(File.dirname(dir.to_s)) == File.expand_path(shipped_dir)
50
50
 
51
- Manifest.read(dir: dir).meta?
52
- rescue Manifest::ValidationError, Psych::Exception, SystemCallError
51
+ BundleManifest.read(dir: dir).meta?
52
+ rescue BundleManifest::ValidationError, Psych::Exception, SystemCallError
53
53
  false
54
54
  end
55
55
 
56
56
  # Whether the installed bundle +name+ (its record +bundle+, an
57
- # InstalledBundle or nil) is a meta: it records includes, or it came
57
+ # InstalledBundles::Record or nil) is a meta: it records includes, or it came
58
58
  # from chi and chi ships it as a meta.
59
59
  def installed_meta?(name, bundle, shipped_dir: SourceNormalizer::SHIPPED_DIR)
60
60
  return false unless bundle
@@ -74,7 +74,7 @@ module Samagotchi
74
74
  bundle.includes
75
75
  end
76
76
 
77
- def installed_names = Provenance.each_installed.map { |name, _| name }
77
+ def installed_names = InstalledBundles.each.map { |name, _| name }
78
78
 
79
79
  # What a profile's install says of a member's memory file the user
80
80
  # already had: chi bundle install says "Skipped … (already exists)",
@@ -95,7 +95,7 @@ module Samagotchi
95
95
  # ones installed now or found installed). Never raises for a member.
96
96
  # @return [InstallResult]
97
97
  def install(dir, shipped_dir: SourceNormalizer::SHIPPED_DIR, chi_version: Samagotchi::VERSION, dry_run: false, force: false)
98
- manifest = Manifest.read(dir: dir)
98
+ manifest = BundleManifest.read(dir: dir)
99
99
  bundle = Provenance.new(name: manifest.name).record
100
100
  was = recorded(bundle, manifest)
101
101
  installed = installed_names
@@ -122,7 +122,7 @@ module Samagotchi
122
122
 
123
123
  def install_member(member, shipped_dir, chi_version, dry_run, force, result)
124
124
  member_dir = File.join(shipped_dir, member)
125
- failure = Manifest.requires_chi_failure(Manifest.read(dir: member_dir).requires_chi, chi_version)
125
+ failure = BundleManifest.requires_chi_failure(BundleManifest.read(dir: member_dir).requires_chi, chi_version)
126
126
  return result.skipped[member] = failure if failure
127
127
 
128
128
  # A dry run runs the Installer's too, which writes nothing, so it
@@ -159,7 +159,7 @@ module Samagotchi
159
159
  raise Uninstaller::UninstallError.not_installed(name) unless bundle
160
160
 
161
161
  shipped = File.join(shipped_dir, name)
162
- manifest = File.file?(File.join(shipped, "manifest.yml")) ? Manifest.read(dir: shipped) : nil
162
+ manifest = File.file?(File.join(shipped, "manifest.yml")) ? BundleManifest.read(dir: shipped) : nil
163
163
  members = recorded(bundle, manifest)
164
164
  result = UninstallResult.new(name: name, removed: [], blocked: {}, gone: false, trash: {}, warnings: [])
165
165
  (members & installed_names).each do |member|
@@ -9,9 +9,8 @@ require_relative "../atomic_file"
9
9
  require_relative "../memory_paths"
10
10
  require_relative "../state_format"
11
11
  require_relative "../version"
12
- require_relative "file_digest"
13
- require_relative "installed_bundle"
14
- require_relative "manifest"
12
+ require_relative "../installed_bundles"
13
+ require_relative "../bundle_manifest"
15
14
 
16
15
  module Samagotchi
17
16
  module MemoryBundle
@@ -21,14 +20,10 @@ module Samagotchi
21
20
  # bases/ — snapshots of every file as installed (for 3-way merge later)
22
21
  # manifest.yml — the bundle's own manifest as installed (#write's
23
22
  # manifest_from:), its sha in the record; read live
24
- # through InstalledManifest
25
- class Provenance
26
- def self.bundles_dir
27
- MemoryPaths.bundles_dir
28
- end
29
-
30
- attr_reader :name, :bundle_dir
31
-
23
+ # through InstalledBundles::Manifest
24
+ # The write side of InstalledBundles::Dir: what the runtime reads of the
25
+ # folder (#record, #plugin_path, #scripts_dir…) is the Dir's.
26
+ class Provenance < InstalledBundles::Dir
32
27
  # What a conflict mark adds to a file's entry.
33
28
  MARK_FIELDS = %w[conflict incoming_sha legacy_base why missed missed_places].freeze
34
29
  # What a merge no report has shown adds to a file's entry (#note_merged).
@@ -81,11 +76,6 @@ module Samagotchi
81
76
  ]
82
77
  )
83
78
 
84
- def initialize(name:)
85
- @name = name
86
- @bundle_dir = File.join(self.class.bundles_dir, name)
87
- end
88
-
89
79
  # The bundle's text a marked file missed: incoming/<file>, there until the
90
80
  # file settles, so it outlives a git or zip source's extract. Nothing globs
91
81
  # the bundle's folder for memories, and an uninstall's rm_rf sweeps it.
@@ -97,35 +87,6 @@ module Samagotchi
97
87
  File.join(@bundle_dir, "hooks")
98
88
  end
99
89
 
100
- # Installed guardrail rule files (guardrails/*.yml).
101
- def guardrails_dir
102
- File.join(@bundle_dir, "guardrails")
103
- end
104
-
105
- # The installed plugin file's dir (plugin/<file>).
106
- def plugin_dir
107
- File.join(@bundle_dir, "plugin")
108
- end
109
-
110
- # The installed scripts' dir (scripts/<file>): what a context
111
- # provider's command finds as {bundle_dir}/scripts.
112
- def scripts_dir
113
- File.join(@bundle_dir, "scripts")
114
- end
115
-
116
- # Why the installed scripts/+file+ isn't as installed, nil when it is:
117
- # the record has no sha for it (+recorded+), it is gone, or it changed.
118
- # The one check a provider's command and a block renderer's load make
119
- # before chi runs or serves the file; a --dev install's links pass.
120
- def script_failure(file, recorded)
121
- path = File.join(scripts_dir, file)
122
- return "bundle #{@name}'s scripts/#{file} isn't among its installed scripts (reinstall the bundle)" if self.class.recorded_sha(recorded).empty?
123
- return "bundle #{@name}'s scripts/#{file} is missing (reinstall the bundle)" unless File.file?(path)
124
- return nil if self.class.sha_matches?(path, recorded)
125
-
126
- "bundle #{@name}'s scripts/#{file} differs from the installed one (reinstall the bundle)"
127
- end
128
-
129
90
  # The intent journal of a run that has not written its record yet
130
91
  # (#write_pending): its scope and target dir, plus the files and sha256s
131
92
  # of the texts a run meant to put there. #write removes it.
@@ -172,7 +133,7 @@ module Samagotchi
172
133
  pending_files(target_dir: target_dir).each do |key, shas|
173
134
  next if files.key?(key)
174
135
 
175
- disk_sha = FileDigest.hex(File.join(target_dir, key))
136
+ disk_sha = InstalledBundles::FileDigest.hex(File.join(target_dir, key))
176
137
  files[key] = {} if shas.include?(disk_sha)
177
138
  rescue SystemCallError
178
139
  next
@@ -180,125 +141,19 @@ module Samagotchi
180
141
  files
181
142
  end
182
143
 
183
- # The bundle's manifest.yml as installed (InstalledBundle#kept_manifest_path).
184
- def kept_manifest_path
185
- File.join(@bundle_dir, "manifest.yml")
186
- end
187
-
188
- # The plugin file as installed, or nil when the bundle has none.
189
- # @param bundle [InstalledBundle, nil] its record (read when not given)
190
- def plugin_path(bundle = record)
191
- file = bundle&.plugin&.file
192
- file ? File.join(plugin_dir, file) : nil
193
- end
194
-
195
144
  # The plugin's base snapshot (bases/plugin/<file>), for bundle diff.
196
145
  def plugin_base_path(file)
197
146
  File.join(@bundle_dir, "bases", "plugin", file.to_s)
198
147
  end
199
148
 
200
- # Yields [name, bundle] for each installed bundle (a dir under
201
- # bundles_dir holding a manifest.json, dot dirs aside), by name. bundle
202
- # is its InstalledBundle, one with error set when manifest.json doesn't
203
- # parse or isn't an object; the rest still come.
204
- #
205
- # With holding: (:hooks, :guardrails or :plugin) only the bundles
206
- # whose manifest has a non-empty mapping under that key come, and a
207
- # manifest that doesn't parse comes (with error) only when its bundle
208
- # has that dir (hooks/, guardrails/, plugin/): what it would load
209
- # can't be checked. One that isn't an object is skipped there.
210
- # @param dir [String] the bundles dir (bundles_dir by default)
211
- def self.each_installed(holding: nil, dir: bundles_dir)
212
- return enum_for(:each_installed, holding: holding, dir: dir) unless block_given?
213
- return unless Dir.exist?(dir)
214
-
215
- Dir.children(dir).reject { |e| e.start_with?(".") }.sort.each do |name|
216
- mjson = File.join(dir, name, "manifest.json")
217
- next unless File.exist?(mjson)
218
-
219
- begin
220
- data = JSON.parse(File.read(mjson), symbolize_names: true)
221
- rescue JSON::ParserError, EncodingError, SystemCallError => e # a key that isn't UTF-8 can't be a symbol
222
- if holding.nil? || Dir.exist?(File.join(dir, name, holding.to_s))
223
- yield name, InstalledBundle.unreadable(name, "manifest.json is unreadable: #{e.message}")
224
- end
225
- next
226
- end
227
- next if holding && !(data.is_a?(Hash) && data[holding].is_a?(Hash) && !data[holding].empty?)
228
-
229
- yield name, InstalledBundle.parse(name, data, File.join(dir, name))
230
- end
231
- end
232
-
233
- # The other installed bundles of +scope+ whose record lists the memory
234
- # file +file_key+, by name (a manifest that doesn't read is left out).
235
- # A project-scoped record doesn't say which project: one from another
236
- # repo counts too, which errs on keeping the file.
237
- def self.claimants(file_key, scope:, except: nil, dir: bundles_dir)
238
- each_installed(dir: dir).filter_map do |name, bundle|
239
- next if name == except || bundle.error?
240
- next unless bundle.effective_scope == scope.to_s && bundle.owns?(file_key)
241
-
242
- name
243
- end
244
- end
245
-
246
- # The hex digest a manifest records, in either of its formats:
247
- # "sha256:<hex>" (hooks, rules, the plugin) or bare <hex> (a memory
248
- # file's checksum:). "" when none is recorded.
249
- def self.recorded_sha(value)
250
- value.to_s.delete_prefix("sha256:")
251
- end
252
-
253
- # The digest of a file, nil when it isn't there (FileDigest). Every
254
- # caller checks existence first; #sha_matches? reads a missing file as
255
- # not the recorded one.
256
- def self.file_sha(path)
257
- FileDigest.hex(path)
258
- end
259
-
260
- # What a dev install (`chi bundle install --dev`) records instead of
261
- # a sha256: its files are links to the source, which change under it.
262
- # Not a hex digest, so no file can be mistaken for it.
263
- DEV_SHA = "sha256:dev"
264
-
265
- # Whether +recorded+ is DEV_SHA: the file isn't checked.
266
- def self.unchecked?(recorded)
267
- recorded_sha(recorded) == recorded_sha(DEV_SHA)
268
- end
269
-
270
- # Whether the file's content is the one recorded (either format); a
271
- # dev install's (DEV_SHA) always is.
272
- def self.sha_matches?(path, recorded)
273
- unchecked?(recorded) || file_sha(path) == recorded_sha(recorded)
274
- end
275
-
276
149
  # Whether a memory on disk is what the install wrote: the entry's
277
150
  # recorded checksum, else its base copy. Unknown counts as edited. The
278
151
  # one rule a prune, an uninstall's refusal and chi bundle status read.
279
152
  def self.unchanged?(path, entry, base_path)
280
153
  recorded = entry[:checksum]
281
- return sha_matches?(path, recorded) unless recorded_sha(recorded).empty?
282
-
283
- File.exist?(base_path) && file_sha(path) == file_sha(base_path)
284
- end
154
+ return InstalledBundles::Sha.matches?(path, recorded) unless InstalledBundles::Sha.recorded(recorded).empty?
285
155
 
286
- # The record's mirror of the manifest keys (InstalledBundle::MIRROR_KEYS)
287
- # as #write records them from its kwargs: what an older chi reads, and
288
- # what #adopt_manifest holds a source's manifest against.
289
- def self.mirror_fields(trust_level:, requires_chi:, needs:, context_providers:, block_renderers:, session_settings:,
290
- session_setting_types:, settings:, setting_types:)
291
- fields = {}
292
- fields["trust_level"] = trust_level.to_s if trust_level && !trust_level.to_s.empty?
293
- fields["requires_chi"] = requires_chi.to_s if requires_chi && !requires_chi.to_s.empty?
294
- fields["needs"] = needs.map { |n| n.transform_keys(&:to_s).compact } if needs.is_a?(Array) && !needs.empty?
295
- fields["context_providers"] = context_providers.map(&:to_h) unless context_providers.empty?
296
- fields["block_renderers"] = block_renderers.map(&:to_h) unless block_renderers.empty?
297
- fields["session_settings"] = Array(session_settings).map(&:to_s) unless Array(session_settings).empty?
298
- fields["session_setting_types"] = session_setting_types.to_h { |k, v| [k.to_s, v.to_s] } unless session_setting_types.to_h.empty?
299
- fields["settings"] = settings.map(&:to_s) unless settings.nil?
300
- fields["setting_types"] = setting_types.to_h { |k, v| [k.to_s, v.to_s] } unless setting_types.to_h.empty?
301
- fields
156
+ File.exist?(base_path) && InstalledBundles::Sha.of_file(path) == InstalledBundles::Sha.of_file(base_path)
302
157
  end
303
158
 
304
159
  # Writes provenance for an installation: merges into existing manifest.json
@@ -319,13 +174,13 @@ module Samagotchi
319
174
  # Engine knows it whatever becomes of the kept manifest.yml
320
175
  # @param requires_chi [String, nil] the manifest's requirement
321
176
  # @param needs [Array<Hash>] the manifest's needs ({command:, why:, hint:})
322
- # @param marks [Array<FileStanding::Behind>] files an upgrade left behind;
177
+ # @param marks [Array<InstalledBundles::FileStanding::Behind>] files an upgrade left behind;
323
178
  # their entries get conflict: true, and what the Behind says
324
179
  # @param incoming_dir [String, nil] the bundle's source: each marked file's
325
180
  # digest goes with the mark (incoming_sha: what Remerge retries) and its
326
181
  # text is saved (#incoming_path), a settled file's going. A mark from an
327
182
  # older chi that stays a mark gets legacy_base: true
328
- # (InstalledBundle.legacy_mark?)
183
+ # (InstalledBundles::Record.legacy_mark?)
329
184
  # @param includes [Array<String>, nil] a profile's recorded members
330
185
  # (MemoryBundle::Profile); nil for a plain bundle
331
186
  # @param scripts_files [Hash] file name => installed script; the
@@ -333,10 +188,10 @@ module Samagotchi
333
188
  # @param context_providers [Array<ContextProviders::Provider>]
334
189
  # @param block_renderers [Array<BlockRenderers::Renderer>]
335
190
  # @param dev [Boolean] a dev install: its files are links to the
336
- # source, recorded as DEV_SHA (no sha256 check) with "dev": true
191
+ # source, recorded as InstalledBundles::Sha::DEV (no sha256 check) with "dev": true
337
192
  # @param manifest_from [String, nil] the source dir: its manifest.yml is
338
193
  # kept at #kept_manifest_path (a copy, a link for a dev install),
339
- # recorded as manifest_sha (DEV_SHA for dev), with installed_by: this
194
+ # recorded as manifest_sha (InstalledBundles::Sha::DEV for dev), with installed_by: this
340
195
  # chi's VERSION. nil leaves the kept file, and the record's
341
196
  # manifest_sha and installed_by, as they are. The keys a record mirrors
342
197
  # (requires_chi … setting_types) are still written from the kwargs:
@@ -360,7 +215,7 @@ module Samagotchi
360
215
  marked = marks.to_h { |mark| [mark.file, mark] }
361
216
 
362
217
  files.each do |file_key, file_path|
363
- legacy = InstalledBundle.legacy_mark?(merged_entries[file_key])
218
+ legacy = InstalledBundles::Record.legacy_mark?(merged_entries[file_key])
364
219
  note = merged_entries.fetch(file_key, {}).slice(*MERGE_NOTE_FIELDS.map(&:to_sym))
365
220
  if file_path
366
221
  content = File.read(file_path)
@@ -407,7 +262,7 @@ module Samagotchi
407
262
  manifest_data["dev"] = true if dev
408
263
  if guardrails_files.is_a?(Hash) && !guardrails_files.empty?
409
264
  manifest_data["guardrails"] = guardrails_files.sort.to_h do |basename, path|
410
- [basename.to_s, { "sha256" => dev ? DEV_SHA : FileDigest.sha256(path.to_s) }]
265
+ [basename.to_s, { "sha256" => dev ? InstalledBundles::Sha::DEV : InstalledBundles::FileDigest.sha256(path.to_s) }]
411
266
  end
412
267
  end
413
268
  manifest_data["source_commit"] = source_commit.to_s if source_commit && !source_commit.to_s.empty?
@@ -415,19 +270,19 @@ module Samagotchi
415
270
  if plugin_file
416
271
  content = File.binread(plugin_file.to_s) # the base snapshot's bytes
417
272
  manifest_data["plugin"] = { "file" => File.basename(plugin_file.to_s),
418
- "sha256" => dev ? DEV_SHA : FileDigest.sha256(plugin_file.to_s) }
273
+ "sha256" => dev ? InstalledBundles::Sha::DEV : InstalledBundles::FileDigest.sha256(plugin_file.to_s) }
419
274
  manifest_data["plugin"]["required"] = true if plugin_required
420
275
  FileUtils.mkdir_p(File.join(bases_dir, "plugin"))
421
276
  File.binwrite(plugin_base_path(File.basename(plugin_file.to_s)), content)
422
277
  end
423
- manifest_data.merge!(self.class.mirror_fields(trust_level: trust_level, requires_chi: requires_chi, needs: needs,
424
- context_providers: context_providers, block_renderers: block_renderers,
425
- session_settings: session_settings, session_setting_types: session_setting_types,
426
- settings: settings, setting_types: setting_types))
278
+ manifest_data.merge!(mirror_fields(trust_level: trust_level, requires_chi: requires_chi, needs: needs,
279
+ context_providers: context_providers, block_renderers: block_renderers,
280
+ session_settings: session_settings, session_setting_types: session_setting_types,
281
+ settings: settings, setting_types: setting_types))
427
282
  manifest_data["includes"] = includes.map(&:to_s) if includes.is_a?(Array)
428
283
  unless scripts_files.empty?
429
284
  manifest_data["scripts"] = scripts_files.sort.to_h do |file, path|
430
- [file.to_s, { "sha256" => dev ? DEV_SHA : FileDigest.sha256(path.to_s) }]
285
+ [file.to_s, { "sha256" => dev ? InstalledBundles::Sha::DEV : InstalledBundles::FileDigest.sha256(path.to_s) }]
431
286
  end
432
287
  end
433
288
  manifest_data.merge!(kept_manifest_fields(existing, manifest_from, dev))
@@ -481,9 +336,9 @@ module Samagotchi
481
336
 
482
337
  # What a retry found of files still marked: the mark says it, its digest
483
338
  # recorded if it had none (a mark made before chi kept it; legacy_base keeps
484
- # that known, InstalledBundle.legacy_mark?), the bundle's text saved. The
339
+ # that known, InstalledBundles::Record.legacy_mark?), the bundle's text saved. The
485
340
  # rest of the manifest stays.
486
- # @param marks [Array<FileStanding::Behind>]
341
+ # @param marks [Array<InstalledBundles::FileStanding::Behind>]
487
342
  # @param incoming_dir [String] the bundle's source
488
343
  def refresh_marks(marks, incoming_dir)
489
344
  return unless installed?
@@ -521,7 +376,7 @@ module Samagotchi
521
376
 
522
377
  # These notes go, and a later merge's on the same file stays. A note that
523
378
  # can't be cleared shows once more: nothing about it fails a report.
524
- # @param notes [Array<FileStanding::MergeNote>] as #record gave them, read
379
+ # @param notes [Array<InstalledBundles::FileStanding::MergeNote>] as #record gave them, read
525
380
  # before a run
526
381
  # @param files [Array<String>] files whose note goes whatever it says: the run
527
382
  # just merged or replaced them. Another file's, a worker's meanwhile, stays.
@@ -545,7 +400,7 @@ module Samagotchi
545
400
  end
546
401
 
547
402
  # Keeps +dir+/manifest.yml for an installed bundle whose record has no
548
- # usable kept manifest (InstalledBundle#manifest_unkept?: an older chi
403
+ # usable kept manifest (InstalledBundles::Record#manifest_unkept?: an older chi
549
404
  # wrote it, or the file is gone), when +dir+ is the source of what's
550
405
  # installed: its manifest names this bundle at the recorded version and
551
406
  # says what the record keeps (#manifest_difference), and every sha the
@@ -581,13 +436,6 @@ module Samagotchi
581
436
  "#{@name}'s record can't take it: #{e.message}"
582
437
  end
583
438
 
584
- # The installed bundle's record, nil when it isn't installed.
585
- # @return [InstalledBundle, nil]
586
- # @raise [JSON::ParserError] manifest.json doesn't parse
587
- def record
588
- InstalledBundle.read(@name, File.join(@bundle_dir, "manifest.json"))
589
- end
590
-
591
439
  def ensure_supported_record_format!
592
440
  format_record(read)
593
441
  end
@@ -687,7 +535,7 @@ module Samagotchi
687
535
  private :kept_manifest_fields
688
536
 
689
537
  # +source+ to #kept_manifest_path: a link to its realpath for a dev
690
- # install (DEV_SHA), else a copy written aside and renamed (a link left
538
+ # install (InstalledBundles::Sha::DEV), else a copy written aside and renamed (a link left
691
539
  # by a dev install is replaced, never written through), its sha.
692
540
  # A source with no manifest.yml leaves none kept: nil.
693
541
  def keep_manifest(source, dev)
@@ -700,10 +548,10 @@ module Samagotchi
700
548
  if dev
701
549
  FileUtils.rm_f(dest)
702
550
  File.symlink(File.realpath(source), dest)
703
- return DEV_SHA
551
+ return InstalledBundles::Sha::DEV
704
552
  end
705
553
  AtomicFile.write(dest, File.binread(source))
706
- FileDigest.sha256(dest)
554
+ InstalledBundles::FileDigest.sha256(dest)
707
555
  end
708
556
  private :keep_manifest
709
557
 
@@ -711,7 +559,7 @@ module Samagotchi
711
559
  def adoption_mismatch(bundle, dir, bytes)
712
560
  manifest = begin
713
561
  parsed(bytes)
714
- rescue Manifest::ValidationError, Psych::Exception, EncodingError => e
562
+ rescue BundleManifest::ValidationError, Psych::Exception, EncodingError => e
715
563
  return "#{dir}'s manifest.yml doesn't read: #{e.message}"
716
564
  end
717
565
  found = "#{manifest.name} #{manifest.version}"
@@ -722,8 +570,8 @@ module Samagotchi
722
570
  return "#{dir}'s manifest.yml isn't the installed one (#{key})" if key
723
571
 
724
572
  installed_shas(bundle, manifest).each do |rel, sha|
725
- next if self.class.recorded_sha(sha).empty?
726
- return "#{dir}'s #{rel} isn't the installed one" unless self.class.file_sha(File.join(dir, rel)) == self.class.recorded_sha(sha)
573
+ next if InstalledBundles::Sha.recorded(sha).empty?
574
+ return "#{dir}'s #{rel} isn't the installed one" unless InstalledBundles::Sha.of_file(File.join(dir, rel)) == InstalledBundles::Sha.recorded(sha)
727
575
  end
728
576
  nil
729
577
  end
@@ -734,33 +582,52 @@ module Samagotchi
734
582
  Dir.mktmpdir("chi-adopt-") do |tmp|
735
583
  path = File.join(tmp, "manifest.yml")
736
584
  File.binwrite(path, bytes)
737
- Manifest.new(path: path)
738
- rescue Manifest::ValidationError => e
739
- raise Manifest::ValidationError, e.message.gsub(tmp, "the source")
585
+ BundleManifest.new(path: path)
586
+ rescue BundleManifest::ValidationError => e
587
+ raise BundleManifest::ValidationError, e.message.gsub(tmp, "the source")
740
588
  end
741
589
  end
742
590
  private :parsed
743
591
 
592
+ # The record's mirror of the manifest keys (InstalledBundles::Record::MIRROR_KEYS)
593
+ # as #write records them from its kwargs: what an older chi reads, and
594
+ # what #adopt_manifest holds a source's manifest against.
595
+ def mirror_fields(trust_level:, requires_chi:, needs:, context_providers:, block_renderers:, session_settings:,
596
+ session_setting_types:, settings:, setting_types:)
597
+ fields = {}
598
+ fields["trust_level"] = trust_level.to_s if trust_level && !trust_level.to_s.empty?
599
+ fields["requires_chi"] = requires_chi.to_s if requires_chi && !requires_chi.to_s.empty?
600
+ fields["needs"] = needs.map { |n| n.transform_keys(&:to_s).compact } if needs.is_a?(Array) && !needs.empty?
601
+ fields["context_providers"] = context_providers.map(&:to_h) unless context_providers.empty?
602
+ fields["block_renderers"] = block_renderers.map(&:to_h) unless block_renderers.empty?
603
+ fields["session_settings"] = Array(session_settings).map(&:to_s) unless Array(session_settings).empty?
604
+ fields["session_setting_types"] = session_setting_types.to_h { |k, v| [k.to_s, v.to_s] } unless session_setting_types.to_h.empty?
605
+ fields["settings"] = settings.map(&:to_s) unless settings.nil?
606
+ fields["setting_types"] = setting_types.to_h { |k, v| [k.to_s, v.to_s] } unless setting_types.to_h.empty?
607
+ fields
608
+ end
609
+ private :mirror_fields
610
+
744
611
  # The SINCE entry of a mirrored key whose shape a newer chi reads.
745
612
  MIRROR_SINCE = { "session_setting_types" => "session_settings.types", "setting_types" => "settings.types" }.freeze
746
613
 
747
614
  # The first manifest key in which +manifest+ says other than what the
748
615
  # record keeps, nil when none: each mirrored key as #write records it (one
749
616
  # the record lacks is fine only when an older chi couldn't have known it:
750
- # Manifest::SINCE has it). An older record's hooks don't count: they
751
- # no longer load, and the source can't list any (Manifest::HooksRetired).
617
+ # BundleManifest::SINCE has it). An older record's hooks don't count: they
618
+ # no longer load, and the source can't list any (BundleManifest::HooksRetired).
752
619
  def manifest_difference(bundle, manifest)
753
620
  recorded = plain(bundle.mirror)
754
- source = plain(self.class.mirror_fields(
621
+ source = plain(mirror_fields(
755
622
  trust_level: manifest.trust_level, requires_chi: manifest.requires_chi, needs: manifest.needs,
756
623
  context_providers: manifest.context_providers, block_renderers: manifest.block_renderers,
757
624
  session_settings: manifest.session_settings, session_setting_types: manifest.session_setting_types,
758
625
  settings: manifest.settings, setting_types: manifest.setting_types
759
626
  ))
760
- InstalledBundle::MIRROR_KEYS.find do |key|
627
+ InstalledBundles::Record::MIRROR_KEYS.find do |key|
761
628
  next false if recorded[key] == source[key]
762
629
 
763
- !(recorded[key].nil? && Manifest::SINCE.key?(MIRROR_SINCE.fetch(key, key)))
630
+ !(recorded[key].nil? && BundleManifest::SINCE.key?(MIRROR_SINCE.fetch(key, key)))
764
631
  end
765
632
  end
766
633
  private :manifest_difference
@@ -792,7 +659,7 @@ module Samagotchi
792
659
  private :mark_fields
793
660
 
794
661
  def incoming_sha(path)
795
- FileDigest.hex(path)
662
+ InstalledBundles::FileDigest.hex(path)
796
663
  rescue SystemCallError
797
664
  nil
798
665
  end