samagotchi 0.172.1 → 0.173.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 +31 -1
- data/docs/architecture.md +16 -5
- data/docs/cli.md +2 -1
- data/docs/configuration.md +1 -1
- data/docs/internals/background-tasks.md +19 -6
- data/docs/internals/bridge.md +2 -2
- data/docs/internals/engine.md +2 -1
- data/docs/internals/participants.md +67 -9
- data/docs/internals/remote-sessions.md +1 -1
- data/docs/internals/web-frontend.md +11 -6
- data/docs/plugins.md +3 -1
- data/docs/sessions.md +2 -2
- data/docs/testing.md +26 -10
- data/lib/samagotchi/bridge.rb +3 -2
- data/lib/samagotchi/bundle_command/try/sandbox.rb +2 -2
- data/lib/samagotchi/bundles/core/manifest.yml +2 -2
- data/lib/samagotchi/bundles/slow-commands/manifest.yml +3 -3
- data/lib/samagotchi/bundles/slow-commands/plugin.rb +3 -3
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/engine/participants.rb +2 -2
- data/lib/samagotchi/engine.rb +0 -1
- data/lib/samagotchi/file_ref.rb +3 -5
- data/lib/samagotchi/guardrails/participant_refusal.rb +1 -1
- data/lib/samagotchi/memory_usage.rb +2 -1
- data/lib/samagotchi/message_parts.rb +5 -3
- data/lib/samagotchi/objectives_backing.rb +6 -10
- data/lib/samagotchi/parent_report.rb +1 -1
- data/lib/samagotchi/participant_prompts.rb +101 -0
- data/lib/samagotchi/participant_turn_mode.rb +1 -1
- data/lib/samagotchi/project_env.rb +7 -5
- data/lib/samagotchi/prompt_history.rb +2 -2
- data/lib/samagotchi/send_command/parent_gate.rb +2 -2
- data/lib/samagotchi/send_command.rb +4 -4
- data/lib/samagotchi/session_command_catalog.rb +210 -0
- data/lib/samagotchi/session_commands.rb +12 -183
- data/lib/samagotchi/session_manager/lifecycle.rb +5 -4
- data/lib/samagotchi/session_manager.rb +2 -1
- data/lib/samagotchi/session_stores.rb +2 -0
- data/lib/samagotchi/spawn_env.rb +75 -0
- data/lib/samagotchi/task_files.rb +51 -0
- data/lib/samagotchi/task_transcript.rb +67 -0
- data/lib/samagotchi/task_watch.rb +8 -6
- data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +3 -3
- data/lib/samagotchi/terminal_ui/attached_loop/questions.rb +2 -2
- data/lib/samagotchi/terminal_ui/attached_loop.rb +3 -3
- data/lib/samagotchi/terminal_ui/input_support.rb +1 -1
- data/lib/samagotchi/testing/boundary.rb +5 -0
- data/lib/samagotchi/tool_activity.rb +63 -79
- data/lib/samagotchi/tool_names.rb +179 -0
- data/lib/samagotchi/tool_view.rb +3 -5
- data/lib/samagotchi/tools/ask_user_question.rb +2 -1
- data/lib/samagotchi/tools/builtins.rb +1 -1
- data/lib/samagotchi/tools/cancel_reminder.rb +2 -1
- data/lib/samagotchi/tools/cancel_schedule.rb +3 -1
- data/lib/samagotchi/tools/context_read.rb +2 -1
- data/lib/samagotchi/tools/delegate.rb +2 -1
- data/lib/samagotchi/tools/delegate_result.rb +2 -1
- data/lib/samagotchi/tools/edit.rb +2 -1
- data/lib/samagotchi/tools/execute.rb +22 -28
- data/lib/samagotchi/tools/forget_outputs.rb +2 -1
- data/lib/samagotchi/tools/list_reminders.rb +2 -1
- data/lib/samagotchi/tools/list_schedules.rb +3 -1
- data/lib/samagotchi/tools/list_sessions.rb +2 -1
- data/lib/samagotchi/tools/memory.rb +3 -2
- data/lib/samagotchi/tools/objectives_tool.rb +5 -4
- data/lib/samagotchi/tools/read.rb +2 -1
- data/lib/samagotchi/tools/register_reminder.rb +2 -1
- data/lib/samagotchi/tools/schedule_task.rb +3 -1
- data/lib/samagotchi/tools/send_note.rb +2 -1
- data/lib/samagotchi/tools/task_create.rb +2 -1
- data/lib/samagotchi/tools/task_get.rb +2 -1
- data/lib/samagotchi/tools/task_list.rb +2 -1
- data/lib/samagotchi/tools/task_runtime.rb +34 -154
- data/lib/samagotchi/tools/task_stop.rb +2 -1
- data/lib/samagotchi/tools/task_wait.rb +3 -2
- data/lib/samagotchi/tools/web_fetch.rb +2 -1
- data/lib/samagotchi/tools/write.rb +2 -1
- data/lib/samagotchi/turn_checkpoint.rb +1 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +38 -36
- data/lib/samagotchi/web/message_display.rb +1 -7
- data/lib/samagotchi/web/participant_access.rb +12 -4
- data/lib/samagotchi/web/participant_history.rb +68 -0
- data/lib/samagotchi/web/participant_images.rb +2 -1
- data/lib/samagotchi/web/participant_show.rb +5 -5
- data/lib/samagotchi/web/participants_admin.rb +1 -1
- data/lib/samagotchi/web/public/app.js +11 -9
- data/lib/samagotchi/web/public/card_registry.js +248 -0
- data/lib/samagotchi/web/public/cards_offscreen.js +3 -3
- data/lib/samagotchi/web/public/cards_place.js +8 -2
- data/lib/samagotchi/web/public/history_shape.js +1 -2
- data/lib/samagotchi/web/public/participant_history.js +27 -0
- data/lib/samagotchi/web/public/question_cards.js +1 -15
- data/lib/samagotchi/web/public/reload_check.js +90 -72
- data/lib/samagotchi/web/public/session_view.js +6 -1
- data/lib/samagotchi/web/public/stage_asking.js +6 -20
- data/lib/samagotchi/web/public/stage_view.js +2 -2
- data/lib/samagotchi/web/public/stage_waiting.js +4 -10
- data/lib/samagotchi/web/public/task_stop.js +1 -1
- data/lib/samagotchi/web/push_routes.rb +1 -1
- data/lib/samagotchi/web/reload_check_route.rb +5 -2
- data/lib/samagotchi/web/request_body.rb +72 -0
- data/lib/samagotchi/web/server.rb +70 -0
- data/lib/samagotchi/web/session_access_routes.rb +13 -11
- data/lib/samagotchi/web/session_control.rb +21 -15
- data/lib/samagotchi/web/session_create.rb +2 -1
- data/lib/samagotchi/web/session_show.rb +8 -8
- data/lib/samagotchi/web/viewer.rb +7 -2
- data/lib/samagotchi/web/working_set_routes.rb +3 -3
- data/lib/samagotchi/worker.rb +49 -182
- data/lib/samagotchi/worker_exit_claim.rb +102 -0
- data/lib/samagotchi/worker_participant_gate.rb +156 -0
- data/lib/samagotchi/worker_task_wakes.rb +2 -2
- metadata +13 -1
|
@@ -6,8 +6,9 @@ require_relative "../delivery"
|
|
|
6
6
|
require_relative "../image_store"
|
|
7
7
|
require_relative "../prompt_history"
|
|
8
8
|
require_relative "../session"
|
|
9
|
-
require_relative "../
|
|
9
|
+
require_relative "../session_command_catalog"
|
|
10
10
|
require_relative "../session_manager"
|
|
11
|
+
require_relative "request_body"
|
|
11
12
|
|
|
12
13
|
module Samagotchi
|
|
13
14
|
module Web
|
|
@@ -49,7 +50,8 @@ module Samagotchi
|
|
|
49
50
|
session = @session_class.load(id, state_dir: default_state_dir) rescue nil
|
|
50
51
|
return session_not_found(id) unless session
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
# Relayed to the Bridge, which takes up to its MAX_BODY_BYTES.
|
|
54
|
+
body = parse_json(request_body(req, RequestBody::BRIDGE_BODY_LIMIT))
|
|
53
55
|
return error_response(400, "invalid_json", "invalid JSON body") unless body.is_a?(Hash)
|
|
54
56
|
|
|
55
57
|
qid = body["id"]
|
|
@@ -155,7 +157,7 @@ module Samagotchi
|
|
|
155
157
|
# One of this chi's own commands the worker doesn't know: it runs an
|
|
156
158
|
# older chi. command: which one, so a remote TUI names it as its
|
|
157
159
|
# Bridge's own 400 unknown_command would (WebBridgeClient).
|
|
158
|
-
name = reply.json&.dig("error") == "unknown_command" &&
|
|
160
|
+
name = reply.json&.dig("error") == "unknown_command" && SessionCommandCatalog.builtin_name(line)
|
|
159
161
|
next older_worker_command(id, name) if name
|
|
160
162
|
|
|
161
163
|
error_response(400, reply.json&.dig("error") || "unknown_command", reply_detail(reply, "not a session command"))
|
|
@@ -181,7 +183,10 @@ module Samagotchi
|
|
|
181
183
|
end
|
|
182
184
|
|
|
183
185
|
def handle_turn(req, id)
|
|
184
|
-
|
|
186
|
+
# A participant's turn is relayed to the Bridge, which takes up to
|
|
187
|
+
# its MAX_BODY_BYTES; the owner's may be a big piped chi send.
|
|
188
|
+
cap = principal(req).owner? ? RequestBody::OWNER_TURN_BODY_LIMIT : RequestBody::BRIDGE_BODY_LIMIT
|
|
189
|
+
body = parse_json(request_body(req, cap))
|
|
185
190
|
unless body.is_a?(Hash)
|
|
186
191
|
return error_response(400, "invalid_json", "invalid JSON body")
|
|
187
192
|
end
|
|
@@ -279,7 +284,7 @@ module Samagotchi
|
|
|
279
284
|
return participant_image_upload(req, id, session_dir) unless principal(req).owner?
|
|
280
285
|
|
|
281
286
|
bytes = image_upload_bytes(req)
|
|
282
|
-
return error_response(413, "too_large",
|
|
287
|
+
return error_response(413, "too_large", bytes.message) if bytes.is_a?(RequestBody::BodyTooLarge)
|
|
283
288
|
|
|
284
289
|
ref = ImageStore.ingest(session_dir, bytes: bytes, name: upload_name(req), source: "user")
|
|
285
290
|
json_response(201, ref)
|
|
@@ -287,21 +292,22 @@ module Samagotchi
|
|
|
287
292
|
error_response(422, "bad_image", e.message)
|
|
288
293
|
end
|
|
289
294
|
|
|
290
|
-
# An upload's body, or
|
|
291
|
-
# its Content-Length or what came).
|
|
295
|
+
# An upload's body, or the BodyTooLarge when it is over
|
|
296
|
+
# MAX_IMAGE_UPLOAD_BYTES (by its Content-Length or what came).
|
|
297
|
+
# Rescued here, not left to App#route's 413: the participant's path
|
|
298
|
+
# (ParticipantImages#participant_upload) audits its refusal as
|
|
299
|
+
# too_large, and its own rescue StandardError would audit a raised one
|
|
300
|
+
# as internal_error.
|
|
292
301
|
def image_upload_bytes(req)
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
bytes.bytesize > MAX_IMAGE_UPLOAD_BYTES ? nil : bytes
|
|
302
|
+
request_body(req, MAX_IMAGE_UPLOAD_BYTES, what: "an image").b
|
|
303
|
+
rescue RequestBody::BodyTooLarge => e
|
|
304
|
+
e
|
|
297
305
|
end
|
|
298
306
|
|
|
299
|
-
def image_too_large_detail = "an image may be up to #{MAX_IMAGE_UPLOAD_BYTES / 1024 / 1024} MB"
|
|
300
|
-
|
|
301
307
|
# An upload's ?name=, a base name without control characters; "image"
|
|
302
308
|
# without one.
|
|
303
309
|
def upload_name(req)
|
|
304
|
-
name = File.basename(req.
|
|
310
|
+
name = File.basename(req.GET["name"].to_s).gsub(/[[:cntrl:]]/, "")[0, 120]
|
|
305
311
|
name.empty? ? "image" : name
|
|
306
312
|
end
|
|
307
313
|
|
|
@@ -517,7 +523,7 @@ module Samagotchi
|
|
|
517
523
|
# worker_running, as chi sessions delete without --force. A one-turn chi run's
|
|
518
524
|
# session is always refused.
|
|
519
525
|
def handle_delete(req, id)
|
|
520
|
-
result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.
|
|
526
|
+
result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
|
|
521
527
|
wait: STOP_WAIT_SECONDS)
|
|
522
528
|
@hub&.touch(result[:id])
|
|
523
529
|
json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped] })
|
|
@@ -17,6 +17,7 @@ require_relative "../session_setup"
|
|
|
17
17
|
require_relative "../thinking"
|
|
18
18
|
require_relative "../toolkit"
|
|
19
19
|
require_relative "../turn_flow"
|
|
20
|
+
require_relative "request_body"
|
|
20
21
|
|
|
21
22
|
module Samagotchi
|
|
22
23
|
module Web
|
|
@@ -40,7 +41,7 @@ module Samagotchi
|
|
|
40
41
|
private
|
|
41
42
|
|
|
42
43
|
def handle_create(req)
|
|
43
|
-
body = parse_json(request_body(req))
|
|
44
|
+
body = parse_json(request_body(req, RequestBody::OWNER_TURN_BODY_LIMIT))
|
|
44
45
|
unless body.is_a?(Hash)
|
|
45
46
|
return error_response(400, "invalid_json", "invalid JSON body")
|
|
46
47
|
end
|
|
@@ -12,7 +12,7 @@ require_relative "../objectives"
|
|
|
12
12
|
require_relative "../output_formatter"
|
|
13
13
|
require_relative "../question_asked"
|
|
14
14
|
require_relative "../guardrails/approvals"
|
|
15
|
-
require_relative "../
|
|
15
|
+
require_relative "../session_command_catalog"
|
|
16
16
|
require_relative "../session_metrics"
|
|
17
17
|
require_relative "../session_setting"
|
|
18
18
|
require_relative "../system_one/log"
|
|
@@ -53,10 +53,10 @@ module Samagotchi
|
|
|
53
53
|
return participant_show_read(req, id) unless principal(req).owner?
|
|
54
54
|
|
|
55
55
|
# The page's light re-reads: never the whole conversation.
|
|
56
|
-
return handle_cards_read(id) if req.
|
|
57
|
-
return handle_tail_read(req, id) if req.
|
|
58
|
-
return handle_timing_read(id) if req.
|
|
59
|
-
return handle_brief_read(id) if req.
|
|
56
|
+
return handle_cards_read(id) if req.GET["cards"] == "1"
|
|
57
|
+
return handle_tail_read(req, id) if req.GET["tail"] == "1"
|
|
58
|
+
return handle_timing_read(id) if req.GET["timing"] == "1"
|
|
59
|
+
return handle_brief_read(id) if req.GET["brief"] == "1"
|
|
60
60
|
|
|
61
61
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
62
62
|
# A tab opened at a chi scratch session's id: its terminal's alone.
|
|
@@ -141,7 +141,7 @@ module Samagotchi
|
|
|
141
141
|
cards: cards_for_display(raw_cards, cwd: session.working_directory),
|
|
142
142
|
# The composer's / autocomplete; the built-ins until a worker
|
|
143
143
|
# names its plugins' too.
|
|
144
|
-
commands: turn_snapshot&.fetch("commands", nil) ||
|
|
144
|
+
commands: turn_snapshot&.fetch("commands", nil) || SessionCommandCatalog.builtin_registry.listing,
|
|
145
145
|
markdown_warning: @markdown_renderer.warning,
|
|
146
146
|
markdown_tip: @markdown_renderer.tip,
|
|
147
147
|
**viewer_capabilities(req),
|
|
@@ -317,7 +317,7 @@ module Samagotchi
|
|
|
317
317
|
def handle_tail_read(req, id)
|
|
318
318
|
return session_not_found(id) unless session_exists?(id)
|
|
319
319
|
|
|
320
|
-
turn_id = req.
|
|
320
|
+
turn_id = req.GET["turn_id"].to_s.strip
|
|
321
321
|
turn_id = nil if turn_id.empty?
|
|
322
322
|
tail = live_tail(id, turn_id: turn_id)
|
|
323
323
|
state = tail && tail["session_state_snapshot"]
|
|
@@ -408,7 +408,7 @@ module Samagotchi
|
|
|
408
408
|
|
|
409
409
|
def handle_output(req, id)
|
|
410
410
|
# Query ?since= may be an ISO8601 time or numeric mtime; we treat it as Time parse.
|
|
411
|
-
since_param = req.
|
|
411
|
+
since_param = req.GET["since"]
|
|
412
412
|
since_time = parse_since(since_param)
|
|
413
413
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
414
414
|
raw = @manager.read_responses(id, since_time: since_time, state_dir: @state_dir)
|
|
@@ -58,12 +58,17 @@ module Samagotchi
|
|
|
58
58
|
# a message sent while their own turn runs goes in at its next step
|
|
59
59
|
# (a worker with Delivery::OWN_TURN_STEER_FEATURE); +revise_own_turn+:
|
|
60
60
|
# Revise on the answer to their own message with only their own turns
|
|
61
|
-
# after it (POST …/turn with revises, ParticipantRevise)
|
|
61
|
+
# after it (POST …/turn with revises, ParticipantRevise);
|
|
62
|
+
# +own_prompt_history+: ↑/↓ over their own accepted messages in this
|
|
63
|
+
# session, redacted (GET …/prompt_history, ParticipantHistory; never the
|
|
64
|
+
# owner's prompt_history, the machine-wide file). And of the owner's,
|
|
62
65
|
# those a participant's route serves filtered: +working_set+ (GET
|
|
63
66
|
# …/working_set, the project's files only) and +images+ (POST …/images
|
|
64
67
|
# within their cap and quota; GET …/images/<name> only for an image
|
|
65
68
|
# they uploaded; their turn takes only those: ParticipantImages).
|
|
66
|
-
Viewer::PARTICIPANT_CAPABILITIES = %w[
|
|
69
|
+
Viewer::PARTICIPANT_CAPABILITIES = %w[
|
|
70
|
+
send stop_own_turn steer_own_turn revise_own_turn own_prompt_history working_set images
|
|
71
|
+
].freeze
|
|
67
72
|
|
|
68
73
|
Viewer::OWNER = Viewer.new(kind: "owner", id: nil, name: nil, can: Viewer::CAPABILITIES)
|
|
69
74
|
end
|
|
@@ -53,8 +53,8 @@ module Samagotchi
|
|
|
53
53
|
# truncate_at_bytes, the ToolRunner cap). +now+ (a memory): what its
|
|
54
54
|
# latest read would answer now and the diff (WorkingSet.memory_now).
|
|
55
55
|
def handle_working_set_item(req, id)
|
|
56
|
-
kind = req.
|
|
57
|
-
key = req.
|
|
56
|
+
kind = req.GET["kind"].to_s
|
|
57
|
+
key = req.GET["key"].to_s
|
|
58
58
|
unless [WorkingSet::FILE, WorkingSet::MEMORY, WorkingSet::URL].include?(kind) && !key.strip.empty?
|
|
59
59
|
return error_response(400, "invalid_item", "expected kind (file, memory or url) and key")
|
|
60
60
|
end
|
|
@@ -63,7 +63,7 @@ module Samagotchi
|
|
|
63
63
|
source = working_set_source(bridge_get_json(id, "snapshot"), session)
|
|
64
64
|
timeline = WorkingSet.timeline_of(source.messages, kind: kind, key: key, strategy: source.strategy, cwd: source.cwd)
|
|
65
65
|
body = timeline.to_h
|
|
66
|
-
if req.
|
|
66
|
+
if req.GET["now"] == "1" && kind == WorkingSet::MEMORY
|
|
67
67
|
body[:now] = WorkingSet.memory_now(source.messages, key: key, strategy: source.strategy, cwd: source.cwd,
|
|
68
68
|
root: MemoryPaths.system_dir)&.to_h
|
|
69
69
|
end
|
data/lib/samagotchi/worker.rb
CHANGED
|
@@ -44,8 +44,8 @@ require_relative "context_candidates"
|
|
|
44
44
|
require_relative "context_poller"
|
|
45
45
|
require_relative "task_watch"
|
|
46
46
|
require_relative "tools/task_runtime"
|
|
47
|
-
require_relative "
|
|
48
|
-
require_relative "
|
|
47
|
+
require_relative "worker_participant_gate"
|
|
48
|
+
require_relative "worker_exit_claim"
|
|
49
49
|
|
|
50
50
|
module Samagotchi
|
|
51
51
|
# The loop of a background session worker, once it owns the session (see
|
|
@@ -151,13 +151,8 @@ module Samagotchi
|
|
|
151
151
|
@poll_interval = poll_interval || FALLBACK_TICK_SECONDS
|
|
152
152
|
@waker = Waker.new
|
|
153
153
|
@command_queue = Thread::Queue.new
|
|
154
|
-
# The client_id of a client that asked the worker to exit (POST /exit).
|
|
155
|
-
@exit_requested = nil
|
|
156
|
-
@exit_requested_by = nil
|
|
157
154
|
# Set as it leaves: nothing happened in the session (#discard?).
|
|
158
155
|
@discard = false
|
|
159
|
-
# The exit asked for is a restart (POST /exit restart: true).
|
|
160
|
-
@exit_restart = false
|
|
161
156
|
# The lines the running turn's drain merged ([prompt, TurnOrigin]), for a
|
|
162
157
|
# failed prompt turn to hand back; reset as each turn begins.
|
|
163
158
|
@merged_this_turn = []
|
|
@@ -176,10 +171,10 @@ module Samagotchi
|
|
|
176
171
|
@loop_started = false
|
|
177
172
|
@left_loop = false
|
|
178
173
|
@signal_mutex = Mutex.new
|
|
179
|
-
# The exit being taken or taken (:exit, :delete, :restart
|
|
180
|
-
# under its own lock before #exit_request's slow
|
|
181
|
-
|
|
182
|
-
@
|
|
174
|
+
# The exit being taken or taken (POST /exit: :exit, :delete, :restart,
|
|
175
|
+
# who asked), claimed under its own lock before #exit_request's slow
|
|
176
|
+
# stop-all, and what that stop-all stopped.
|
|
177
|
+
@exit_claim = WorkerExitClaim.new
|
|
183
178
|
@grace_expired = false
|
|
184
179
|
# The running turn has ended (its end event) and its after_turn hooks
|
|
185
180
|
# run: a signal's grace waits for them (#signalled).
|
|
@@ -232,9 +227,13 @@ module Samagotchi
|
|
|
232
227
|
end
|
|
233
228
|
|
|
234
229
|
# The step that stops what the user can stop (the reminders, the watched
|
|
235
|
-
# tasks and the delegate children the exit ask lists)
|
|
236
|
-
#
|
|
237
|
-
attr_reader :hold_stop
|
|
230
|
+
# tasks and the delegate children the exit ask lists). /exit's 2 uses
|
|
231
|
+
# it; the web's Stop will too.
|
|
232
|
+
attr_reader :hold_stop
|
|
233
|
+
|
|
234
|
+
# @return [HoldStop::Stopped, nil] the last stop-all's list (the Bridge's
|
|
235
|
+
# exit_stopped)
|
|
236
|
+
def stopped_holds = @exit_claim.stopped
|
|
238
237
|
|
|
239
238
|
private
|
|
240
239
|
|
|
@@ -291,9 +290,14 @@ module Samagotchi
|
|
|
291
290
|
# stream gets the session's history and status in its snapshot, not
|
|
292
291
|
# an empty session until the first turn.
|
|
293
292
|
@engine.session = @session
|
|
293
|
+
# May this participant act (loop thread only): a turn as it starts,
|
|
294
|
+
# the drain, the mid-turn stop, the Continue.
|
|
295
|
+
@participant_gate = WorkerParticipantGate.new(engine: @engine, session: @session, session_id: @session_id,
|
|
296
|
+
state_dir: @state_dir, session_dir: @session_dir,
|
|
297
|
+
hook: Engine::PARTICIPANTS_HOOK)
|
|
294
298
|
# Who is in the review as the worker starts: no notice for them
|
|
295
|
-
# (#
|
|
296
|
-
|
|
299
|
+
# (WorkerParticipantGate#watch announces the changes from here on).
|
|
300
|
+
@participant_gate.watch
|
|
297
301
|
# Input already waiting in the inbox starts the next turn at once, and
|
|
298
302
|
# a command queued mid-turn (/thinking, /model, /llm-context) changes
|
|
299
303
|
# what it starts with: no turn-end warm-up then.
|
|
@@ -305,7 +309,7 @@ module Samagotchi
|
|
|
305
309
|
# Bridge exists by the time anyone answers).
|
|
306
310
|
@continue_offer = ContinueOffer.new(engine: @engine, turn_flow: @turn_flow, run_turn: method(:run_engine_turn),
|
|
307
311
|
max_iterations: method(:max_iterations),
|
|
308
|
-
participant_refused: method(:
|
|
312
|
+
participant_refused: @participant_gate.method(:refusal),
|
|
309
313
|
save: -> { save_or_log(:command) { save_session } },
|
|
310
314
|
queue_command: lambda { |line, client_id:|
|
|
311
315
|
@bridge.queue_command(line, client_id: client_id, card: true)
|
|
@@ -320,7 +324,7 @@ module Samagotchi
|
|
|
320
324
|
# one that finishes unseen wakes the idle session (#run_task_wake).
|
|
321
325
|
@task_wakes = WorkerTaskWakes.new(engine: @engine, session: @session, wakes: @wakes,
|
|
322
326
|
awaiting_continue: -> { @turn_flow.awaiting_continue? },
|
|
323
|
-
idle: -> { nothing_queued? && !@
|
|
327
|
+
idle: -> { nothing_queued? && !@exit_claim.taken? },
|
|
324
328
|
save: -> { save_or_log(:task_notes) { save_session } })
|
|
325
329
|
@engine.subscribe(observer: lambda { |event|
|
|
326
330
|
if Events::TURN_END.include?(event[:type])
|
|
@@ -525,7 +529,7 @@ module Samagotchi
|
|
|
525
529
|
# ahead of an exit or restart a client asked for: it is a note then.
|
|
526
530
|
@inbound.absorb_notes
|
|
527
531
|
absorb_fresh_verdicts
|
|
528
|
-
return if absorb_context(wake: nothing_queued? && !@
|
|
532
|
+
return if absorb_context(wake: nothing_queued? && !@exit_claim.taken?)
|
|
529
533
|
|
|
530
534
|
if (prompt = @inbound.take_initial_prompt)
|
|
531
535
|
run_initial_prompt(prompt) unless @inbound.initial_command?(prompt)
|
|
@@ -555,23 +559,14 @@ module Samagotchi
|
|
|
555
559
|
return left(:scratch_left) if scratch_unattended? && !@engine.turn_running? && leave_scratch
|
|
556
560
|
return left(:idle_exit) if @idle_exit.due? && leave_idle
|
|
557
561
|
|
|
558
|
-
leaving = @
|
|
562
|
+
leaving = @exit_claim.taken? && leave_on_request
|
|
559
563
|
return left(leaving) if leaving
|
|
560
564
|
|
|
561
|
-
|
|
565
|
+
@participant_gate.watch
|
|
562
566
|
@waker.wait(idle_wait)
|
|
563
567
|
nil
|
|
564
568
|
end
|
|
565
569
|
|
|
566
|
-
# A participant invited, revoked or expired since the last look: the
|
|
567
|
-
# UIs hear it (Engine#announce_participants: :participants_changed).
|
|
568
|
-
# Between turns, at most once a fallback tick; a failure only logs.
|
|
569
|
-
def watch_participants
|
|
570
|
-
@engine.announce_participants
|
|
571
|
-
rescue StandardError => e
|
|
572
|
-
Log.warn(:worker, "participants_watch_failed", error: e.class.name)
|
|
573
|
-
end
|
|
574
|
-
|
|
575
570
|
# A participant's input file is held (left in the inbox, still queued
|
|
576
571
|
# and counted as theirs) while the owner's Continue offer stands: the
|
|
577
572
|
# offer is the owner's to answer (continue, stop, or a turn of their
|
|
@@ -681,7 +676,7 @@ module Samagotchi
|
|
|
681
676
|
# one waits, like ask_user_question.
|
|
682
677
|
engine.interface = :worker
|
|
683
678
|
# A pair turn stops mid-turn at its participant's cap.
|
|
684
|
-
engine.participant_stop = ->(author) {
|
|
679
|
+
engine.participant_stop = ->(author) { @participant_gate.turn_stop(author) }
|
|
685
680
|
engine.guardrail_state_dir = @state_dir if @state_dir
|
|
686
681
|
engine.session_state_dir = @state_dir if @state_dir
|
|
687
682
|
engine
|
|
@@ -813,8 +808,8 @@ module Samagotchi
|
|
|
813
808
|
# A participant's turn queued before their link ended or their cap was
|
|
814
809
|
# reached doesn't start (chi web refuses new ones; this is the
|
|
815
810
|
# worker's check).
|
|
816
|
-
if (reason =
|
|
817
|
-
return
|
|
811
|
+
if (reason = @participant_gate.refusal(origin.author))
|
|
812
|
+
return @participant_gate.refuse(origin, reason)
|
|
818
813
|
end
|
|
819
814
|
|
|
820
815
|
# A candidate the prompt mentions is attached and fetched first, so
|
|
@@ -837,7 +832,7 @@ module Samagotchi
|
|
|
837
832
|
@continue_offer.after_turn(result, no_interrupt: prompt.no_interrupt)
|
|
838
833
|
end
|
|
839
834
|
end
|
|
840
|
-
|
|
835
|
+
@participant_gate.refusal(origin.author)
|
|
841
836
|
end
|
|
842
837
|
|
|
843
838
|
# +origin+ as this session's turns run it: a participant's Verify fresh
|
|
@@ -847,106 +842,6 @@ module Samagotchi
|
|
|
847
842
|
fresh ? fresh.origin_for(origin) : TurnOrigin.of(origin)
|
|
848
843
|
end
|
|
849
844
|
|
|
850
|
-
# Why the participant +author+ ("p:<id>") may run no more turns, nil
|
|
851
|
-
# while they may (and for the owner): by ParticipantTurnMode, "outdated"
|
|
852
|
-
# for an older chi's read-only invite (an entry, or a Verify fresh child
|
|
853
|
-
# saved, without "pair": this chi runs no read-only turn), "revoked" when
|
|
854
|
-
# participants.json has no current entry for them (revoked, expired,
|
|
855
|
-
# never invited) or they aren't invited to this (the owner's own Verify
|
|
856
|
-
# fresh child), "cap_reached" once they spent their cap (its own,
|
|
857
|
-
# else the config's; their turns and the side requests they caused,
|
|
858
|
-
# Engine#metrics). The owner hears the cap once per cap (#cap_notice).
|
|
859
|
-
def participant_refusal(author)
|
|
860
|
-
author = Steer.sender_author(author) or return nil
|
|
861
|
-
|
|
862
|
-
entry = participant_entry(author)
|
|
863
|
-
case ParticipantTurnMode.of(entry: entry, session: @session, now: Time.now)
|
|
864
|
-
when :outdated then return "outdated"
|
|
865
|
-
when :revoked, :not_invited then return "revoked"
|
|
866
|
-
end
|
|
867
|
-
|
|
868
|
-
cap = participant_cap(entry)
|
|
869
|
-
return nil if participant_spend(author) < cap
|
|
870
|
-
|
|
871
|
-
cap_notice(author, entry, cap)
|
|
872
|
-
"cap_reached"
|
|
873
|
-
end
|
|
874
|
-
|
|
875
|
-
# Why a participant's running pair turn stops (Engine#participant_stop,
|
|
876
|
-
# asked before each of its generations): their link ended (revoked,
|
|
877
|
-
# expired, the session archived) or their cap reached
|
|
878
|
-
# (#participant_refusal, which gives the owner the cap notice), else nil.
|
|
879
|
-
def participant_turn_stop(author)
|
|
880
|
-
reason = participant_refusal(author) or return nil
|
|
881
|
-
|
|
882
|
-
entry = participant_entry(author)
|
|
883
|
-
name = participant_label(author, entry&.name)
|
|
884
|
-
return format(REVOKED_STOP, name: name) if %w[revoked outdated].include?(reason)
|
|
885
|
-
|
|
886
|
-
format(CAP_STOP, name: name, cap: participant_cap(entry))
|
|
887
|
-
end
|
|
888
|
-
|
|
889
|
-
# +author+'s participants.json entry: this session's, a Verify fresh
|
|
890
|
-
# child's parent's (Session#participants_source_id); nil for none.
|
|
891
|
-
def participant_entry(author)
|
|
892
|
-
Participants.read(participants_id, state_dir: @state_dir).find { |e| Steer.author_for(e.id) == author }
|
|
893
|
-
end
|
|
894
|
-
|
|
895
|
-
# The session whose participants.json and audit trail this session's
|
|
896
|
-
# participants are in.
|
|
897
|
-
def participants_id = @session&.participants_source_id || @session_id
|
|
898
|
-
|
|
899
|
-
# What +author+ spent against their cap: this session's metrics (live)
|
|
900
|
-
# and, through Verify fresh, the parent's or the children's saved ones
|
|
901
|
-
# (VerifyFresh.related_spend).
|
|
902
|
-
def participant_spend(author)
|
|
903
|
-
@engine.metrics.spend(author) + VerifyFresh.related_spend(@session, author, state_dir: @state_dir)
|
|
904
|
-
end
|
|
905
|
-
|
|
906
|
-
# A participant's cap in dollars: their own, else the config's.
|
|
907
|
-
def participant_cap(entry) = entry&.cap.is_a?(Numeric) ? entry.cap.to_f : Grants.default_cap
|
|
908
|
-
|
|
909
|
-
# A participant's cleaned name, else their id.
|
|
910
|
-
def participant_label(author, name)
|
|
911
|
-
who = ParticipantName.clean(name)
|
|
912
|
-
who.empty? ? Steer.participant_id(author) : who
|
|
913
|
-
end
|
|
914
|
-
|
|
915
|
-
# The owner's notice that a participant reached their cap: once per
|
|
916
|
-
# participant and cap (a raised cap that is reached again says so again).
|
|
917
|
-
def cap_notice(author, entry, cap)
|
|
918
|
-
key = [author, cap]
|
|
919
|
-
return if (@cap_noticed ||= Set.new).include?(key)
|
|
920
|
-
|
|
921
|
-
@cap_noticed << key
|
|
922
|
-
id = Steer.participant_id(author)
|
|
923
|
-
@engine.announce(type: :hook_notice, hook: Engine::PARTICIPANTS_HOOK, level: :warn, between_turns: true,
|
|
924
|
-
text: format(CAP_NOTICE, name: participant_label(author, entry.name), cap: cap,
|
|
925
|
-
session: participants_id[0, 8], id: id))
|
|
926
|
-
end
|
|
927
|
-
|
|
928
|
-
CAP_NOTICE = "%<name>s reached their $%<cap>.2f cap: their turns stop and are refused until you raise it " \
|
|
929
|
-
"(chi share cap %<session>s %<id>s AMOUNT)."
|
|
930
|
-
# The reason a pair turn stopped at its participant's cap (the turn's
|
|
931
|
-
# "stopped the turn: …" notice).
|
|
932
|
-
CAP_STOP = "%<name>s reached their $%<cap>.2f cap"
|
|
933
|
-
# The reason a pair turn stopped because its participant's link ended
|
|
934
|
-
# (revoked, expired, or the session archived).
|
|
935
|
-
REVOKED_STOP = "%<name>s's link was revoked or expired"
|
|
936
|
-
|
|
937
|
-
# A participant's queued turn that may not start (#participant_refusal):
|
|
938
|
-
# taken off the queue (:turn_dequeued, so every UI drops its bubble)
|
|
939
|
-
# and audited; it never runs. Its settled record (canceled, the reason)
|
|
940
|
-
# ends the sender's wait on it (chi send --wait, chi web's /wait).
|
|
941
|
-
# @param origin [TurnOrigin] a participant's
|
|
942
|
-
def refuse_participant_turn(origin, reason)
|
|
943
|
-
TurnCheckpoint.record_refused(@session_dir, origin.enqueued_id, reason: reason, author: origin.participant_author)
|
|
944
|
-
Participants.log(participants_id, :turn, state_dir: @state_dir, participant: origin.participant_id,
|
|
945
|
-
enqueued_id: origin.enqueued_id, status: "refused", reason: reason)
|
|
946
|
-
@engine.announce(type: :turn_dequeued, enqueued_id: origin.enqueued_id, client_id: origin.client_id)
|
|
947
|
-
nil
|
|
948
|
-
end
|
|
949
|
-
|
|
950
845
|
# A due reminder runs as a continue turn: Engine#run_turn
|
|
951
846
|
# injects the reminders as a tail system message, and no user message is
|
|
952
847
|
# added. A prompt's turn may have injected them already (a stale latch):
|
|
@@ -1625,10 +1520,10 @@ module Samagotchi
|
|
|
1625
1520
|
end
|
|
1626
1521
|
begin
|
|
1627
1522
|
# A participant's line gets the checks their queued turn gets at
|
|
1628
|
-
# its start (#
|
|
1523
|
+
# its start (WorkerParticipantGate#refusal: the link, the cap): refused,
|
|
1629
1524
|
# it is taken off as that turn would be, and never merged.
|
|
1630
|
-
if (author = input.origin.participant_author) && (reason =
|
|
1631
|
-
|
|
1525
|
+
if (author = input.origin.participant_author) && (reason = @participant_gate.refusal(author))
|
|
1526
|
+
@participant_gate.refuse(input.origin, reason)
|
|
1632
1527
|
next
|
|
1633
1528
|
end
|
|
1634
1529
|
prompt = input.prompt.to_s.strip
|
|
@@ -1822,7 +1717,7 @@ module Samagotchi
|
|
|
1822
1717
|
return if @marked_leaving
|
|
1823
1718
|
|
|
1824
1719
|
@marked_leaving = true
|
|
1825
|
-
wait ||= @
|
|
1720
|
+
wait ||= @exit_claim.restart? ? 0 : @engine.recap_timeout.to_f
|
|
1826
1721
|
@owner_lock&.mark_leaving(wait: wait)
|
|
1827
1722
|
rescue StandardError => e
|
|
1828
1723
|
Log.warn(:worker, "mark_leaving_failed", error: e.class.name, msg: e.message)
|
|
@@ -1857,55 +1752,24 @@ module Samagotchi
|
|
|
1857
1752
|
# stop-all, so a request of another kind meanwhile is refused (409),
|
|
1858
1753
|
# not told it went through. Held at the leave, it is forgotten and the
|
|
1859
1754
|
# next request counts again.
|
|
1860
|
-
refused =
|
|
1755
|
+
refused = @exit_claim.claim(kind)
|
|
1861
1756
|
return refused.value if refused
|
|
1862
1757
|
|
|
1863
1758
|
hold = exit_hold(client_id, kind: kind, stop: stop, ask: ask)
|
|
1864
1759
|
if hold
|
|
1865
|
-
|
|
1760
|
+
@exit_claim.forget
|
|
1866
1761
|
return hold
|
|
1867
1762
|
end
|
|
1868
1763
|
|
|
1869
|
-
@
|
|
1870
|
-
@exit_requested_by = client_id
|
|
1871
|
-
@exit_deletes = kind == :delete
|
|
1872
|
-
@exit_restart = kind == :restart
|
|
1873
|
-
@exit_requested = true
|
|
1764
|
+
@exit_claim.accept(by: client_id, stop: stop)
|
|
1874
1765
|
@waker.wake
|
|
1875
1766
|
nil
|
|
1876
1767
|
rescue StandardError
|
|
1877
1768
|
# A look or a stop that raised lets the claim go (the Bridge answers 500).
|
|
1878
|
-
|
|
1769
|
+
@exit_claim.release
|
|
1879
1770
|
raise
|
|
1880
1771
|
end
|
|
1881
1772
|
|
|
1882
|
-
# One answer for an exit request that doesn't get the claim, boxed so a
|
|
1883
|
-
# nil (accepted) is told apart from "claimed".
|
|
1884
|
-
ExitRefused = Data.define(:value)
|
|
1885
|
-
|
|
1886
|
-
# @return [ExitRefused, nil] nil when this request now holds the claim;
|
|
1887
|
-
# else its answer: nil (accepted: an exit of the same kind is already
|
|
1888
|
-
# taken) or WorkerIdleExit.leaving (one of another kind is, or is still
|
|
1889
|
-
# being taken)
|
|
1890
|
-
def claim_exit(kind)
|
|
1891
|
-
@exit_claim_mutex.synchronize do
|
|
1892
|
-
next ExitRefused.new(value: nil) if @exit_claim == kind && @exit_requested
|
|
1893
|
-
next ExitRefused.new(value: WorkerIdleExit.leaving(@exit_claim)) if @exit_claim
|
|
1894
|
-
|
|
1895
|
-
@exit_claim = kind
|
|
1896
|
-
nil
|
|
1897
|
-
end
|
|
1898
|
-
end
|
|
1899
|
-
|
|
1900
|
-
# The taken exit is let go: held at the leave, or its hand-off failed.
|
|
1901
|
-
def forget_exit
|
|
1902
|
-
@exit_claim_mutex.synchronize do
|
|
1903
|
-
@exit_requested = nil
|
|
1904
|
-
@exit_restart = false
|
|
1905
|
-
@exit_claim = nil
|
|
1906
|
-
end
|
|
1907
|
-
end
|
|
1908
|
-
|
|
1909
1773
|
# What keeps the worker up for this request, nil when nothing does; a
|
|
1910
1774
|
# stop request stops what the user can stop first (#exit_request).
|
|
1911
1775
|
# @return [WorkerIdleExit::Hold, nil]
|
|
@@ -1928,7 +1792,7 @@ module Samagotchi
|
|
|
1928
1792
|
# The ask's 409: the list, for the terminal to ask with.
|
|
1929
1793
|
return hold unless stop
|
|
1930
1794
|
|
|
1931
|
-
@
|
|
1795
|
+
@exit_claim.stopped!(@hold_stop.stop_all)
|
|
1932
1796
|
# An unstoppable hold may have arrived while stopping (a child stopped
|
|
1933
1797
|
# here is no longer counted).
|
|
1934
1798
|
@idle_exit.request_hold(requester: client_id, hint: TASK_STOP_HINT)
|
|
@@ -1944,23 +1808,26 @@ module Samagotchi
|
|
|
1944
1808
|
def leave_on_request
|
|
1945
1809
|
stop_bridge_after do
|
|
1946
1810
|
@engine.synchronize_events do
|
|
1947
|
-
|
|
1811
|
+
# One read: the kind and the asker of the same exit.
|
|
1812
|
+
taken = @exit_claim.taken
|
|
1813
|
+
restart = taken&.restart?
|
|
1814
|
+
hold = if restart
|
|
1948
1815
|
@idle_exit.hold_for_restart
|
|
1949
1816
|
else
|
|
1950
|
-
@idle_exit.hold_for_request(requester:
|
|
1817
|
+
@idle_exit.hold_for_request(requester: taken&.by, streams: false)
|
|
1951
1818
|
end
|
|
1952
1819
|
if hold
|
|
1953
|
-
|
|
1820
|
+
@exit_claim.forget
|
|
1954
1821
|
Log.info(:worker, "exit_held", reason: hold)
|
|
1955
1822
|
next false
|
|
1956
1823
|
end
|
|
1957
|
-
next false if
|
|
1824
|
+
next false if restart && !carry_reminders
|
|
1958
1825
|
|
|
1959
1826
|
@bridge&.close_intake
|
|
1960
1827
|
# Not joined: a recap tick's emit waits for the event log held here.
|
|
1961
1828
|
@engine.stop_idle(join: false)
|
|
1962
|
-
Log.info(:worker,
|
|
1963
|
-
|
|
1829
|
+
Log.info(:worker, restart ? "restart_requested" : "exit_requested", by: taken&.by || "a client")
|
|
1830
|
+
restart ? :restart : :exit_requested
|
|
1964
1831
|
end
|
|
1965
1832
|
end
|
|
1966
1833
|
end
|
|
@@ -1979,7 +1846,7 @@ module Samagotchi
|
|
|
1979
1846
|
end
|
|
1980
1847
|
true
|
|
1981
1848
|
rescue StandardError => e
|
|
1982
|
-
|
|
1849
|
+
@exit_claim.forget
|
|
1983
1850
|
Log.warn(:worker, "reminders_carry_failed", error: e.class.name, msg: e.message)
|
|
1984
1851
|
false
|
|
1985
1852
|
end
|
|
@@ -2012,7 +1879,7 @@ module Samagotchi
|
|
|
2012
1879
|
# A restart keeps even an empty session (a new worker takes it over at
|
|
2013
1880
|
# once) and leaves the recap to the conversation's next real pause.
|
|
2014
1881
|
def left(result)
|
|
2015
|
-
deleting = (result == :exit_requested && @
|
|
1882
|
+
deleting = (result == :exit_requested && @exit_claim.deletes?) || result == :scratch_left
|
|
2016
1883
|
# The attached context goes with this process: a change that landed after
|
|
2017
1884
|
# the leaving pass looked at it has no later pass to note it, so note it
|
|
2018
1885
|
# here (no wake turn: the worker is on its way out). Not into a session
|