samagotchi 0.172.0 → 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 +44 -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 +20 -7
- 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 +44 -15
- data/docs/plugins.md +3 -1
- data/docs/sessions.md +2 -2
- data/docs/testing.md +43 -3
- 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/leftovers.rb +8 -4
- 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 +13 -10
- data/lib/samagotchi/web/public/card_kinds.js +51 -0
- 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 +21 -4
- 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 +16 -15
- data/lib/samagotchi/web/public/reload_check.js +90 -72
- data/lib/samagotchi/web/public/session_sync.js +4 -2
- data/lib/samagotchi/web/public/session_view.js +6 -1
- data/lib/samagotchi/web/public/stage_asking.js +18 -13
- data/lib/samagotchi/web/public/stage_place.js +144 -0
- data/lib/samagotchi/web/public/stage_probe.js +64 -0
- data/lib/samagotchi/web/public/stage_view.js +52 -104
- data/lib/samagotchi/web/public/stage_waiting.js +28 -17
- 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 +16 -1
|
@@ -6,33 +6,28 @@ require "securerandom"
|
|
|
6
6
|
require "shellwords"
|
|
7
7
|
require "time"
|
|
8
8
|
require_relative "../atomic_file"
|
|
9
|
-
require_relative "../bundler_env"
|
|
10
|
-
require_relative "../child_env"
|
|
11
9
|
require_relative "../log"
|
|
12
10
|
require_relative "../process_group"
|
|
11
|
+
require_relative "../spawn_env"
|
|
12
|
+
require_relative "../task_files"
|
|
13
|
+
require_relative "../task_transcript"
|
|
14
|
+
require_relative "../tool_names"
|
|
13
15
|
require_relative "leftovers"
|
|
14
16
|
require_relative "shell"
|
|
15
17
|
require_relative "task_record"
|
|
16
18
|
|
|
17
19
|
module Samagotchi
|
|
18
20
|
module Tools
|
|
19
|
-
# Execute requires this file (it hands a command over with adopt), and
|
|
20
|
-
# this one needs Execute only at run time: autoload keeps the two from
|
|
21
|
-
# requiring each other.
|
|
22
|
-
autoload :Execute, File.expand_path("execute", __dir__)
|
|
23
|
-
|
|
24
21
|
module TaskRuntime
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
22
|
+
# Moved to TaskFiles; kept here one release for bundles that name
|
|
23
|
+
# them (the boundary's stable list). Remove in the release after the
|
|
24
|
+
# one that ships TaskFiles, with their stable-list entries.
|
|
25
|
+
TASKS_DIR = TaskFiles::TASKS_DIR
|
|
26
|
+
EXIT_CODE_FILENAME = TaskFiles::EXIT_CODE_FILENAME
|
|
29
27
|
STOP_GRACE_SEC = 3.0
|
|
30
28
|
STOP_POLL_INTERVAL_SEC = 0.1
|
|
31
29
|
METADATA_VERSION = TaskRecord::METADATA_VERSION
|
|
32
30
|
OUTPUT_TAIL_READ_BYTES = 16 * 1024
|
|
33
|
-
SANITIZED_ENV_KEYS = BundlerEnv::CHILD_RUBY
|
|
34
|
-
# Set by chi (Builtins.parent_env); the model's env can't change it.
|
|
35
|
-
MARKER_ENV_KEYS = ChildEnv::SESSION_MARKERS
|
|
36
31
|
# task_wait and task_get add it to a task the user stopped (the web's
|
|
37
32
|
# stop-task button), so the model doesn't rerun the job.
|
|
38
33
|
# A record whose pid isn't a process group chi started (missing, 0, 1,
|
|
@@ -73,17 +68,17 @@ module Samagotchi
|
|
|
73
68
|
resolved_cwd = normalize_cwd(cwd)
|
|
74
69
|
return [nil, "Error: cwd not found: #{resolved_cwd}"] unless Dir.exist?(resolved_cwd)
|
|
75
70
|
|
|
76
|
-
spawn_env, env_error = spawn_env_for(env)
|
|
71
|
+
spawn_env, env_error = SpawnEnv.spawn_env_for(env)
|
|
77
72
|
return [nil, env_error] if env_error
|
|
78
73
|
|
|
79
74
|
spawn_env = spawn_env.merge(marker_env || {})
|
|
80
75
|
|
|
81
76
|
task_id = generate_task_id
|
|
82
|
-
task_dir = task_dir_for(task_id)
|
|
83
|
-
output_path = File.join(task_dir, OUTPUT_FILENAME)
|
|
77
|
+
task_dir = TaskFiles.task_dir_for(task_id)
|
|
78
|
+
output_path = File.join(task_dir, TaskFiles::OUTPUT_FILENAME)
|
|
84
79
|
# Absolute: the trap writes it wherever the shell is when it exits
|
|
85
80
|
# (a +cwd+, or a cd in the command). Ruby opens output_path here.
|
|
86
|
-
exit_code_path = File.expand_path(File.join(task_dir, EXIT_CODE_FILENAME))
|
|
81
|
+
exit_code_path = File.expand_path(File.join(task_dir, TaskFiles::EXIT_CODE_FILENAME))
|
|
87
82
|
|
|
88
83
|
FileUtils.mkdir_p(task_dir)
|
|
89
84
|
|
|
@@ -140,7 +135,7 @@ module Samagotchi
|
|
|
140
135
|
# @return [Thread]
|
|
141
136
|
def record_exit_status(waiter, path, task_id:)
|
|
142
137
|
# Absolute now: safe against a later chdir.
|
|
143
|
-
record_path = File.expand_path(metadata_path(task_id))
|
|
138
|
+
record_path = File.expand_path(TaskFiles.metadata_path(task_id))
|
|
144
139
|
path = File.expand_path(path)
|
|
145
140
|
started = record_at(record_path)
|
|
146
141
|
logs = log_paths(started).map { |log| File.expand_path(log) }
|
|
@@ -285,7 +280,7 @@ module Samagotchi
|
|
|
285
280
|
end
|
|
286
281
|
|
|
287
282
|
# A record for a command chi already runs: an execute still running at
|
|
288
|
-
# its limit, handed over as a task (Execute::HANDED_OVER), given as a
|
|
283
|
+
# its limit, handed over as a task (ToolNames::Execute::HANDED_OVER), given as a
|
|
289
284
|
# TaskRecord.running whose +started_at+ is the spawn's. Its logs and
|
|
290
285
|
# exit_code file stay where execute put them, so the paths must be
|
|
291
286
|
# absolute; +stderr_path+ is a log of its own (task_wait tails it).
|
|
@@ -297,7 +292,7 @@ module Samagotchi
|
|
|
297
292
|
def adopt(task)
|
|
298
293
|
id = task.id
|
|
299
294
|
return [nil, "Error: invalid task id: #{id}"] unless TASK_ID_SHAPE.match?(id.to_s)
|
|
300
|
-
return [nil, "Error: task already exists: #{id}"] if File.exist?(metadata_path(id))
|
|
295
|
+
return [nil, "Error: task already exists: #{id}"] if File.exist?(TaskFiles.metadata_path(id))
|
|
301
296
|
|
|
302
297
|
relative = %i[output_path stderr_path exit_code_path].find { |key| !File.absolute_path?(task.public_send(key).to_s) }
|
|
303
298
|
return [nil, "Error: #{relative} must be absolute: #{task.public_send(relative)}"] if relative
|
|
@@ -307,7 +302,7 @@ module Samagotchi
|
|
|
307
302
|
|
|
308
303
|
cwd = task.cwd
|
|
309
304
|
record = task.with(command: task.command.to_s.strip, cwd: File.expand_path(cwd.to_s.empty? ? Dir.pwd : cwd),
|
|
310
|
-
pid_started: ProcessGroup.start_time(pid), handed_over_by: Execute::NAME)
|
|
305
|
+
pid_started: ProcessGroup.start_time(pid), handed_over_by: ToolNames::Execute::NAME)
|
|
311
306
|
write_record(record)
|
|
312
307
|
[record, nil]
|
|
313
308
|
rescue StandardError => e
|
|
@@ -316,7 +311,7 @@ module Samagotchi
|
|
|
316
311
|
|
|
317
312
|
def list_records
|
|
318
313
|
ensure_tasks_dir
|
|
319
|
-
task_dirs = Dir.glob(File.join(TASKS_DIR, "*"))
|
|
314
|
+
task_dirs = Dir.glob(File.join(TaskFiles::TASKS_DIR, "*"))
|
|
320
315
|
.select { |path| File.directory?(path) }
|
|
321
316
|
|
|
322
317
|
task_dirs.map do |task_dir|
|
|
@@ -332,83 +327,15 @@ module Samagotchi
|
|
|
332
327
|
# task_create calls and the executes it handed over at their limit.
|
|
333
328
|
# Only those results count: records are shared by every session in the
|
|
334
329
|
# worker's cwd, and task_list shows them all.
|
|
335
|
-
#
|
|
330
|
+
# TaskTranscript.created_ids_in reads which.
|
|
336
331
|
# @return [Array<Hash>] {id:, command:}, oldest first
|
|
337
332
|
def running_created_in(messages)
|
|
338
|
-
created_ids_in(messages).filter_map do |id|
|
|
333
|
+
TaskTranscript.created_ids_in(messages).filter_map do |id|
|
|
339
334
|
record, _error = get_record(id)
|
|
340
335
|
{ id: id, command: record.command.to_s } if record&.running?
|
|
341
336
|
end
|
|
342
337
|
end
|
|
343
338
|
|
|
344
|
-
# The ids of every task this conversation started (task_create, a
|
|
345
|
-
# handed-over execute), whatever their status now, oldest first.
|
|
346
|
-
# @return [Array<String>]
|
|
347
|
-
def created_ids_in(messages)
|
|
348
|
-
Array(messages).flat_map do |message|
|
|
349
|
-
next [] unless (message[:role] || message["role"]).to_s == "tool_response"
|
|
350
|
-
|
|
351
|
-
(message[:content] || message["content"]).to_s.split("\n\n---\n\n").filter_map do |block|
|
|
352
|
-
block[CREATED_BLOCK, 1] || handed_over_id(block)
|
|
353
|
-
end
|
|
354
|
-
end.uniq
|
|
355
|
-
end
|
|
356
|
-
|
|
357
|
-
# A task_create result as the model gets it: its "[task_create]" lead,
|
|
358
|
-
# after the one "ran as: …" line a gate-corrected call has.
|
|
359
|
-
RAN_AS_LINE = "(?:ran as: [^\n]*\n)?"
|
|
360
|
-
CREATED_BLOCK = /\A#{RAN_AS_LINE}\[task_create\]\ntask_id: (\S+)/
|
|
361
|
-
CREATED_OUTPUT = /\A#{RAN_AS_LINE}(?:\[task_create\]\n)?task_id: (\S+)/
|
|
362
|
-
|
|
363
|
-
# The task id of a tool output that started a task, the "[tool]" lead
|
|
364
|
-
# optional (a UI's row has it or not): a task_create's, or an execute's
|
|
365
|
-
# handed_over_id. nil for any other tool or output.
|
|
366
|
-
# @return [String, nil]
|
|
367
|
-
def created_id(tool, text)
|
|
368
|
-
case tool
|
|
369
|
-
when "task_create" then text.to_s[CREATED_OUTPUT, 1]
|
|
370
|
-
when Execute::NAME then handed_over_id(text)
|
|
371
|
-
end
|
|
372
|
-
end
|
|
373
|
-
|
|
374
|
-
# The task id an execute's hand-over result names: anchored on its
|
|
375
|
-
# first line (Execute::HANDED_OVER), never a "task_id:" line anywhere,
|
|
376
|
-
# which the command's own output can print. The "[execute]" lead and
|
|
377
|
-
# a "ran as: …" line before it are optional. web/public/task_stop.js
|
|
378
|
-
# has the JS twin (handedOverId).
|
|
379
|
-
# @return [String, nil]
|
|
380
|
-
def handed_over_id(text)
|
|
381
|
-
text = text.to_s
|
|
382
|
-
# A regexp raises on bytes that aren't UTF-8 (a command's raw output).
|
|
383
|
-
text = text.scrub unless text.valid_encoding?
|
|
384
|
-
text[handed_over_pattern, 1]
|
|
385
|
-
end
|
|
386
|
-
|
|
387
|
-
def handed_over_pattern
|
|
388
|
-
@handed_over_pattern ||= /\A#{RAN_AS_LINE}(?:\[#{Execute::NAME}\]\n)?#{Regexp.escape(Execute::HANDED_OVER)} #{Execute::HANDED_OVER_AFTER}[^\n]*\ntask_id: (\S+)/
|
|
389
|
-
end
|
|
390
|
-
|
|
391
|
-
# A task id as task_create makes them: no path parts.
|
|
392
|
-
TASK_ID = /\A[A-Za-z0-9_-]+\z/
|
|
393
|
-
|
|
394
|
-
# The command of the task +task_id+ under +root+ (a session's working
|
|
395
|
-
# directory) as its record has it, for a UI row's title; nil for no
|
|
396
|
-
# such task or an unreadable record. Reads the record only (no
|
|
397
|
-
# refresh), so it is cheap and never fails.
|
|
398
|
-
# @return [String, nil]
|
|
399
|
-
def command_of(task_id, root: Dir.pwd)
|
|
400
|
-
id = task_id.to_s.strip
|
|
401
|
-
return nil unless id.match?(TASK_ID)
|
|
402
|
-
|
|
403
|
-
path = File.join(root || Dir.pwd, TASKS_DIR, id, METADATA_FILENAME)
|
|
404
|
-
return nil unless File.file?(path)
|
|
405
|
-
|
|
406
|
-
command = TaskRecord.from_file(JSON.parse(File.read(path)))&.command
|
|
407
|
-
command.is_a?(String) && !command.strip.empty? ? command : nil
|
|
408
|
-
rescue StandardError
|
|
409
|
-
nil
|
|
410
|
-
end
|
|
411
|
-
|
|
412
339
|
def get_record(task_id)
|
|
413
340
|
record = load_record(task_id)
|
|
414
341
|
return [nil, not_found(task_id)] unless record
|
|
@@ -562,7 +489,7 @@ module Samagotchi
|
|
|
562
489
|
def load_record(task_id)
|
|
563
490
|
return nil unless TASK_ID_SHAPE.match?(task_id.to_s)
|
|
564
491
|
|
|
565
|
-
path = metadata_path(task_id)
|
|
492
|
+
path = TaskFiles.metadata_path(task_id)
|
|
566
493
|
return nil unless File.exist?(path)
|
|
567
494
|
|
|
568
495
|
TaskRecord.from_file(JSON.parse(File.read(path)))
|
|
@@ -572,31 +499,23 @@ module Samagotchi
|
|
|
572
499
|
|
|
573
500
|
def write_record(record)
|
|
574
501
|
ensure_tasks_dir
|
|
575
|
-
dir = task_dir_for(record.id)
|
|
502
|
+
dir = TaskFiles.task_dir_for(record.id)
|
|
576
503
|
FileUtils.mkdir_p(dir)
|
|
577
504
|
|
|
578
|
-
path = File.join(dir, METADATA_FILENAME)
|
|
505
|
+
path = File.join(dir, TaskFiles::METADATA_FILENAME)
|
|
579
506
|
AtomicFile.write(path, record_json(record))
|
|
580
507
|
end
|
|
581
508
|
|
|
582
509
|
def record_json(record) = JSON.pretty_generate(record.to_file) + "\n"
|
|
583
510
|
|
|
584
|
-
def metadata_path(task_id)
|
|
585
|
-
File.join(task_dir_for(task_id), METADATA_FILENAME)
|
|
586
|
-
end
|
|
587
|
-
|
|
588
|
-
def task_dir_for(task_id)
|
|
589
|
-
File.join(TASKS_DIR, task_id.to_s)
|
|
590
|
-
end
|
|
591
|
-
|
|
592
511
|
def ensure_tasks_dir
|
|
593
|
-
FileUtils.mkdir_p(TASKS_DIR)
|
|
512
|
+
FileUtils.mkdir_p(TaskFiles::TASKS_DIR)
|
|
594
513
|
end
|
|
595
514
|
|
|
596
515
|
def generate_task_id
|
|
597
516
|
loop do
|
|
598
517
|
candidate = "#{Time.now.utc.strftime("%Y%m%d%H%M%S")}-#{SecureRandom.hex(4)}"
|
|
599
|
-
return candidate unless File.exist?(metadata_path(candidate))
|
|
518
|
+
return candidate unless File.exist?(TaskFiles.metadata_path(candidate))
|
|
600
519
|
end
|
|
601
520
|
end
|
|
602
521
|
|
|
@@ -620,54 +539,6 @@ module Samagotchi
|
|
|
620
539
|
"trap #{Shellwords.escape(write)} EXIT\n#{command}\n"
|
|
621
540
|
end
|
|
622
541
|
|
|
623
|
-
# chi's own Ruby/Bundler setup, kept out of a command's environment
|
|
624
|
-
# (execute and task_create both spawn with it): run under `bundle
|
|
625
|
-
# exec`, chi would otherwise hand a project's `ruby -e` or
|
|
626
|
-
# `bundle exec` its Gemfile (RUBYOPT=-rbundler/setup, BUNDLE_GEMFILE,
|
|
627
|
-
# BUNDLER_SETUP) and lockfile.
|
|
628
|
-
def sanitized_spawn_env
|
|
629
|
-
SANITIZED_ENV_KEYS.to_h { |key| [key, nil] }.merge("RUBYOPT" => sanitized_rubyopt)
|
|
630
|
-
end
|
|
631
|
-
|
|
632
|
-
# The model's way out: a Ruby or Bundler variable it does want, for one command.
|
|
633
|
-
def sanitized_key_error(key)
|
|
634
|
-
"Error: env key is reserved: #{key} (chi clears it for every command; to set it for this one, " \
|
|
635
|
-
"put it in the command: #{key}=value cmd)"
|
|
636
|
-
end
|
|
637
|
-
|
|
638
|
-
def spawn_env_for(overrides)
|
|
639
|
-
return [sanitized_spawn_env, nil] if overrides.nil? || overrides.to_s.empty?
|
|
640
|
-
|
|
641
|
-
overrides = JSON.parse(overrides) if overrides.is_a?(String)
|
|
642
|
-
return [nil, "Error: env must be an object"] unless overrides.is_a?(Hash)
|
|
643
|
-
|
|
644
|
-
normalized = {}
|
|
645
|
-
overrides.each do |key, value|
|
|
646
|
-
return [nil, "Error: env keys and values must be strings"] unless key.is_a?(String) && value.is_a?(String)
|
|
647
|
-
return [nil, "Error: env keys and values cannot contain NUL bytes"] if key.include?("\0") || value.include?("\0")
|
|
648
|
-
return [nil, "Error: env key is reserved: #{key}"] if MARKER_ENV_KEYS.include?(key)
|
|
649
|
-
return [nil, sanitized_key_error(key)] if SANITIZED_ENV_KEYS.include?(key)
|
|
650
|
-
|
|
651
|
-
normalized[key] = value
|
|
652
|
-
end
|
|
653
|
-
|
|
654
|
-
[sanitized_spawn_env.merge(normalized), nil]
|
|
655
|
-
rescue JSON::ParserError
|
|
656
|
-
[nil, "Error: env must be a JSON object"]
|
|
657
|
-
end
|
|
658
|
-
|
|
659
|
-
def sanitized_rubyopt
|
|
660
|
-
rubyopt = ENV["RUBYOPT"].to_s
|
|
661
|
-
return nil if rubyopt.empty?
|
|
662
|
-
|
|
663
|
-
filtered = rubyopt.split(/\s+/).reject do |arg|
|
|
664
|
-
arg == "-rbundler/setup" || arg.match?(%r{\A-r.*bundler/setup\z})
|
|
665
|
-
end
|
|
666
|
-
return nil if filtered.empty?
|
|
667
|
-
|
|
668
|
-
filtered.join(" ")
|
|
669
|
-
end
|
|
670
|
-
|
|
671
542
|
# @param leftovers [Symbol, nil] what a refresh found left holding the log
|
|
672
543
|
def mark_finished_without_exit_code(record, missing_reason: "process_ended_without_exit_code", leftovers: nil)
|
|
673
544
|
record = current_record(record)
|
|
@@ -719,6 +590,15 @@ module Samagotchi
|
|
|
719
590
|
def timestamp
|
|
720
591
|
Time.now.utc.iso8601
|
|
721
592
|
end
|
|
593
|
+
|
|
594
|
+
# No caller outside this file. A function a spec calls by name stays
|
|
595
|
+
# public (refresh_record, waiter_for, load_record,
|
|
596
|
+
# write_record, quiesce!…): no spec sends a private one.
|
|
597
|
+
private_class_method :remember_waiter, :forget_waiter, :leftovers_after_exit, :settle, :log_paths,
|
|
598
|
+
:status_code, :correct_finished_record, :record_at,
|
|
599
|
+
:leftover_group?, :settle_from_refresh, :chis_group?, :mark_not_chis, :not_found,
|
|
600
|
+
:record_json, :ensure_tasks_dir, :normalize_cwd, :mark_finished_without_exit_code, :finished_with, :current_record,
|
|
601
|
+
:mark_stopped, :read_exit_code, :timestamp
|
|
722
602
|
end
|
|
723
603
|
end
|
|
724
604
|
end
|
|
@@ -2,12 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "task_runtime"
|
|
4
4
|
require_relative "ref_text"
|
|
5
|
+
require_relative "../tool_names"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
7
8
|
module Tools
|
|
8
9
|
class TaskWait
|
|
9
|
-
NAME =
|
|
10
|
-
TIMEOUT_DEFAULT =
|
|
10
|
+
NAME = ToolNames::TaskWait::NAME
|
|
11
|
+
TIMEOUT_DEFAULT = ToolNames::TaskWait::TIMEOUT_DEFAULT
|
|
11
12
|
TAIL_LINES_DEFAULT = 10
|
|
12
13
|
TAIL_LINES_MAX = 100
|
|
13
14
|
POLL_INTERVAL = 0.5
|
|
@@ -4,12 +4,13 @@ require "net/http"
|
|
|
4
4
|
require "uri"
|
|
5
5
|
require "ipaddr"
|
|
6
6
|
require_relative "../version"
|
|
7
|
+
require_relative "../tool_names"
|
|
7
8
|
|
|
8
9
|
module Samagotchi
|
|
9
10
|
module Tools
|
|
10
11
|
# Fetches the content of a URL and returns cleaned text content.
|
|
11
12
|
class WebFetch
|
|
12
|
-
NAME =
|
|
13
|
+
NAME = ToolNames::WebFetch::NAME
|
|
13
14
|
TIMEOUT_SEC = 15
|
|
14
15
|
MAX_OUTPUT_BYTES = 32 * 1024
|
|
15
16
|
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "fileutils"
|
|
4
4
|
require_relative "tool_path"
|
|
5
|
+
require_relative "../tool_names"
|
|
5
6
|
|
|
6
7
|
module Samagotchi
|
|
7
8
|
module Tools
|
|
@@ -9,7 +10,7 @@ module Samagotchi
|
|
|
9
10
|
# The path is passed as an XML attribute:
|
|
10
11
|
# <tool name="write" path="lib/samagotchi/tools/new_tool.rb">content</tool>
|
|
11
12
|
class Write
|
|
12
|
-
NAME =
|
|
13
|
+
NAME = ToolNames::Write::NAME
|
|
13
14
|
|
|
14
15
|
def self.name = NAME
|
|
15
16
|
|
|
@@ -122,7 +122,7 @@ module Samagotchi
|
|
|
122
122
|
Log.warn(:worker, "command_outcome_write_failed", error: e.class.name, msg: e.message, enqueued_id: id)
|
|
123
123
|
end
|
|
124
124
|
|
|
125
|
-
# A participant's queued turn the worker refused (
|
|
125
|
+
# A participant's queued turn the worker refused (WorkerParticipantGate#refuse:
|
|
126
126
|
# +reason+ revoked or cap_reached) ends its sender's wait: no turn ran,
|
|
127
127
|
# canceled for that reason (ParentReport reads it as refused). Its
|
|
128
128
|
# +author+ (the participant's "p:<id>") says whose wait it is to a chi
|
data/lib/samagotchi/version.rb
CHANGED
data/lib/samagotchi/web/app.rb
CHANGED
|
@@ -25,7 +25,7 @@ require_relative "../bridge/card_store"
|
|
|
25
25
|
require_relative "../session"
|
|
26
26
|
require_relative "../session_manager"
|
|
27
27
|
require_relative "../session_metrics"
|
|
28
|
-
require_relative "../
|
|
28
|
+
require_relative "../session_command_catalog"
|
|
29
29
|
require_relative "../steer"
|
|
30
30
|
require_relative "../host_registry"
|
|
31
31
|
require_relative "../effective_model"
|
|
@@ -68,8 +68,10 @@ require_relative "session_hub"
|
|
|
68
68
|
require_relative "project_list"
|
|
69
69
|
require_relative "session_access_routes"
|
|
70
70
|
require_relative "principal"
|
|
71
|
+
require_relative "request_body"
|
|
71
72
|
require_relative "viewer"
|
|
72
73
|
require_relative "participant_access"
|
|
74
|
+
require_relative "participant_history"
|
|
73
75
|
require_relative "participant_images"
|
|
74
76
|
require_relative "participants_admin"
|
|
75
77
|
require_relative "participant_review"
|
|
@@ -106,8 +108,10 @@ module Samagotchi
|
|
|
106
108
|
class App
|
|
107
109
|
# @lan holds the LAN access token.
|
|
108
110
|
include RedactedInspect
|
|
111
|
+
include RequestBody
|
|
109
112
|
include SessionAccessRoutes
|
|
110
113
|
include ParticipantAccess
|
|
114
|
+
include ParticipantHistory
|
|
111
115
|
include ParticipantImages
|
|
112
116
|
include ParticipantsAdmin
|
|
113
117
|
include ParticipantReview
|
|
@@ -325,6 +329,7 @@ module Samagotchi
|
|
|
325
329
|
["POST", %r{\A/api/sessions/([^/]+)/context/([^/]+)/push\z}, :handle_context_push, :owner, :session],
|
|
326
330
|
["DELETE", %r{\A/api/sessions/([^/]+)/context/([^/]+)\z}, :handle_context_delete, :owner, :session],
|
|
327
331
|
["GET", %r{\A/api/sessions/([^/]+)/working_set\z}, :handle_working_set, :participant, :session],
|
|
332
|
+
["GET", %r{\A/api/sessions/([^/]+)/prompt_history\z}, :handle_prompt_history, :participant, :session],
|
|
328
333
|
["GET", %r{\A/api/sessions/([^/]+)/working_set/item\z}, :handle_working_set_item, :owner, :session],
|
|
329
334
|
["GET", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_list, :owner, :session],
|
|
330
335
|
["POST", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_invite, :owner, :session],
|
|
@@ -386,6 +391,11 @@ module Samagotchi
|
|
|
386
391
|
# with an invalid groups: (Toolkit.check_new!), whichever handler
|
|
387
392
|
# spawned it.
|
|
388
393
|
error_response(400, "toolkit_refused", e.message)
|
|
394
|
+
rescue BodyTooLarge => e
|
|
395
|
+
# Connection: close, so Web::Server ends the connection rather than
|
|
396
|
+
# read the rest of the body to keep it alive.
|
|
397
|
+
status, headers, body = error_response(413, "too_large", e.message)
|
|
398
|
+
[status, headers.merge("Connection" => "close"), body]
|
|
389
399
|
rescue StandardError => e
|
|
390
400
|
Log.exception(:web, "request_failed", e, method: env["REQUEST_METHOD"], path: env["PATH_INFO"])
|
|
391
401
|
# A participant never reads an exception's words (they may name a
|
|
@@ -600,14 +610,18 @@ module Samagotchi
|
|
|
600
610
|
# but for a grant's code bound to another identity: decision C, kept
|
|
601
611
|
# for the right one, and refused token_bound, #code_token).
|
|
602
612
|
def traded_token(req)
|
|
603
|
-
|
|
613
|
+
# The page's trade is the query string's only: req.params would
|
|
614
|
+
# parse a urlencoded or multipart body too, before the gate.
|
|
615
|
+
return req.GET["token"] unless fragment_trade?(req)
|
|
604
616
|
|
|
605
617
|
req.env.fetch(TRADED_TOKEN_ENV) do
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
618
|
+
value = begin
|
|
619
|
+
# Under TRADE_BODY_LIMIT: a body of the limit or more is no token.
|
|
620
|
+
raw = request_body(req, TRADE_BODY_LIMIT - 1)
|
|
621
|
+
form_post?(req) ? Rack::Utils.parse_query(raw) : parse_json(raw)
|
|
622
|
+
rescue BodyTooLarge
|
|
623
|
+
nil
|
|
624
|
+
end
|
|
611
625
|
value = {} unless value.is_a?(Hash)
|
|
612
626
|
req.env[TRADED_TOKEN_ENV] = value.key?("code") ? code_token(req, value["code"]) : string_or_nil(value["token"])
|
|
613
627
|
end
|
|
@@ -683,7 +697,7 @@ module Samagotchi
|
|
|
683
697
|
# Through the proxy no GET trade (a ?token= rides the sign-in
|
|
684
698
|
# redirect, and a crafted link would swap the owner's cookie): a
|
|
685
699
|
# link's #code= and the 401 page's POST do it.
|
|
686
|
-
page = req.get? && PAGE_PATHS.include?(req.path_info) && req.
|
|
700
|
+
page = req.get? && PAGE_PATHS.include?(req.path_info) && req.GET.key?("token") && !(@public_url && proxied?(req))
|
|
687
701
|
return false unless page || fragment_trade?(req)
|
|
688
702
|
|
|
689
703
|
token_required?(req) || Grants.token?(traded_token(req))
|
|
@@ -752,10 +766,7 @@ module Samagotchi
|
|
|
752
766
|
def handle_update(req)
|
|
753
767
|
return error_response(503, "no_hub", "this chi web has no session hub") unless @hub
|
|
754
768
|
|
|
755
|
-
|
|
756
|
-
return error_response(413, "body_too_large", "update request is too large") if raw.bytesize > 16_384
|
|
757
|
-
|
|
758
|
-
payload = parse_json(raw)
|
|
769
|
+
payload = parse_json(request_body(req, 16_384, what: "an update request"))
|
|
759
770
|
return error_response(400, "invalid_update", "expected a JSON object with a version") unless payload.is_a?(Hash)
|
|
760
771
|
|
|
761
772
|
version = payload["version"]
|
|
@@ -986,15 +997,15 @@ module Samagotchi
|
|
|
986
997
|
def handle_list(req)
|
|
987
998
|
return error_response(503, "no_hub", "this chi web has no session hub") unless @hub
|
|
988
999
|
|
|
989
|
-
dir, error = scope_dir(req.
|
|
1000
|
+
dir, error = scope_dir(req.GET["dir"])
|
|
990
1001
|
return error if error
|
|
991
1002
|
|
|
992
1003
|
root = dir && ProjectScope.root_for(dir)
|
|
993
1004
|
# Lazy retention sweep (once per 24h)
|
|
994
1005
|
Degrade.call(:web, "retention_sweep_failed") { @manager.retention_sweep_if_due(state_dir: @state_dir) }
|
|
995
|
-
limit = sanitize_limit(req.
|
|
996
|
-
offset = sanitize_offset(req.
|
|
997
|
-
all = @hub.snapshot(project_root: root, sort: sanitize_sort(req.
|
|
1006
|
+
limit = sanitize_limit(req.GET["limit"])
|
|
1007
|
+
offset = sanitize_offset(req.GET["offset"])
|
|
1008
|
+
all = @hub.snapshot(project_root: root, sort: sanitize_sort(req.GET["sort"]), order: sanitize_order(req.GET["order"]))
|
|
998
1009
|
page = all.drop(offset)
|
|
999
1010
|
page = page.first(limit) if limit
|
|
1000
1011
|
headers = { "Content-Type" => "application/json; charset=utf-8", "Cache-Control" => "no-store" }
|
|
@@ -1007,7 +1018,7 @@ module Samagotchi
|
|
|
1007
1018
|
def handle_projects(req)
|
|
1008
1019
|
return error_response(503, "no_hub", "this chi web has no session hub") unless @hub
|
|
1009
1020
|
|
|
1010
|
-
dir, error = scope_dir(req.
|
|
1021
|
+
dir, error = scope_dir(req.GET["dir"])
|
|
1011
1022
|
return error if error
|
|
1012
1023
|
|
|
1013
1024
|
current_root = dir && ProjectScope.root_for(dir)
|
|
@@ -1408,7 +1419,7 @@ module Samagotchi
|
|
|
1408
1419
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
1409
1420
|
own = ContextSources.session_location(id, state_dir: default_state_dir)
|
|
1410
1421
|
subs = own.subscriptions
|
|
1411
|
-
full = truthy_param?(req.
|
|
1422
|
+
full = truthy_param?(req.GET["full"])
|
|
1412
1423
|
rows = attached_context(session, shadowed: full).map { |attached| context_row(attached, own, subs, full: full) }
|
|
1413
1424
|
body = { session_id: id, sources: rows, can_add_url: ContextProviders.any? }
|
|
1414
1425
|
# The candidates too, for chi context ls (no UI shows them yet).
|
|
@@ -1426,7 +1437,7 @@ module Samagotchi
|
|
|
1426
1437
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
1427
1438
|
attached = find_context(session, name) or return error_response(404, "context_missing", "no source #{name[0, 40]}")
|
|
1428
1439
|
own = ContextSources.session_location(id, state_dir: default_state_dir)
|
|
1429
|
-
row = context_row(attached, own, own.subscriptions, full: truthy_param?(req.
|
|
1440
|
+
row = context_row(attached, own, own.subscriptions, full: truthy_param?(req.GET["full"]))
|
|
1430
1441
|
json_response(200, row.merge(text: attached.snapshot.text))
|
|
1431
1442
|
end
|
|
1432
1443
|
|
|
@@ -1507,7 +1518,7 @@ module Samagotchi
|
|
|
1507
1518
|
# detached: a project's is 409 project_source, as chi context rm
|
|
1508
1519
|
# refuses it.
|
|
1509
1520
|
def handle_context_delete(req, id, name)
|
|
1510
|
-
return context_remove_own(id, name) if truthy_param?(req.
|
|
1521
|
+
return context_remove_own(id, name) if truthy_param?(req.GET["own"])
|
|
1511
1522
|
|
|
1512
1523
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
1513
1524
|
attached = find_context(session, name) or return error_response(404, "context_missing", "no source #{name[0, 40]}")
|
|
@@ -1641,7 +1652,7 @@ module Samagotchi
|
|
|
1641
1652
|
def handle_events(req)
|
|
1642
1653
|
return error_response(503, "no_hub", "this chi web has no session hub") unless @hub
|
|
1643
1654
|
|
|
1644
|
-
dir, error = scope_dir(req.
|
|
1655
|
+
dir, error = scope_dir(req.GET["dir"])
|
|
1645
1656
|
return error if error
|
|
1646
1657
|
|
|
1647
1658
|
root = dir && ProjectScope.root_for(dir)
|
|
@@ -1905,13 +1916,13 @@ module Samagotchi
|
|
|
1905
1916
|
# participant's page shows it (claim_link.js).
|
|
1906
1917
|
attrs["home"] = Dir.home
|
|
1907
1918
|
# Only when on: the page reads its presence.
|
|
1908
|
-
attrs["working-set"] = "1" if working_set?(req.
|
|
1919
|
+
attrs["working-set"] = "1" if working_set?(req.GET["working_set"])
|
|
1909
1920
|
# The opt-in reload check (reload_check.js): only when on.
|
|
1910
1921
|
attrs["reload-check"] = "1" if @reload_check
|
|
1911
1922
|
attrs.merge!(annotate_presets_data)
|
|
1912
1923
|
# Who looks and what they may do (Viewer; viewer.js reads it at boot).
|
|
1913
1924
|
attrs.merge!(Viewer.for(principal(req)).body_attributes)
|
|
1914
|
-
dir, = scope_dir(req.
|
|
1925
|
+
dir, = scope_dir(req.GET["dir"])
|
|
1915
1926
|
unless dir
|
|
1916
1927
|
dir = default_scope_dir(req)
|
|
1917
1928
|
attrs["scope-dir"] = dir if dir
|
|
@@ -1923,7 +1934,7 @@ module Samagotchi
|
|
|
1923
1934
|
elsif dir.nil?
|
|
1924
1935
|
# The all view's way back: the project view it came from
|
|
1925
1936
|
# (?from=), else the project chi web starts in.
|
|
1926
|
-
back, = scope_dir(req.
|
|
1937
|
+
back, = scope_dir(req.GET["from"])
|
|
1927
1938
|
back ||= start_dir
|
|
1928
1939
|
if (back_root = ProjectScope.root_for(back))
|
|
1929
1940
|
attrs["back-name"] = File.basename(back_root)
|
|
@@ -1942,7 +1953,7 @@ module Samagotchi
|
|
|
1942
1953
|
def participant_index_data(req)
|
|
1943
1954
|
grant = principal(req).grant
|
|
1944
1955
|
attrs = { "version" => Samagotchi::VERSION, "session" => grant.session_id }
|
|
1945
|
-
attrs["working-set"] = "1" if working_set?(req.
|
|
1956
|
+
attrs["working-set"] = "1" if working_set?(req.GET["working_set"])
|
|
1946
1957
|
title = Degrade.call(:web, "participant_title_unreadable", fallback: nil) do
|
|
1947
1958
|
participant_title(@session_class.load(grant.session_id, state_dir: default_state_dir))
|
|
1948
1959
|
end
|
|
@@ -1964,8 +1975,8 @@ module Samagotchi
|
|
|
1964
1975
|
# start folder when it is still a folder in a repo, else nil (every
|
|
1965
1976
|
# session).
|
|
1966
1977
|
def default_scope_dir(req)
|
|
1967
|
-
return nil unless req.
|
|
1968
|
-
return nil if req.
|
|
1978
|
+
return nil unless req.GET["dir"].to_s.strip.empty?
|
|
1979
|
+
return nil if req.GET["scope"] == "all"
|
|
1969
1980
|
|
|
1970
1981
|
dir = File.expand_path(start_dir)
|
|
1971
1982
|
File.directory?(dir) && ProjectScope.root_for(dir) ? dir : nil
|
|
@@ -1986,15 +1997,6 @@ module Samagotchi
|
|
|
1986
1997
|
[status, { "Content-Type" => "application/json; charset=utf-8", "Content-Length" => body.bytesize.to_s, "Cache-Control" => "no-store" }, [body]]
|
|
1987
1998
|
end
|
|
1988
1999
|
|
|
1989
|
-
# The request body, "" when there is none: WEBrick (through rackup)
|
|
1990
|
-
# raises LengthRequired on reading a POST that has neither a
|
|
1991
|
-
# Content-Length nor a chunked body (plain `curl -X POST`).
|
|
1992
|
-
def request_body(req, limit = nil)
|
|
1993
|
-
return "" if req.get_header("CONTENT_LENGTH").nil? && req.get_header("HTTP_TRANSFER_ENCODING").nil?
|
|
1994
|
-
|
|
1995
|
-
req.body.read(*limit)
|
|
1996
|
-
end
|
|
1997
|
-
|
|
1998
2000
|
def error_response(status, code, detail)
|
|
1999
2001
|
json_response(status, { error: code, detail: detail })
|
|
2000
2002
|
end
|
|
@@ -376,13 +376,7 @@ module Samagotchi
|
|
|
376
376
|
# reply), session-free. {html: nil} when markdown is off or
|
|
377
377
|
# commonmarker is missing: the page keeps the raw text.
|
|
378
378
|
def handle_render(req)
|
|
379
|
-
|
|
380
|
-
return too_large if req.content_length.to_i > MAX_RENDER_BYTES
|
|
381
|
-
|
|
382
|
-
raw = request_body(req, MAX_RENDER_BYTES + 1).to_s
|
|
383
|
-
return too_large if raw.bytesize > MAX_RENDER_BYTES
|
|
384
|
-
|
|
385
|
-
body = parse_json(raw)
|
|
379
|
+
body = parse_json(request_body(req, MAX_RENDER_BYTES, what: "a text to render"))
|
|
386
380
|
return error_response(400, "invalid_json", "give {text}") unless body.is_a?(Hash) && body["text"].is_a?(String)
|
|
387
381
|
|
|
388
382
|
html = @markdown_renderer.render(body["text"]) if @markdown_renderer.available?
|