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,10 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "index_updater"
4
- require_relative "manifest"
3
+ require_relative "../memory_index/updater"
4
+ require_relative "../bundle_manifest"
5
5
  require_relative "memory_files"
6
6
  require_relative "file_actions"
7
- require_relative "file_digest"
7
+ require_relative "../installed_bundles/file_digest"
8
8
  require_relative "merger"
9
9
  require_relative "provenance"
10
10
  require_relative "trash"
@@ -36,7 +36,7 @@ module Samagotchi
36
36
  # - conflict: behind, and the mark records why, only when that changed;
37
37
  # - fast_forward (one Merger::Upgrade#outcome lets go ahead): the next
38
38
  # upgrade's (its save_first), behind until then;
39
- # - install: a missing file, which isn't behind (FileStanding.behind?): the
39
+ # - install: a missing file, which isn't behind (InstalledBundles::FileStanding.behind?): the
40
40
  # next upgrade or a restore puts it back, and a warning says so;
41
41
  # - one it can't read or replace: left as it is, behind, saying why.
42
42
  #
@@ -48,14 +48,14 @@ module Samagotchi
48
48
  class Remerge
49
49
  # merged: replaced (or, in a dry run, would be). settled: their mark gone
50
50
  # with no merge. stamped: their mark was updated. behind: each file that
51
- # stays behind, and why (FileStanding::Behind). work? says whether a real run
51
+ # stays behind, and why (InstalledBundles::FileStanding::Behind). work? says whether a real run
52
52
  # would change anything.
53
53
  Result = Data.define(:merged, :settled, :stamped, :behind, :warnings, :trash_copy) do
54
54
  def work? = [merged, settled, stamped].any?(&:any?)
55
55
  end
56
56
  NOTHING = Result.new(merged: [], settled: [], stamped: [], behind: [], warnings: [], trash_copy: nil).freeze
57
57
 
58
- # @param record [InstalledBundle, nil] the bundle's record (read when not given)
58
+ # @param record [InstalledBundles::Record, nil] the bundle's record (read when not given)
59
59
  def initialize(name:, scope:, target_dir:, source_dir:, record: nil)
60
60
  @name = name
61
61
  @scope = scope
@@ -78,7 +78,7 @@ module Samagotchi
78
78
  @behind = []
79
79
  @warnings = []
80
80
  @trash = Trash.new(@name)
81
- @manifest = Manifest.read(dir: @source_dir)
81
+ @manifest = BundleManifest.read(dir: @source_dir)
82
82
  shipped = MemoryFiles.select(@source_dir, @manifest)
83
83
  marked.each { |key, entry| retry_file(key, entry, shipped) }
84
84
  settle unless dry_run
@@ -102,7 +102,7 @@ module Samagotchi
102
102
 
103
103
  retry_row(key, entry, state, FileActions.for(:remerge, state))
104
104
  rescue SystemCallError => e
105
- stay(key, FileStanding.left_as_it_is(e.message))
105
+ stay(key, InstalledBundles::FileStanding.left_as_it_is(e.message))
106
106
  end
107
107
 
108
108
  # The file's REMERGE row, done. A :decide cell, and a behind one, asks
@@ -116,19 +116,19 @@ module Samagotchi
116
116
  if action.do == :merge then merge_file(key, entry, state, decision.outcome)
117
117
  elsif action.behind?
118
118
  waits = decision.outcome.status == :fast_forward
119
- mark(key, entry, waits ? behind_for(key, FileStanding::WAITS_FOR_UPGRADE) : view.behind(key, decision.outcome, @record.version))
119
+ mark(key, entry, waits ? behind_for(key, InstalledBundles::FileStanding::WAITS_FOR_UPGRADE) : view.behind(key, decision.outcome, @record.version))
120
120
  elsif action.word == :settled then @settled << key
121
- elsif state.kind == :missing then @warnings << "#{key}: #{FileStanding::FILE_MISSING}"
122
- elsif state.kind == :unreadable then FileDigest.hex(state.paths[:current])
121
+ elsif state.kind == :missing then @warnings << "#{key}: #{InstalledBundles::FileStanding::FILE_MISSING}"
122
+ elsif state.kind == :unreadable then InstalledBundles::FileDigest.hex(state.paths[:current])
123
123
  end
124
124
  end
125
125
 
126
- def behind_for(key, why) = FileStanding::Behind.new(file: key, version: @record.version, why: why)
126
+ def behind_for(key, why) = InstalledBundles::FileStanding::Behind.new(file: key, version: @record.version, why: why)
127
127
 
128
128
  # The record says it too, so chi bundle status reads as the row does. There
129
129
  # is no bundle text to save or compare with: only the words can change.
130
130
  def not_shipped(key, entry)
131
- behind = behind_for(key, FileStanding::NOT_SHIPPED)
131
+ behind = behind_for(key, InstalledBundles::FileStanding::NOT_SHIPPED)
132
132
  @behind << behind
133
133
  @stamped << behind unless @record.behind_entry(key, entry) == behind
134
134
  end
@@ -144,12 +144,12 @@ module Samagotchi
144
144
  end
145
145
 
146
146
  def merge_file(key, entry, state, outcome)
147
- warning = FileDigest.mismatch(key, state.paths[:incoming], @manifest.checksum_for(key))
147
+ warning = InstalledBundles::FileDigest.mismatch(key, state.paths[:incoming], @manifest.checksum_for(key))
148
148
  @warnings << warning if warning
149
149
  return @merged << key if @dry_run
150
150
  return @merged << key if Merger.write_merged(view.paths(key)[:current], outcome, @trash)
151
151
 
152
- mark(key, entry, behind_for(key, FileStanding.no_merge(TextMerge::RACED)))
152
+ mark(key, entry, behind_for(key, InstalledBundles::FileStanding.no_merge(TextMerge::RACED)))
153
153
  end
154
154
 
155
155
  def settle
@@ -171,7 +171,7 @@ module Samagotchi
171
171
  def refresh_index(key)
172
172
  return if ModelOverlay.bundle_overlay?(key, bundle_files: @record.files.keys, target_dir: @target_dir)
173
173
 
174
- IndexUpdater.update_index(@scope, key.delete_suffix(".md"), File.size(view.paths(key)[:current]), nil, source: @name)
174
+ MemoryIndex::Updater.update_index(@scope, key.delete_suffix(".md"), File.size(view.paths(key)[:current]), nil, source: @name)
175
175
  rescue StandardError => e
176
176
  @warnings << "index.md: line for #{key.delete_suffix(".md")} not updated (#{e.message})"
177
177
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "yaml"
4
- require_relative "file_digest"
4
+ require_relative "../installed_bundles/file_digest"
5
5
 
6
6
  module Samagotchi
7
7
  module MemoryBundle
@@ -99,7 +99,7 @@ module Samagotchi
99
99
 
100
100
  def digest(dir, file)
101
101
  path = File.join(dir, file)
102
- hex = FileDigest.hex(path)
102
+ hex = InstalledBundles::FileDigest.hex(path)
103
103
  raise Error, "#{File.basename(dir)}/manifest.yml names #{file}, which isn't there" if hex.nil?
104
104
 
105
105
  hex
@@ -67,7 +67,7 @@ module Samagotchi
67
67
  end
68
68
 
69
69
  # The name a bundle's manifest gives, nil when it doesn't read. Not
70
- # Manifest.read: an older manifest needn't pass this chi's checks.
70
+ # BundleManifest.read: an older manifest needn't pass this chi's checks.
71
71
  def manifest_name(dir)
72
72
  data = YAML.safe_load_file(File.join(dir, "manifest.yml"))
73
73
  data.is_a?(Hash) ? data["name"] : nil
@@ -4,7 +4,7 @@ require_relative "asset_installer"
4
4
  require_relative "asset_state"
5
5
  require_relative "installer"
6
6
  require_relative "listing"
7
- require_relative "manifest"
7
+ require_relative "../bundle_manifest"
8
8
  require_relative "manifest_standing"
9
9
  require_relative "merger"
10
10
  require_relative "profile"
@@ -50,7 +50,7 @@ module Samagotchi
50
50
  # :up_to_date, :skipped, :failed. note says why for skipped/failed,
51
51
  # what a retry at the same version warned of, and an update's Checksum
52
52
  # mismatch warnings. merged and replaced are file
53
- # names, behind FileStanding::Behind; under plan they are what apply would
53
+ # names, behind InstalledBundles::FileStanding::Behind; under plan they are what apply would
54
54
  # merge, replace or leave behind.
55
55
  # added_files is the new version's memory files the bundle didn't own
56
56
  # before; adds is nil but for a profile: the members it installs.
@@ -59,7 +59,7 @@ module Samagotchi
59
59
  # trash_copy is nil but under apply when it saved edited files
60
60
  # (Installer#trash_copy_line).
61
61
  # earlier: the merge notes on its record when the plan read it
62
- # (FileStanding::MergeNote): this run's own merges aren't among them.
62
+ # (InstalledBundles::FileStanding::MergeNote): this run's own merges aren't among them.
63
63
  # manifest: what a row at the shipped version says of its manifest.yml
64
64
  # (ManifestStanding.keep's line: would keep or kept it, or why not and
65
65
  # the command), nil when nothing.
@@ -94,7 +94,7 @@ module Samagotchi
94
94
  # The installed bundles chi doesn't update (plan's "not from chi"), for
95
95
  # chi update's re-check: not a profile (no code of its own, its members
96
96
  # are chi's), nor one whose record doesn't read (its row says so).
97
- # @return [Array<InstalledBundle>]
97
+ # @return [Array<InstalledBundles::Record>]
98
98
  def external(shipped_dir: SourceNormalizer::SHIPPED_DIR)
99
99
  shipped = Listing.shipped(dir: shipped_dir).to_h { |s| [s.name, s] }
100
100
  installed.filter_map { |name, bundle| bundle unless bundle.error? || bundle.profile? || from_chi?(bundle, shipped[name]) }
@@ -175,9 +175,9 @@ module Samagotchi
175
175
  end
176
176
  end
177
177
 
178
- # [name, InstalledBundle] for each installed bundle but the system one, by name.
178
+ # [name, InstalledBundles::Record] for each installed bundle but the system one, by name.
179
179
  def installed
180
- Provenance.each_installed.reject { |name, _| name == SystemBundle::BUNDLE_NAME }
180
+ InstalledBundles.each.reject { |name, _| name == SystemBundle::BUNDLE_NAME }
181
181
  end
182
182
 
183
183
  # Whether it is the bundle chi ships as +ship+ (nil: none of its name).
@@ -198,14 +198,14 @@ module Samagotchi
198
198
  return profile_row(row, bundle, shipped_dir, chi_version) if Profile.shipped_meta?(row.source_dir, shipped_dir: shipped_dir)
199
199
  return same_version_row(row, bundle) unless Listing.newer?(row.to, row.from)
200
200
 
201
- manifest = Manifest.read(dir: row.source_dir)
202
- if Manifest.requires_chi_failure(manifest.requires_chi, chi_version)
201
+ manifest = BundleManifest.read(dir: row.source_dir)
202
+ if BundleManifest.requires_chi_failure(manifest.requires_chi, chi_version)
203
203
  return skip(row, "needs chi #{manifest.requires_chi}", keep_to: true)
204
204
  end
205
205
 
206
- # The upgrade would refuse it (Manifest::HooksRetired.refuse_source_files).
207
- hooks = Manifest::HooksRetired.files_in(row.source_dir)
208
- return skip(row, Manifest::HooksRetired.for_files(hooks).message, keep_to: true) unless hooks.empty?
206
+ # The upgrade would refuse it (BundleManifest::HooksRetired.refuse_source_files).
207
+ hooks = BundleManifest::HooksRetired.files_in(row.source_dir)
208
+ return skip(row, BundleManifest::HooksRetired.for_files(hooks).message, keep_to: true) unless hooks.empty?
209
209
  return skip(row, "project scope: chi bundle upgrade #{ship.source} in the project") if row.scope == "project"
210
210
  return skip(row, "unknown scope #{row.scope}: upgrade chi or reinstall") unless MemoryPaths.scope_dir(row.scope)
211
211
 
@@ -263,15 +263,15 @@ module Samagotchi
263
263
  end
264
264
 
265
265
  def profile_row(row, bundle, shipped_dir, chi_version)
266
- manifest = Manifest.read(dir: row.source_dir)
266
+ manifest = BundleManifest.read(dir: row.source_dir)
267
267
  newer = Listing.newer?(row.to, row.from)
268
- if newer && Manifest.requires_chi_failure(manifest.requires_chi, chi_version)
268
+ if newer && BundleManifest.requires_chi_failure(manifest.requires_chi, chi_version)
269
269
  return skip(row, "needs chi #{manifest.requires_chi}", keep_to: true)
270
270
  end
271
271
 
272
272
  row.adds = Profile.to_install(manifest, bundle).reject do |member|
273
- Manifest.requires_chi_failure(Manifest.read(dir: File.join(shipped_dir, member)).requires_chi, chi_version)
274
- rescue Manifest::ValidationError, Psych::Exception
273
+ BundleManifest.requires_chi_failure(BundleManifest.read(dir: File.join(shipped_dir, member)).requires_chi, chi_version)
274
+ rescue BundleManifest::ValidationError, Psych::Exception
275
275
  false # apply reports it failing
276
276
  end
277
277
  row.to = nil unless newer
@@ -333,9 +333,9 @@ module Samagotchi
333
333
 
334
334
  # Whether the file differs from its recorded sha (none recorded: no).
335
335
  def edited?(path, recorded)
336
- return false if Provenance.recorded_sha(recorded).empty? || !File.file?(path)
336
+ return false if InstalledBundles::Sha.recorded(recorded).empty? || !File.file?(path)
337
337
 
338
- !Provenance.sha_matches?(path, recorded)
338
+ !InstalledBundles::Sha.matches?(path, recorded)
339
339
  end
340
340
  end
341
341
  end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "file_digest"
3
+ require_relative "../installed_bundles/file_digest"
4
4
  require_relative "provenance"
5
5
  require_relative "file_state"
6
6
  require_relative "asset_state"
7
- require_relative "manifest"
7
+ require_relative "../bundle_manifest"
8
8
  require_relative "../version"
9
- require_relative "index_updater"
9
+ require_relative "../memory_index/updater"
10
10
  require_relative "../memory_paths"
11
11
  require_relative "../bundle_needs"
12
12
  require_relative "../model_overlay"
@@ -20,8 +20,8 @@ module Samagotchi
20
20
  # disk, its installed plugin, and its needs
21
21
  # with what this process's PATH found. The +files+ entries and +plugin+
22
22
  # stay Hashes: their fields differ per case (an unchecked file has no
23
- # paths at all). An entry's behind is its FileStanding::Behind, nil for a
24
- # file that isn't; merge_note its FileStanding::MergeNote, nil without one;
23
+ # paths at all). An entry's behind is its InstalledBundles::FileStanding::Behind, nil for a
24
+ # file that isn't; merge_note its InstalledBundles::FileStanding::MergeNote, nil without one;
25
25
  # unreadable: the file is there, but this chi can't read it.
26
26
  # rules and scripts: each recorded one by name, "ok", "modified" or
27
27
  # "missing" (asset_states).
@@ -115,18 +115,18 @@ module Samagotchi
115
115
 
116
116
  # The installed plugin: {file:, path:, state:, requires_chi:,
117
117
  # requires_failure:}; state is "ok", "modified" (its sha256 differs
118
- # from the installed one: it won't load) or "missing". nil without one.
118
+ # from the installed one, or none is recorded: it won't load) or
119
+ # "missing", from the verdict (Dir#file_refusal(:plugin),
120
+ # InstalledBundles::Standing). nil without one.
119
121
  def self.plugin_status(provenance, bundle)
120
122
  path = provenance.plugin_path(bundle)
121
123
  return nil unless path
122
124
 
123
- state = if !File.file?(path) then "missing"
124
- elsif Provenance.sha_matches?(path, bundle.plugin.sha256) then "ok"
125
- else "modified"
126
- end
127
- manifest = bundle.manifest
128
- { file: File.basename(path), path: path, state: state, requires_chi: manifest.requires_chi,
129
- requires_failure: manifest.requires_chi_failure }
125
+ refusal = provenance.file_refusal(:plugin, bundle.plugin.file, bundle.plugin.sha256)
126
+ state = { nil => "ok", file_missing: "missing" }.fetch(refusal&.kind, "modified")
127
+ standing = InstalledBundles::Standing.of(provenance.name, bundle)
128
+ { file: File.basename(path), path: path, state: state, requires_chi: bundle.manifest.requires_chi,
129
+ requires_failure: standing.too_new? ? standing.refusal.text : nil }
130
130
  end
131
131
 
132
132
  def self.unchecked_file(meta)
@@ -138,11 +138,11 @@ module Samagotchi
138
138
  # Index lines name the entry without ".md" (Installer#update_target_index,
139
139
  # memory_write); a legacy "name.md" line counts too.
140
140
  def self.index_has_entry?(scope, file_key)
141
- path = IndexUpdater.index_path_for(scope)
141
+ path = MemoryIndex::Updater.index_path_for(scope)
142
142
  return false unless path && File.exist?(path)
143
143
 
144
144
  content = File.read(path)
145
- [file_key.delete_suffix(".md"), file_key].uniq.any? { |name| content.match?(IndexUpdater.managed_pattern(name)) }
145
+ [file_key.delete_suffix(".md"), file_key].uniq.any? { |name| content.match?(MemoryIndex::Updater.managed_pattern(name)) }
146
146
  end
147
147
  end
148
148
  end
@@ -2,10 +2,10 @@
2
2
 
3
3
  require "fileutils"
4
4
  require_relative "installer"
5
- require_relative "file_digest"
5
+ require_relative "../installed_bundles/file_digest"
6
6
  require_relative "provenance"
7
- require_relative "manifest"
8
- require_relative "index_updater"
7
+ require_relative "../bundle_manifest"
8
+ require_relative "../memory_index/updater"
9
9
  require_relative "memory_files"
10
10
  require_relative "merger"
11
11
  require_relative "remerge"
@@ -40,7 +40,7 @@ module Samagotchi
40
40
  # :newer_installed (left as is), :skipped (SKIP_ENV or no shipped
41
41
  # bundle) or :failed (error: says why); merged lists the files a text
42
42
  # merge replaced (trash_copy says where the old ones went), behind each file
43
- # that stays behind (FileStanding::Behind). ensure! says only what an upgrade
43
+ # that stays behind (InstalledBundles::FileStanding::Behind). ensure! says only what an upgrade
44
44
  # left behind: a file that was already behind isn't news at every start.
45
45
  Result = Struct.new(:from, :to, :status, :merged, :behind, :trash_copy, :warnings, :error, keyword_init: true) do
46
46
  def changed? = %i[installed updated restored merged].include?(status)
@@ -60,12 +60,12 @@ module Samagotchi
60
60
  with_lock { note_newer_installed(result.from, result.to) }
61
61
  end
62
62
  unless result.merged.empty?
63
- note = [FileStanding.merged_line(result.merged, result.to), result.trash_copy].compact.join("; ")
63
+ note = [InstalledBundles::FileStanding.merged_line(result.merged, result.to), result.trash_copy].compact.join("; ")
64
64
  Log.info(:memory, "system_bundle_merged", echo: "[samagotchi-system] #{note}", files: result.merged.join(","), to: result.to.to_s)
65
65
  end
66
66
  if result.status == :updated
67
67
  result.behind.each do |file|
68
- Log.info(:memory, "system_file_behind", echo: "[samagotchi-system] #{file.line} (#{FileStanding.diff_hint(BUNDLE_NAME, file.file)})", file: file.file)
68
+ Log.info(:memory, "system_file_behind", echo: "[samagotchi-system] #{file.line} (#{InstalledBundles::FileStanding.diff_hint(BUNDLE_NAME, file.file)})", file: file.file)
69
69
  end
70
70
  end
71
71
  result.warnings.each { |w| Log.warn(:memory, "system_bundle_warning", echo: "[samagotchi-system] #{w}") }
@@ -74,7 +74,7 @@ module Samagotchi
74
74
 
75
75
  # The merges no chi update has shown (MergeNote): ask before .sync, whose
76
76
  # own merges its caller reports itself.
77
- # @return [Array<FileStanding::MergeNote>]
77
+ # @return [Array<InstalledBundles::FileStanding::MergeNote>]
78
78
  def merge_notes
79
79
  Provenance.new(name: BUNDLE_NAME).record&.merge_notes || []
80
80
  rescue StandardError
@@ -100,8 +100,8 @@ module Samagotchi
100
100
  return result(:skipped, error: "no shipped system bundle") unless File.directory?(GEM_BUNDLE_DIR)
101
101
 
102
102
  begin
103
- gem_manifest = Manifest.read(dir: GEM_BUNDLE_DIR)
104
- rescue Manifest::ValidationError => e
103
+ gem_manifest = BundleManifest.read(dir: GEM_BUNDLE_DIR)
104
+ rescue BundleManifest::ValidationError => e
105
105
  return result(:failed, error: "invalid gem manifest: #{e.message}")
106
106
  end
107
107
  to = gem_manifest.version.to_s
@@ -146,7 +146,7 @@ module Samagotchi
146
146
  # the provenance the first one wrote, instead of installing over it and
147
147
  # warning "Skipped … already exists" for every file.
148
148
  def with_lock
149
- dir = Provenance.bundles_dir
149
+ dir = InstalledBundles.dir
150
150
  FileUtils.mkdir_p(dir)
151
151
  File.open(File.join(dir, "#{BUNDLE_NAME}.lock"), File::RDWR | File::CREAT, 0o644) do |f|
152
152
  f.flock(File::LOCK_EX)
@@ -252,7 +252,7 @@ module Samagotchi
252
252
  Provenance.new(name: BUNDLE_NAME).record_restored(key, source, description: description)
253
253
  warnings = [restore_mismatch(key, gem_manifest)].compact
254
254
  begin
255
- IndexUpdater.update_index(SCOPE, key.delete_suffix(".md"), File.size(target), description, source: BUNDLE_NAME)
255
+ MemoryIndex::Updater.update_index(SCOPE, key.delete_suffix(".md"), File.size(target), description, source: BUNDLE_NAME)
256
256
  rescue StandardError => e
257
257
  warnings << "index.md: line for #{key.delete_suffix(".md")} not updated (#{e.message})"
258
258
  end
@@ -260,7 +260,7 @@ module Samagotchi
260
260
  end
261
261
 
262
262
  def restore_mismatch(key, gem_manifest)
263
- FileDigest.mismatch(key, File.join(GEM_BUNDLE_DIR, key), gem_manifest.checksum_for(key))
263
+ InstalledBundles::FileDigest.mismatch(key, File.join(GEM_BUNDLE_DIR, key), gem_manifest.checksum_for(key))
264
264
  end
265
265
 
266
266
  # An up-to-date bundle's marked files, retried. No marks: nothing is read.
@@ -11,7 +11,7 @@ module Samagotchi
11
11
  # <system memories>/.bundles/.trash/<bundle>-<YYYYmmdd-HHMMSS>/, one dir
12
12
  # per operation (-2, -3… when one already has that name). A file about to
13
13
  # be overwritten is copied there instead (#copy). A dot dir, so
14
- # Provenance.each_installed never reads it as a bundle.
14
+ # InstalledBundles.each never reads it as a bundle.
15
15
  #
16
16
  # `chi bundle trash` lists and empties the trash.
17
17
  class Trash
@@ -117,7 +117,7 @@ module Samagotchi
117
117
  return false if File.symlink?(path) || !File.file?(path)
118
118
  return false if incoming && File.file?(incoming) && FileUtils.identical?(incoming, path)
119
119
 
120
- Provenance.recorded_sha(recorded).empty? || !Provenance.sha_matches?(path, recorded)
120
+ InstalledBundles::Sha.recorded(recorded).empty? || !InstalledBundles::Sha.matches?(path, recorded)
121
121
  end
122
122
 
123
123
  # What #copy_edits would copy (read-only), as <label>/<its path in dir>.
@@ -6,7 +6,7 @@ require_relative "../memory_paths"
6
6
  require_relative "provenance"
7
7
  require_relative "asset_state"
8
8
  require_relative "file_actions"
9
- require_relative "index_updater"
9
+ require_relative "../memory_index/updater"
10
10
  require_relative "trash"
11
11
  require_relative "../model_overlay"
12
12
 
@@ -74,7 +74,7 @@ module Samagotchi
74
74
 
75
75
  # A file another installed bundle lists too stays, edited or not.
76
76
  shared = files.keys.map(&:to_s).to_h do |key|
77
- [key, Provenance.claimants(key, scope: target_scope, except: @name)]
77
+ [key, InstalledBundles.claimants(key, scope: target_scope, except: @name)]
78
78
  end.reject { |_, others| others.empty? }
79
79
 
80
80
  # Each file's UNINSTALL row (its source-less FileState): an edit
@@ -90,13 +90,13 @@ module Samagotchi
90
90
  next if state.shared_with.any?
91
91
 
92
92
  # Whether it was edited can't be told: the read raises (nothing lost).
93
- FileDigest.hex(state.paths[:current]) if state.kind == :unreadable
93
+ InstalledBundles::FileDigest.hex(state.paths[:current]) if state.kind == :unreadable
94
94
  blocked << key
95
- @warnings << "Skipped #{key}: local edits detected (#{FileStanding::FORCE_MOVES})"
95
+ @warnings << "Skipped #{key}: local edits detected (#{InstalledBundles::FileStanding::FORCE_MOVES})"
96
96
  end
97
97
  end
98
98
 
99
- raise UninstallError, "Uninstall blocked: #{blocked.join(", ")} has local edits (#{FileStanding::FORCE_MOVES})" if blocked.any?
99
+ raise UninstallError, "Uninstall blocked: #{blocked.join(", ")} has local edits (#{InstalledBundles::FileStanding::FORCE_MOVES})" if blocked.any?
100
100
 
101
101
  save_edits(provenance, bundle)
102
102
 
@@ -118,8 +118,8 @@ module Samagotchi
118
118
  end
119
119
  # Remove index line (not fatal: a warning says so)
120
120
  begin
121
- IndexUpdater.remove_index(target_scope, file_key_str.delete_suffix(".md"), target_dir: target_dir)
122
- IndexUpdater.remove_index(target_scope, file_key_str, target_dir: target_dir) # legacy "name.md" line
121
+ MemoryIndex::Updater.remove_index(target_scope, file_key_str.delete_suffix(".md"), target_dir: target_dir)
122
+ MemoryIndex::Updater.remove_index(target_scope, file_key_str, target_dir: target_dir) # legacy "name.md" line
123
123
  rescue StandardError => e
124
124
  @warnings << "index.md: line for #{file_key_str.delete_suffix(".md")} not removed (#{e.message})"
125
125
  end
@@ -177,7 +177,7 @@ module Samagotchi
177
177
  # folder the file lives in, which for an install interrupted before its
178
178
  # record is the journal's target dir, not the current folder's.
179
179
  def retag(scope, path, file_key, bundle, target_dir:)
180
- IndexUpdater.update_index(scope, file_key.delete_suffix(".md"), File.size(path), source: bundle, target_dir: target_dir)
180
+ MemoryIndex::Updater.update_index(scope, file_key.delete_suffix(".md"), File.size(path), source: bundle, target_dir: target_dir)
181
181
  rescue StandardError => e
182
182
  @warnings << "index.md: line for #{file_key.delete_suffix(".md")} not updated (#{e.message})"
183
183
  end
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "memory_bundle/manifest"
3
+ require_relative "bundle_manifest"
4
4
  require_relative "memory_bundle/shas"
5
5
  require_relative "memory_bundle/source"
6
6
  require_relative "memory_bundle/provenance"
7
7
  require_relative "memory_bundle/placeholder"
8
- require_relative "memory_bundle/index_updater"
8
+ require_relative "memory_index/updater"
9
9
  require_relative "memory_bundle/merger"
10
10
  require_relative "memory_bundle/installer"
11
11
  require_relative "memory_bundle/uninstaller"
@@ -7,8 +7,8 @@ require_relative "cli/flags"
7
7
  require_relative "memory_paths"
8
8
  require_relative "system_prompt"
9
9
  require_relative "tools/memory"
10
- require_relative "memory_bundle/index_updater"
11
- require_relative "memory_bundle/provenance"
10
+ require_relative "memory_index/updater"
11
+ require_relative "installed_bundles"
12
12
  require_relative "memory_usage"
13
13
 
14
14
  module Samagotchi
@@ -105,7 +105,7 @@ module Samagotchi
105
105
  # name within a scope.
106
106
  # @return [Array<Entry>]
107
107
  def self.entries(scope = nil)
108
- owners = MemoryBundle::Provenance.each_installed.reject { |_, bundle| bundle.error? }
108
+ owners = InstalledBundles.standings.reject(&:unreadable?).map { |standing| [standing.name, standing.record] }
109
109
  (scope ? [scope] : SCOPES).flat_map { |s| scope_entries(s, owners) }
110
110
  end
111
111
 
@@ -155,7 +155,7 @@ module Samagotchi
155
155
  end
156
156
 
157
157
  def self.entry(name, scope, path, line, file, overlay_keys, owners)
158
- source = line && MemoryBundle::IndexUpdater.extract_source(line)
158
+ source = line && MemoryIndex::Updater.extract_source(line)
159
159
  source ||= owners.find { |_, bundle| bundle.effective_scope == scope && bundle.owns?("#{name}.md") }&.first
160
160
  Entry.new(
161
161
  name: name, scope: scope, path: path, bytes: file ? File.size(path) : nil,
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../config"
4
- require_relative "index_size"
5
- require_relative "index_sync"
6
- require_relative "index_updater"
4
+ require_relative "size"
5
+ require_relative "sync"
6
+ require_relative "updater"
7
7
  require_relative "../model_overlay"
8
8
  require_relative "../tools/memory"
9
9
 
10
10
  module Samagotchi
11
- module MemoryBundle
11
+ module MemoryIndex
12
12
  # The size of one memory's text, which each memory_read of it pays
13
13
  # (memory_read is not cut by max_tool_output_chars). memory_write and
14
14
  # write/edit on a memory compare the size before and after a write
15
15
  # against memory.entry_warn_tokens (#crossing_note); `chi self` lists
16
- # the memories over it (SelfReport). tokens is the estimate IndexSize
16
+ # the memories over it (SelfReport). tokens is the estimate Size
17
17
  # uses (characters / context.chars_per_token, rounded up).
18
18
  #
19
19
  # A memory that didn't exist before a write counts as 0 tokens. Not
@@ -22,7 +22,7 @@ module Samagotchi
22
22
  EntrySize = Data.define(:name, :scope, :tokens) do
23
23
  # @param text [String, nil] the memory's text
24
24
  # @return [EntrySize]
25
- def self.of_text(name, scope, text, chars_per_token: IndexSize.chars_per_token)
25
+ def self.of_text(name, scope, text, chars_per_token: Size.chars_per_token)
26
26
  new(name: name.to_s, scope: scope.to_s, tokens: (text.to_s.length / chars_per_token).ceil)
27
27
  end
28
28
 
@@ -30,7 +30,7 @@ module Samagotchi
30
30
  # model note).
31
31
  def self.counted?(name)
32
32
  name = name.to_s
33
- !name.casecmp?(IndexUpdater::MEMORY_INDEX) && !name.start_with?(Tools::MODEL_NOTES_PREFIX)
33
+ !name.casecmp?(Updater::MEMORY_INDEX) && !name.start_with?(Tools::MODEL_NOTES_PREFIX)
34
34
  end
35
35
 
36
36
  # The memory file at +path+ as it is now (0 tokens when it isn't
@@ -42,7 +42,7 @@ module Samagotchi
42
42
  return nil unless warn_limit.positive?
43
43
 
44
44
  path = File.expand_path(path.to_s)
45
- scope = IndexSync.memory_scope(path)
45
+ scope = Sync.memory_scope(path)
46
46
  name = File.basename(path, ".md")
47
47
  return nil unless scope && counted?(name) && !ModelOverlay.overlay_file?(path)
48
48
 
@@ -5,7 +5,7 @@ require_relative "../tools/memory"
5
5
  require_relative "../model_notes"
6
6
 
7
7
  module Samagotchi
8
- module MemoryBundle
8
+ module MemoryIndex
9
9
  # The size of one scope's memory index as a prompt carries it: every
10
10
  # session's system prompt holds both indexes in full, so their size is
11
11
  # paid on every request. tokens is the estimate ContextStatus uses
@@ -16,10 +16,10 @@ module Samagotchi
16
16
  # after a write against memory.index_warn_tokens (#crossing_note);
17
17
  # chi self shows it (#summary), and a session's figure comes from the
18
18
  # index text its prompt holds (SystemPrompt).
19
- IndexSize = Data.define(:scope, :bytes, :lines, :tokens) do
19
+ Size = Data.define(:scope, :bytes, :lines, :tokens) do
20
20
  # @param text [String, nil] the index text
21
21
  # @param chars_per_token [Float]
22
- # @return [IndexSize]
22
+ # @return [Size]
23
23
  def self.of_text(scope, text, chars_per_token: self.chars_per_token)
24
24
  text = text.to_s
25
25
  new(scope: scope.to_s, bytes: text.bytesize, lines: text.each_line.count { |line| !line.strip.empty? },
@@ -30,7 +30,7 @@ module Samagotchi
30
30
  # filters (mutes): index.md, or the file listing without one, minus
31
31
  # the model notes' lines (ModelNotes.filter_index).
32
32
  # @param env [Hash] the XDG env the memories dir resolves from
33
- # @return [IndexSize]
33
+ # @return [Size]
34
34
  def self.of_scope(scope, chars_per_token: self.chars_per_token, env: ENV)
35
35
  text = Tools::MemoryRead.scoped_index(scope, env: env)
36
36
  of_text(scope, ModelNotes.filter_index(text, scope, env: env), chars_per_token: chars_per_token)
@@ -66,8 +66,8 @@ module Samagotchi
66
66
  # start a loop of small tightenings. Sessions sharing the dir measure
67
67
  # on their own (before and after aren't one locked step), so writes
68
68
  # in parallel sessions racing over the limit may each get it.
69
- # @param before [IndexSize, nil]
70
- # @param after [IndexSize, nil]
69
+ # @param before [Size, nil]
70
+ # @param after [Size, nil]
71
71
  # @return [String, nil]
72
72
  def self.crossing_note(before, after, limit = warn_limit)
73
73
  return nil unless before && after && after.over?(limit) && !before.over?(limit)
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require_relative "index_updater"
3
+ require_relative "updater"
4
4
  require_relative "../tools/memory"
5
5
  require_relative "../model_overlay"
6
6
  require_relative "../log"
7
7
 
8
8
  module Samagotchi
9
- module MemoryBundle
9
+ module MemoryIndex
10
10
  # Keeps a memory's managed index.md line current when the file tools
11
11
  # (write, edit) change it, as memory_write does: the ToolRunner calls
12
12
  # .refresh after a write/edit that changed its file (one it could diff:
@@ -18,7 +18,7 @@ module Samagotchi
18
18
  # name has a dot and a sibling of the same stem reads as an overlay too,
19
19
  # a leftover ambiguity). Writes into another project's folder and
20
20
  # execute's writes aren't seen.
21
- module IndexSync
21
+ module Sync
22
22
  SCOPES = %w[project system].freeze
23
23
 
24
24
  # @param path [String] the file write/edit changed
@@ -31,7 +31,7 @@ module Samagotchi
31
31
  name = File.basename(path, ".md")
32
32
  return false if ModelOverlay.overlay_file?(path)
33
33
 
34
- IndexUpdater.update_index(scope, name, File.size(path), nil)
34
+ Updater.update_index(scope, name, File.size(path), nil)
35
35
  rescue StandardError => e
36
36
  Log.warn(:memory, "index_sync_failed", path: path, error: "#{e.class}: #{e.message}")
37
37
  false
@@ -48,7 +48,7 @@ module Samagotchi
48
48
  name = File.basename(path, ".md")
49
49
  return nil unless File.extname(path) == ".md"
50
50
  # Any case: on a case-insensitive disk INDEX.md is the index itself.
51
- return nil if name.casecmp?(IndexUpdater::MEMORY_INDEX) || name.start_with?(".") || File.symlink?(path)
51
+ return nil if name.casecmp?(Updater::MEMORY_INDEX) || name.start_with?(".") || File.symlink?(path)
52
52
 
53
53
  dir = real(File.dirname(path))
54
54
  SCOPES.find { |s| real(Tools::MemoryRead.memories_dir(s)) == dir }