samagotchi 0.188.0 → 0.190.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 +76 -1
- data/docs/architecture.md +9 -4
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +15 -0
- data/docs/cli.md +38 -9
- data/docs/configuration.md +1 -0
- data/docs/guardrails.md +22 -9
- data/docs/internals/attached-context.md +4 -1
- data/docs/internals/bundles.md +3 -3
- data/docs/internals/engine.md +18 -2
- data/docs/internals/pages.md +65 -18
- data/docs/internals/session-lifecycle.md +19 -6
- data/docs/internals/web-frontend.md +3 -3
- data/docs/internals/web-push.md +11 -5
- data/docs/pages.md +69 -15
- data/docs/peers.md +3 -0
- data/docs/plugins.md +25 -1
- data/docs/releasing.md +1 -1
- data/docs/scheduling.md +168 -11
- data/docs/sessions.md +4 -4
- data/docs/sub-agent.md +1 -1
- data/docs/testing.md +1 -1
- data/lib/samagotchi/bundle_manifest.rb +34 -5
- data/lib/samagotchi/bundles/guardrails/guardrails/rules.yml +15 -0
- data/lib/samagotchi/bundles/guardrails/manifest.yml +2 -2
- data/lib/samagotchi/bundles/schedules/manifest.yml +11 -0
- data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +169 -0
- data/lib/samagotchi/bundles/system/manifest.yml +3 -3
- data/lib/samagotchi/bundles/system/self_map.md +2 -2
- data/lib/samagotchi/bundles/system/skill_pages.md +9 -4
- data/lib/samagotchi/config.rb +2 -0
- data/lib/samagotchi/context_fetch.rb +9 -3
- data/lib/samagotchi/engine/models.rb +6 -4
- data/lib/samagotchi/engine/turn_end.rb +20 -2
- data/lib/samagotchi/engine.rb +5 -1
- data/lib/samagotchi/failure_line.rb +43 -0
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/chi_dirs.rb +20 -5
- data/lib/samagotchi/guardrails/parent_approvals.rb +1 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +8 -4
- data/lib/samagotchi/guardrails/shell_paths.rb +16 -2
- data/lib/samagotchi/guardrails/targets.rb +1 -1
- data/lib/samagotchi/guardrails/verdict.rb +5 -3
- data/lib/samagotchi/image_store.rb +4 -0
- data/lib/samagotchi/installed_bundles/manifest.rb +8 -1
- data/lib/samagotchi/last_turn.rb +2 -1
- data/lib/samagotchi/llm/utf8_scrub.rb +26 -0
- data/lib/samagotchi/llm_context_view.rb +6 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +3 -2
- data/lib/samagotchi/memory_bundle/builder.rb +1 -0
- data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
- data/lib/samagotchi/model_profile.rb +4 -1
- data/lib/samagotchi/page_command.rb +9 -6
- data/lib/samagotchi/pages/bundled.rb +83 -0
- data/lib/samagotchi/pages/launch.rb +14 -2
- data/lib/samagotchi/pages/runner.rb +7 -2
- data/lib/samagotchi/pages/runtime.rb +84 -0
- data/lib/samagotchi/pages/store.rb +18 -5
- data/lib/samagotchi/pages.rb +47 -3
- data/lib/samagotchi/peer_continue.rb +14 -8
- data/lib/samagotchi/reply_wait.rb +1 -1
- data/lib/samagotchi/run_options.rb +1 -1
- data/lib/samagotchi/schedule_command.rb +175 -39
- data/lib/samagotchi/schedule_display.rb +85 -0
- data/lib/samagotchi/schedules/busy_part.rb +146 -0
- data/lib/samagotchi/schedules/condition_part.rb +312 -0
- data/lib/samagotchi/schedules/edit_part.rb +144 -0
- data/lib/samagotchi/schedules/history_part.rb +51 -0
- data/lib/samagotchi/schedules/listing_part.rb +52 -0
- data/lib/samagotchi/schedules/notices_part.rb +122 -0
- data/lib/samagotchi/schedules/protection_part.rb +113 -0
- data/lib/samagotchi/schedules/prune_part.rb +79 -0
- data/lib/samagotchi/schedules/recurrence.rb +103 -0
- data/lib/samagotchi/schedules.rb +188 -409
- data/lib/samagotchi/session_access/local.rb +9 -1
- data/lib/samagotchi/session_access/remote.rb +2 -1
- data/lib/samagotchi/session_chain/delete.rb +7 -5
- data/lib/samagotchi/session_lifecycle/allowed.rb +2 -1
- data/lib/samagotchi/session_lifecycle.rb +3 -2
- data/lib/samagotchi/session_manager/removal.rb +16 -9
- data/lib/samagotchi/session_protection.rb +99 -0
- data/lib/samagotchi/session_retention.rb +21 -6
- data/lib/samagotchi/sessions_command.rb +4 -0
- data/lib/samagotchi/system_one/self_protect.rb +4 -2
- data/lib/samagotchi/testing/boundary.rb +13 -0
- data/lib/samagotchi/tools/cancel_schedule.rb +32 -4
- data/lib/samagotchi/tools/execute.rb +5 -1
- data/lib/samagotchi/tools/list_schedules.rb +1 -0
- data/lib/samagotchi/tools/task_runtime.rb +6 -0
- data/lib/samagotchi/turn_endings.rb +14 -2
- data/lib/samagotchi/turn_flow.rb +2 -1
- data/lib/samagotchi/turn_outcome.rb +41 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/page_routes.rb +13 -8
- data/lib/samagotchi/web/public/app.js +12 -18
- data/lib/samagotchi/web/public/index.html +11 -2
- data/lib/samagotchi/web/public/owner_claim.js +7 -5
- data/lib/samagotchi/web/public/page_links.js +1 -1
- data/lib/samagotchi/web/public/page_view.js +8 -5
- data/lib/samagotchi/web/public/pages.css +9 -2
- data/lib/samagotchi/web/public/pages_group.js +21 -19
- data/lib/samagotchi/web/public/pages_list.js +73 -0
- data/lib/samagotchi/web/public/pages_model.js +54 -0
- data/lib/samagotchi/web/public/pages_wiring.js +66 -0
- data/lib/samagotchi/web/public/stream_handlers.js +8 -0
- data/lib/samagotchi/web/public/views.js +77 -23
- data/lib/samagotchi/web/push/notifier.rb +4 -3
- data/lib/samagotchi/web/server.rb +3 -1
- data/lib/samagotchi/web/session_hub.rb +75 -19
- data/lib/samagotchi/worker.rb +6 -4
- metadata +20 -1
|
@@ -553,7 +553,15 @@ module Samagotchi
|
|
|
553
553
|
end
|
|
554
554
|
return PeerPaired.new(status: :old_worker) if old_peer_worker?(id)
|
|
555
555
|
|
|
556
|
-
|
|
556
|
+
link = PeerMailbox.pair(id, peer, address: address, from: from, state_dir: state_dir!)
|
|
557
|
+
location = ContextSources.session_location(id, state_dir: state_dir!)
|
|
558
|
+
# These are exactly the bundles' LEGACY_INBOX and LEGACY_OUTBOX names.
|
|
559
|
+
["#{peer}-inbox", "#{peer}-outbox"].each do |name|
|
|
560
|
+
location.remove(name)
|
|
561
|
+
rescue SystemCallError
|
|
562
|
+
nil
|
|
563
|
+
end
|
|
564
|
+
PeerPaired.new(status: :paired, link: link)
|
|
557
565
|
end
|
|
558
566
|
|
|
559
567
|
# Ends the pairing (link.json); the messages stay.
|
|
@@ -840,7 +840,8 @@ module Samagotchi
|
|
|
840
840
|
raise SessionManager::ScratchSession, sid if json["scratch"] == true
|
|
841
841
|
|
|
842
842
|
raise SessionManager::OwnedByTUI, sid
|
|
843
|
-
when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled"
|
|
843
|
+
when "worker_running", "still_stopping", "scheduled", "continuing", "delegate_unmovable", "chain_scheduled",
|
|
844
|
+
"protection_unreadable"
|
|
844
845
|
raise SessionManager::DeleteRefused.new(sid, reason || error.to_sym, message: message, ids: ids_of(json["ids"]),
|
|
845
846
|
schedule_id: json["schedule_id"])
|
|
846
847
|
when "tasks_running" then raise tasks_refusal(json, sid)
|
|
@@ -10,6 +10,7 @@ require_relative "../continued_from"
|
|
|
10
10
|
require_relative "../note_move"
|
|
11
11
|
require_relative "../owner_lock"
|
|
12
12
|
require_relative "../session"
|
|
13
|
+
require_relative "../session_protection"
|
|
13
14
|
require_relative "../starting_marker"
|
|
14
15
|
require_relative "../worker_presence"
|
|
15
16
|
|
|
@@ -18,7 +19,6 @@ module Samagotchi
|
|
|
18
19
|
# (a require cycle otherwise), and session_continue requires session_manager.
|
|
19
20
|
autoload :SessionManager, File.expand_path("../session_manager", __dir__)
|
|
20
21
|
autoload :SessionContinue, File.expand_path("../session_continue", __dir__)
|
|
21
|
-
autoload :Schedules, File.expand_path("../schedules", __dir__)
|
|
22
22
|
|
|
23
23
|
module SessionChain
|
|
24
24
|
# A user's delete of a session (the CLI, the web, the TUI's /exit
|
|
@@ -145,7 +145,7 @@ module Samagotchi
|
|
|
145
145
|
# task the link or such a delegate started that still runs.
|
|
146
146
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
147
147
|
def check_link!(plan, state_dir, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
148
|
-
SessionManager.
|
|
148
|
+
SessionManager.refuse_protected!(plan.id, state_dir)
|
|
149
149
|
refuse_unmovable!(plan.id, plan.moved, state_dir)
|
|
150
150
|
precheck!(plan.delegates.delete, state_dir)
|
|
151
151
|
SessionManager::RunningTasks.check!([*plan.delegates.delete, plan.id], state_dir: state_dir,
|
|
@@ -218,8 +218,11 @@ module Samagotchi
|
|
|
218
218
|
def precheck!(ids, state_dir)
|
|
219
219
|
ids.each do |sid|
|
|
220
220
|
begin
|
|
221
|
-
SessionManager.
|
|
221
|
+
SessionManager.refuse_protected!(sid, state_dir)
|
|
222
222
|
rescue SessionManager::DeleteRefused => e
|
|
223
|
+
# Only a schedule names the delegate; any other refusal keeps its words.
|
|
224
|
+
raise unless e.reason == :scheduled
|
|
225
|
+
|
|
223
226
|
raise SessionManager::DeleteRefused.new(
|
|
224
227
|
sid, :scheduled, schedule_id: e.schedule_id,
|
|
225
228
|
message: "its delegate #{sid[0, 8]} is the target of an enabled schedule; " \
|
|
@@ -356,8 +359,7 @@ module Samagotchi
|
|
|
356
359
|
# a background task any of them started that still runs.
|
|
357
360
|
# @raise [SessionManager::DeleteRefused, SessionManager::OwnedByTUI]
|
|
358
361
|
def check_chain!(plan, state_dir, stop:, running_tasks: SessionManager::RunningTasks::Policy::IGNORE)
|
|
359
|
-
|
|
360
|
-
raise SessionManager::DeleteRefused.new(plan.id, :chain_scheduled, schedule_id: schedule.id) if schedule
|
|
362
|
+
SessionManager.refuse_protected!(plan.id, state_dir, chain: true)
|
|
361
363
|
|
|
362
364
|
plan.links.each do |link|
|
|
363
365
|
presence = WorkerPresence.read(link, state_dir: state_dir, owner: SessionManager.session_owner(link, state_dir: state_dir))
|
|
@@ -25,7 +25,8 @@ module Samagotchi
|
|
|
25
25
|
# or offer, not SIGSTOPped, features restart and activity, an idle worker
|
|
26
26
|
# with no streams). For schedules, worker_busy is "its worker is starting
|
|
27
27
|
# or leaving", deleting is DeliverResult::DELETING's detail, and
|
|
28
|
-
# worker_newer is WorkerLink::Skew#short_message
|
|
28
|
+
# worker_newer is WorkerLink::Skew#short_message (Schedules::BusyPart's
|
|
29
|
+
# BUSY_WORDS; Schedules#busy_cell reads these two columns). Resume's live is "a
|
|
29
30
|
# worker holds it"; restart's fallback is its unresponsive-worker path,
|
|
30
31
|
# its stop_marked a stop marked and its deleting a deleter holds the lock
|
|
31
32
|
# (it reads this table: restart_session's precheck); attach's gone_* are
|
|
@@ -12,8 +12,9 @@ module Samagotchi
|
|
|
12
12
|
# stored: see docs/internals/session-lifecycle.md (Derived states).
|
|
13
13
|
#
|
|
14
14
|
# A read only: it writes, clears and unlinks nothing (the marker writers
|
|
15
|
-
# are pinned in spec/session_lifecycle_writers_spec.rb).
|
|
16
|
-
#
|
|
15
|
+
# are pinned in spec/session_lifecycle_writers_spec.rb). Two deciders
|
|
16
|
+
# read it (restart_session's precheck, ALLOWED[:restart]; a schedule's busy
|
|
17
|
+
# check, Schedules#busy_cell, ALLOWED[:sched_chain] / [:sched_same]); ALLOWED is what
|
|
17
18
|
# each operation does in each state, pinned against the deciders by
|
|
18
19
|
# spec/session_lifecycle/matrix_spec.rb.
|
|
19
20
|
module SessionLifecycle
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../session_protection"
|
|
3
4
|
require_relative "../session_stores"
|
|
4
5
|
require_relative "running_tasks"
|
|
5
6
|
|
|
@@ -14,7 +15,9 @@ module Samagotchi
|
|
|
14
15
|
# the previous one a delete would bring back), :delegate_unmovable (a
|
|
15
16
|
# delegate the deleted link's continue moved can't go back: +ids+,
|
|
16
17
|
# +detail+ says why), :chain_scheduled (a schedule is on the chain a
|
|
17
|
-
# chain delete would take: +schedule_id+), :
|
|
18
|
+
# chain delete would take: +schedule_id+), :protection_unreadable (a
|
|
19
|
+
# SessionProtection protector's store doesn't read: its own words),
|
|
20
|
+
# :plan_changed (the plan no
|
|
18
21
|
# longer matches the digest the caller confirmed), :tasks_running
|
|
19
22
|
# (background tasks it or a session the delete takes started still run:
|
|
20
23
|
# +tasks+, a RunningTasks::Listing; +worker_stopped+ when a worker was
|
|
@@ -327,7 +330,7 @@ module Samagotchi
|
|
|
327
330
|
sd = state_dir
|
|
328
331
|
path = Session.session_file(id, state_dir: sd)
|
|
329
332
|
dir = Session.session_dir(id, state_dir: sd)
|
|
330
|
-
|
|
333
|
+
refuse_protected!(id, sd) unless allow_scheduled
|
|
331
334
|
RunningTasks.check!([id], state_dir: sd, policy: running_tasks) do |listing|
|
|
332
335
|
DeleteRefused.new(id, :tasks_running, tasks: listing)
|
|
333
336
|
end
|
|
@@ -391,13 +394,17 @@ module Samagotchi
|
|
|
391
394
|
# @return [Array<String>]
|
|
392
395
|
def delete_tree_ids(id, state_dir) = [*descendant_ids(id, state_dir).reverse, id]
|
|
393
396
|
|
|
394
|
-
# DeleteRefused
|
|
395
|
-
#
|
|
396
|
-
#
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
397
|
+
# DeleteRefused with the reason a SessionProtection protector holds +id+
|
|
398
|
+
# for (:scheduled: an enabled schedule's target, anchor or head;
|
|
399
|
+
# deleting a head would make the schedule fall back to an archived link
|
|
400
|
+
# and lose the links after it), or with +chain+ the chain +id+ is in
|
|
401
|
+
# (:chain_scheduled). A protector's store that doesn't read refuses it
|
|
402
|
+
# too (:protection_unreadable, its words); any other raise raises here.
|
|
403
|
+
def refuse_protected!(id, state_dir, chain: false)
|
|
404
|
+
hold = chain ? SessionProtection.chain_hold(id, state_dir: state_dir) : SessionProtection.hold(id, state_dir: state_dir)
|
|
405
|
+
raise DeleteRefused.new(id, hold.reason, schedule_id: hold.ref) if hold
|
|
406
|
+
rescue SessionProtection::Unreadable => e
|
|
407
|
+
raise DeleteRefused.new(id, :protection_unreadable, message: e.message)
|
|
401
408
|
end
|
|
402
409
|
|
|
403
410
|
# Remove every store with state scoped to +id+. The file and sidecar
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "log"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
# Who protects a session from a delete (docs/sessions.md, "Deleting a
|
|
7
|
+
# session"): the sibling of SessionStores, which says what a delete
|
|
8
|
+
# removes; this says what refuses it. The retention sweep keeps every
|
|
9
|
+
# protected id, a link's delete is refused by its first #hold and a chain's
|
|
10
|
+
# by its first #chain_hold (SessionManager::DeleteRefused, whose reasons
|
|
11
|
+
# hold the words).
|
|
12
|
+
#
|
|
13
|
+
# A protector is what a PROTECTORS entry builds for a state dir, answering
|
|
14
|
+
# three questions: #protected_ids (Array<String>), #hold(session_id) and
|
|
15
|
+
# #chain_hold(session_id) (a Hold or nil). Schedules is the one protector
|
|
16
|
+
# (Schedules::ProtectionPart: an enabled schedule's anchor, head and
|
|
17
|
+
# resolved target).
|
|
18
|
+
#
|
|
19
|
+
# Fail-closed: a protector that raises is logged and the error re-raised,
|
|
20
|
+
# never read as "unprotected". The sweep (SessionRetention.sweep_if_due)
|
|
21
|
+
# logs retention_failed and deletes nothing (an Unreadable store:
|
|
22
|
+
# retention_skipped, once per sweep interval); a delete raises to its
|
|
23
|
+
# caller, and a store that is there but doesn't read (Unreadable) is its
|
|
24
|
+
# refusal (DeleteRefused :protection_unreadable,
|
|
25
|
+
# SessionManager.refuse_protected!). A missing store protects nothing.
|
|
26
|
+
module SessionProtection
|
|
27
|
+
# +session_id+ the session held, +reason+ the DeleteRefused reason the
|
|
28
|
+
# holder asks for (:scheduled for a link, :chain_scheduled for a chain),
|
|
29
|
+
# +ref+ the holder's id for the words (the schedule id).
|
|
30
|
+
Hold = Data.define(:session_id, :reason, :ref)
|
|
31
|
+
|
|
32
|
+
# A protector's store is there but doesn't read (schedules.json not
|
|
33
|
+
# JSON, not a schedules document, a file chi can't open, or one schedule
|
|
34
|
+
# in it that doesn't read): who it protects is unknown, so no session is
|
|
35
|
+
# deleted until it is fixed. Its words, one per condition: the whole
|
|
36
|
+
# file, or one entry (+entry+, its id) and the way to drop that entry.
|
|
37
|
+
class Unreadable < StandardError
|
|
38
|
+
# @param file [String] the store's name, for the words
|
|
39
|
+
# @param error [String] why it doesn't read
|
|
40
|
+
# @param entry [String, nil] the id of the one entry that doesn't read
|
|
41
|
+
def initialize(file, error, entry: nil)
|
|
42
|
+
if entry
|
|
43
|
+
super("schedule entry #{entry} in #{file} is unreadable (#{error}): chi schedule cancel #{entry}, " \
|
|
44
|
+
"or fix the file, before deleting sessions")
|
|
45
|
+
else
|
|
46
|
+
super("#{file} is unreadable (#{error}): fix or remove it before deleting sessions")
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
module_function
|
|
52
|
+
|
|
53
|
+
# Deferred: schedules.rb requires session_manager, whose removal part
|
|
54
|
+
# requires this file.
|
|
55
|
+
def schedule_protector(state_dir)
|
|
56
|
+
require_relative "schedules"
|
|
57
|
+
Schedules.new(state_dir: state_dir)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
PROTECTORS = { schedule: method(:schedule_protector) }.freeze
|
|
61
|
+
|
|
62
|
+
# @return [Array<String>] every id some protector holds (retention keeps them)
|
|
63
|
+
def protected_ids(state_dir:)
|
|
64
|
+
PROTECTORS.flat_map { |name, build| ask(name, build, state_dir, &:protected_ids) }.uniq
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# @return [Hold, nil] the first protector's hold on a delete of +session_id+
|
|
68
|
+
def hold(session_id, state_dir:)
|
|
69
|
+
first_hold(session_id, state_dir) { |protector| protector.hold(session_id) }
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# @return [Hold, nil] the first protector's hold on a delete of the chain
|
|
73
|
+
# +session_id+ belongs to
|
|
74
|
+
def chain_hold(session_id, state_dir:)
|
|
75
|
+
first_hold(session_id, state_dir) { |protector| protector.chain_hold(session_id) }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def first_hold(session_id, state_dir, &question)
|
|
79
|
+
PROTECTORS.each do |name, build|
|
|
80
|
+
found = ask(name, build, state_dir, sid: session_id, &question)
|
|
81
|
+
return found if found
|
|
82
|
+
end
|
|
83
|
+
nil
|
|
84
|
+
end
|
|
85
|
+
private_class_method :first_hold
|
|
86
|
+
|
|
87
|
+
# An Unreadable store is logged once, by the caller (retention_skipped,
|
|
88
|
+
# or the delete's refusal), not here.
|
|
89
|
+
def ask(name, build, state_dir, sid: nil)
|
|
90
|
+
yield build.call(state_dir)
|
|
91
|
+
rescue Unreadable
|
|
92
|
+
raise
|
|
93
|
+
rescue StandardError => e
|
|
94
|
+
Log.warn(:worker, "protector_failed", protector: name, sid: sid, error: e.class.name, msg: e.message)
|
|
95
|
+
raise
|
|
96
|
+
end
|
|
97
|
+
private_class_method :ask
|
|
98
|
+
end
|
|
99
|
+
end
|
|
@@ -10,6 +10,7 @@ require_relative "config"
|
|
|
10
10
|
require_relative "log"
|
|
11
11
|
require_relative "model_profile"
|
|
12
12
|
require_relative "session_manager"
|
|
13
|
+
require_relative "session_protection"
|
|
13
14
|
require_relative "system_one/runs"
|
|
14
15
|
|
|
15
16
|
module Samagotchi
|
|
@@ -143,9 +144,15 @@ module Samagotchi
|
|
|
143
144
|
age = Time.now - File.mtime(marker)
|
|
144
145
|
return if age < interval
|
|
145
146
|
end
|
|
146
|
-
result =
|
|
147
|
+
result = begin
|
|
148
|
+
prune(state_dir: sd)
|
|
149
|
+
rescue SessionProtection::Unreadable => e
|
|
150
|
+
skipped_round(sd, e)
|
|
151
|
+
end
|
|
147
152
|
sweep_runs(sd)
|
|
148
153
|
FileUtils.touch(marker)
|
|
154
|
+
return nil if result.nil?
|
|
155
|
+
|
|
149
156
|
if result[:deleted].any?
|
|
150
157
|
stops = result[:tasks_stopped].to_i
|
|
151
158
|
suffix = stops.positive? ? "; stopped #{stops} background tasks" : ""
|
|
@@ -157,6 +164,16 @@ module Samagotchi
|
|
|
157
164
|
nil
|
|
158
165
|
end
|
|
159
166
|
|
|
167
|
+
# A protector's store doesn't read: no session is deleted this round,
|
|
168
|
+
# logged once (the next try is at the next interval, the marker touched
|
|
169
|
+
# as for a sweep that ran); the parts that ask no protector still run
|
|
170
|
+
# (the skeleton-only dirs here, the saved runs after).
|
|
171
|
+
private_class_method def self.skipped_round(state_dir, error)
|
|
172
|
+
Log.warn(:worker, "retention_skipped", msg: error.message)
|
|
173
|
+
prune_orphan_dirs(state_dir, dry_run: false) if SessionManager.discard_empty?
|
|
174
|
+
nil
|
|
175
|
+
end
|
|
176
|
+
|
|
160
177
|
# System 1's saved runs last as long as sessions, by day file. Never a
|
|
161
178
|
# reason the session sweep fails.
|
|
162
179
|
private_class_method def self.sweep_runs(state_dir)
|
|
@@ -214,9 +231,7 @@ module Samagotchi
|
|
|
214
231
|
def self.apply(state_dir: Session.default_state_dir, days: DEFAULT_RETENTION_DAYS, max_count: DEFAULT_MAX_COUNT,
|
|
215
232
|
keep_status: DEFAULT_KEEP_STATUS, dry_run: false, test_only: false, alive_check: nil,
|
|
216
233
|
empty_check: nil, any_age: false, now: Time.now)
|
|
217
|
-
|
|
218
|
-
schedules = Schedules.new(state_dir: state_dir)
|
|
219
|
-
scheduled_targets = schedules.protected_session_ids
|
|
234
|
+
protected_ids = SessionProtection.protected_ids(state_dir: state_dir)
|
|
220
235
|
keep_status = Array(keep_status).map(&:to_s)
|
|
221
236
|
# Fetch all sessions sorted newest-first for count logic
|
|
222
237
|
all = Session.list(state_dir: state_dir, sort: "updated_at", order: "desc")
|
|
@@ -243,7 +258,7 @@ module Samagotchi
|
|
|
243
258
|
next
|
|
244
259
|
end
|
|
245
260
|
|
|
246
|
-
if
|
|
261
|
+
if protected_ids.include?(session.id)
|
|
247
262
|
kept << session.id
|
|
248
263
|
next
|
|
249
264
|
end
|
|
@@ -318,7 +333,7 @@ module Samagotchi
|
|
|
318
333
|
# once, at the start): resumed or taken by a worker since, it stays.
|
|
319
334
|
elsif held?(session.id, state_dir, alive_check)
|
|
320
335
|
kept << session.id
|
|
321
|
-
elsif
|
|
336
|
+
elsif protected_ids.include?(session.id)
|
|
322
337
|
kept << session.id
|
|
323
338
|
else
|
|
324
339
|
policy = SessionManager::RunningTasks::Policy.stop(by: "retention")
|
|
@@ -4,6 +4,7 @@ require "json"
|
|
|
4
4
|
require_relative "session"
|
|
5
5
|
require_relative "session_row"
|
|
6
6
|
require_relative "session_manager"
|
|
7
|
+
require_relative "session_protection"
|
|
7
8
|
require_relative "session_retention"
|
|
8
9
|
require_relative "project_scope"
|
|
9
10
|
require_relative "analytics_file"
|
|
@@ -364,6 +365,9 @@ module Samagotchi
|
|
|
364
365
|
@stdout.puts "\nRun without --dry-run to delete."
|
|
365
366
|
end
|
|
366
367
|
0
|
|
368
|
+
rescue Samagotchi::SessionProtection::Unreadable => e
|
|
369
|
+
@stderr.puts e.message
|
|
370
|
+
1
|
|
367
371
|
end
|
|
368
372
|
end
|
|
369
373
|
end
|
|
@@ -46,8 +46,10 @@ module Samagotchi
|
|
|
46
46
|
module SelfProtect
|
|
47
47
|
# The rules that let one agent answer chi's questions: an auto-allow
|
|
48
48
|
# there is one agent approving another's, so the user decides them even
|
|
49
|
-
# when they are listed in `guardrails.system_one.rules
|
|
50
|
-
|
|
49
|
+
# when they are listed in `guardrails.system_one.rules`; and a
|
|
50
|
+
# schedule's condition (chi-schedule-if), a shell command chi's tick
|
|
51
|
+
# runs later, outside every gate, which only the owner sets.
|
|
52
|
+
NEVER_AUTO = %w[chi-answer-piped chi-answer-http chi-broadcast chi-schedule-if].freeze
|
|
51
53
|
# chi's default state dir as a shell command may spell it when the
|
|
52
54
|
# settings point elsewhere (the sessions and the guardrails store live
|
|
53
55
|
# there).
|
|
@@ -115,6 +115,19 @@ module Samagotchi
|
|
|
115
115
|
"Samagotchi::ProjectEnv.respond_to?" => "checks the worker has ProjectEnv.spawn_env (an older chi doesn't)",
|
|
116
116
|
"Samagotchi::ProjectEnv.spawn_env" => "starts a server in the session's project env: needs a ctx tools-env API"
|
|
117
117
|
},
|
|
118
|
+
# The kit has no page runner: the page's spec runs it through chi's own (Pages::Launch).
|
|
119
|
+
"schedules" => {
|
|
120
|
+
'require "samagotchi/memory_bundle/installer"' => "spec: installs the bundle as chi does (the page is listed from the record)",
|
|
121
|
+
"Samagotchi::MemoryBundle::Installer.new" => "spec: installs the bundle as chi does",
|
|
122
|
+
'require "samagotchi/pages"' => "spec: for Pages",
|
|
123
|
+
'require "samagotchi/pages/launch"' => "spec: for Pages::Launch",
|
|
124
|
+
"Samagotchi::Pages::Launch.run" => "spec: a run as chi web's Refresh starts it: a kit page runner could replace it",
|
|
125
|
+
"Samagotchi::Pages::Store" => "spec: the page as listed and stored",
|
|
126
|
+
"Samagotchi::Pages.scripts_dir" => "spec: the owner's pages folder stays unmade",
|
|
127
|
+
"Samagotchi::Pages::MAX_OUTPUT_BYTES" => "spec: the output cap the page stays under",
|
|
128
|
+
"Samagotchi::Pages::MAX_TEXT_BYTES" => "spec: the text block cap the off and unreadable texts stay under",
|
|
129
|
+
"Samagotchi::Paths.state_dir" => "spec: where the fixture schedules.json goes"
|
|
130
|
+
},
|
|
118
131
|
"slow-commands" => {
|
|
119
132
|
'require "samagotchi/guardrails/shell_lex"' => "for ShellLex",
|
|
120
133
|
"Samagotchi::Guardrails::ShellLex" => "splits a command into its parts: needs a stable shell-parsing helper",
|
|
@@ -7,7 +7,11 @@ module Samagotchi
|
|
|
7
7
|
# Cancel a durable schedule of this session's chain by its id, a unique
|
|
8
8
|
# prefix of it, or its title (docs/scheduling.md). A schedule of another
|
|
9
9
|
# session's chain is refused unless the call says `all: true`: `chi
|
|
10
|
-
# schedule cancel` is the machine-wide way.
|
|
10
|
+
# schedule cancel` is the machine-wide way. An entry of schedules.json
|
|
11
|
+
# that doesn't read (which list leaves out) goes by its exact id, as
|
|
12
|
+
# chi schedule cancel ID takes it: in this chain when its stored anchor
|
|
13
|
+
# or head is a link of it, else refused like another chain's; one whose
|
|
14
|
+
# chain can't be told (neither stored) only with `all: true`.
|
|
11
15
|
class CancelSchedule
|
|
12
16
|
NAME = ToolNames::CancelSchedule::NAME
|
|
13
17
|
|
|
@@ -23,10 +27,12 @@ module Samagotchi
|
|
|
23
27
|
want_all = all == true || all.to_s.strip.casecmp?("true")
|
|
24
28
|
schedules = Schedules.new(state_dir: peers&.state_dir)
|
|
25
29
|
entry = schedules.lookup(name)
|
|
26
|
-
return
|
|
30
|
+
return cancel_unreadable(schedules, name, want_all, peers) if entry.nil?
|
|
27
31
|
|
|
32
|
+
# nil: gone since the lookup (another cancel in between).
|
|
28
33
|
if want_all || peers&.session_id.nil?
|
|
29
|
-
schedules.cancel(name)
|
|
34
|
+
return "No schedule named #{name}." unless schedules.cancel(name)
|
|
35
|
+
|
|
30
36
|
return "Cancelled #{entry.id} (#{entry.title || entry.recurrence_words})."
|
|
31
37
|
end
|
|
32
38
|
|
|
@@ -35,11 +41,33 @@ module Samagotchi
|
|
|
35
41
|
"chi schedule cancel #{entry.id} cancels it, or say all: true if you meant it."
|
|
36
42
|
end
|
|
37
43
|
|
|
38
|
-
schedules.cancel(name)
|
|
44
|
+
return "No schedule named #{name}." unless schedules.cancel(name)
|
|
45
|
+
|
|
39
46
|
"Cancelled #{entry.id} (#{entry.title || entry.recurrence_words})."
|
|
40
47
|
rescue StandardError => e
|
|
41
48
|
"Error: #{e.message}"
|
|
42
49
|
end
|
|
50
|
+
|
|
51
|
+
# @return [String] the answer for +name+ when no readable entry has it
|
|
52
|
+
def self.cancel_unreadable(schedules, name, want_all, peers)
|
|
53
|
+
raw = schedules.unreadable_record(name)
|
|
54
|
+
return "No schedule named #{name}." if raw.nil?
|
|
55
|
+
|
|
56
|
+
unless want_all || peers&.session_id.nil?
|
|
57
|
+
ends = [raw["anchor"], raw["head"]].select { |id| id.is_a?(String) && !id.empty? }
|
|
58
|
+
if ends.empty?
|
|
59
|
+
return "Error: schedule #{name} can't be read, so its chain can't be told. " \
|
|
60
|
+
"chi schedule cancel #{name} removes it, or say all: true if you meant it."
|
|
61
|
+
end
|
|
62
|
+
unless schedules.chain_of(peers.session_id).any? { |id| ends.include?(id) }
|
|
63
|
+
return "Error: #{name} belongs to another session's chain. " \
|
|
64
|
+
"chi schedule cancel #{name} cancels it, or say all: true if you meant it."
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
return "No schedule named #{name}." unless schedules.cancel(name)
|
|
68
|
+
|
|
69
|
+
"Cancelled #{name} (it couldn't be read)."
|
|
70
|
+
end
|
|
43
71
|
end
|
|
44
72
|
end
|
|
45
73
|
end
|
|
@@ -334,7 +334,11 @@ module Samagotchi
|
|
|
334
334
|
return "" unless File.file?(path)
|
|
335
335
|
|
|
336
336
|
size = File.size(path)
|
|
337
|
-
|
|
337
|
+
# Valid UTF-8 (Utf8Scrub's rules, a bad byte "?" as the request's
|
|
338
|
+
# body has it): a command's output can hold any bytes, and the turn's
|
|
339
|
+
# later regex passes raise on invalid ones (the preview below is
|
|
340
|
+
# scrubbed too).
|
|
341
|
+
return File.binread(path).force_encoding(Encoding::UTF_8).scrub("?") if size <= truncate_at_bytes
|
|
338
342
|
|
|
339
343
|
LogPreview.new(size, OutputGuardrails.head_tail_from_file(path: path, file_size: size, preview_bytes: preview_bytes))
|
|
340
344
|
end
|
|
@@ -36,6 +36,7 @@ module Samagotchi
|
|
|
36
36
|
flags = []
|
|
37
37
|
flags << "skips #{entry.skips} of #{entry.max_skips}" if entry.skips.positive?
|
|
38
38
|
flags << "disabled after #{entry.failures} failures" unless entry.enabled
|
|
39
|
+
flags << "if: #{ScheduleDisplay.condition_words(entry)}" unless entry.condition.nil?
|
|
39
40
|
"#{entry.id} #{ScheduleDisplay.name(entry)} #{entry.target_words} #{entry.recurrence_words} " \
|
|
40
41
|
"next #{entry.next_due} #{flags.join(", ")}".rstrip
|
|
41
42
|
end
|
|
@@ -382,6 +382,12 @@ module Samagotchi
|
|
|
382
382
|
file.seek(offset)
|
|
383
383
|
file.read
|
|
384
384
|
end
|
|
385
|
+
# Read binary: relabel as UTF-8 and scrub before the split (which
|
|
386
|
+
# raises on invalid bytes). The seek can land mid-character and a log
|
|
387
|
+
# can hold real binary bytes; a BINARY tail raises
|
|
388
|
+
# Encoding::CompatibilityError once joined with UTF-8 text (a task
|
|
389
|
+
# finish wake's note, task_wait's response, execute's hand-over).
|
|
390
|
+
content = content.force_encoding(Encoding::UTF_8).scrub
|
|
385
391
|
content = content.split("\n", 2).last.to_s if offset.positive?
|
|
386
392
|
content.lines.last(line_count).join
|
|
387
393
|
end
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "llm/errors"
|
|
4
|
+
require_relative "failure_line"
|
|
4
5
|
require_relative "tools/task_runtime"
|
|
5
6
|
require_relative "turn_note"
|
|
6
7
|
|
|
@@ -12,6 +13,14 @@ module Samagotchi
|
|
|
12
13
|
# forwards each here and keeps the steps that hold the event lock or save
|
|
13
14
|
# (end_turn, complete_turn, store_answer_display, record_last_turn).
|
|
14
15
|
class TurnEndings
|
|
16
|
+
# A failed turn's error_kind when chi itself failed (FailureLine).
|
|
17
|
+
CHI_ERROR = FailureLine::CHI_ERROR
|
|
18
|
+
|
|
19
|
+
def self.chi_error?(error) = FailureLine.chi_error?(error)
|
|
20
|
+
|
|
21
|
+
# The one line of a failed turn (FailureLine.for).
|
|
22
|
+
def self.failure_line(error) = FailureLine.for(error)
|
|
23
|
+
|
|
15
24
|
# @param failure_summary [#call] (error) → a failed turn's one line (Engine#failure_summary)
|
|
16
25
|
def initialize(failure_summary:)
|
|
17
26
|
@failure_summary = failure_summary
|
|
@@ -94,11 +103,12 @@ module Samagotchi
|
|
|
94
103
|
|
|
95
104
|
steps = LLM::FailedTurn.progress(turn.session.messages, kept)
|
|
96
105
|
error.kept_steps = steps if steps && error.is_a?(LLM::FailedTurn) && LLM::FailedTurn.keeps_work?(error)
|
|
97
|
-
|
|
98
|
-
TurnNote.replace_trailing(kept, TurnNote.failed(summary, continued: turn.continue, steps: steps))
|
|
106
|
+
TurnNote.replace_trailing(kept, TurnNote.failed(self.class.failure_line(error), continued: turn.continue, steps: steps))
|
|
99
107
|
end
|
|
100
108
|
|
|
101
109
|
# Called after #failed_messages, which marks the error's kept steps.
|
|
110
|
+
# A provider error and chi's own (.chi_error?) say their kind and their
|
|
111
|
+
# line (summary:); a UserError's message is its line.
|
|
102
112
|
def failed_event(error, seconds)
|
|
103
113
|
failed = { type: :turn_failed, error_class: error.class.name, message: error.message,
|
|
104
114
|
duration_ms: (seconds * 1000).round }
|
|
@@ -108,6 +118,8 @@ module Samagotchi
|
|
|
108
118
|
# A provider error says what kind it is, for one line per kind in the UIs.
|
|
109
119
|
if error.is_a?(LLM::ProviderError)
|
|
110
120
|
failed.merge!(error_kind: error.kind, retryable: error.retryable?, host: error.host, summary: failure_summary(error))
|
|
121
|
+
elsif self.class.chi_error?(error)
|
|
122
|
+
failed.merge!(error_kind: CHI_ERROR, summary: self.class.failure_line(error))
|
|
111
123
|
end
|
|
112
124
|
failed
|
|
113
125
|
end
|
data/lib/samagotchi/turn_flow.rb
CHANGED
|
@@ -5,6 +5,7 @@ require_relative "output_formatter"
|
|
|
5
5
|
require_relative "turn_note"
|
|
6
6
|
require_relative "context_note"
|
|
7
7
|
require_relative "llm/errors"
|
|
8
|
+
require_relative "failure_line"
|
|
8
9
|
|
|
9
10
|
module Samagotchi
|
|
10
11
|
# The state around turns a session worker (and a one-shot) keeps out of
|
|
@@ -137,7 +138,7 @@ module Samagotchi
|
|
|
137
138
|
# model)
|
|
138
139
|
# @return [Symbol] :kept or :restored
|
|
139
140
|
def prompt_turn_failed(error: nil, wake: nil, wake_kept: nil, note: true)
|
|
140
|
-
summary = error &&
|
|
141
|
+
summary = error && FailureLine.for(error)
|
|
141
142
|
steps = LLM::FailedTurn.kept_steps(error)
|
|
142
143
|
@offer = nil
|
|
143
144
|
if steps
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "session_inbox"
|
|
4
|
+
require_relative "turn_checkpoint"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
# What a queued turn came to, from its files: whether it left a reply
|
|
8
|
+
# (SessionInbox.reply_for) and the turn it settled as (the session's
|
|
9
|
+
# last_turn when it is that turn, else TurnCheckpoint.settled_turn). A
|
|
10
|
+
# schedule's pending firing reads it (Schedules#resolve_pending, which
|
|
11
|
+
# keeps its own order around it: the reply, its question window, the turn,
|
|
12
|
+
# expiry).
|
|
13
|
+
# @!attribute replied [Boolean] a reply file names the turn
|
|
14
|
+
# @!attribute turn [LastTurn, nil] the settled turn, nil while it runs
|
|
15
|
+
TurnOutcome = Data.define(:replied, :turn) do
|
|
16
|
+
# @param session [Session] the session the turn was queued in
|
|
17
|
+
# @param turn_id [String] its enqueued id
|
|
18
|
+
# @param session_dir [String] that session's sidecar dir
|
|
19
|
+
# @return [TurnOutcome]
|
|
20
|
+
def self.for(session, turn_id, session_dir:)
|
|
21
|
+
turn = session.last_turn if session.last_turn&.enqueued_id == turn_id
|
|
22
|
+
turn ||= TurnCheckpoint.settled_turn(session_dir, turn_id)
|
|
23
|
+
new(replied: !SessionInbox.reply_for(session_dir, turn_id).nil?, turn: turn)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def settled? = replied || !turn.nil?
|
|
27
|
+
|
|
28
|
+
# The settled turn completed (the turn alone: a reply is #replied).
|
|
29
|
+
def completed? = turn&.outcome == "completed"
|
|
30
|
+
|
|
31
|
+
# The words of a settled outcome, nil before it settles.
|
|
32
|
+
# @return [String, nil]
|
|
33
|
+
def detail
|
|
34
|
+
return nil unless settled?
|
|
35
|
+
return "turn completed" if replied || completed?
|
|
36
|
+
|
|
37
|
+
reason = [turn.outcome, turn.error_kind, turn.cancel_reason, turn.stopped_by].compact.join(": ")
|
|
38
|
+
"turn #{reason.empty? ? "did not complete" : reason}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|