samagotchi 0.178.0 → 0.180.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -1
- data/docs/architecture.md +4 -2
- data/docs/bundles.md +48 -8
- data/docs/cli.md +3 -1
- data/docs/configuration.md +1 -1
- data/docs/context.md +32 -1
- data/docs/guardrails.md +2 -1
- data/docs/internals/attached-context.md +26 -4
- data/docs/internals/background-tasks.md +5 -1
- data/docs/internals/block-renderers.md +4 -3
- data/docs/internals/bridge.md +11 -2
- data/docs/internals/bundles.md +65 -34
- data/docs/internals/engine.md +1 -1
- data/docs/internals/web-frontend.md +81 -3
- data/docs/plugins.md +20 -5
- data/docs/releasing.md +3 -3
- data/docs/scheduling.md +4 -0
- data/docs/sessions.md +8 -2
- data/docs/testing.md +8 -1
- data/lib/samagotchi/block_renderers.rb +33 -16
- data/lib/samagotchi/bootstrap/bundles.rb +2 -2
- data/lib/samagotchi/bootstrap_command.rb +1 -1
- data/lib/samagotchi/bridge/card_wait.rb +34 -0
- data/lib/samagotchi/bridge/turn_accumulator.rb +22 -4
- data/lib/samagotchi/bundle_command/check.rb +10 -10
- data/lib/samagotchi/bundle_command/installed_check.rb +2 -2
- data/lib/samagotchi/bundle_command/try/sandbox.rb +3 -3
- data/lib/samagotchi/bundle_command.rb +22 -18
- data/lib/samagotchi/bundle_manifest.rb +579 -0
- data/lib/samagotchi/bundle_needs.rb +9 -8
- data/lib/samagotchi/bundle_settings_check.rb +13 -9
- data/lib/samagotchi/bundles/claude-peer/manifest.yml +5 -3
- data/lib/samagotchi/bundles/claude-peer/plugin.rb +4 -4
- data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +11 -6
- data/lib/samagotchi/bundles/codex-peer/manifest.yml +5 -3
- data/lib/samagotchi/bundles/codex-peer/plugin.rb +4 -3
- data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +11 -6
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/bundles/system/memory_guide.md +1 -1
- data/lib/samagotchi/config.rb +2 -2
- data/lib/samagotchi/context_absorber.rb +36 -6
- data/lib/samagotchi/context_note.rb +8 -5
- data/lib/samagotchi/context_providers.rb +30 -29
- data/lib/samagotchi/context_sources.rb +42 -12
- data/lib/samagotchi/engine/messages.rb +1 -1
- data/lib/samagotchi/events.rb +6 -5
- data/lib/samagotchi/guardrail_wiring.rb +20 -21
- data/lib/samagotchi/guardrails/approval.rb +3 -2
- data/lib/samagotchi/guardrails/outside.rb +3 -3
- data/lib/samagotchi/{memory_bundle → installed_bundles}/bundle_hook.rb +1 -1
- data/lib/samagotchi/installed_bundles/dir.rb +91 -0
- data/lib/samagotchi/{memory_bundle → installed_bundles}/file_digest.rb +1 -1
- data/lib/samagotchi/{memory_bundle → installed_bundles}/file_standing.rb +2 -2
- data/lib/samagotchi/{memory_bundle/installed_manifest.rb → installed_bundles/manifest.rb} +21 -21
- data/lib/samagotchi/{memory_bundle → installed_bundles}/plugin_ref.rb +2 -2
- data/lib/samagotchi/{memory_bundle/installed_bundle.rb → installed_bundles/record.rb} +17 -17
- data/lib/samagotchi/installed_bundles/sha.rb +41 -0
- data/lib/samagotchi/installed_bundles/standing.rb +65 -0
- data/lib/samagotchi/installed_bundles.rb +105 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +7 -7
- data/lib/samagotchi/memory_bundle/builder.rb +13 -13
- data/lib/samagotchi/memory_bundle/file_state.rb +9 -9
- data/lib/samagotchi/memory_bundle/installed_copy.rb +6 -6
- data/lib/samagotchi/memory_bundle/installer.rb +33 -33
- data/lib/samagotchi/memory_bundle/listing.rb +5 -5
- data/lib/samagotchi/memory_bundle/manifest_standing.rb +7 -7
- data/lib/samagotchi/memory_bundle/merger.rb +15 -15
- data/lib/samagotchi/memory_bundle/profile.rb +8 -8
- data/lib/samagotchi/memory_bundle/provenance.rb +59 -192
- data/lib/samagotchi/memory_bundle/remerge.rb +16 -16
- data/lib/samagotchi/memory_bundle/shas.rb +2 -2
- data/lib/samagotchi/memory_bundle/shipped_history.rb +1 -1
- data/lib/samagotchi/memory_bundle/shipped_update.rb +17 -17
- data/lib/samagotchi/memory_bundle/status.rb +19 -16
- data/lib/samagotchi/memory_bundle/system_bundle.rb +12 -12
- data/lib/samagotchi/memory_bundle/trash.rb +2 -2
- data/lib/samagotchi/memory_bundle/uninstaller.rb +8 -8
- data/lib/samagotchi/memory_bundle.rb +2 -2
- data/lib/samagotchi/memory_command.rb +4 -4
- data/lib/samagotchi/{memory_bundle → memory_index}/entry_size.rb +8 -8
- data/lib/samagotchi/{memory_bundle/index_size.rb → memory_index/size.rb} +6 -6
- data/lib/samagotchi/{memory_bundle/index_sync.rb → memory_index/sync.rb} +5 -5
- data/lib/samagotchi/{memory_bundle/index_updater.rb → memory_index/updater.rb} +2 -2
- data/lib/samagotchi/memory_usage.rb +5 -5
- data/lib/samagotchi/muted_memories.rb +1 -1
- data/lib/samagotchi/peer_exchanges.rb +76 -0
- data/lib/samagotchi/plugin/context.rb +4 -0
- data/lib/samagotchi/plugin/loader.rb +35 -33
- data/lib/samagotchi/plugin/peer.rb +42 -11
- data/lib/samagotchi/project_subfolders.rb +9 -9
- data/lib/samagotchi/reply_wait/target.rb +11 -4
- data/lib/samagotchi/self_report.rb +14 -14
- data/lib/samagotchi/session_access/local.rb +16 -1
- data/lib/samagotchi/session_access/remote.rb +2 -1
- data/lib/samagotchi/session_config.rb +29 -19
- data/lib/samagotchi/session_manager/running_tasks.rb +15 -2
- data/lib/samagotchi/session_retention.rb +13 -4
- data/lib/samagotchi/session_stores.rb +3 -0
- data/lib/samagotchi/system_prompt.rb +4 -4
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +5 -1
- data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -5
- data/lib/samagotchi/terminal_ui/join_replay.rb +1 -1
- data/lib/samagotchi/terminal_ui/model_segments_text.rb +5 -5
- data/lib/samagotchi/terminal_ui.rb +4 -1
- data/lib/samagotchi/testing/bundle.rb +3 -3
- data/lib/samagotchi/testing/install.rb +3 -3
- data/lib/samagotchi/testing/sandbox.rb +2 -2
- data/lib/samagotchi/tool_runner.rb +14 -14
- data/lib/samagotchi/tools/memory.rb +22 -22
- data/lib/samagotchi/tools/task_runtime.rb +12 -3
- data/lib/samagotchi/update_command.rb +4 -4
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +14 -1
- data/lib/samagotchi/web/message_display.rb +6 -0
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/proxy_gate.rb +1 -0
- data/lib/samagotchi/web/public/app.js +13 -1
- data/lib/samagotchi/web/public/card_kinds.js +8 -3
- data/lib/samagotchi/web/public/cards_place.js +15 -1
- data/lib/samagotchi/web/public/context_chips.js +184 -49
- data/lib/samagotchi/web/public/ctx.js +1 -1
- data/lib/samagotchi/web/public/history_shape.js +8 -3
- data/lib/samagotchi/web/public/index.html +66 -0
- data/lib/samagotchi/web/public/peer_chip.js +171 -0
- data/lib/samagotchi/web/public/peer_data.js +119 -0
- data/lib/samagotchi/web/public/peer_exchange.js +83 -0
- data/lib/samagotchi/web/public/peer_links.js +161 -0
- data/lib/samagotchi/web/public/peer_note.js +49 -0
- data/lib/samagotchi/web/public/session_view.js +6 -1
- data/lib/samagotchi/web/public/stream_handlers.js +17 -4
- data/lib/samagotchi/web/public/turn_block.js +46 -8
- data/lib/samagotchi/web/public/turn_html.js +11 -2
- data/lib/samagotchi/web/public/turn_settle.js +117 -0
- data/lib/samagotchi/web/push/notifier.rb +2 -2
- data/lib/samagotchi/web/session_access_routes.rb +21 -1
- data/lib/samagotchi/web/session_hub.rb +20 -4
- data/lib/samagotchi/worker_boot.rb +4 -1
- metadata +24 -12
- 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,
|
|
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 (
|
|
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 (
|
|
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 = {
|
|
@@ -213,6 +213,9 @@ module Samagotchi
|
|
|
213
213
|
def no_installed_bundle(bundle) = "no installed bundle #{bundle}"
|
|
214
214
|
def takes_keys(bundle, keys) = keys.empty? ? "#{bundle} takes no settings" : "#{bundle} takes #{keys.join(", ")}"
|
|
215
215
|
def wrong_type(bundle, key, what, value) = "#{bundle} takes #{what} for #{key}, not #{value.inspect}"
|
|
216
|
+
# A bundle the verdict refuses as a whole (InstalledBundles::Standing:
|
|
217
|
+
# requires_chi), +reason+ its refusal's text.
|
|
218
|
+
def unusable(bundle, reason) = "#{bundle} is installed but this chi can't use it: #{reason}"
|
|
216
219
|
|
|
217
220
|
# --set's values parsed and checked (.parse, .check!) against the
|
|
218
221
|
# installed bundles (.availability).
|
|
@@ -288,23 +291,30 @@ module Samagotchi
|
|
|
288
291
|
def offered(dir: nil) = availability(dir: dir).offered
|
|
289
292
|
|
|
290
293
|
# The installed bundles a session may set settings of: a record that
|
|
291
|
-
# doesn't read is left out, and so is one
|
|
292
|
-
#
|
|
293
|
-
# the file, its sha256),
|
|
294
|
+
# doesn't read is left out, and so is one this chi is too old for
|
|
295
|
+
# (InstalledBundles::Standing: requires_chi, plugin or not) or whose
|
|
296
|
+
# plugin doesn't load (Dir#file_refusal(:plugin): the file, its sha256),
|
|
297
|
+
# with a note saying why. A record with no
|
|
294
298
|
# session_settings whose shipped bundle lists some (installed before
|
|
295
299
|
# them) gets a note on refreshing it.
|
|
296
300
|
# @return [Availability]
|
|
297
301
|
def availability(dir: nil, shipped_dir: SHIPPED_DIR)
|
|
298
|
-
require_relative "
|
|
299
|
-
require_relative "
|
|
300
|
-
dir ||=
|
|
302
|
+
require_relative "installed_bundles"
|
|
303
|
+
require_relative "bundle_manifest"
|
|
304
|
+
dir ||= InstalledBundles.dir
|
|
301
305
|
offered = {}
|
|
302
306
|
notes = {}
|
|
303
307
|
types = {}
|
|
304
|
-
|
|
305
|
-
next if
|
|
308
|
+
InstalledBundles.standings(dir: dir) do |standing|
|
|
309
|
+
next if standing.unreadable?
|
|
306
310
|
|
|
307
|
-
|
|
311
|
+
name = standing.name
|
|
312
|
+
bundle = standing.record
|
|
313
|
+
if standing.too_new?
|
|
314
|
+
notes[name] = unusable(name, standing.refusal.text)
|
|
315
|
+
next
|
|
316
|
+
end
|
|
317
|
+
if (reason = unloadable(dir, standing))
|
|
308
318
|
notes[name] = "#{name} is installed but its plugin doesn't load: #{reason}"
|
|
309
319
|
next
|
|
310
320
|
end
|
|
@@ -374,14 +384,14 @@ module Samagotchi
|
|
|
374
384
|
"setting#{"s" unless clashing.size == 1} (--set #{clashing.first}=… sets chi's); rename the setting or the bundle"
|
|
375
385
|
end
|
|
376
386
|
|
|
377
|
-
# Why +
|
|
378
|
-
# (
|
|
379
|
-
|
|
380
|
-
|
|
387
|
+
# Why a usable +standing+'s plugin doesn't load, as the plugin loader
|
|
388
|
+
# would refuse it (Dir#file_refusal(:plugin)), nil when it does or it has
|
|
389
|
+
# none.
|
|
390
|
+
def unloadable(dir, standing)
|
|
391
|
+
plugin = standing.record.plugin
|
|
392
|
+
return nil unless plugin
|
|
381
393
|
|
|
382
|
-
|
|
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)
|
|
394
|
+
InstalledBundles::Dir.new(name: standing.name, dir: dir).file_refusal(:plugin, plugin.file, plugin.sha256)&.text
|
|
385
395
|
end
|
|
386
396
|
|
|
387
397
|
# 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
|
|
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
|
|
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
|
-
|
|
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 "
|
|
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: { … } };
|
|
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 =
|
|
656
|
+
chars_per_token = MemoryIndex::Size.chars_per_token
|
|
657
657
|
%i[system project].to_h do |scope|
|
|
658
|
-
[scope,
|
|
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.
|
|
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
|
|
@@ -324,7 +324,7 @@ module Samagotchi
|
|
|
324
324
|
# line in the scrollback; the notes slot is for question choices.
|
|
325
325
|
when :context_added
|
|
326
326
|
mark_activity
|
|
327
|
-
@screen.commit(context_note_line(event[:label], event[:text]))
|
|
327
|
+
@screen.commit(context_note_line(event[:label], event[:text], event[:peer]))
|
|
328
328
|
when :continue_offered then offer_continue(event)
|
|
329
329
|
when :continue_resolved then continue_resolved(event)
|
|
330
330
|
# The note comes with the kernel's :pending_input_merged (EventRenderer),
|
|
@@ -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 "../
|
|
10
|
+
require_relative "../memory_index/size"
|
|
11
11
|
require_relative "../prompt_note"
|
|
12
12
|
require_relative "../prompt_files"
|
|
13
13
|
require_relative "../session_config"
|
|
@@ -325,8 +325,18 @@ module Samagotchi
|
|
|
325
325
|
|
|
326
326
|
CONTEXT_NOTE_PREVIEW = 60
|
|
327
327
|
|
|
328
|
-
# "note from slack: <first line>", cut to one line.
|
|
329
|
-
|
|
328
|
+
# "note from slack: <first line>", cut to one line. A peer's answer
|
|
329
|
+
# (+peer+, the note's peer hash) reads "Claude answered · <summary>".
|
|
330
|
+
def context_note_line(label, text, peer = nil)
|
|
331
|
+
peer = peer.transform_keys(&:to_s) if peer.is_a?(Hash)
|
|
332
|
+
summary = peer && peer["summary"].to_s.strip.split("\n").first.to_s
|
|
333
|
+
who = peer && (peer["label"] || peer["peer"]).to_s.strip
|
|
334
|
+
return context_note_preview("#{who} answered \u00b7 ", summary) if summary && !summary.empty? && !who.empty?
|
|
335
|
+
|
|
336
|
+
context_note_preview("note from #{label || "?"}: ", text)
|
|
337
|
+
end
|
|
338
|
+
|
|
339
|
+
def context_note_preview(prefix, text)
|
|
330
340
|
lines = text.to_s.strip.split("\n")
|
|
331
341
|
first = lines.first.to_s
|
|
332
342
|
if first.length > CONTEXT_NOTE_PREVIEW
|
|
@@ -334,7 +344,7 @@ module Samagotchi
|
|
|
334
344
|
elsif lines.size > 1
|
|
335
345
|
first += " \u2026"
|
|
336
346
|
end
|
|
337
|
-
paint("
|
|
347
|
+
paint("#{prefix}#{first}", 2)
|
|
338
348
|
end
|
|
339
349
|
|
|
340
350
|
# A tool call from the snapshot: it has no action text, only the tool.
|
|
@@ -492,7 +502,7 @@ module Samagotchi
|
|
|
492
502
|
end
|
|
493
503
|
return "" if scopes.empty?
|
|
494
504
|
|
|
495
|
-
count = ->(tokens) {
|
|
505
|
+
count = ->(tokens) { MemoryIndex::Size.count_text(tokens) }
|
|
496
506
|
"~#{count.call(scopes.sum(&:last))} tokens in this session's prompt " \
|
|
497
507
|
"(#{scopes.map { |scope, tokens| "#{scope} #{count.call(tokens)}" }.join(", ")})"
|
|
498
508
|
end
|
|
@@ -209,7 +209,7 @@ module Samagotchi
|
|
|
209
209
|
return if after_exchange != !from.nil?
|
|
210
210
|
|
|
211
211
|
messages[(from ? from + 1 : 0)..].select { |m| ContextNote.note?(m) }.each do |m|
|
|
212
|
-
@screen.commit(context_note_line(ContextNote.label_of(m), ContextNote.text_of(m)))
|
|
212
|
+
@screen.commit(context_note_line(ContextNote.label_of(m), ContextNote.text_of(m), ContextNote.fetch(m, :peer)))
|
|
213
213
|
end
|
|
214
214
|
end
|
|
215
215
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require "time"
|
|
4
|
-
require_relative "../
|
|
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) {
|
|
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 #{
|
|
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
|
-
"#{
|
|
91
|
-
"#{
|
|
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.
|
|
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 "../
|
|
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 [
|
|
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 =
|
|
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 "../
|
|
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 =
|
|
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(
|
|
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 "../
|
|
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
|
-
|
|
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 "
|
|
21
|
-
require_relative "
|
|
22
|
-
require_relative "
|
|
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 :
|
|
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 *
|
|
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 (
|
|
356
|
+
# as memory_write does (MemoryIndex::Sync; a no-op elsewhere).
|
|
357
357
|
def refresh_memory_index(call)
|
|
358
|
-
|
|
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
|
-
# (
|
|
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 =
|
|
367
|
-
return nil unless scope &&
|
|
366
|
+
scope = MemoryIndex::Sync.memory_scope(Tools::ToolPath.normalize(call[:path]))
|
|
367
|
+
return nil unless scope && MemoryIndex::Size.warn_limit.positive?
|
|
368
368
|
|
|
369
|
-
|
|
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 =
|
|
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 (
|
|
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 =
|
|
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
|
|