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
@@ -13,7 +13,7 @@ module Samagotchi
13
13
  # (RecapSetup). Its
14
14
  # bundles part: {bundle => {key => value}}, string keys, scalar values
15
15
  # (.typed). A bundle offers the keys a session may set in its manifest's
16
- # session_settings: (recorded at install, InstalledManifest#session_settings);
16
+ # session_settings: (recorded at install, InstalledBundles::Manifest#session_settings);
17
17
  # any other bundle or key is refused, so a typo isn't saved, and so is a
18
18
  # value of another type than the manifest gives the key (session_settings:
19
19
  # {cap: number}). The session keeps them (Session#settings, SessionSetup)
@@ -22,7 +22,7 @@ module Samagotchi
22
22
  # A refused --set (or request field): the message says why.
23
23
  class Error < ArgumentError; end
24
24
 
25
- # A bundle name (MemoryBundle::Manifest::BUNDLE_NAME) and a setting key.
25
+ # A bundle name (BundleManifest::BUNDLE_NAME) and a setting key.
26
26
  BUNDLE = /\A[A-Za-z0-9][A-Za-z0-9_-]*\z/
27
27
  KEY = /\A[A-Za-z0-9_][A-Za-z0-9_-]*\z/
28
28
  # The values a setting may take.
@@ -84,7 +84,7 @@ module Samagotchi
84
84
  def initialize(offered:, notes:, types: {}) = super
85
85
  end
86
86
 
87
- # What each type (MemoryBundle::Manifest::SESSION_SETTING_TYPES) takes,
87
+ # What each type (BundleManifest::SESSION_SETTING_TYPES) takes,
88
88
  # as a value .typed gives it, and how a refusal names it. A number may
89
89
  # be off (false), as a bundle's own settings are turned off.
90
90
  TYPES = {
@@ -288,23 +288,25 @@ module Samagotchi
288
288
  def offered(dir: nil) = availability(dir: dir).offered
289
289
 
290
290
  # The installed bundles a session may set settings of: a record that
291
- # doesn't read is left out, and so is one whose plugin doesn't load as
292
- # chi's loader decides (Plugin::Loader#unloadable_reason: requires_chi,
293
- # the file, its sha256), with a note saying why. A record with no
291
+ # doesn't read is left out, and so is one whose plugin doesn't load, as
292
+ # the verdict says for the loader (InstalledBundles::Standing: requires_chi;
293
+ # Dir#file_refusal(:plugin): the file, its sha256), with a note saying why. A record with no
294
294
  # session_settings whose shipped bundle lists some (installed before
295
295
  # them) gets a note on refreshing it.
296
296
  # @return [Availability]
297
297
  def availability(dir: nil, shipped_dir: SHIPPED_DIR)
298
- require_relative "memory_bundle/provenance"
299
- require_relative "memory_bundle/manifest"
300
- dir ||= MemoryBundle::Provenance.bundles_dir
298
+ require_relative "installed_bundles"
299
+ require_relative "bundle_manifest"
300
+ dir ||= InstalledBundles.dir
301
301
  offered = {}
302
302
  notes = {}
303
303
  types = {}
304
- MemoryBundle::Provenance.each_installed(dir: dir) do |name, bundle|
305
- next if bundle.error?
304
+ InstalledBundles.standings(dir: dir) do |standing|
305
+ next if standing.unreadable?
306
306
 
307
- if (reason = unloadable(dir, name, bundle))
307
+ name = standing.name
308
+ bundle = standing.record
309
+ if (reason = unloadable(dir, standing))
308
310
  notes[name] = "#{name} is installed but its plugin doesn't load: #{reason}"
309
311
  next
310
312
  end
@@ -374,14 +376,16 @@ module Samagotchi
374
376
  "setting#{"s" unless clashing.size == 1} (--set #{clashing.first}=… sets chi's); rename the setting or the bundle"
375
377
  end
376
378
 
377
- # Why +bundle+'s plugin doesn't load, as chi's loader decides it
378
- # (Plugin::Loader.unloadable_reason), nil when it does or has none.
379
- def unloadable(dir, name, bundle)
380
- return nil unless bundle.plugin
379
+ # Why +standing+'s plugin doesn't load, as the plugin loader would refuse
380
+ # it (InstalledBundles' verdict), nil when it does or it has none. A bundle
381
+ # with no plugin is never refused here, even one too new (the plan's D2).
382
+ def unloadable(dir, standing)
383
+ plugin = standing.record.plugin
384
+ return nil unless plugin
381
385
 
382
- require_relative "plugin/loader"
383
- # +dir+, not Provenance#plugin_path's bundles_dir: a spec's records live elsewhere.
384
- Plugin::Loader.unloadable_reason(File.join(dir, name, "plugin", bundle.plugin.file.to_s), bundle)
386
+ refusal = standing.refusal ||
387
+ InstalledBundles::Dir.new(name: standing.name, dir: dir).file_refusal(:plugin, plugin.file, plugin.sha256)
388
+ refusal&.text
385
389
  end
386
390
 
387
391
  # Whether chi ships a bundle +name+ whose manifest lists session_settings.
@@ -206,6 +206,19 @@ module Samagotchi
206
206
  tally
207
207
  end
208
208
 
209
+ # Automatic cleanup logs only ids and outcome categories, never commands
210
+ # or error text, which can contain secrets.
211
+ def log_automatic!(policy)
212
+ policy.tally.outcomes.each do |outcome|
213
+ task = outcome.task
214
+ if outcome.outcome == :stopped
215
+ Log.info(:worker, "#{policy.by}_task_stopped", sid: task.session_id, task_id: task.id)
216
+ elsif Tally::NOT_STOPPED.include?(outcome.outcome)
217
+ Log.warn(:worker, "#{policy.by}_task_not_stopped", sid: task.session_id, task_id: task.id, why: outcome.outcome)
218
+ end
219
+ end
220
+ end
221
+
209
222
  # One task's stop, its outcome into +tally+.
210
223
  def stop_thread(task, by, tally)
211
224
  Thread.new do
@@ -286,7 +299,7 @@ module Samagotchi
286
299
  rescue Session::NotFound
287
300
  nil
288
301
  rescue ArgumentError, SystemCallError => e
289
- Log.warn(:worker, CHECK_FAILED, sid: sid, error: e.class.name, msg: e.message.to_s[0, 200])
302
+ Log.warn(:worker, CHECK_FAILED, sid: sid, error: e.class.name)
290
303
  nil
291
304
  end
292
305
 
@@ -304,7 +317,7 @@ module Samagotchi
304
317
  kind = liveness(record)
305
318
  kind && [kind, RunningTask.new(session_id: sid, id: id, command: record.command.to_s, root: root)]
306
319
  rescue JSON::ParserError, SystemCallError => e
307
- Log.warn(:worker, CHECK_FAILED, sid: sid, task_id: id, error: e.class.name, msg: e.message.to_s[0, 200])
320
+ Log.warn(:worker, CHECK_FAILED, sid: sid, task_id: id, error: e.class.name)
308
321
  [:unreadable, RunningTask.new(session_id: sid, id: id, root: root)]
309
322
  end
310
323
 
@@ -93,8 +93,10 @@ module Samagotchi
93
93
  next if owned?(name, state_dir) || starting?(name, state_dir)
94
94
 
95
95
  unless dry_run
96
+ policy = SessionManager::RunningTasks::Policy.stop(by: "retention")
96
97
  SessionManager.remove_files(name, path, dir, state_dir: state_dir, purge_runs: false,
97
- still: -> { orphan_dir?(path, dir) })
98
+ still: -> { orphan_dir?(path, dir) }, running_tasks: policy)
99
+ SessionManager::RunningTasks.log_automatic!(policy)
98
100
  end
99
101
  name
100
102
  rescue SessionManager::DeleteRefused, SystemCallError
@@ -145,7 +147,9 @@ module Samagotchi
145
147
  sweep_runs(sd)
146
148
  FileUtils.touch(marker)
147
149
  if result[:deleted].any?
148
- Log.info(:worker, "retention_pruned", echo: "[retention] pruned #{result[:deleted].size} sessions (kept #{result[:kept].size})", deleted: result[:deleted].size, kept: result[:kept].size)
150
+ stops = result[:tasks_stopped].to_i
151
+ suffix = stops.positive? ? "; stopped #{stops} background tasks" : ""
152
+ Log.info(:worker, "retention_pruned", echo: "[retention] pruned #{result[:deleted].size} sessions (kept #{result[:kept].size})#{suffix}", deleted: result[:deleted].size, kept: result[:kept].size)
149
153
  end
150
154
  result
151
155
  rescue StandardError => e
@@ -228,6 +232,7 @@ module Samagotchi
228
232
  kept = []
229
233
  skipped = []
230
234
  purge_ids = []
235
+ tasks_stopped = 0
231
236
  passed_over = 0
232
237
 
233
238
  all.each_with_index do |session, idx|
@@ -316,22 +321,26 @@ module Samagotchi
316
321
  elsif scheduled_targets.include?(session.id)
317
322
  kept << session.id
318
323
  else
324
+ policy = SessionManager::RunningTasks::Policy.stop(by: "retention")
319
325
  begin
320
326
  # Under the owner lock, the start read again (as a delete).
321
327
  SessionManager.remove_files(session.id, path, File.join(state_dir, session.id), state_dir: state_dir,
322
- purge_runs: false)
328
+ purge_runs: false, running_tasks: policy)
323
329
  deleted << session.id
324
330
  purge_ids << session.id
325
331
  rescue SessionManager::DeleteRefused
326
332
  kept << session.id
327
333
  rescue StandardError
328
334
  skipped << session.id
335
+ ensure
336
+ SessionManager::RunningTasks.log_automatic!(policy)
337
+ tasks_stopped += policy.tally.stopped.size
329
338
  end
330
339
  end
331
340
  end
332
341
 
333
342
  SessionManager.purge_removal_stores(purge_ids, state_dir: state_dir)
334
- { deleted: deleted, kept: kept, skipped: skipped }
343
+ { deleted: deleted, kept: kept, skipped: skipped, tasks_stopped: tasks_stopped }
335
344
  end
336
345
  end
337
346
  end
@@ -57,6 +57,9 @@ module Samagotchi
57
57
  continue: "checks belong to the previous link", corpus_dir: SIDE_CAR),
58
58
  Store.new(file: "objective_tracks.jsonl", owner: "ObjectivesTrack", removal: :session_files, cross_version: true,
59
59
  continue: "tracks belong to the previous link", corpus_dir: SIDE_CAR),
60
+ Store.new(file: "peer_exchanges.jsonl", owner: "PeerExchanges", removal: :session_files, cross_version: true,
61
+ continue: "exchanges stay with the previous link; the new pairing starts empty", corpus_dir: nil,
62
+ corpus_exception: "older readers do not read peer exchange metadata"),
60
63
  Store.new(file: "owner.lock", owner: "OwnerLock", removal: :session_files, cross_version: false,
61
64
  continue: "the owner lock is not session data", corpus_dir: SIDE_CAR),
62
65
  Store.new(file: "reminders.json", owner: "ReminderCarry", removal: :session_files, cross_version: true,
@@ -12,7 +12,7 @@ require_relative "tools/tool_surface"
12
12
  require_relative "muted_memories"
13
13
  require_relative "model_notes"
14
14
  require_relative "bundle_needs"
15
- require_relative "memory_bundle/index_size"
15
+ require_relative "memory_index/size"
16
16
  require_relative "thinking"
17
17
  require_relative "prompt_files"
18
18
  require_relative "plugin/prompt_sections"
@@ -142,7 +142,7 @@ module Samagotchi
142
142
  end
143
143
 
144
144
  # The memory indexes the prompt #build returned last holds, per scope
145
- # ({ system: { tokens:, lines: }, project: { … } }; MemoryBundle::IndexSize
145
+ # ({ system: { tokens:, lines: }, project: { … } }; MemoryIndex::Size
146
146
  # of the text after the session's mutes and the model notes' lines), nil
147
147
  # before a build. Measured when the prompt is built: a memory write
148
148
  # during the session changes neither the prompt nor this.
@@ -653,9 +653,9 @@ module Samagotchi
653
653
  # the prompt holds; nil when they can't be measured.
654
654
  def measure_indexes(indexes)
655
655
  Degrade.call(:prompt, "index_size_failed") do
656
- chars_per_token = MemoryBundle::IndexSize.chars_per_token
656
+ chars_per_token = MemoryIndex::Size.chars_per_token
657
657
  %i[system project].to_h do |scope|
658
- [scope, MemoryBundle::IndexSize.of_text(scope, indexes[scope], chars_per_token: chars_per_token).figures]
658
+ [scope, MemoryIndex::Size.of_text(scope, indexes[scope], chars_per_token: chars_per_token).figures]
659
659
  end
660
660
  end
661
661
  end
@@ -169,12 +169,16 @@ module Samagotchi
169
169
  # Stop the scratch session's worker and delete the session; its worker
170
170
  # may have deleted it first (it found no client left).
171
171
  def end_scratch(session_id, state_dir:, quiet: false, out: $stdout, err: $stderr)
172
- SessionManager.delete_session(session_id, state_dir: state_dir, stop: true, wait: LocalLink::DELETE_WAIT)
172
+ policy = SessionManager::RunningTasks::Policy.stop(by: "scratch")
173
+ SessionManager.delete_session(session_id, state_dir: state_dir, stop: true, wait: LocalLink::DELETE_WAIT,
174
+ running_tasks: policy)
173
175
  out.puts(SCRATCH_DELETED) unless quiet
174
176
  rescue ArgumentError
175
177
  out.puts(SCRATCH_DELETED) unless quiet
176
178
  rescue SessionManager::DeleteRefused, SystemCallError => e
177
179
  err.puts("Scratch session #{session_id} was not deleted (#{e.message}): chi sessions delete #{session_id}")
180
+ ensure
181
+ SessionManager::RunningTasks.log_automatic!(policy) if policy
178
182
  end
179
183
 
180
184
  # This TUI's client id: tui:PID, or the connector's own (a remote one
@@ -7,7 +7,7 @@ require_relative "../turn_note"
7
7
  require_relative "../steer"
8
8
  require_relative "../participant_name"
9
9
  require_relative "../steer_cut"
10
- require_relative "../memory_bundle/index_size"
10
+ require_relative "../memory_index/size"
11
11
  require_relative "../prompt_note"
12
12
  require_relative "../prompt_files"
13
13
  require_relative "../session_config"
@@ -492,7 +492,7 @@ module Samagotchi
492
492
  end
493
493
  return "" if scopes.empty?
494
494
 
495
- count = ->(tokens) { MemoryBundle::IndexSize.count_text(tokens) }
495
+ count = ->(tokens) { MemoryIndex::Size.count_text(tokens) }
496
496
  "~#{count.call(scopes.sum(&:last))} tokens in this session's prompt " \
497
497
  "(#{scopes.map { |scope, tokens| "#{scope} #{count.call(tokens)}" }.join(", ")})"
498
498
  end
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "time"
4
- require_relative "../memory_bundle/index_size"
4
+ require_relative "../memory_index/size"
5
5
 
6
6
  module Samagotchi
7
7
  class TerminalUI
@@ -26,7 +26,7 @@ module Samagotchi
26
26
 
27
27
  # "6.0k in / 40 out": the row's prompt and completion sums.
28
28
  def model_segment_tokens_text(row)
29
- count = ->(key) { MemoryBundle::IndexSize.count_text(segment_value(row, key).to_i) }
29
+ count = ->(key) { MemoryIndex::Size.count_text(segment_value(row, key).to_i) }
30
30
  "#{count.call(:prompt_sum)} in / #{count.call(:completion_sum)} out"
31
31
  end
32
32
 
@@ -66,7 +66,7 @@ module Samagotchi
66
66
  return "" unless tokens.positive?
67
67
 
68
68
  cost = cost_text(segment_value(row, :prefill_cost_estimate))
69
- "switch prefill #{MemoryBundle::IndexSize.count_text(tokens)} uncached prompt#{" ≈#{cost}" unless cost.empty?}"
69
+ "switch prefill #{MemoryIndex::Size.count_text(tokens)} uncached prompt#{" ≈#{cost}" unless cost.empty?}"
70
70
  end
71
71
 
72
72
  private
@@ -87,8 +87,8 @@ module Samagotchi
87
87
  [model_segment_tokens_text(row), speed, model_segment_cost_text(row)].reject(&:empty?).join(" ")]
88
88
  end
89
89
  total = ["total", "", turns_text(turns),
90
- "#{MemoryBundle::IndexSize.count_text(tokens[:prompt_sum].to_i)} in / " \
91
- "#{MemoryBundle::IndexSize.count_text(tokens[:completion_sum].to_i)} out " \
90
+ "#{MemoryIndex::Size.count_text(tokens[:prompt_sum].to_i)} in / " \
91
+ "#{MemoryIndex::Size.count_text(tokens[:completion_sum].to_i)} out " \
92
92
  "(all requests, #{token_src_label}#{token_breakdown_text(tokens)})"]
93
93
  widths = (0..2).map { |i| (cells + [total]).map { |cell| cell[i].length }.max }
94
94
  row_text = ->(cell) { cell.each_with_index.map { |text, i| i < 3 ? text.ljust(widths[i]) : text }.join(" ") }
@@ -378,9 +378,12 @@ module Samagotchi
378
378
  def delete_scratch(session)
379
379
  @owner_lock&.release
380
380
  @owner_lock = nil
381
- SessionManager.delete_session(session.id)
381
+ policy = SessionManager::RunningTasks::Policy.stop(by: "scratch")
382
+ SessionManager.delete_session(session.id, running_tasks: policy)
382
383
  rescue StandardError => e # degrade-ok: the line says how to delete it by hand
383
384
  warn "Scratch session #{session.id} was not deleted (#{e.message}): chi sessions delete #{session.id}"
385
+ ensure
386
+ SessionManager::RunningTasks.log_automatic!(policy) if policy
384
387
  end
385
388
 
386
389
  # The session's cards.json and pending_card.json, kept as a worker's
@@ -12,7 +12,7 @@ require_relative "../peer_sources"
12
12
  require_relative "../toolkit"
13
13
  require_relative "../guardrails/gate"
14
14
  require_relative "../turn_context"
15
- require_relative "../memory_bundle/manifest"
15
+ require_relative "../bundle_manifest"
16
16
  require_relative "../memory_bundle/source"
17
17
  require_relative "../session_setup"
18
18
  require_relative "sandbox"
@@ -109,7 +109,7 @@ module Samagotchi
109
109
 
110
110
  # @return [String] the bundle's name (manifest.yml)
111
111
  attr_reader :name
112
- # @return [MemoryBundle::Manifest]
112
+ # @return [BundleManifest]
113
113
  attr_reader :manifest
114
114
  # @return [Plugin::Registries]
115
115
  attr_reader :registries
@@ -143,7 +143,7 @@ module Samagotchi
143
143
 
144
144
  def initialize(dir, settings: {}, host: {}, session_settings: {}, sandbox: nil)
145
145
  @dir = File.expand_path(dir)
146
- @manifest = MemoryBundle::Manifest.read(dir: @dir)
146
+ @manifest = BundleManifest.read(dir: @dir)
147
147
  @name = @manifest.name
148
148
  @settings = stringify(settings || {})
149
149
  @session_moves = 1
@@ -3,7 +3,7 @@
3
3
  require_relative "../extension_load"
4
4
  require_relative "../guardrails/load_failures"
5
5
  require_relative "../memory_bundle/installer"
6
- require_relative "../memory_bundle/provenance"
6
+ require_relative "../installed_bundles"
7
7
  require_relative "../hooks"
8
8
  require_relative "bundle"
9
9
  require_relative "sandbox"
@@ -52,7 +52,7 @@ module Samagotchi
52
52
 
53
53
  sandbox ||= Sandbox.current
54
54
  dir = File.expand_path(dir)
55
- name = MemoryBundle::Manifest.read(dir: dir).name
55
+ name = BundleManifest.read(dir: dir).name
56
56
  sandbox.config("bundles" => { name => settings })
57
57
  installer = MemoryBundle::Installer.new(source: dir, name: name, scope: "system", force: force)
58
58
  installer.run
@@ -74,7 +74,7 @@ module Samagotchi
74
74
  init: ->(_bundle, label, _plugin_label, **, &_block) { inits << { label: label } }
75
75
  )
76
76
  extension.load_plugins(registries, failures: plugin_failures)
77
- installed = File.join(MemoryBundle::Provenance.bundles_dir, name)
77
+ installed = File.join(InstalledBundles.dir, name)
78
78
  Result.new(name: name, warnings: warnings.dup,
79
79
  plugin_failures: own(plugin_failures, name, installed, dir),
80
80
  hook_failures: own(hook_failures, name, installed, dir),
@@ -115,12 +115,12 @@ module Samagotchi
115
115
  # @raise [ArgumentError] what memory_write refused
116
116
  def write_memory(name, content, scope: "system", description: nil, source: nil, cwd: nil)
117
117
  require_relative "../tools/memory"
118
- require_relative "../memory_bundle/index_updater"
118
+ require_relative "../memory_index/updater"
119
119
  in_dir(cwd) do
120
120
  result = Tools::MemoryWrite.call(content, path: name, scope: scope, description: description)
121
121
  raise ArgumentError, result if result.start_with?("Error")
122
122
 
123
- MemoryBundle::IndexUpdater.update_index(scope, name, content.bytesize, description, source: source) if source
123
+ MemoryIndex::Updater.update_index(scope, name, content.bytesize, description, source: source) if source
124
124
  end
125
125
  File.join(memory_dir(scope, cwd: cwd || Dir.pwd), "#{name}.md")
126
126
  end
@@ -17,9 +17,9 @@ require_relative "tools/tool_path"
17
17
  require_relative "tools/tool_surface"
18
18
  require_relative "tools/memory"
19
19
  require_relative "config"
20
- require_relative "memory_bundle/index_sync"
21
- require_relative "memory_bundle/index_size"
22
- require_relative "memory_bundle/entry_size"
20
+ require_relative "memory_index/sync"
21
+ require_relative "memory_index/size"
22
+ require_relative "memory_index/entry_size"
23
23
 
24
24
  module Samagotchi
25
25
  # One call's result from ToolRunner#run, read by ToolResponse (the loops'
@@ -141,7 +141,7 @@ module Samagotchi
141
141
  end
142
142
  before = verdict.deny? ? NOT_AN_EDIT : file_before(call)
143
143
  index_before = before.equal?(NOT_AN_EDIT) ? nil : memory_index_size(call)
144
- entry_before = before.equal?(NOT_AN_EDIT) ? nil : MemoryBundle::EntrySize.measure_file(Tools::ToolPath.normalize(call[:path]))
144
+ entry_before = before.equal?(NOT_AN_EDIT) ? nil : MemoryIndex::EntrySize.measure_file(Tools::ToolPath.normalize(call[:path]))
145
145
  elapsed_ms = nil
146
146
  if verdict.deny?
147
147
  result = denied(call, verdict, context)
@@ -302,7 +302,7 @@ module Samagotchi
302
302
  window = @window ? @window.call(settings) : (@kernel.tool_output_window(settings) if @kernel.respond_to?(:tool_output_window))
303
303
  return nil unless window&.tokens&.positive? && window.source != :default
304
304
 
305
- (window.tokens * MemoryBundle::IndexSize.chars_per_token / 2).floor
305
+ (window.tokens * MemoryIndex::Size.chars_per_token / 2).floor
306
306
  end
307
307
  end
308
308
 
@@ -353,20 +353,20 @@ module Samagotchi
353
353
  end
354
354
 
355
355
  # A write/edit that changed a memory's file refreshes its index line,
356
- # as memory_write does (MemoryBundle::IndexSync; a no-op elsewhere).
356
+ # as memory_write does (MemoryIndex::Sync; a no-op elsewhere).
357
357
  def refresh_memory_index(call)
358
- MemoryBundle::IndexSync.refresh(Tools::ToolPath.normalize(call[:path]))
358
+ MemoryIndex::Sync.refresh(Tools::ToolPath.normalize(call[:path]))
359
359
  end
360
360
 
361
361
  # The index size of the memory scope an edit/write's path is in
362
- # (MemoryBundle::IndexSize), nil for any other path, or when
362
+ # (MemoryIndex::Size), nil for any other path, or when
363
363
  # memory.index_warn_tokens is off: other writes cost nothing.
364
364
  def memory_index_size(call)
365
365
  Degrade.call(:memory, "index_size_unreadable") do
366
- scope = MemoryBundle::IndexSync.memory_scope(Tools::ToolPath.normalize(call[:path]))
367
- return nil unless scope && MemoryBundle::IndexSize.warn_limit.positive?
366
+ scope = MemoryIndex::Sync.memory_scope(Tools::ToolPath.normalize(call[:path]))
367
+ return nil unless scope && MemoryIndex::Size.warn_limit.positive?
368
368
 
369
- MemoryBundle::IndexSize.measure(scope)
369
+ MemoryIndex::Size.measure(scope)
370
370
  end
371
371
  end
372
372
 
@@ -375,17 +375,17 @@ module Samagotchi
375
375
  def with_index_note(result, before, call)
376
376
  return result unless before
377
377
 
378
- note = MemoryBundle::IndexSize.crossing_note(before, memory_index_size(call))
378
+ note = MemoryIndex::Size.crossing_note(before, memory_index_size(call))
379
379
  note ? result.merge(output: "#{result[:output]}\n\n#{note}") : result
380
380
  end
381
381
 
382
382
  # +result+ with the note a write/edit gets when it took the memory over
383
- # memory.entry_warn_tokens (MemoryBundle::EntrySize). It is apart from
383
+ # memory.entry_warn_tokens (MemoryIndex::EntrySize). It is apart from
384
384
  # the index's: that one is off with index_warn_tokens at 0, this one isn't.
385
385
  def with_entry_note(result, before, call)
386
386
  return result unless before
387
387
 
388
- note = MemoryBundle::EntrySize.crossing_note(before, MemoryBundle::EntrySize.measure_file(Tools::ToolPath.normalize(call[:path])))
388
+ note = MemoryIndex::EntrySize.crossing_note(before, MemoryIndex::EntrySize.measure_file(Tools::ToolPath.normalize(call[:path])))
389
389
  note ? result.merge(output: "#{result[:output]}\n\n#{note}") : result
390
390
  end
391
391
 
@@ -316,7 +316,7 @@ module Samagotchi
316
316
 
317
317
  dir = MemoryRead.memories_dir(resolved_scope)
318
318
  FileUtils.mkdir_p(dir)
319
- require_relative "../memory_bundle/index_updater"
319
+ require_relative "../memory_index/updater"
320
320
 
321
321
  if current_model_only && model_key
322
322
  # Overlays are only read through their base entry (same scope), so an
@@ -340,7 +340,7 @@ module Samagotchi
340
340
  # And the memory's own size, for memory.entry_warn_tokens.
341
341
  entry_before = entry_size(resolved_scope, entry_name, file_path) unless entry_name == MEMORY_INDEX
342
342
  if entry_name == MEMORY_INDEX
343
- Samagotchi::MemoryBundle::IndexUpdater.locked_write(dir) { body }
343
+ Samagotchi::MemoryIndex::Updater.locked_write(dir) { body }
344
344
  else
345
345
  Samagotchi::AtomicFile.write(file_path, body)
346
346
  end
@@ -417,11 +417,11 @@ module Samagotchi
417
417
  "#{index_crossing_note(before, resolved_scope)}"
418
418
  end
419
419
 
420
- # +scope+'s index size (MemoryBundle::IndexSize), or nil when
420
+ # +scope+'s index size (MemoryIndex::Size), or nil when
421
421
  # memory.index_warn_tokens is off or the index can't be read.
422
422
  def self.index_size(scope)
423
- require_relative "../memory_bundle/index_size"
424
- size = Samagotchi::MemoryBundle::IndexSize
423
+ require_relative "../memory_index/size"
424
+ size = Samagotchi::MemoryIndex::Size
425
425
  size.measure(scope) if size.warn_limit.positive?
426
426
  end
427
427
 
@@ -429,8 +429,8 @@ module Samagotchi
429
429
  # when memory.entry_warn_tokens is off, for a model note (its own
430
430
  # warning) or when it can't be measured.
431
431
  def self.entry_size(scope, name, file_path)
432
- require_relative "../memory_bundle/entry_size"
433
- size = Samagotchi::MemoryBundle::EntrySize
432
+ require_relative "../memory_index/entry_size"
433
+ size = Samagotchi::MemoryIndex::EntrySize
434
434
  return nil unless size.warn_limit.positive? && size.counted?(name)
435
435
 
436
436
  size.of_text(name, scope, File.file?(file_path) ? File.read(file_path, encoding: "UTF-8") : "")
@@ -443,7 +443,7 @@ module Samagotchi
443
443
  # (+before+: its size before the write), else "". +body+ is what was
444
444
  # written.
445
445
  def self.entry_crossing_note(before, scope, name, body)
446
- size = Samagotchi::MemoryBundle::EntrySize
446
+ size = Samagotchi::MemoryIndex::EntrySize
447
447
  note = size.crossing_note(before, size.of_text(name, scope, body))
448
448
  note ? "\n\n#{note}" : ""
449
449
  rescue StandardError
@@ -456,7 +456,7 @@ module Samagotchi
456
456
  def self.index_crossing_note(before, scope)
457
457
  return "" unless before
458
458
 
459
- note = Samagotchi::MemoryBundle::IndexSize.crossing_note(before, index_size(scope))
459
+ note = Samagotchi::MemoryIndex::Size.crossing_note(before, index_size(scope))
460
460
  note ? "\n\n#{note}" : ""
461
461
  end
462
462
 
@@ -469,8 +469,8 @@ module Samagotchi
469
469
  def self.remove(entry_name, scope)
470
470
  return "Error: the index can't be removed; it is kept up to date for you" if entry_name == MEMORY_INDEX
471
471
 
472
- require_relative "../memory_bundle/index_updater"
473
- require_relative "../memory_bundle/provenance"
472
+ require_relative "../memory_index/updater"
473
+ require_relative "../installed_bundles"
474
474
  require_relative "../memory_bundle/trash"
475
475
  resolved_scope = MemoryRead.normalize_scope(scope)
476
476
  dir = MemoryRead.memories_dir(resolved_scope)
@@ -479,20 +479,20 @@ module Samagotchi
479
479
  line = index_text[managed_pattern(entry_name)]
480
480
  file = File.join(dir, "#{entry_name}.md")
481
481
 
482
- owner = Samagotchi::MemoryBundle::Provenance.claimants("#{entry_name}.md", scope: resolved_scope).first ||
483
- (line && Samagotchi::MemoryBundle::IndexUpdater.extract_source(line))
482
+ owner = Samagotchi::InstalledBundles.claimants("#{entry_name}.md", scope: resolved_scope).first ||
483
+ (line && Samagotchi::MemoryIndex::Updater.extract_source(line))
484
484
  return "Error: memory '#{entry_name}' came from bundle #{owner}; `chi bundle uninstall #{owner}` removes it" if owner
485
485
 
486
486
  unless File.file?(file)
487
487
  return "Error: no memory '#{entry_name}' in #{resolved_scope} scope" unless line
488
488
 
489
- Samagotchi::MemoryBundle::IndexUpdater.remove_index(resolved_scope, entry_name)
489
+ Samagotchi::MemoryIndex::Updater.remove_index(resolved_scope, entry_name)
490
490
  return "Memory '#{entry_name}' had no file in #{resolved_scope} scope; its dangling index line was dropped."
491
491
  end
492
492
 
493
493
  trash = Samagotchi::MemoryBundle::Trash.new("memory-#{entry_name}")
494
494
  ([file] + overlay_files(dir, entry_name, index_text)).each { |path| trash.move(path) }
495
- Samagotchi::MemoryBundle::IndexUpdater.remove_index(resolved_scope, entry_name)
495
+ Samagotchi::MemoryIndex::Updater.remove_index(resolved_scope, entry_name)
496
496
  "Memory '#{entry_name}' removed from #{resolved_scope} scope: moved to #{trash.dir} " \
497
497
  "(`chi bundle trash` lists and empties it)#{"; index line dropped" if line}."
498
498
  end
@@ -513,30 +513,30 @@ module Samagotchi
513
513
 
514
514
  # Adds or refreshes a single managed line for `entry_name` in the given
515
515
  # scope's index.md, preserving every other byte byte-for-byte.
516
- # Delegates to IndexUpdater to maintain a single source of truth.
516
+ # Delegates to MemoryIndex::Updater to maintain a single source of truth.
517
517
  # Returns true when an entry was written (i.e. the index was managed).
518
518
  def self.manage_index(scope, entry_name, byte_count, description = nil)
519
- require_relative "../memory_bundle/index_updater"
520
- Samagotchi::MemoryBundle::IndexUpdater.update_index(scope, entry_name, byte_count, description)
519
+ require_relative "../memory_index/updater"
520
+ Samagotchi::MemoryIndex::Updater.update_index(scope, entry_name, byte_count, description)
521
521
  end
522
522
 
523
523
  def self.managed_line(name, scope, byte_count, description)
524
- Samagotchi::MemoryBundle::IndexUpdater.managed_line(name, scope, byte_count, description)
524
+ Samagotchi::MemoryIndex::Updater.managed_line(name, scope, byte_count, description)
525
525
  end
526
526
 
527
527
  # Extracts the human description from an existing managed line so that a
528
528
  # legacy `- **name**: desc` or a new-format line with a description keeps
529
529
  # its description when the entry is updated without a new one.
530
530
  def self.extract_description(line)
531
- Samagotchi::MemoryBundle::IndexUpdater.extract_description(line)
531
+ Samagotchi::MemoryIndex::Updater.extract_description(line)
532
532
  end
533
533
 
534
534
  def self.auto_index_header
535
- Samagotchi::MemoryBundle::IndexUpdater.auto_index_header
535
+ Samagotchi::MemoryIndex::Updater.auto_index_header
536
536
  end
537
537
 
538
538
  def self.date_str
539
- Samagotchi::MemoryBundle::IndexUpdater.date_str
539
+ Samagotchi::MemoryIndex::Updater.date_str
540
540
  end
541
541
 
542
542
  def self.index_path_for(scope)
@@ -9,7 +9,7 @@ require_relative "desktop"
9
9
  require_relative "gem_update"
10
10
  require_relative "installed_gem"
11
11
  require_relative "live_versions"
12
- require_relative "memory_bundle/file_standing"
12
+ require_relative "installed_bundles/file_standing"
13
13
  require_relative "memory_bundle/manifest_standing"
14
14
  require_relative "memory_bundle/shipped_update"
15
15
  require_relative "memory_bundle/system_bundle"
@@ -384,8 +384,8 @@ module Samagotchi
384
384
  row.kept = behind.map(&:file)
385
385
  @unsure_behind ||= behind.any?(&:unsure)
386
386
  row.trash_copy = trash_copy
387
- notes = MemoryBundle::FileStanding.clauses(bundle, version, merged: merged, behind: behind, earlier: earlier, dry_run: @dry_run)
388
- trash_copy || earlier.any?(&:trash) ? [*notes, MemoryBundle::FileStanding::IN_THE_TRASH] : notes
387
+ notes = InstalledBundles::FileStanding.clauses(bundle, version, merged: merged, behind: behind, earlier: earlier, dry_run: @dry_run)
388
+ trash_copy || earlier.any?(&:trash) ? [*notes, InstalledBundles::FileStanding::IN_THE_TRASH] : notes
389
389
  end
390
390
 
391
391
  # Whether a row printed now ends the notes for what it printed, this run's
@@ -494,7 +494,7 @@ module Samagotchi
494
494
  return "some parts failed (see above)" if rows.any?(&:failed)
495
495
 
496
496
  behind = rows.sum { |row| Array(row.kept).size }
497
- return "#{MemoryBundle::FileStanding.stay_behind(behind, unsure: @unsure_behind)} (see above)" if behind.positive?
497
+ return "#{InstalledBundles::FileStanding.stay_behind(behind, unsure: @unsure_behind)} (see above)" if behind.positive?
498
498
 
499
499
  # A finding on a bundle chi didn't ship repeats until its bundle changes; it isn't a change.
500
500
  changes = rows.reject(&:level)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.178.0"
4
+ VERSION = "0.179.0"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end