samagotchi 0.191.0 → 0.192.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 +40 -1
- data/docs/architecture.md +5 -4
- data/docs/bundles.md +1 -1
- data/docs/cli.md +15 -7
- data/docs/configuration.md +6 -1
- data/docs/internals/inline-widgets.md +2 -1
- data/docs/internals/participants.md +1 -5
- data/docs/internals/remote-sessions.md +3 -0
- data/docs/internals/session-lifecycle.md +33 -19
- data/docs/internals/web-frontend.md +107 -9
- data/docs/scheduling.md +7 -1
- data/docs/sessions.md +2 -2
- data/docs/testing.md +1 -2
- data/docs/thin-clients.md +98 -12
- data/lib/samagotchi/bridge.rb +2 -3
- data/lib/samagotchi/bundles/mcp/manifest.yml +4 -2
- data/lib/samagotchi/bundles/mcp/plugin.rb +7 -398
- data/lib/samagotchi/bundles/mcp/server_config.rb +158 -0
- data/lib/samagotchi/bundles/mcp/sign_in.rb +261 -0
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +2 -19
- data/lib/samagotchi/compat.rb +1 -7
- data/lib/samagotchi/config.rb +3 -0
- data/lib/samagotchi/delivery.rb +1 -5
- data/lib/samagotchi/schedule_command.rb +1 -0
- data/lib/samagotchi/schedules/prune_part.rb +32 -0
- data/lib/samagotchi/schedules.rb +14 -9
- data/lib/samagotchi/session.rb +7 -0
- data/lib/samagotchi/session_access/remote.rb +1 -1
- data/lib/samagotchi/session_continue.rb +8 -1
- data/lib/samagotchi/session_lifecycle.rb +4 -0
- data/lib/samagotchi/session_manager/delivery.rb +0 -6
- data/lib/samagotchi/session_manager/lifecycle.rb +30 -17
- data/lib/samagotchi/session_manager/listing.rb +2 -1
- data/lib/samagotchi/session_manager/removal.rb +7 -5
- data/lib/samagotchi/session_manager/spawn.rb +2 -3
- data/lib/samagotchi/session_manager.rb +4 -0
- data/lib/samagotchi/session_row.rb +2 -0
- data/lib/samagotchi/session_transition.rb +61 -15
- data/lib/samagotchi/tools/cancel_schedule.rb +10 -6
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +3 -1
- data/lib/samagotchi/web/ask_create.rb +138 -0
- data/lib/samagotchi/web/ask_routes.rb +56 -9
- data/lib/samagotchi/web/last_ask.rb +78 -0
- data/lib/samagotchi/web/public/answer_refs.js +3 -9
- data/lib/samagotchi/web/public/answer_write.js +28 -0
- data/lib/samagotchi/web/public/app.js +72 -96
- data/lib/samagotchi/web/public/block_fix.js +30 -12
- data/lib/samagotchi/web/public/chains.js +3 -1
- data/lib/samagotchi/web/public/command_list.js +5 -3
- data/lib/samagotchi/web/public/composer.js +9 -6
- data/lib/samagotchi/web/public/composer_mount.js +18 -11
- data/lib/samagotchi/web/public/draft_keys.js +10 -0
- data/lib/samagotchi/web/public/drafts.js +278 -0
- data/lib/samagotchi/web/public/owner_claim.js +7 -12
- data/lib/samagotchi/web/public/prompt_history.js +6 -0
- data/lib/samagotchi/web/public/send_turn.js +51 -20
- data/lib/samagotchi/web/public/session_actions.js +80 -16
- data/lib/samagotchi/web/public/session_prefs.js +9 -3
- data/lib/samagotchi/web/public/session_view.js +7 -2
- data/lib/samagotchi/web/public/turn_events.js +0 -2
- data/lib/samagotchi/web/public/unqueue.js +22 -9
- data/lib/samagotchi/web/public/viewer_self.js +1 -6
- data/lib/samagotchi/web/public/views.js +12 -1
- data/lib/samagotchi/web/session_access_routes.rb +6 -3
- data/lib/samagotchi/web/session_control.rb +4 -2
- data/lib/samagotchi/web/session_create.rb +50 -33
- data/lib/samagotchi/web/session_hub.rb +52 -36
- data/lib/samagotchi/web/session_summary.rb +45 -7
- data/lib/samagotchi/web/start_dir.rb +6 -4
- data/lib/samagotchi/worker_presence.rb +22 -6
- metadata +8 -1
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# The mcp bundle's sign-in plugin part (manifest.yml plugin: parts:, loaded
|
|
4
|
+
# before plugin.rb): /mcp login|logout and a 401's sign-in card, wired to
|
|
5
|
+
# the OAuth in oauth.rb.
|
|
6
|
+
require "digest"
|
|
7
|
+
require "uri"
|
|
8
|
+
|
|
9
|
+
class Plugin
|
|
10
|
+
private
|
|
11
|
+
|
|
12
|
+
# ── Sign-in (the OAuth in oauth.rb; here its wiring) ──────────────────
|
|
13
|
+
|
|
14
|
+
# Why /mcp login|logout can't act on server +name+, nil when it can.
|
|
15
|
+
def sign_in_refusal(name)
|
|
16
|
+
server = @servers.find { |s| s.name == name }
|
|
17
|
+
return "no MCP server #{name}#{" (#{@servers.map(&:name).join(", ")})" unless @servers.empty?}" unless server
|
|
18
|
+
return "#{name} is a local server: sign-in is for remote ones (url:)" unless server.url
|
|
19
|
+
return "#{name} has a static token (bearer_token_env: or an Authorization header): it doesn't sign in" unless server.signs_in
|
|
20
|
+
return "#{name}'s url: has a query: chi can't sign in to it (it would go out with the sign-in)" if URI.parse(server.url).query
|
|
21
|
+
|
|
22
|
+
config_error(server)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# /mcp login NAME: answers at once; discovery, the client, the flow and
|
|
26
|
+
# the exchange run on a thread of the plugin's (OAuth::Login), whose
|
|
27
|
+
# result or error replaces the flow's card.
|
|
28
|
+
def login(name, ctx)
|
|
29
|
+
refusal = sign_in_refusal(name)
|
|
30
|
+
return refusal if refusal
|
|
31
|
+
return "sign-in needs the web or a terminal: this run is one-shot" if ctx.frontend == :one_shot
|
|
32
|
+
if @settings.key?("client_metadata_url") && @settings["client_metadata_url"] != false && client_metadata_url.nil?
|
|
33
|
+
return "bundles: mcp: client_metadata_url: must be an https address with a path (chi's client document)"
|
|
34
|
+
end
|
|
35
|
+
return "this chi can't sign in (it has no ctx.sign_in)" unless ctx.respond_to?(:sign_in) && ctx.respond_to?(:secrets)
|
|
36
|
+
|
|
37
|
+
server = @servers.find { |s| s.name == name }
|
|
38
|
+
return "#{name} is signed in already (from another session): connected." if signed_in_elsewhere?(server, ctx)
|
|
39
|
+
|
|
40
|
+
started = @sign_in_lock.synchronize do
|
|
41
|
+
next false if @sign_ins[name]&.alive? || @sign_in_cancel.cancelled?
|
|
42
|
+
|
|
43
|
+
@sign_ins[name] = Thread.new { sign_in(server, ctx) }
|
|
44
|
+
end
|
|
45
|
+
return "a sign-in to #{name} is already waiting: finish it in the browser, or let it time out" unless started
|
|
46
|
+
|
|
47
|
+
"Opening sign-in for #{name}…"
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# The login's thread: every error ends on a card, never raised into nothing.
|
|
51
|
+
def sign_in(server, ctx)
|
|
52
|
+
login = OAuth::Login.new(ctx: ctx, server: server, secret_lookup: ->(name) { env_value(server, name) },
|
|
53
|
+
client_metadata_url: client_metadata_url, cimd_refused: @cimd_refused, cancel: @sign_in_cancel,
|
|
54
|
+
log: ->(event, **fields) { ctx.log.info("mcp_#{event}", **fields) })
|
|
55
|
+
card_id = login.run
|
|
56
|
+
signed_in(server, card_id, ctx) if card_id
|
|
57
|
+
rescue OAuth::Refusal => e
|
|
58
|
+
ctx.log.warn("mcp_sign_in_failed", server: server.name, error: e.class.name)
|
|
59
|
+
ctx.card(id: e.card_id, title: "Sign-in to #{server.name} failed", body: e.message, level: :warn, between_turns: true)
|
|
60
|
+
rescue StandardError => e
|
|
61
|
+
ctx.log.warn("mcp_sign_in_failed", server: server.name, error: e.class.name)
|
|
62
|
+
ctx.card(title: "Sign-in to #{server.name} failed", body: "chi's sign-in broke (#{e.class.name}); the debug log has it",
|
|
63
|
+
level: :warn, between_turns: true)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Kept: the server connects as a lazy start does (handshake, tools/list,
|
|
67
|
+
# the cache), with spawn directly when its service already runs (a 401
|
|
68
|
+
# met during a call, a sign-out), then the flow's card says so.
|
|
69
|
+
def signed_in(server, card_id, ctx)
|
|
70
|
+
server.token.forget
|
|
71
|
+
begin
|
|
72
|
+
reconnect(server, ctx)
|
|
73
|
+
count = offered(server).size
|
|
74
|
+
ctx.card(id: card_id, title: "Signed in to #{server.name}", body: "#{count} tool#{"s" unless count == 1}, ready to use.",
|
|
75
|
+
between_turns: true)
|
|
76
|
+
rescue StandardError => e
|
|
77
|
+
if needs_sign_in?(server, e)
|
|
78
|
+
signed_out(server, ctx, e)
|
|
79
|
+
return
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
ctx.log.warn("mcp_server_failed", server: server.name, error: e.class.name, msg: e.message, signed_in: true)
|
|
83
|
+
ctx.card(id: card_id, title: "Signed in, but #{server.name} didn't connect", body: e.message, level: :warn,
|
|
84
|
+
between_turns: true)
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# Connect a server that has a sign-in now (spawn when its service
|
|
89
|
+
# already runs, else its first start), then it runs; its "needs sign-in"
|
|
90
|
+
# card loses its button (a later refusal shows it again).
|
|
91
|
+
def reconnect(server, ctx)
|
|
92
|
+
@restart.synchronize do
|
|
93
|
+
if server.service.state == :running
|
|
94
|
+
old = server.client
|
|
95
|
+
spawn(server, ctx, cancelled: @sign_in_cancel)
|
|
96
|
+
old.close unless old.equal?(server.client)
|
|
97
|
+
else
|
|
98
|
+
server.service.value
|
|
99
|
+
end
|
|
100
|
+
@sign_in_lock.synchronize do
|
|
101
|
+
server.state = :running
|
|
102
|
+
server.error = nil
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
publish
|
|
106
|
+
return unless @sign_in_lock.synchronize { server.card_shown.tap { server.card_shown = false } }
|
|
107
|
+
|
|
108
|
+
ctx.card(id: "mcp-sign-in-#{server.name}", title: "#{server.name} needs sign-in", body: "Signed in since.", between_turns: true)
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
# A server that needs sign-in, signed in to meanwhile from another
|
|
112
|
+
# session (the kept token, read again): connected now. False when there
|
|
113
|
+
# is no kept token, or it doesn't connect.
|
|
114
|
+
def signed_in_elsewhere?(server, ctx)
|
|
115
|
+
return false unless server.state == :needs_sign_in && server.token.respond_to?(:kept)
|
|
116
|
+
|
|
117
|
+
server.token.forget
|
|
118
|
+
kept = kept_digest(server)
|
|
119
|
+
return false if kept.nil? || kept == server.refused
|
|
120
|
+
|
|
121
|
+
reconnect(server, ctx)
|
|
122
|
+
ctx.log.info("mcp_signed_in_elsewhere", server: server.name)
|
|
123
|
+
true
|
|
124
|
+
rescue StandardError => e
|
|
125
|
+
server.refused = kept_digest(server) # what it holds after a renew, too: not tried again
|
|
126
|
+
ctx.log.warn("mcp_server_failed", server: server.name, error: e.class.name, msg: e.message, signed_in_elsewhere: true)
|
|
127
|
+
false
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
# 16 hex of the SHA-256 of the kept access token, nil without one.
|
|
131
|
+
def kept_digest(server)
|
|
132
|
+
kept = server.token.respond_to?(:kept) && server.token.kept
|
|
133
|
+
kept ? Digest::SHA256.hexdigest(kept["access_token"])[0, 16] : nil
|
|
134
|
+
rescue StandardError
|
|
135
|
+
nil
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Whether the kept sign-in is newer than the token +error+ refused (or,
|
|
139
|
+
# none sent, whether there is one now): another session or a login
|
|
140
|
+
# signed in after the request went out.
|
|
141
|
+
def newer_sign_in?(server, error)
|
|
142
|
+
kept = server.token.respond_to?(:kept) && server.token.kept
|
|
143
|
+
return false unless kept
|
|
144
|
+
|
|
145
|
+
error.sent_digest.nil? || Digest::SHA256.hexdigest(kept["access_token"])[0, 16] != error.sent_digest
|
|
146
|
+
rescue StandardError
|
|
147
|
+
false
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
# /mcp logout NAME: the kept token goes (the provider's revocation asked,
|
|
151
|
+
# best effort), the client closes, the server needs sign-in. chi's
|
|
152
|
+
# registration with the provider stays.
|
|
153
|
+
def logout(name, ctx)
|
|
154
|
+
refusal = sign_in_refusal(name)
|
|
155
|
+
return refusal if refusal
|
|
156
|
+
|
|
157
|
+
server = @servers.find { |s| s.name == name }
|
|
158
|
+
kept = server.token.kept
|
|
159
|
+
ctx.secrets.delete(server.token.name)
|
|
160
|
+
server.token.forget
|
|
161
|
+
OAuth.revoke(ctx.http, kept, log: ->(event, **fields) { ctx.log.info("mcp_#{event}", server: name, **fields) }) if kept
|
|
162
|
+
server.client&.close
|
|
163
|
+
server.state = :needs_sign_in
|
|
164
|
+
publish
|
|
165
|
+
"Signed out of #{name}."
|
|
166
|
+
rescue Samagotchi::Plugin::Secrets::Unavailable, Samagotchi::Plugin::Secrets::Busy => e
|
|
167
|
+
"#{name}'s sign-in wasn't forgotten: #{e.message}"
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
# Whether +error+ means the server wants a sign-in: a 401 from one that signs in.
|
|
171
|
+
def needs_sign_in?(server, error) = server.signs_in && error.is_a?(HttpClient::Refused) && error.status == 401
|
|
172
|
+
|
|
173
|
+
# The server needs sign-in: its state (said once), its card, and the
|
|
174
|
+
# line its call answers.
|
|
175
|
+
# Nil when +error+ (the 401) was for an older token than the kept one: the
|
|
176
|
+
# state stays, the next request sends the new token.
|
|
177
|
+
def signed_out(server, ctx, error = nil)
|
|
178
|
+
if error && newer_sign_in?(server, error)
|
|
179
|
+
server.token.forget
|
|
180
|
+
return nil
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
first = @sign_in_lock.synchronize { server.state == :needs_sign_in ? false : (server.state = :needs_sign_in) }
|
|
184
|
+
if first
|
|
185
|
+
server.refused = kept_digest(server)
|
|
186
|
+
server.error = nil
|
|
187
|
+
publish
|
|
188
|
+
ctx.log.info("mcp_needs_sign_in", server: server.name)
|
|
189
|
+
end
|
|
190
|
+
offer_sign_in(server, ctx)
|
|
191
|
+
"Error: #{sign_in_line(server, ctx.turn_author)}"
|
|
192
|
+
end
|
|
193
|
+
|
|
194
|
+
# What a call of a server that needs sign-in answers: the owner's
|
|
195
|
+
# command, or (a participant's turn) who signs in, no command.
|
|
196
|
+
def sign_in_line(server, author = nil)
|
|
197
|
+
return "#{server.name} needs sign-in: the session's owner signs in" if author
|
|
198
|
+
|
|
199
|
+
"#{server.name} needs sign-in: the user runs /mcp login #{server.name}"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# The "needs sign-in" card, once per server per worker, never a turn's;
|
|
203
|
+
# none on a one-shot; on a participant's turn held for the owner's next
|
|
204
|
+
# turn end or command.
|
|
205
|
+
def offer_sign_in(server, ctx)
|
|
206
|
+
return if ctx.frontend == :one_shot
|
|
207
|
+
|
|
208
|
+
if ctx.turn_author
|
|
209
|
+
@sign_in_lock.synchronize { @held_cards << server.name unless @held_cards.include?(server.name) }
|
|
210
|
+
return
|
|
211
|
+
end
|
|
212
|
+
first = @sign_in_lock.synchronize { server.card_shown ? false : (server.card_shown = true) }
|
|
213
|
+
return unless first
|
|
214
|
+
|
|
215
|
+
ctx.card(id: "mcp-sign-in-#{server.name}", title: "#{server.name} needs sign-in",
|
|
216
|
+
body: "The server asks chi to sign in. Sign in opens its provider's page in your browser.",
|
|
217
|
+
actions: [{ label: "Sign in", command: "/mcp login #{server.name}" }], between_turns: true)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
def show_held_cards(ctx)
|
|
221
|
+
names = @sign_in_lock.synchronize { @held_cards.slice!(0..) }
|
|
222
|
+
names.each do |name|
|
|
223
|
+
server = @servers.find { |s| s.name == name }
|
|
224
|
+
offer_sign_in(server, ctx) if server&.state == :needs_sign_in
|
|
225
|
+
end
|
|
226
|
+
end
|
|
227
|
+
|
|
228
|
+
# chi's client document's address (bundles: mcp: client_metadata_url:,
|
|
229
|
+
# else CLIENT_METADATA_URL): an https address with a path, else nil
|
|
230
|
+
# (false: none, never a CIMD client_id).
|
|
231
|
+
def client_metadata_url
|
|
232
|
+
value = @settings.fetch("client_metadata_url", CLIENT_METADATA_URL)
|
|
233
|
+
return nil if value == false
|
|
234
|
+
|
|
235
|
+
uri = URI.parse(value.to_s)
|
|
236
|
+
uri.is_a?(URI::HTTPS) && !uri.host.to_s.empty? && uri.path.to_s.length > 1 && uri.userinfo.nil? ? value.to_s : nil
|
|
237
|
+
rescue URI::Error
|
|
238
|
+
nil
|
|
239
|
+
end
|
|
240
|
+
|
|
241
|
+
# The service's stop: the waiting sign-ins end (core stops their flows
|
|
242
|
+
# too), their threads get a moment to put their cards right.
|
|
243
|
+
def stop_sign_ins
|
|
244
|
+
@sign_in_cancel.cancel!
|
|
245
|
+
threads = @sign_in_lock.synchronize { @sign_ins.values }
|
|
246
|
+
threads.each { |thread| thread.join(2) }
|
|
247
|
+
end
|
|
248
|
+
|
|
249
|
+
# A remote server's token object: OAuth::Token for one that signs in
|
|
250
|
+
# (with this chi's ctx.secrets), else the static one.
|
|
251
|
+
def token_of(server, ctx)
|
|
252
|
+
return token_lookup(server) unless server.signs_in && ctx.respond_to?(:secrets)
|
|
253
|
+
|
|
254
|
+
oauth = server.oauth.is_a?(Hash) ? server.oauth : {}
|
|
255
|
+
variable = oauth["client_secret_env"]
|
|
256
|
+
client = oauth["client_id"] ? { id: oauth["client_id"], issuer: oauth["issuer"] } : nil
|
|
257
|
+
OAuth::Token.new(secrets: ctx.secrets, resource: URI.parse(server.url).query ? nil : OAuth.canonical(server.url),
|
|
258
|
+
http: ctx.http, client: client, client_secret: -> { variable && env_value(server, variable) },
|
|
259
|
+
log: ->(event, **fields) { ctx.log.info("mcp_#{event}", server: server.name, **fields) })
|
|
260
|
+
end
|
|
261
|
+
end
|
data/lib/samagotchi/client_id.rb
CHANGED
|
@@ -62,12 +62,6 @@ module Samagotchi
|
|
|
62
62
|
# their author (Steer::PARTICIPANT_PREFIX, "p:<id>"), which says who
|
|
63
63
|
# wrote a turn: this says which sender delivered it.
|
|
64
64
|
PARTICIPANT_PREFIX = "participant:"
|
|
65
|
-
# compat shim review_client_id: the legacy review:<id> participant id
|
|
66
|
-
# used before 0.172 (still in turn_drawings.jsonl, input files and an
|
|
67
|
-
# older chi web still running): read it as a participant's, never the
|
|
68
|
-
# owner's, and send it to a worker without Delivery::PARTICIPANT_ID_FEATURE.
|
|
69
|
-
LEGACY_REVIEW_PREFIX = "review:"
|
|
70
|
-
|
|
71
65
|
# A worker's own first prompt (spawn_session's) when no sender was
|
|
72
66
|
# saved for it (WorkerInbound#initial_origin: a person's session saves
|
|
73
67
|
# no first_turn_client_id, nor did an older chi): the owner's, as chi
|
|
@@ -144,10 +138,7 @@ module Samagotchi
|
|
|
144
138
|
Sender.new(match: RELAY_PREFIX, **unattended),
|
|
145
139
|
agent,
|
|
146
140
|
session_question,
|
|
147
|
-
participant
|
|
148
|
-
# compat shim review_client_id: the legacy review:<id> id uses the
|
|
149
|
-
# participant sender row too.
|
|
150
|
-
participant.with(match: LEGACY_REVIEW_PREFIX)
|
|
141
|
+
participant
|
|
151
142
|
].freeze
|
|
152
143
|
|
|
153
144
|
# No client id: reaches only a worker's own seams (an input file from
|
|
@@ -256,17 +247,9 @@ module Samagotchi
|
|
|
256
247
|
# grant): participant:<participant id>.
|
|
257
248
|
def participant(participant_id) = "#{PARTICIPANT_PREFIX}#{participant_id}"
|
|
258
249
|
|
|
259
|
-
# Whether +client_id+ is a participant's
|
|
260
|
-
# review:).
|
|
250
|
+
# Whether +client_id+ is a participant's.
|
|
261
251
|
def participant?(client_id) = sender(client_id).wake == :participant
|
|
262
252
|
|
|
263
|
-
# compat shim review_client_id: translate participant:<id> for a worker
|
|
264
|
-
# that still expects review:<id>.
|
|
265
|
-
def legacy_participant(client_id)
|
|
266
|
-
id = client_id.to_s
|
|
267
|
-
id.start_with?(PARTICIPANT_PREFIX) ? "#{LEGACY_REVIEW_PREFIX}#{id.delete_prefix(PARTICIPANT_PREFIX)}" : client_id
|
|
268
|
-
end
|
|
269
|
-
|
|
270
253
|
# The client id a delegating parent's task and follow-ups carry:
|
|
271
254
|
# delegate:<parent8>.
|
|
272
255
|
def delegate(parent_id) = "#{DELEGATE_PREFIX}#{parent_id.to_s[0, 8]}"
|
data/lib/samagotchi/compat.rb
CHANGED
|
@@ -17,13 +17,7 @@ module Samagotchi
|
|
|
17
17
|
Shim = Data.define(:id, :added_in, :added_on, :remove_after, :files, :what)
|
|
18
18
|
MIN_AGE_DAYS = 14
|
|
19
19
|
|
|
20
|
-
SHIMS = [
|
|
21
|
-
Shim.new(id: :review_client_id, added_in: "0.172.0", added_on: "2026-11-14", remove_after: "0.173.0",
|
|
22
|
-
files: ["lib/samagotchi/client_id.rb", "lib/samagotchi/session_manager/delivery.rb",
|
|
23
|
-
"lib/samagotchi/web/public/turn_events.js", "lib/samagotchi/web/public/viewer_self.js"],
|
|
24
|
-
what: "A participant's client id from before participant:<id> (review:<id>: drawings, input files, an " \
|
|
25
|
-
"older chi web) reads as a participant's, and a worker without participant_client_id is sent it")
|
|
26
|
-
].freeze
|
|
20
|
+
SHIMS = [].freeze
|
|
27
21
|
|
|
28
22
|
module_function
|
|
29
23
|
|
data/lib/samagotchi/config.rb
CHANGED
|
@@ -411,6 +411,9 @@ module Samagotchi
|
|
|
411
411
|
# The folder chi web starts in (Web::StartDir): its bare page shows that folder's project, and a new session
|
|
412
412
|
# without a folder (the all view's) starts there. Unset: the folder chi web runs in. chi web --start-dir wins.
|
|
413
413
|
Entry.new(key: "web.start_dir", yaml_path: %w[web start_dir], type: :string, default: nil, expose: %i[env config], lifecycle: :web_start),
|
|
414
|
+
# The folder a thin client's one-call create (POST /api/sessions with reply_for, timeout or request_id) starts
|
|
415
|
+
# in when it names no dir (Web::AskCreate; ~ and home-relative work). Unset: web.start_dir's. Read per create.
|
|
416
|
+
Entry.new(key: "web.ask_dir", yaml_path: %w[web ask_dir], type: :string, default: nil, expose: %i[env config], lifecycle: :session_creation),
|
|
414
417
|
# Participants (chi share): a new grant's lifetime (Nh or Nd, 1h to 30d), and what a grant without its own
|
|
415
418
|
# takes: its spending cap in dollars ($0 to $1000) and how many of its turns may wait (0 to 20). Read when used.
|
|
416
419
|
Entry.new(key: "web.participants.expires", yaml_path: %w[web participants expires], type: :string, default: "7d", expose: %i[env config], lifecycle: :next_matching_turn_display_poll_write),
|
data/lib/samagotchi/delivery.rb
CHANGED
|
@@ -62,11 +62,7 @@ module Samagotchi
|
|
|
62
62
|
# Workers advertise REVIEW_FEATURE too: an older chi web refuses
|
|
63
63
|
# participants' turns to a worker without it.
|
|
64
64
|
PAIR_FEATURE = "pair_turns"
|
|
65
|
-
#
|
|
66
|
-
# (ClientId::PARTICIPANT_PREFIX). One without it reads only review:<id>
|
|
67
|
-
# (an unknown id to it: never the owner's, but its lines' source would
|
|
68
|
-
# be automatic:… and a participant's Verify fresh child would ring its
|
|
69
|
-
# parent), so chi web sends it the old id (SessionManager's delivery).
|
|
65
|
+
# The previous release's chi web checks this before sending participant:<id>.
|
|
70
66
|
PARTICIPANT_ID_FEATURE = "participant_client_id"
|
|
71
67
|
# A worker (Bridge::FEATURES) or a chi web (Web::App::INFO_FEATURES)
|
|
72
68
|
# that reads an agent's client id (ClientId::AGENT_PREFIX). One without
|
|
@@ -9,6 +9,21 @@ require_relative "../session_transition"
|
|
|
9
9
|
|
|
10
10
|
module Samagotchi
|
|
11
11
|
class Schedules
|
|
12
|
+
# What #cancel removed: +entry+ (an Entry, or the raw Hash of one that
|
|
13
|
+
# doesn't read) and +newest+, the chain's newest link, set only
|
|
14
|
+
# when this cancel took away the last bridge over a pruned anchor's gap
|
|
15
|
+
# (PrunePart#cancel_strands). It owns the line that says so; callers only
|
|
16
|
+
# add context.
|
|
17
|
+
Cancelled = Data.define(:entry, :newest) do
|
|
18
|
+
def initialize(entry:, newest: nil) = super
|
|
19
|
+
|
|
20
|
+
def id = entry.is_a?(Hash) ? entry["id"] : entry.id
|
|
21
|
+
|
|
22
|
+
def warning
|
|
23
|
+
newest && "#{entry.anchor[0, 8]}'s chain was pruned; its newest link is #{newest[0, 8]} — send there from now on."
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
12
27
|
# The chain prune after a firing (docs/scheduling.md, "Keeping the chain
|
|
13
28
|
# short"): the links behind a chain entry's keep_links window go, oldest
|
|
14
29
|
# first, each under the transition locks of it and the link after it.
|
|
@@ -169,6 +184,23 @@ module Samagotchi
|
|
|
169
184
|
Log.warn(:worker, "prune_waiting", sid: link, id: entry.id, msg: reason) if logged
|
|
170
185
|
end
|
|
171
186
|
|
|
187
|
+
# The chain's newest link when removing +removed+ from +entries+ (the
|
|
188
|
+
# file's list, under its lock) takes away the last bridge over its
|
|
189
|
+
# anchor's gap (SessionChain::Links#anchor_forward), else nil. A disabled
|
|
190
|
+
# entry still bridges, so only a cancel ends one. The heads are given,
|
|
191
|
+
# not read: the file still holds the entry.
|
|
192
|
+
# @return [String, nil]
|
|
193
|
+
def cancel_strands(entries, removed)
|
|
194
|
+
return nil if removed.head == removed.anchor
|
|
195
|
+
|
|
196
|
+
before = SessionChain::Links.new(state_dir: @state_dir, anchor_heads: heads_by_anchor(entries))
|
|
197
|
+
return nil unless before.anchor_forward(removed.anchor)
|
|
198
|
+
|
|
199
|
+
rest = entries.reject { |entry| entry.equal?(removed) }
|
|
200
|
+
after = SessionChain::Links.new(state_dir: @state_dir, anchor_heads: heads_by_anchor(rest))
|
|
201
|
+
after.anchor_forward(removed.anchor) ? nil : before.latest(removed.anchor)
|
|
202
|
+
end
|
|
203
|
+
|
|
172
204
|
# Neither a session file nor a folder: nothing left to delete.
|
|
173
205
|
def link_gone?(link)
|
|
174
206
|
!File.exist?(Session.session_file(link, state_dir: @state_dir)) &&
|
data/lib/samagotchi/schedules.rb
CHANGED
|
@@ -268,11 +268,7 @@ module Samagotchi
|
|
|
268
268
|
# anchor's gap: a paused schedule's chain is still one chain). Lenient and
|
|
269
269
|
# quiet: a file that doesn't read forwards nothing.
|
|
270
270
|
# @return [Hash{String => Array<String>}]
|
|
271
|
-
def anchor_heads
|
|
272
|
-
read(quiet: true).entries.grep(Entry).each_with_object({}) do |entry, heads|
|
|
273
|
-
(heads[entry.anchor] ||= []) << entry.head
|
|
274
|
-
end
|
|
275
|
-
end
|
|
271
|
+
def anchor_heads = heads_by_anchor(read(quiet: true).entries)
|
|
276
272
|
|
|
277
273
|
# One match is the answer; several are a usage error naming them, the way
|
|
278
274
|
# Session::AmbiguousId does for session prefixes.
|
|
@@ -336,8 +332,11 @@ module Samagotchi
|
|
|
336
332
|
entry
|
|
337
333
|
end
|
|
338
334
|
|
|
339
|
-
# @return [
|
|
340
|
-
def cancel(id_or_name)
|
|
335
|
+
# @return [Cancelled, nil] nil when no schedule matches
|
|
336
|
+
def cancel(id_or_name)
|
|
337
|
+
change(id_or_name, delete: true) || cancel_unreadable(id_or_name)&.then { |record| Cancelled.new(entry: record.raw) }
|
|
338
|
+
end
|
|
339
|
+
|
|
341
340
|
def enable(id_or_name) = change(id_or_name, enabled: true)
|
|
342
341
|
def disable(id_or_name) = change(id_or_name, enabled: false)
|
|
343
342
|
|
|
@@ -574,6 +573,11 @@ module Samagotchi
|
|
|
574
573
|
# one-firing-per-chain guard and the firing see the same chain.
|
|
575
574
|
def links_now = SessionChain::Links.new(state_dir: @state_dir)
|
|
576
575
|
|
|
576
|
+
# #anchor_heads of +entries+ (the readable ones), for PrunePart#cancel_strands too.
|
|
577
|
+
def heads_by_anchor(entries)
|
|
578
|
+
entries.grep(Entry).each_with_object({}) { |entry, heads| (heads[entry.anchor] ||= []) << entry.head }
|
|
579
|
+
end
|
|
580
|
+
|
|
577
581
|
# @param strict [Boolean] a file that is not readable JSON raises rather
|
|
578
582
|
# than reading as empty: a write starting from an empty list would rewrite
|
|
579
583
|
# the file and lose every schedule on it. #entries stays lenient.
|
|
@@ -625,7 +629,7 @@ module Samagotchi
|
|
|
625
629
|
# an ambiguous one raises Usage rather than taking the first match. nil
|
|
626
630
|
# too when the entry is gone by the time the lock is held (another cancel
|
|
627
631
|
# landed in between): the caller says "no schedule", not "ok".
|
|
628
|
-
# @return [Entry, nil] the entry as changed (
|
|
632
|
+
# @return [Entry, Cancelled, nil] the entry as changed (Cancelled, for a delete)
|
|
629
633
|
def change(id_or_name, enabled: nil, delete: false)
|
|
630
634
|
entry = lookup(id_or_name)
|
|
631
635
|
return nil if entry.nil?
|
|
@@ -636,7 +640,8 @@ module Samagotchi
|
|
|
636
640
|
next if position.nil?
|
|
637
641
|
|
|
638
642
|
if delete
|
|
639
|
-
|
|
643
|
+
removed = file.entries[position]
|
|
644
|
+
result = Cancelled.new(entry: removed, newest: cancel_strands(file.entries, removed))
|
|
640
645
|
file.entries[position] = nil
|
|
641
646
|
file.entries.compact!
|
|
642
647
|
else
|
data/lib/samagotchi/session.rb
CHANGED
|
@@ -465,6 +465,11 @@ module Samagotchi
|
|
|
465
465
|
# not saved.
|
|
466
466
|
attr_accessor :model_warning
|
|
467
467
|
|
|
468
|
+
# The status the session file held when loaded, before the stop marker's
|
|
469
|
+
# overlay (Session.load and .summary_from_file set it; nil for a session
|
|
470
|
+
# built in memory); not saved.
|
|
471
|
+
attr_accessor :loaded_status
|
|
472
|
+
|
|
468
473
|
# The delegates a continue moved to this new link (ChildMove;
|
|
469
474
|
# SessionContinue.run sets it); not saved.
|
|
470
475
|
attr_accessor :moved_children
|
|
@@ -653,6 +658,7 @@ module Samagotchi
|
|
|
653
658
|
|
|
654
659
|
session = from_h(JSON.parse(File.read(path)))
|
|
655
660
|
session.persisted!
|
|
661
|
+
session.loaded_status = session.status
|
|
656
662
|
session.status = STATUS_STOPPED if stopped_marker?(session.id, state_dir: state_dir)
|
|
657
663
|
session.parent_id = parent_override(session.id, state_dir: state_dir) || session.parent_id
|
|
658
664
|
session
|
|
@@ -782,6 +788,7 @@ module Samagotchi
|
|
|
782
788
|
|
|
783
789
|
session = from_h(data, messages: false)
|
|
784
790
|
session.persisted!
|
|
791
|
+
session.loaded_status = session.status
|
|
785
792
|
session.status = STATUS_STOPPED if stopped_marker?(session.id, state_dir: File.dirname(path))
|
|
786
793
|
session.parent_id = parent_override(session.id, state_dir: File.dirname(path)) || session.parent_id
|
|
787
794
|
session
|
|
@@ -859,7 +859,7 @@ module Samagotchi
|
|
|
859
859
|
raise Refused.new(message.to_s, code: error)
|
|
860
860
|
# No model configured there, or --model's host unknown there: what spawn_session raises here (chi send: could not start a session).
|
|
861
861
|
when "invalid_model" then raise ModelProfile::MissingModel, message.to_s
|
|
862
|
-
when "stop_held" then raise SessionTransition::Busy.new(sid, kind: json["kind"]&.to_sym, successor_id: json["successor_id"])
|
|
862
|
+
when "stop_held" then raise SessionTransition::Busy.new(sid, kind: json["kind"]&.to_sym, successor_id: json["successor_id"], taker: :stop)
|
|
863
863
|
# A toolkit whose config kit's groups: is invalid there: the same.
|
|
864
864
|
when "toolkit_refused" then raise Toolkit::Refused, message.to_s
|
|
865
865
|
# The grants store doesn't read there: chi share says what it says here.
|
|
@@ -247,7 +247,14 @@ module Samagotchi
|
|
|
247
247
|
return :moved if follow && SessionChain.next_of(id, state_dir: state_dir)
|
|
248
248
|
|
|
249
249
|
previous = Session.load(id, state_dir: state_dir)
|
|
250
|
-
|
|
250
|
+
# The link may be continued between the next_of look above and this check: move on, not refuse.
|
|
251
|
+
begin
|
|
252
|
+
checked = check!(previous, state_dir, model: model)
|
|
253
|
+
rescue Refused => e
|
|
254
|
+
raise unless follow && e.reason == :continued
|
|
255
|
+
|
|
256
|
+
return :moved
|
|
257
|
+
end
|
|
251
258
|
# archive_session's refusals that can be known now, before the wait.
|
|
252
259
|
SessionManager.archivable!(id, state_dir, except: checked.plan.move)
|
|
253
260
|
recap_before_archive(id, state_dir, wait: recap_wait)
|
|
@@ -25,6 +25,10 @@ module Samagotchi
|
|
|
25
25
|
STATES = %i[absent tui_owned deleting stopping skew leaving booting_stuck booting live
|
|
26
26
|
stopped_starting stopped archived_starting starting archived idle].freeze
|
|
27
27
|
|
|
28
|
+
# The states whose session is shown "stopped" (the hub shows the stopped
|
|
29
|
+
# status only there; the file's own status is shown everywhere else).
|
|
30
|
+
STOPPED_STATES = %i[stopping stopped_starting stopped].freeze
|
|
31
|
+
|
|
28
32
|
autoload :Reading, File.expand_path("session_lifecycle/reading", __dir__)
|
|
29
33
|
autoload :CANONICAL, File.expand_path("session_lifecycle/canonical", __dir__)
|
|
30
34
|
autoload :ALLOWED, File.expand_path("session_lifecycle/allowed", __dir__)
|
|
@@ -301,12 +301,6 @@ module Samagotchi
|
|
|
301
301
|
return DeliverResult.new(status: :refused, code: 409, ack: ReviseAnchor::PARTICIPANT_UNAVAILABLE)
|
|
302
302
|
end
|
|
303
303
|
|
|
304
|
-
# compat shim review_client_id: a worker from before participant:
|
|
305
|
-
# reads a participant's turn by review:<id> (dequeue is left out:
|
|
306
|
-
# its client id is only echoed back).
|
|
307
|
-
if ClientId.participant?(client_id) && !worker_has?(client, Delivery::PARTICIPANT_ID_FEATURE)
|
|
308
|
-
client_id = ClientId.legacy_participant(client_id)
|
|
309
|
-
end
|
|
310
304
|
# compat shim agent_client_id: a worker from before agent: reads
|
|
311
305
|
# an agent's line as chi send's (today's owner row).
|
|
312
306
|
client_id = ClientId::CLI_SEND if ClientId.agent?(client_id) && !agent_reader
|
|
@@ -89,15 +89,19 @@ module Samagotchi
|
|
|
89
89
|
# holds the lock: STOP_WAIT plus the recap's own wait (SessionContinue::RECAP_WAIT).
|
|
90
90
|
FALLBACK_STOP_WAIT = 40
|
|
91
91
|
|
|
92
|
+
# How long a stop waits for its turn cancel: the Bridge's reply (its
|
|
93
|
+
# read timeout) and then the worker's save, each.
|
|
92
94
|
STOP_CANCEL_WAIT = 3.0
|
|
93
95
|
# How long a stop from the CLI (chi sessions stop, delete, a continue's
|
|
94
96
|
# orphan) waits for the worker to let go of the session, here or
|
|
95
97
|
# through chi web (POST …/stop's wait, capped at it).
|
|
96
98
|
STOP_WAIT = 10
|
|
97
|
-
# How long a stop waits for its session's transition lock (
|
|
98
|
-
#
|
|
99
|
-
#
|
|
100
|
-
#
|
|
99
|
+
# How long a stop waits for its session's transition lock (a short
|
|
100
|
+
# hold: a send's shared one, a holder that names nothing) before it is
|
|
101
|
+
# refused (SessionTransition::Busy). A continue naming its new link, a
|
|
102
|
+
# delete or an archive is refused at once (refuse_transitions). An
|
|
103
|
+
# owner-side stop (a worker stopping its delegates) tries once (0): it
|
|
104
|
+
# holds its own owner.lock.
|
|
101
105
|
STOP_LOCK_WAIT = 10
|
|
102
106
|
# The cancel reason of a turn a session stop cancels (Bridge::CANCEL_REASONS):
|
|
103
107
|
# not the user's Stop, which only ends the turn ("user").
|
|
@@ -218,27 +222,32 @@ module Samagotchi
|
|
|
218
222
|
# With +wait+ (seconds), also wait for the owner to let go of the session,
|
|
219
223
|
# so a resume right after spawns a fresh worker instead of finding the
|
|
220
224
|
# dying one.
|
|
221
|
-
#
|
|
222
|
-
#
|
|
223
|
-
#
|
|
224
|
-
#
|
|
225
|
-
# exclusively (kind :stop, +lock_wait+ seconds to take it; one this
|
|
225
|
+
# The cancel, the mark and the TERM run under the session's transition
|
|
226
|
+
# lock, held exclusively (kind :stop, +lock_wait+ seconds to take it,
|
|
227
|
+
# a continue naming its new link, a delete or an archive refused at
|
|
228
|
+
# once: SessionTransition.exclusive's refuse_transitions; one this
|
|
226
229
|
# thread holds, a continue's or a delete's, passes through), never the
|
|
227
|
-
#
|
|
228
|
-
#
|
|
229
|
-
# (#wake_for_report) comes before the stop or after it,
|
|
230
|
+
# owner wait: a send, a continue or a delete doesn't interleave with
|
|
231
|
+
# them, a refused stop leaves the running turn alone, and a ring
|
|
232
|
+
# wake's start (#wake_for_report) comes before the stop or after it,
|
|
233
|
+
# whole. A turn still running is canceled first, as the web's Cancel
|
|
234
|
+
# does, with reason STOP_CANCEL_REASON: its worker saves it. That is
|
|
235
|
+
# the one Bridge wait under the hold, bounded (STOP_CANCEL_WAIT for
|
|
236
|
+
# the reply, then as long for the save). The TERM stops (and saves)
|
|
237
|
+
# what still runs (WorkerSignals), with a short grace after the marker.
|
|
230
238
|
# @param lock_wait [Numeric] 0 from a worker (it holds an owner.lock:
|
|
231
239
|
# one try, never a wait)
|
|
232
240
|
# @return [Boolean, nil] with +wait+: whether the owner was gone in time
|
|
233
241
|
# @raise [OwnedByTUI] when the interactive TUI owns the session
|
|
234
242
|
# @raise [SessionTransition::Busy] another change held the lock
|
|
235
|
-
# past +lock_wait
|
|
243
|
+
# past +lock_wait+, or a transition held it (its words name the
|
|
244
|
+
# holder; a stop's: Busy#taker :stop); nothing was canceled or marked
|
|
236
245
|
def stop_session(session_id, state_dir: nil, wait: nil, lock_wait: STOP_LOCK_WAIT)
|
|
237
246
|
sd = state_dir || Session.default_state_dir
|
|
238
247
|
refuse_tui!(session_id, state_dir: sd)
|
|
239
248
|
|
|
240
|
-
cancel_running_turn(session_id, state_dir: sd)
|
|
241
249
|
under_stop_lock(session_id, sd, lock_wait) do
|
|
250
|
+
cancel_running_turn(session_id, state_dir: sd)
|
|
242
251
|
# The owner read again under the lock: a worker a ring wake started
|
|
243
252
|
# while the stop waited for it is the one to TERM.
|
|
244
253
|
owner = refuse_tui!(session_id, state_dir: sd)
|
|
@@ -268,7 +277,8 @@ module Samagotchi
|
|
|
268
277
|
return yield unless Session.exist?(session_id, state_dir: state_dir)
|
|
269
278
|
|
|
270
279
|
begin
|
|
271
|
-
SessionTransition.exclusive([session_id], kind: :stop, state_dir: state_dir, deadline: lock_wait,
|
|
280
|
+
SessionTransition.exclusive([session_id], kind: :stop, state_dir: state_dir, deadline: lock_wait,
|
|
281
|
+
refuse_transitions: true, &)
|
|
272
282
|
ensure
|
|
273
283
|
SessionTransition.forget(session_id, state_dir: state_dir)
|
|
274
284
|
end
|
|
@@ -286,11 +296,14 @@ module Samagotchi
|
|
|
286
296
|
|
|
287
297
|
# Cancel the session's running turn over its Bridge and wait (up to
|
|
288
298
|
# STOP_CANCEL_WAIT) for the worker to save it. Best effort: without a
|
|
289
|
-
# live Bridge, or when it doesn't answer
|
|
299
|
+
# live Bridge, or when it doesn't answer within STOP_CANCEL_WAIT (its
|
|
300
|
+
# read timeout: the stop holds the transition lock), the stop goes on
|
|
301
|
+
# as before.
|
|
290
302
|
private def cancel_running_turn(session_id, state_dir:)
|
|
291
303
|
return unless Session.load(session_id, state_dir: state_dir).status == Session::STATUS_RUNNING
|
|
292
304
|
|
|
293
|
-
client = WorkerLink.client(session_id, session_dir: Session.session_dir(session_id, state_dir: state_dir)
|
|
305
|
+
client = WorkerLink.client(session_id, session_dir: Session.session_dir(session_id, state_dir: state_dir),
|
|
306
|
+
read_timeout: STOP_CANCEL_WAIT)
|
|
294
307
|
return unless client && client.cancel(reason: STOP_CANCEL_REASON).status == 202
|
|
295
308
|
|
|
296
309
|
BridgeClient.poll(STOP_CANCEL_WAIT) do
|