samagotchi 0.163.0 → 0.165.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 +77 -1
- data/docs/architecture.md +7 -5
- data/docs/bundles.md +4 -2
- data/docs/cli.md +23 -11
- data/docs/configuration.md +5 -1
- data/docs/guardrails.md +13 -16
- data/docs/internals/block-renderers.md +3 -1
- data/docs/internals/bridge.md +4 -3
- data/docs/internals/bundles.md +4 -2
- data/docs/internals/inline-widgets.md +13 -3
- data/docs/internals/remote-sessions.md +7 -2
- data/docs/internals/web-frontend.md +36 -9
- data/docs/memory.md +12 -7
- data/docs/plugins.md +6 -4
- data/docs/releasing.md +6 -2
- data/docs/scheduling.md +25 -3
- data/docs/sessions.md +9 -6
- data/docs/sub-agent.md +11 -0
- data/docs/testing.md +28 -4
- data/lib/samagotchi/block_renderers.rb +14 -7
- data/lib/samagotchi/bridge/card_store.rb +2 -1
- data/lib/samagotchi/bridge/turn_accumulator.rb +7 -8
- data/lib/samagotchi/bridge.rb +45 -3
- data/lib/samagotchi/bundle_command/installed_check.rb +2 -1
- data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +8 -4
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/chi_home.rb +44 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -1
- data/lib/samagotchi/client_id.rb +34 -3
- data/lib/samagotchi/compat.rb +36 -0
- data/lib/samagotchi/context_command.rb +3 -0
- data/lib/samagotchi/context_providers.rb +5 -6
- data/lib/samagotchi/context_sources.rb +2 -2
- data/lib/samagotchi/continue_intent.rb +93 -0
- data/lib/samagotchi/events.rb +4 -4
- data/lib/samagotchi/extension_load.rb +2 -1
- data/lib/samagotchi/guardrails/child_boundary.rb +7 -27
- data/lib/samagotchi/guardrails/rules.rb +11 -9
- data/lib/samagotchi/guardrails/targets.rb +3 -4
- data/lib/samagotchi/inline_widgets.rb +16 -1
- data/lib/samagotchi/memory_bundle/provenance.rb +46 -4
- data/lib/samagotchi/memory_bundle/status.rb +2 -1
- data/lib/samagotchi/memory_command.rb +3 -0
- data/lib/samagotchi/memory_orphans.rb +4 -7
- data/lib/samagotchi/memory_paths.rb +64 -7
- data/lib/samagotchi/memory_usage.rb +1 -1
- data/lib/samagotchi/message_parts.rb +13 -0
- data/lib/samagotchi/note_command.rb +3 -6
- data/lib/samagotchi/parent_report.rb +3 -2
- data/lib/samagotchi/plugin/sessions.rb +4 -2
- data/lib/samagotchi/project_command.rb +4 -1
- data/lib/samagotchi/project_id_clash.rb +37 -26
- data/lib/samagotchi/project_init.rb +24 -7
- data/lib/samagotchi/reply_wait/poll.rb +2 -1
- data/lib/samagotchi/schedules.rb +124 -106
- data/lib/samagotchi/send_command/parent_gate.rb +8 -5
- data/lib/samagotchi/send_command.rb +203 -45
- data/lib/samagotchi/session.rb +3 -2
- data/lib/samagotchi/session_access/local.rb +25 -2
- data/lib/samagotchi/session_access/remote.rb +40 -4
- data/lib/samagotchi/session_chain/links.rb +109 -0
- data/lib/samagotchi/session_chain.rb +7 -19
- data/lib/samagotchi/session_continue.rb +143 -57
- data/lib/samagotchi/session_inbox_delivery.rb +94 -28
- data/lib/samagotchi/session_manager/delivery.rb +106 -18
- data/lib/samagotchi/session_manager/removal.rb +13 -7
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_retention.rb +20 -10
- data/lib/samagotchi/session_setup.rb +31 -1
- data/lib/samagotchi/state_format.rb +12 -8
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +6 -7
- data/lib/samagotchi/terminal_ui.rb +20 -9
- data/lib/samagotchi/tools/delegate.rb +2 -1
- data/lib/samagotchi/verify_fresh.rb +4 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/message_display.rb +1 -9
- data/lib/samagotchi/web/public/activity.js +2 -2
- data/lib/samagotchi/web/public/app.js +13 -13
- data/lib/samagotchi/web/public/apple-touch-icon.png +0 -0
- data/lib/samagotchi/web/public/bar_fold.js +293 -0
- data/lib/samagotchi/web/public/card.js +1 -1
- data/lib/samagotchi/web/public/composer_mode.js +5 -7
- data/lib/samagotchi/web/public/favicon.svg +1 -1
- data/lib/samagotchi/web/public/icons/chi-180.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-192.png +0 -0
- data/lib/samagotchi/web/public/icons/chi-512.png +0 -0
- data/lib/samagotchi/web/public/index.html +132 -65
- data/lib/samagotchi/web/public/info_bar.js +28 -16
- data/lib/samagotchi/web/public/info_bar_model.js +70 -1
- data/lib/samagotchi/web/public/inline_widgets.js +4 -1
- data/lib/samagotchi/web/public/more_sheet.js +98 -40
- data/lib/samagotchi/web/public/stage_view.js +3 -1
- data/lib/samagotchi/web/public/stream_handlers.js +1 -1
- data/lib/samagotchi/web/public/turn_events.js +0 -122
- data/lib/samagotchi/web/public/turn_model.js +1 -1
- data/lib/samagotchi/web/public/widget_picks.js +18 -3
- data/lib/samagotchi/web/public/widget_walk.js +14 -4
- data/lib/samagotchi/web/session_access_routes.rb +8 -1
- data/lib/samagotchi/web/session_control.rb +15 -2
- data/lib/samagotchi/web/session_create.rb +4 -2
- data/lib/samagotchi/web/update_check.rb +27 -8
- metadata +4 -1
|
@@ -38,7 +38,7 @@ module Samagotchi
|
|
|
38
38
|
end
|
|
39
39
|
if @json && !@reported && status == 1
|
|
40
40
|
detail = @stderr.last.to_s.delete_prefix("#{command_name}: ")
|
|
41
|
-
@stdout.puts(ParentReport.error_line(detail, session_id: @session_id))
|
|
41
|
+
@stdout.puts(ParentReport.error_line(detail, session_id: @session_id, extra: error_extra))
|
|
42
42
|
@stdout.flush
|
|
43
43
|
end
|
|
44
44
|
status
|
|
@@ -89,6 +89,10 @@ module Samagotchi
|
|
|
89
89
|
Exit::FAILED
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
+
# More keys for the error object, after session_id: {} (chi send's
|
|
93
|
+
# names the id asked).
|
|
94
|
+
def error_extra = {}
|
|
95
|
+
|
|
92
96
|
# More keys for the JSON object: answered_here: false when chi
|
|
93
97
|
# answer's answer didn't go in (the question was no longer open).
|
|
94
98
|
def report_extra
|
data/lib/samagotchi/client_id.rb
CHANGED
|
@@ -11,6 +11,10 @@ module Samagotchi
|
|
|
11
11
|
WEB_RESTART = "web:restart"
|
|
12
12
|
# A web page's first prompt when creating a session.
|
|
13
13
|
WEB_CREATE = "web:create"
|
|
14
|
+
# The owner's turn or command route (POST /api/sessions/:id/turn,
|
|
15
|
+
# /command) given no client_id (a script, curl with the owner's token):
|
|
16
|
+
# named at that seam, a web tab's rights, never a silent nil.
|
|
17
|
+
WEB_UNNAMED = "web:unnamed"
|
|
14
18
|
# An attached chi TUI: tui:<pid>.
|
|
15
19
|
TUI_PREFIX = "tui:"
|
|
16
20
|
# chi send's turns and commands.
|
|
@@ -45,9 +49,13 @@ module Samagotchi
|
|
|
45
49
|
|
|
46
50
|
# Input a human typed: a web tab, a chi TUI, chi send, a participant
|
|
47
51
|
# (review:, a person or an agent they run: never automatic input), and
|
|
48
|
-
# nil
|
|
49
|
-
#
|
|
50
|
-
#
|
|
52
|
+
# nil, which reaches only a worker's own seams: its initial prompt
|
|
53
|
+
# (spawn_session saves no first_turn_client_id for a person's), a Bridge
|
|
54
|
+
# POST /turn naming none (the Bridge's secret holder: a local TUI or chi
|
|
55
|
+
# web, which names its sender) and an input file from an older chi.
|
|
56
|
+
# SessionManager.deliver_turn refuses nil; chi web's turn and command routes
|
|
57
|
+
# name it (WEB_UNNAMED). An allowlist: delegates, plugins, reminders, context
|
|
58
|
+
# changes and any id chi doesn't know are not.
|
|
51
59
|
HUMAN_PREFIXES = [WEB_PREFIX, TUI_PREFIX, REVIEW_PREFIX].freeze
|
|
52
60
|
HUMAN_IDS = [CLI_SEND].freeze
|
|
53
61
|
|
|
@@ -58,6 +66,14 @@ module Samagotchi
|
|
|
58
66
|
# A turn's origin by its sender (#origin, LastTurn#origin): the ids chi
|
|
59
67
|
# runs turns for on its own, by prefix, and a participant's ("review");
|
|
60
68
|
# system:reminder is "reminder", anything else "client".
|
|
69
|
+
# Which SessionInbox::WakePolicy row a turn delivery from a sender reads
|
|
70
|
+
# (#wake_kind): a person's (:send: un-stops, un-archives) by allowlist,
|
|
71
|
+
# like HUMAN_PREFIXES; the unattended senders' own rows; any other id
|
|
72
|
+
# the safe :automatic row (no un-stop, no un-archive).
|
|
73
|
+
PERSON_WAKE_PREFIXES = [WEB_PREFIX, TUI_PREFIX].freeze
|
|
74
|
+
WAKE_PREFIXES = { SCHEDULE_PREFIX => :schedule, DELEGATE_PREFIX => :delegate,
|
|
75
|
+
REVIEW_PREFIX => :participant }.freeze
|
|
76
|
+
|
|
61
77
|
ORIGIN_PREFIXES = { DELEGATE_PREFIX => "delegate", CHILD_PREFIX => "delegate_report",
|
|
62
78
|
CONTEXT_PREFIX => "context", TASK_PREFIX => "task", REVIEW_PREFIX => "review",
|
|
63
79
|
SCHEDULE_PREFIX => "schedule" }.freeze
|
|
@@ -93,6 +109,21 @@ module Samagotchi
|
|
|
93
109
|
HUMAN_IDS.include?(id) || HUMAN_PREFIXES.any? { |prefix| id.start_with?(prefix) }
|
|
94
110
|
end
|
|
95
111
|
|
|
112
|
+
# The WakePolicy row a turn delivery from +client_id+ reads (deliver_turn's
|
|
113
|
+
# two liveness checks): :send for a person (a web tab, a chi TUI, chi
|
|
114
|
+
# send), :schedule, :plugin, :delegate or :participant for those
|
|
115
|
+
# senders, :automatic for anything else (nil too: deliver_turn refuses it
|
|
116
|
+
# first). Only :send un-stops a stopped session or starts an archived one.
|
|
117
|
+
# @return [Symbol]
|
|
118
|
+
def wake_kind(client_id)
|
|
119
|
+
id = client_id.to_s
|
|
120
|
+
return :send if id == CLI_SEND || PERSON_WAKE_PREFIXES.any? { |prefix| id.start_with?(prefix) }
|
|
121
|
+
return :plugin if id == PLUGIN
|
|
122
|
+
|
|
123
|
+
WAKE_PREFIXES.each { |prefix, kind| return kind if id.start_with?(prefix) }
|
|
124
|
+
:automatic
|
|
125
|
+
end
|
|
126
|
+
|
|
96
127
|
# The client id a participant's turn carries (chi web's, from their
|
|
97
128
|
# grant): review:<participant id>.
|
|
98
129
|
def review(participant_id) = "#{REVIEW_PREFIX}#{participant_id}"
|
data/lib/samagotchi/compat.rb
CHANGED
|
@@ -6,8 +6,44 @@ require_relative "version"
|
|
|
6
6
|
module Samagotchi
|
|
7
7
|
# The compatibility decision for state and bridge data from a chi version.
|
|
8
8
|
module Compat
|
|
9
|
+
# Code kept only for another chi version's data or protocol, and the
|
|
10
|
+
# release after which it goes. +remove_after+ nil: the owner hasn't
|
|
11
|
+
# decided (kept until then). spec/compat_spec.rb fails once VERSION
|
|
12
|
+
# passes a shim's +remove_after+, naming it. Each Ruby file names its
|
|
13
|
+
# shim's id in a comment ("compat shim <id>").
|
|
14
|
+
# @!attribute files [Array<String>] where the shim lives (from the repository root)
|
|
15
|
+
Shim = Data.define(:id, :added_in, :remove_after, :files, :what)
|
|
16
|
+
|
|
17
|
+
SHIMS = [
|
|
18
|
+
Shim.new(id: :cards_v1_seed, added_in: "0.151.0", remove_after: "0.170.0",
|
|
19
|
+
files: ["lib/samagotchi/bridge/card_store.rb"],
|
|
20
|
+
what: "CardStore#seed_v1 reads a cards.json from before the turn log; its comment says a retention " \
|
|
21
|
+
"period after the release that wrote version 2 (0.151.0, 2026-11-05)"),
|
|
22
|
+
Shim.new(id: :merge_inference, added_in: "0.157.0", remove_after: "0.165.0",
|
|
23
|
+
files: ["lib/samagotchi/reply_wait/poll.rb", "lib/samagotchi/tools/delegate.rb"],
|
|
24
|
+
what: "a wait infers a merged steer when the worker wrote no merge record (a 0.156 worker)"),
|
|
25
|
+
Shim.new(id: :session_not_found_words, added_in: "0.156.0", remove_after: "0.165.0",
|
|
26
|
+
files: ["lib/samagotchi/session_access/remote.rb"],
|
|
27
|
+
what: "SESSION_NOT_FOUND also reads chi web 0.155.0's \"Session not found: X\" (its comment: compat N-1)"),
|
|
28
|
+
Shim.new(id: :remote_refusal_details, added_in: "0.122.0", remove_after: "0.165.0",
|
|
29
|
+
files: ["lib/samagotchi/session_access/remote.rb"],
|
|
30
|
+
what: "context_missing? and no_provider? read an older chi web's 404 detail words, before their error codes"),
|
|
31
|
+
Shim.new(id: :retired_class_file_hooks, added_in: "0.158.0", remove_after: "0.166.0",
|
|
32
|
+
files: ["lib/samagotchi/extension_load.rb", "lib/samagotchi/memory_bundle/status.rb",
|
|
33
|
+
"lib/samagotchi/bundle_command/installed_check.rb"],
|
|
34
|
+
what: "a 0.157 install record's class-file hooks: announced as not loading, shown and checked until " \
|
|
35
|
+
"an upgrade or uninstall clears them (installed state, not protocol)")
|
|
36
|
+
].freeze
|
|
37
|
+
|
|
9
38
|
module_function
|
|
10
39
|
|
|
40
|
+
# The shims +version+ has passed the remove_after of.
|
|
41
|
+
# @return [Array<Shim>]
|
|
42
|
+
def overdue_shims(version: VERSION, shims: SHIMS)
|
|
43
|
+
current = Gem::Version.new(version)
|
|
44
|
+
shims.select { |shim| shim.remove_after && current > Gem::Version.new(shim.remove_after) }
|
|
45
|
+
end
|
|
46
|
+
|
|
11
47
|
def verdict(worker_version:, version: VERSION, worker_protocol: nil, protocol: nil)
|
|
12
48
|
return :refused if protocol && worker_protocol.to_i > protocol
|
|
13
49
|
|
|
@@ -130,6 +130,9 @@ module Samagotchi
|
|
|
130
130
|
rescue ContextSources::Invalid => e
|
|
131
131
|
error_line("#{command_name}: #{e.message}")
|
|
132
132
|
CLI::Exit::FAILED
|
|
133
|
+
rescue MemoryPaths::ProjectIdConflict => e
|
|
134
|
+
error_line("#{command_name}: #{e.message}")
|
|
135
|
+
CLI::Exit::FAILED
|
|
133
136
|
end
|
|
134
137
|
|
|
135
138
|
private
|
|
@@ -149,8 +149,9 @@ module Samagotchi
|
|
|
149
149
|
# installed (they run outside the guardrails gate, as a plugin loads:
|
|
150
150
|
# checked the same way).
|
|
151
151
|
# @return [String]
|
|
152
|
-
# @raise [Invalid] the bundle is gone, a script
|
|
153
|
-
# doesn't meet the
|
|
152
|
+
# @raise [Invalid] the bundle is gone, a script is gone, unrecorded or
|
|
153
|
+
# changed (Provenance#script_failure), or this chi doesn't meet the
|
|
154
|
+
# bundle's requires_chi
|
|
154
155
|
def command_for(source)
|
|
155
156
|
return source.cmd unless source.provider
|
|
156
157
|
|
|
@@ -165,10 +166,8 @@ module Samagotchi
|
|
|
165
166
|
end
|
|
166
167
|
|
|
167
168
|
record.scripts.each do |file, sha|
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
raise Invalid, "bundle #{source.provider}'s scripts/#{file} differs from the installed one (reinstall the bundle)"
|
|
169
|
+
failure = provenance.script_failure(file, sha)
|
|
170
|
+
raise Invalid, failure if failure
|
|
172
171
|
end
|
|
173
172
|
source.cmd.gsub(BUNDLE_DIR) { Shellwords.escape(provenance.bundle_dir) }.gsub(RUBY) { Shellwords.escape(RbConfig.ruby) }
|
|
174
173
|
end
|
|
@@ -636,10 +636,10 @@ module Samagotchi
|
|
|
636
636
|
# @param project_root [String, nil] Session#project_root
|
|
637
637
|
# @param project_id [String, nil] Session#project_id (a marked project's
|
|
638
638
|
# id keys its folder: MemoryPaths.key_for)
|
|
639
|
-
def project_location_for(project_root, project_id: nil, state_dir: nil)
|
|
639
|
+
def project_location_for(project_root, project_id: nil, state_dir: nil, env: ENV)
|
|
640
640
|
return nil if project_root.to_s.empty?
|
|
641
641
|
|
|
642
|
-
project_location(MemoryPaths.key_for(root: project_root, id: project_id), state_dir: state_dir)
|
|
642
|
+
project_location(MemoryPaths.key_for(root: project_root, id: project_id, env: env), state_dir: state_dir)
|
|
643
643
|
end
|
|
644
644
|
|
|
645
645
|
# A session's sources: its own, then the project's it doesn't shadow
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "atomic_file"
|
|
6
|
+
require_relative "session"
|
|
7
|
+
|
|
8
|
+
module Samagotchi
|
|
9
|
+
# The files a continue (SessionContinue.run) keeps in the previous link's
|
|
10
|
+
# folder while it runs: LOCK, held for the whole continue, and FILE, its
|
|
11
|
+
# intent (the successor's id), written before the link is archived and
|
|
12
|
+
# removed once the successor is saved or the continue is undone. A send
|
|
13
|
+
# (SessionInbox::Head) and a schedule tick (Schedules#continuing_reason)
|
|
14
|
+
# read them to tell a continue in flight from a link nobody continues.
|
|
15
|
+
module ContinueIntent
|
|
16
|
+
# One continue of a session at a time.
|
|
17
|
+
LOCK = "continue.lock"
|
|
18
|
+
# The intent: { successor_id:, previously_archived:, archived_ids: }.
|
|
19
|
+
FILE = "continue.json"
|
|
20
|
+
|
|
21
|
+
# The intent as read: the successor's id (nil when it names no valid
|
|
22
|
+
# one), the ids archived before the continue, and the ones it archived
|
|
23
|
+
# (nil when the file has no list: written before the archive).
|
|
24
|
+
# CROSS-VERSION: another chi's repair reads this file
|
|
25
|
+
# (spec/compat/readers_spec.rb).
|
|
26
|
+
Intent = Data.define(:successor_id, :previously_archived, :archived_ids)
|
|
27
|
+
|
|
28
|
+
module_function
|
|
29
|
+
|
|
30
|
+
# Write the intent at +path+ (the continue's one writer).
|
|
31
|
+
# @param archived_ids [Array<String>, nil] nil: not archived yet (not written)
|
|
32
|
+
def write(path, successor_id:, previously_archived:, archived_ids: nil)
|
|
33
|
+
data = { successor_id: successor_id, previously_archived: previously_archived, archived_ids: archived_ids }.compact
|
|
34
|
+
AtomicFile.write(path, "#{JSON.generate(data)}\n", perm: 0o600)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# The intent at +path+; a file that isn't a JSON object reads as one
|
|
38
|
+
# naming nothing.
|
|
39
|
+
# @return [Intent]
|
|
40
|
+
# @raise [JSON::ParserError, SystemCallError] it doesn't parse, or is gone
|
|
41
|
+
def read(path)
|
|
42
|
+
data = JSON.parse(File.read(path))
|
|
43
|
+
data = {} unless data.is_a?(Hash)
|
|
44
|
+
successor = data["successor_id"]
|
|
45
|
+
archived = data["archived_ids"].is_a?(Array) ? data["archived_ids"].select { |id| Session.valid_id?(id) } : nil
|
|
46
|
+
Intent.new(successor_id: Session.valid_id?(successor) ? successor : nil,
|
|
47
|
+
previously_archived: Array(data["previously_archived"]).select { |id| Session.valid_id?(id) },
|
|
48
|
+
archived_ids: archived)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# The successor's id of a continue of +id+ in flight: its intent written
|
|
52
|
+
# and its lock held. nil when there is none, or the intent is stale (no
|
|
53
|
+
# continue holds the lock: a crash left it, and
|
|
54
|
+
# SessionContinue.repair_interrupted undoes it) or unreadable.
|
|
55
|
+
# @return [String, nil]
|
|
56
|
+
def in_flight(id, state_dir:)
|
|
57
|
+
return nil unless Session.valid_id?(id)
|
|
58
|
+
|
|
59
|
+
dir = Session.session_dir(id, state_dir: state_dir)
|
|
60
|
+
path = File.join(dir, FILE)
|
|
61
|
+
return nil unless File.file?(path) && locked?(File.join(dir, LOCK))
|
|
62
|
+
|
|
63
|
+
read(path).successor_id
|
|
64
|
+
rescue JSON::ParserError, SystemCallError
|
|
65
|
+
nil
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The block under a shared hold of +id+'s continue lock, so no continue
|
|
69
|
+
# of it starts until the block returns (its exclusive lock waits); the
|
|
70
|
+
# block's argument is false when a continue holds the lock now (not
|
|
71
|
+
# waited for: nothing is held then). A followed send's check and the
|
|
72
|
+
# resume that would un-archive the link run inside it
|
|
73
|
+
# (SessionManager.deliver_turn's follow:).
|
|
74
|
+
# @yieldparam held [Boolean]
|
|
75
|
+
def hold(id, state_dir:)
|
|
76
|
+
return yield(false) unless Session.valid_id?(id)
|
|
77
|
+
|
|
78
|
+
dir = Session.session_dir(id, state_dir: state_dir)
|
|
79
|
+
FileUtils.mkdir_p(dir)
|
|
80
|
+
File.open(File.join(dir, LOCK), File::RDWR | File::CREAT, 0o600) do |lock|
|
|
81
|
+
yield(lock.flock(File::LOCK_SH | File::LOCK_NB) ? true : false)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Whether a continue holds the lock at +path+: a shared try that fails.
|
|
86
|
+
# The try is let go at once (the file is closed).
|
|
87
|
+
def locked?(path)
|
|
88
|
+
File.open(path, File::RDONLY) { |lock| !lock.flock(File::LOCK_SH | File::LOCK_NB) }
|
|
89
|
+
rescue Errno::ENOENT
|
|
90
|
+
false
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
data/lib/samagotchi/events.rb
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
module Samagotchi
|
|
4
4
|
# The sets of event types more than one place reads (the Worker, the
|
|
5
5
|
# Bridge's observers, the TUI, the web), kept here once. The web's own
|
|
6
|
-
# copies (app.js's stream handlers
|
|
7
|
-
#
|
|
6
|
+
# copies (app.js's stream handlers) are checked against these by
|
|
7
|
+
# spec/events_drift_spec.rb.
|
|
8
8
|
module Events
|
|
9
9
|
# A turn is over: it completed, was canceled, or failed.
|
|
10
10
|
TURN_END = %i[turn_completed turn_canceled turn_failed].freeze
|
|
@@ -17,8 +17,8 @@ module Samagotchi
|
|
|
17
17
|
# turn's drawing: a join takes its cards from CardStore).
|
|
18
18
|
PART_KINDS = %w[generation thinking text tool input steer reminder notice card].freeze
|
|
19
19
|
# Types no Engine emits that a UI handles: the Bridge's stream frames
|
|
20
|
-
# and the
|
|
21
|
-
#
|
|
20
|
+
# and the one synthetic replay event (a prompt merged into the turn,
|
|
21
|
+
# TurnAccumulator.replay_events).
|
|
22
22
|
STREAM_FRAMES = %i[snapshot reset stream_closed].freeze
|
|
23
23
|
SYNTHETIC = %i[merged_input].freeze
|
|
24
24
|
|
|
@@ -35,7 +35,8 @@ module Samagotchi
|
|
|
35
35
|
# The hooks: config.yml's (Hooks::Loader; an empty Registry without a
|
|
36
36
|
# hooks config). An installed record that still has class-file hooks (a
|
|
37
37
|
# 0.157 install) adds a not-required failure each, which the Engine
|
|
38
|
-
# announces at the first turn: they don't load (#tell_retired_hooks
|
|
38
|
+
# announces at the first turn: they don't load (#tell_retired_hooks;
|
|
39
|
+
# compat shim retired_class_file_hooks: Compat::SHIMS).
|
|
39
40
|
# @return [Hooks::Registry]
|
|
40
41
|
def hooks
|
|
41
42
|
tell_retired_hooks
|
|
@@ -21,16 +21,14 @@ module Samagotchi
|
|
|
21
21
|
# the call writes (writes: true), the same declaration
|
|
22
22
|
# ProtectedPaths reads — a call is judged by what its targets say it
|
|
23
23
|
# acts on, not by its tool's name;
|
|
24
|
-
# execute, task_create
|
|
24
|
+
# a shell call (execute, task_create, or a plugin tool whose targets:
|
|
25
|
+
# carry a command): mutating git runs in a dir outside root
|
|
25
26
|
# (ShellGitDirs), or a command that isn't read-only starts outside
|
|
26
27
|
# root (its cwd:, else Dir.pwd) or names a path in the repository's
|
|
27
28
|
# other checkouts (the main one and sibling worktrees) or its
|
|
28
29
|
# common git dir; or it runs git that changes what all checkouts
|
|
29
30
|
# share, from anywhere (SharedGit: config, update-ref, worktree add,
|
|
30
|
-
# stash clear, tags, another branch's delete).
|
|
31
|
-
# declared command: is checked for what chi can read for sure only:
|
|
32
|
-
# SharedGit, and whether the call's cwd: is outside root (its text
|
|
33
|
-
# may not be shell; Targets#rule_command).
|
|
31
|
+
# stash clear, tags, another branch's delete).
|
|
34
32
|
# Paths elsewhere in a command (/usr/bin, ~/.gem) don't count, and
|
|
35
33
|
# read-only commands never ask, nor do ones that only write into a tmp
|
|
36
34
|
# dir (> / >>, cp's destination, tee; Targets#read_only_but_tmp_writes?). Paths resolve under chi's env and the
|
|
@@ -41,8 +39,6 @@ module Samagotchi
|
|
|
41
39
|
RULE = "child-boundary"
|
|
42
40
|
SOURCE = "core"
|
|
43
41
|
SCOPES = %w[once session].freeze
|
|
44
|
-
SHELL_TOOLS = %w[execute task_create].freeze
|
|
45
|
-
|
|
46
42
|
# @param root [#call] → String, nil: the child's folder (nil: no check)
|
|
47
43
|
# @param worktrees [#call] → Array<String>: every checkout of root's
|
|
48
44
|
# repository (GitInfo#worktrees); root's own is left out here
|
|
@@ -74,15 +70,15 @@ module Samagotchi
|
|
|
74
70
|
def crosses?(targets, root)
|
|
75
71
|
case targets.tool
|
|
76
72
|
when "write", "edit" then paths_cross?(targets, root)
|
|
77
|
-
when *SHELL_TOOLS then shell_crosses?(targets, root)
|
|
78
73
|
else
|
|
74
|
+
return true if !Targets::CORE_TOOLS.include?(targets.tool) && targets.writes? && paths_cross?(targets, root)
|
|
75
|
+
return shell_crosses?(targets, root) if targets.shell?
|
|
76
|
+
|
|
79
77
|
# chi's other own tools (read, memory_write, …) are unchanged:
|
|
80
78
|
# memory_write carries writes: for ProtectedPaths, but its paths
|
|
81
79
|
# are memory files (Outside exempts them) and its `..` entry names
|
|
82
80
|
# asked for nothing here before.
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
plugin_crosses?(targets, root)
|
|
81
|
+
false
|
|
86
82
|
end
|
|
87
83
|
end
|
|
88
84
|
|
|
@@ -90,22 +86,6 @@ module Samagotchi
|
|
|
90
86
|
targets.paths.any? { |path| Outside.outside?(path, root: root) }
|
|
91
87
|
end
|
|
92
88
|
|
|
93
|
-
# A plugin tool, by what its targets: say it acts on (Targets#writes?,
|
|
94
|
-
# #command, #cwd), not by its name: paths its targets say it writes
|
|
95
|
-
# like a write's, and a declared command like a shell's — but for the
|
|
96
|
-
# command only what chi can read for sure: git that changes what every
|
|
97
|
-
# checkout shares (SharedGit), and where the call runs (its cwd:). A
|
|
98
|
-
# plugin's command text may not be shell (Targets#rule_command), so
|
|
99
|
-
# the shell half's git-dir, cd and redirect reads are left out; a
|
|
100
|
-
# plugin that wants more can bring a rule of its own.
|
|
101
|
-
def plugin_crosses?(targets, root)
|
|
102
|
-
return true if targets.writes? && paths_cross?(targets, root)
|
|
103
|
-
return :shared if targets.command && SharedGit.changes?(targets.command, own_branch: @branch.call)
|
|
104
|
-
return true if targets.command && targets.cwd && Outside.outside?(targets.cwd, root: root)
|
|
105
|
-
|
|
106
|
-
false
|
|
107
|
-
end
|
|
108
|
-
|
|
109
89
|
def shell_crosses?(targets, root)
|
|
110
90
|
return true if targets.git_dirs.any? { |dir| dir.is_a?(String) && Outside.outside?(dir, root: root) }
|
|
111
91
|
return :shared if SharedGit.changes?(targets.command, own_branch: @branch.call)
|
|
@@ -9,10 +9,10 @@ module Samagotchi
|
|
|
9
9
|
module Guardrails
|
|
10
10
|
# Declarative rules from config.yml's `guardrails:` section (and, later,
|
|
11
11
|
# installed bundles). All the fields a rule gives must match:
|
|
12
|
-
# tool: a tool name, a list, or "shell" (
|
|
13
|
-
#
|
|
14
|
-
#
|
|
15
|
-
# acts as (Targets#acts_as
|
|
12
|
+
# tool: a tool name, a list, or "shell" (a shell call, including a
|
|
13
|
+
# plugin tool whose targets: declare a command); a name may be
|
|
14
|
+
# a glob ("mcp_*", "mcp_{git,gh}_*"); it matches the call's
|
|
15
|
+
# name or the tool a plugin tool's call acts as (Targets#acts_as)
|
|
16
16
|
# command: a Ruby regex on a shell tool's command, the body of a
|
|
17
17
|
# heredoc its command only reads as data left out
|
|
18
18
|
# (Targets#rule_command, ShellLex.code_text)
|
|
@@ -60,12 +60,13 @@ module Samagotchi
|
|
|
60
60
|
MODES = %w[auto strict].freeze
|
|
61
61
|
GLOB_FLAGS = File::FNM_PATHNAME | File::FNM_DOTMATCH | File::FNM_EXTGLOB
|
|
62
62
|
|
|
63
|
-
Rule = Struct.new(:id, :tools, :command, :path, :git, :touches, :rm, :memory, :models, :modes, :skip_read_only, :verdict, :reason,
|
|
63
|
+
Rule = Struct.new(:id, :tools, :shell, :command, :path, :git, :touches, :rm, :memory, :models, :modes, :skip_read_only, :verdict, :reason,
|
|
64
64
|
:scopes, :source, keyword_init: true) do
|
|
65
65
|
def matches?(targets)
|
|
66
66
|
return false unless targets
|
|
67
67
|
return false unless for_model?(targets.model_name, targets.model_key, -> { targets.small_model? })
|
|
68
|
-
return false if tools && !tool_matches?(targets.tool) && !(targets.acts_as && tool_matches?(targets.acts_as))
|
|
68
|
+
return false if tools && !tool_matches?(targets.tool) && !(targets.acts_as && tool_matches?(targets.acts_as)) &&
|
|
69
|
+
!(shell && targets.shell?)
|
|
69
70
|
return false if command && !(targets.rule_command && command.match?(targets.rule_command))
|
|
70
71
|
return false if path && !path_matches?(targets)
|
|
71
72
|
return false if git && !targets.git_outside_repo?
|
|
@@ -158,7 +159,8 @@ module Samagotchi
|
|
|
158
159
|
raise ParseError, "#{label}: give at least one of #{MATCH_KEYS.join(", ")}"
|
|
159
160
|
end
|
|
160
161
|
|
|
161
|
-
|
|
162
|
+
tools, shell = tools_of(raw["tool"], label)
|
|
163
|
+
Rule.new(id: id, tools: tools, shell: shell, command: regex_of(raw["command"], label),
|
|
162
164
|
path: path_of(raw["path"], label), git: git_of(raw["git"], label),
|
|
163
165
|
touches: touches_of(raw["touches"], label), rm: rm_of(raw["rm"], label), memory: memory_of(raw["memory"], label),
|
|
164
166
|
models: models_of(raw["models"], label),
|
|
@@ -168,12 +170,12 @@ module Samagotchi
|
|
|
168
170
|
end
|
|
169
171
|
|
|
170
172
|
def self.tools_of(value, label)
|
|
171
|
-
return nil if value.nil?
|
|
173
|
+
return [nil, false] if value.nil?
|
|
172
174
|
|
|
173
175
|
names = Array(value).map(&:to_s)
|
|
174
176
|
raise ParseError, "#{label}: tool must be a name or a list of names" if names.empty? || names.any?(&:empty?)
|
|
175
177
|
|
|
176
|
-
names.flat_map { |n| n == "shell" ? Targets::SHELL_TOOLS : [n] }.uniq
|
|
178
|
+
[names.flat_map { |n| n == "shell" ? Targets::SHELL_TOOLS : [n] }.uniq, names.include?("shell")]
|
|
177
179
|
end
|
|
178
180
|
|
|
179
181
|
# Whether a rule's tool name is a glob.
|
|
@@ -357,7 +357,7 @@ module Samagotchi
|
|
|
357
357
|
@repo_root = repo_root
|
|
358
358
|
end
|
|
359
359
|
|
|
360
|
-
def shell? = SHELL_TOOLS.include?(@tool)
|
|
360
|
+
def shell? = SHELL_TOOLS.include?(@tool) || !@command.nil?
|
|
361
361
|
|
|
362
362
|
# The homes and environments a shell call's words may resolve
|
|
363
363
|
# against, as [home, env] pairs: chi's own, and when the call's env:
|
|
@@ -381,9 +381,8 @@ module Samagotchi
|
|
|
381
381
|
@redefines_shell = self.class.redefines_shell?(@env)
|
|
382
382
|
end
|
|
383
383
|
|
|
384
|
-
# What a rule's command: reads:
|
|
385
|
-
# heredoc its command only reads as data left out (ShellLex.code_text)
|
|
386
|
-
# a plugin tool's own command as given (its text may not be shell).
|
|
384
|
+
# What a rule's command: reads: a shell command with the body of each
|
|
385
|
+
# heredoc its command only reads as data left out (ShellLex.code_text).
|
|
387
386
|
# Approvals, the approval question and hooks keep #command.
|
|
388
387
|
def rule_command
|
|
389
388
|
return @rule_command if defined?(@rule_command)
|
|
@@ -46,6 +46,9 @@ module Samagotchi
|
|
|
46
46
|
# The deepest legal block nests 6 (root, steps, a step, cases, a case, a
|
|
47
47
|
# folded option); past this it is refused before the stack can overflow.
|
|
48
48
|
MAX_DEPTH = 8
|
|
49
|
+
# What a row id may not start with: a shell line's ! and a session
|
|
50
|
+
# command's / (CommandIntake), with the spaces between them.
|
|
51
|
+
COMMAND_LEAD = %r{\A[!/\s]+}
|
|
49
52
|
# How much of a bad type value an error repeats.
|
|
50
53
|
MAX_ECHO = 40
|
|
51
54
|
|
|
@@ -212,7 +215,7 @@ module Samagotchi
|
|
|
212
215
|
where = "row #{index + 1}"
|
|
213
216
|
raise Refused, "#{where} isn't a mapping" unless row.is_a?(Hash)
|
|
214
217
|
|
|
215
|
-
id = text(row["id"], "#{where}'s id", required: true)
|
|
218
|
+
id = row_id(text(row["id"], "#{where}'s id", required: true), where)
|
|
216
219
|
where = "row #{id}"
|
|
217
220
|
options = list(row["options"], "#{where}'s options")
|
|
218
221
|
raise Refused, "#{where} has #{options.size} options (#{OPTIONS.min}-#{OPTIONS.max})" unless OPTIONS.cover?(options.size)
|
|
@@ -230,6 +233,18 @@ module Samagotchi
|
|
|
230
233
|
"recommended" => recommended, "why" => text(row["why"], "#{where}'s why", free: true))
|
|
231
234
|
end
|
|
232
235
|
|
|
236
|
+
# A row's id leads the page's filled reply ("<id>: <option>"), so it never
|
|
237
|
+
# starts with ! or / (a shell line, a session command): those are dropped,
|
|
238
|
+
# a mend like a fold, and an id that is nothing else is refused.
|
|
239
|
+
def row_id(id, where)
|
|
240
|
+
safe = id.sub(COMMAND_LEAD, "")
|
|
241
|
+
return id if safe == id
|
|
242
|
+
raise Refused, "#{where}'s id is only ! and /" if safe.empty?
|
|
243
|
+
|
|
244
|
+
@folded = true
|
|
245
|
+
safe
|
|
246
|
+
end
|
|
247
|
+
|
|
233
248
|
def walk(tree)
|
|
234
249
|
title = text(tree["title"], "the title", required: true, free: true)
|
|
235
250
|
actors = list(tree["actors"], "actors").map { |actor| text(actor, "an actor", required: true) }
|
|
@@ -66,6 +66,21 @@ module Samagotchi
|
|
|
66
66
|
]
|
|
67
67
|
)
|
|
68
68
|
|
|
69
|
+
# pending.json's format (#write_pending), in its pending_format key:
|
|
70
|
+
# its version key is the bundle's. A journal with no pending_format is
|
|
71
|
+
# one from before it (the same members); a newer chi's keeps its
|
|
72
|
+
# format and the keys this one doesn't know through a write.
|
|
73
|
+
PENDING_FORMAT_VERSION = 1
|
|
74
|
+
PENDING_FORMAT = StateFormat.new(
|
|
75
|
+
version: PENDING_FORMAT_VERSION,
|
|
76
|
+
members: [
|
|
77
|
+
StateFormat.required(:scope),
|
|
78
|
+
StateFormat.required(:target_dir),
|
|
79
|
+
StateFormat.added(:version, in_version: 1, default: nil),
|
|
80
|
+
StateFormat.added(:files, in_version: 1, default: {})
|
|
81
|
+
]
|
|
82
|
+
)
|
|
83
|
+
|
|
69
84
|
def initialize(name:)
|
|
70
85
|
@name = name
|
|
71
86
|
@bundle_dir = File.join(self.class.bundles_dir, name)
|
|
@@ -98,6 +113,19 @@ module Samagotchi
|
|
|
98
113
|
File.join(@bundle_dir, "scripts")
|
|
99
114
|
end
|
|
100
115
|
|
|
116
|
+
# Why the installed scripts/+file+ isn't as installed, nil when it is:
|
|
117
|
+
# the record has no sha for it (+recorded+), it is gone, or it changed.
|
|
118
|
+
# The one check a provider's command and a block renderer's load make
|
|
119
|
+
# before chi runs or serves the file; a --dev install's links pass.
|
|
120
|
+
def script_failure(file, recorded)
|
|
121
|
+
path = File.join(scripts_dir, file)
|
|
122
|
+
return "bundle #{@name}'s scripts/#{file} isn't among its installed scripts (reinstall the bundle)" if self.class.recorded_sha(recorded).empty?
|
|
123
|
+
return "bundle #{@name}'s scripts/#{file} is missing (reinstall the bundle)" unless File.file?(path)
|
|
124
|
+
return nil if self.class.sha_matches?(path, recorded)
|
|
125
|
+
|
|
126
|
+
"bundle #{@name}'s scripts/#{file} differs from the installed one (reinstall the bundle)"
|
|
127
|
+
end
|
|
128
|
+
|
|
101
129
|
# The intent journal of a run that has not written its record yet
|
|
102
130
|
# (#write_pending): its scope and target dir, plus the files and sha256s
|
|
103
131
|
# of the texts a run meant to put there. #write removes it.
|
|
@@ -112,9 +140,14 @@ module Samagotchi
|
|
|
112
140
|
def write_pending(version:, scope:, target_dir:, files:)
|
|
113
141
|
shas = pending_files(target_dir: target_dir)
|
|
114
142
|
files.each { |key, sha| shas[key] = (shas.fetch(key, []) + [sha]).uniq }
|
|
143
|
+
target = File.expand_path(target_dir)
|
|
144
|
+
earlier = pending_metadata
|
|
145
|
+
kept = earlier && earlier["target_dir"] == target ? earlier.except("version", "scope", "target_dir", "files") : {}
|
|
146
|
+
format = [kept.delete("pending_format").to_i, PENDING_FORMAT_VERSION].max
|
|
115
147
|
FileUtils.mkdir_p(@bundle_dir)
|
|
116
148
|
AtomicFile.write(pending_path, JSON.pretty_generate(
|
|
117
|
-
"version" => version.to_s, "scope" => scope.to_s, "target_dir" =>
|
|
149
|
+
kept.merge("pending_format" => format, "version" => version.to_s, "scope" => scope.to_s, "target_dir" => target,
|
|
150
|
+
"files" => shas)
|
|
118
151
|
))
|
|
119
152
|
end
|
|
120
153
|
|
|
@@ -571,12 +604,21 @@ module Samagotchi
|
|
|
571
604
|
files.is_a?(Hash) ? files.to_h { |key, shas| [key, Array(shas).grep(String)] } : {}
|
|
572
605
|
end
|
|
573
606
|
|
|
607
|
+
# The journal as PENDING_FORMAT reads it, string keys, its format under
|
|
608
|
+
# pending_format; nil when there is none or it doesn't read.
|
|
574
609
|
def pending_metadata
|
|
575
610
|
data = JSON.parse(File.read(pending_path))
|
|
576
|
-
return unless data.is_a?(Hash)
|
|
611
|
+
return unless data.is_a?(Hash)
|
|
612
|
+
|
|
613
|
+
format = data["pending_format"]
|
|
614
|
+
format = nil unless format.is_a?(Integer) && format >= 0
|
|
615
|
+
document = PENDING_FORMAT.read({ "version" => format, "journals" => [data] }, collection: "journals")
|
|
616
|
+
journal = document.entries.first
|
|
617
|
+
return unless journal.readable?
|
|
618
|
+
return unless journal.attributes[:scope].is_a?(String) && journal.attributes[:target_dir].is_a?(String)
|
|
577
619
|
|
|
578
|
-
|
|
579
|
-
rescue JSON::ParserError, EncodingError, SystemCallError
|
|
620
|
+
journal.to_h.merge("pending_format" => document.version)
|
|
621
|
+
rescue JSON::ParserError, EncodingError, SystemCallError, StateFormat::InvalidDocument
|
|
580
622
|
nil
|
|
581
623
|
end
|
|
582
624
|
private :pending_files, :pending_metadata
|
|
@@ -85,7 +85,8 @@ module Samagotchi
|
|
|
85
85
|
|
|
86
86
|
# The class-file hooks the record still has (a 0.157 install's; chi
|
|
87
87
|
# loads none, Manifest::HooksRetired), each "present" or "missing" in
|
|
88
|
-
# hooks/, by name; {} for none
|
|
88
|
+
# hooks/, by name; {} for none (compat shim retired_class_file_hooks:
|
|
89
|
+
# Compat::SHIMS).
|
|
89
90
|
# @return [Hash{String => String}]
|
|
90
91
|
def self.retired_hooks(provenance, bundle)
|
|
91
92
|
bundle.hooks.keys.sort.to_h do |file|
|
|
@@ -96,6 +96,9 @@ module Samagotchi
|
|
|
96
96
|
return usage_error("unknown scope #{scope.inspect}: use --scope project|system") if scope && !SCOPES.include?(scope)
|
|
97
97
|
|
|
98
98
|
send(:"run_#{@sub}", parsed.options, parsed.args)
|
|
99
|
+
rescue MemoryPaths::ProjectIdConflict => e
|
|
100
|
+
error_line("#{command_name}: #{e.message}")
|
|
101
|
+
CLI::Exit::FAILED
|
|
99
102
|
end
|
|
100
103
|
|
|
101
104
|
# The memories of +scope+ (project first, then system, when nil), by
|
|
@@ -24,10 +24,6 @@ module Samagotchi
|
|
|
24
24
|
# @!attribute computed_exists [Boolean] whether +computed+ is a folder (empty of memories)
|
|
25
25
|
Found = Data.define(:computed, :candidates, :computed_exists)
|
|
26
26
|
|
|
27
|
-
# A project keyed by its path is <name>_<8 hex>; a marked project's folder is
|
|
28
|
-
# its id, <name>-<8 hex>.
|
|
29
|
-
PATH_HASH = /_\h{8}\z/
|
|
30
|
-
|
|
31
27
|
module_function
|
|
32
28
|
|
|
33
29
|
# A candidate folder's key, in words: an earlier path's hash, or a chi project
|
|
@@ -37,8 +33,9 @@ module Samagotchi
|
|
|
37
33
|
hash_keyed?(candidate) ? "another path's hash" : "project id #{File.basename(candidate)}"
|
|
38
34
|
end
|
|
39
35
|
|
|
40
|
-
# Internal: is this candidate keyed by its path
|
|
41
|
-
|
|
36
|
+
# Internal: is this candidate keyed by its path (<name>_<8 hex>) rather
|
|
37
|
+
# than by a project id (<name>-<8 hex>)?
|
|
38
|
+
def hash_keyed?(candidate) = MemoryPaths.path_key_shape?(File.basename(candidate))
|
|
42
39
|
private_class_method :hash_keyed?
|
|
43
40
|
|
|
44
41
|
# The orphan candidates for +cwd+'s project, or nil: only inside a
|
|
@@ -66,7 +63,7 @@ module Samagotchi
|
|
|
66
63
|
return nil if candidates.empty?
|
|
67
64
|
|
|
68
65
|
Found.new(computed: computed, candidates: candidates, computed_exists: File.directory?(computed))
|
|
69
|
-
rescue SystemCallError
|
|
66
|
+
rescue MemoryPaths::ProjectIdConflict, SystemCallError
|
|
70
67
|
nil
|
|
71
68
|
end
|
|
72
69
|
|