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.
Files changed (115) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/docs/architecture.md +16 -5
  4. data/docs/cli.md +2 -1
  5. data/docs/configuration.md +1 -1
  6. data/docs/internals/background-tasks.md +19 -6
  7. data/docs/internals/bridge.md +2 -2
  8. data/docs/internals/engine.md +2 -1
  9. data/docs/internals/participants.md +67 -9
  10. data/docs/internals/remote-sessions.md +1 -1
  11. data/docs/internals/web-frontend.md +11 -6
  12. data/docs/plugins.md +3 -1
  13. data/docs/sessions.md +2 -2
  14. data/docs/testing.md +26 -10
  15. data/lib/samagotchi/bridge.rb +3 -2
  16. data/lib/samagotchi/bundle_command/try/sandbox.rb +2 -2
  17. data/lib/samagotchi/bundles/core/manifest.yml +2 -2
  18. data/lib/samagotchi/bundles/slow-commands/manifest.yml +3 -3
  19. data/lib/samagotchi/bundles/slow-commands/plugin.rb +3 -3
  20. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  21. data/lib/samagotchi/engine/participants.rb +2 -2
  22. data/lib/samagotchi/engine.rb +0 -1
  23. data/lib/samagotchi/file_ref.rb +3 -5
  24. data/lib/samagotchi/guardrails/participant_refusal.rb +1 -1
  25. data/lib/samagotchi/memory_usage.rb +2 -1
  26. data/lib/samagotchi/message_parts.rb +5 -3
  27. data/lib/samagotchi/objectives_backing.rb +6 -10
  28. data/lib/samagotchi/parent_report.rb +1 -1
  29. data/lib/samagotchi/participant_prompts.rb +101 -0
  30. data/lib/samagotchi/participant_turn_mode.rb +1 -1
  31. data/lib/samagotchi/project_env.rb +7 -5
  32. data/lib/samagotchi/prompt_history.rb +2 -2
  33. data/lib/samagotchi/send_command/parent_gate.rb +2 -2
  34. data/lib/samagotchi/send_command.rb +4 -4
  35. data/lib/samagotchi/session_command_catalog.rb +210 -0
  36. data/lib/samagotchi/session_commands.rb +12 -183
  37. data/lib/samagotchi/session_manager/lifecycle.rb +5 -4
  38. data/lib/samagotchi/session_manager.rb +2 -1
  39. data/lib/samagotchi/session_stores.rb +2 -0
  40. data/lib/samagotchi/spawn_env.rb +75 -0
  41. data/lib/samagotchi/task_files.rb +51 -0
  42. data/lib/samagotchi/task_transcript.rb +67 -0
  43. data/lib/samagotchi/task_watch.rb +8 -6
  44. data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +3 -3
  45. data/lib/samagotchi/terminal_ui/attached_loop/questions.rb +2 -2
  46. data/lib/samagotchi/terminal_ui/attached_loop.rb +3 -3
  47. data/lib/samagotchi/terminal_ui/input_support.rb +1 -1
  48. data/lib/samagotchi/testing/boundary.rb +5 -0
  49. data/lib/samagotchi/tool_activity.rb +63 -79
  50. data/lib/samagotchi/tool_names.rb +179 -0
  51. data/lib/samagotchi/tool_view.rb +3 -5
  52. data/lib/samagotchi/tools/ask_user_question.rb +2 -1
  53. data/lib/samagotchi/tools/builtins.rb +1 -1
  54. data/lib/samagotchi/tools/cancel_reminder.rb +2 -1
  55. data/lib/samagotchi/tools/cancel_schedule.rb +3 -1
  56. data/lib/samagotchi/tools/context_read.rb +2 -1
  57. data/lib/samagotchi/tools/delegate.rb +2 -1
  58. data/lib/samagotchi/tools/delegate_result.rb +2 -1
  59. data/lib/samagotchi/tools/edit.rb +2 -1
  60. data/lib/samagotchi/tools/execute.rb +22 -28
  61. data/lib/samagotchi/tools/forget_outputs.rb +2 -1
  62. data/lib/samagotchi/tools/list_reminders.rb +2 -1
  63. data/lib/samagotchi/tools/list_schedules.rb +3 -1
  64. data/lib/samagotchi/tools/list_sessions.rb +2 -1
  65. data/lib/samagotchi/tools/memory.rb +3 -2
  66. data/lib/samagotchi/tools/objectives_tool.rb +5 -4
  67. data/lib/samagotchi/tools/read.rb +2 -1
  68. data/lib/samagotchi/tools/register_reminder.rb +2 -1
  69. data/lib/samagotchi/tools/schedule_task.rb +3 -1
  70. data/lib/samagotchi/tools/send_note.rb +2 -1
  71. data/lib/samagotchi/tools/task_create.rb +2 -1
  72. data/lib/samagotchi/tools/task_get.rb +2 -1
  73. data/lib/samagotchi/tools/task_list.rb +2 -1
  74. data/lib/samagotchi/tools/task_runtime.rb +34 -154
  75. data/lib/samagotchi/tools/task_stop.rb +2 -1
  76. data/lib/samagotchi/tools/task_wait.rb +3 -2
  77. data/lib/samagotchi/tools/web_fetch.rb +2 -1
  78. data/lib/samagotchi/tools/write.rb +2 -1
  79. data/lib/samagotchi/turn_checkpoint.rb +1 -1
  80. data/lib/samagotchi/version.rb +1 -1
  81. data/lib/samagotchi/web/app.rb +38 -36
  82. data/lib/samagotchi/web/message_display.rb +1 -7
  83. data/lib/samagotchi/web/participant_access.rb +12 -4
  84. data/lib/samagotchi/web/participant_history.rb +68 -0
  85. data/lib/samagotchi/web/participant_images.rb +2 -1
  86. data/lib/samagotchi/web/participant_show.rb +5 -5
  87. data/lib/samagotchi/web/participants_admin.rb +1 -1
  88. data/lib/samagotchi/web/public/app.js +11 -9
  89. data/lib/samagotchi/web/public/card_registry.js +248 -0
  90. data/lib/samagotchi/web/public/cards_offscreen.js +3 -3
  91. data/lib/samagotchi/web/public/cards_place.js +8 -2
  92. data/lib/samagotchi/web/public/history_shape.js +1 -2
  93. data/lib/samagotchi/web/public/participant_history.js +27 -0
  94. data/lib/samagotchi/web/public/question_cards.js +1 -15
  95. data/lib/samagotchi/web/public/reload_check.js +90 -72
  96. data/lib/samagotchi/web/public/session_view.js +6 -1
  97. data/lib/samagotchi/web/public/stage_asking.js +6 -20
  98. data/lib/samagotchi/web/public/stage_view.js +2 -2
  99. data/lib/samagotchi/web/public/stage_waiting.js +4 -10
  100. data/lib/samagotchi/web/public/task_stop.js +1 -1
  101. data/lib/samagotchi/web/push_routes.rb +1 -1
  102. data/lib/samagotchi/web/reload_check_route.rb +5 -2
  103. data/lib/samagotchi/web/request_body.rb +72 -0
  104. data/lib/samagotchi/web/server.rb +70 -0
  105. data/lib/samagotchi/web/session_access_routes.rb +13 -11
  106. data/lib/samagotchi/web/session_control.rb +21 -15
  107. data/lib/samagotchi/web/session_create.rb +2 -1
  108. data/lib/samagotchi/web/session_show.rb +8 -8
  109. data/lib/samagotchi/web/viewer.rb +7 -2
  110. data/lib/samagotchi/web/working_set_routes.rb +3 -3
  111. data/lib/samagotchi/worker.rb +49 -182
  112. data/lib/samagotchi/worker_exit_claim.rb +102 -0
  113. data/lib/samagotchi/worker_participant_gate.rb +156 -0
  114. data/lib/samagotchi/worker_task_wakes.rb +2 -2
  115. metadata +13 -1
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "tool_names"
4
+
5
+ module Samagotchi
6
+ # Which background tasks a conversation started, read from its tool
7
+ # results: a task_create's "task_id:" and an execute's hand-over at its
8
+ # limit (or early, for a message). Text only: no record is read and no
9
+ # process looked at (Tools::TaskRuntime.running_created_in adds the
10
+ # refresh). The worker's finish wake (TaskWatch, WorkerTaskWakes), the
11
+ # Bridge's stop-task check, a turn's cancel note and SessionManager's
12
+ # running-task check read it. web/public/task_stop.js has the JS twin of
13
+ # handed_over_id (handedOverId).
14
+ module TaskTranscript
15
+ # A task_create result as the model gets it: its "[task_create]" lead,
16
+ # after the one "ran as: …" line a gate-corrected call has.
17
+ RAN_AS_LINE = "(?:ran as: [^\n]*\n)?"
18
+ CREATED_BLOCK = /\A#{RAN_AS_LINE}\[task_create\]\ntask_id: (\S+)/
19
+ CREATED_OUTPUT = /\A#{RAN_AS_LINE}(?:\[task_create\]\n)?task_id: (\S+)/
20
+
21
+ module_function
22
+
23
+ # The ids of every task this conversation started (task_create, a
24
+ # handed-over execute), whatever their status now, oldest first.
25
+ # A native tool_response joins its calls' results with "---".
26
+ # @return [Array<String>]
27
+ def created_ids_in(messages)
28
+ Array(messages).flat_map do |message|
29
+ next [] unless (message[:role] || message["role"]).to_s == "tool_response"
30
+
31
+ (message[:content] || message["content"]).to_s.split("\n\n---\n\n").filter_map do |block|
32
+ block[CREATED_BLOCK, 1] || handed_over_id(block)
33
+ end
34
+ end.uniq
35
+ end
36
+
37
+ # The task id of a tool output that started a task, the "[tool]" lead
38
+ # optional (a UI's row has it or not): a task_create's, or an execute's
39
+ # handed_over_id. nil for any other tool or output.
40
+ # @return [String, nil]
41
+ def created_id(tool, text)
42
+ case tool
43
+ when "task_create" then text.to_s[CREATED_OUTPUT, 1]
44
+ when ToolNames::Execute::NAME then handed_over_id(text)
45
+ end
46
+ end
47
+
48
+ # The task id an execute's hand-over result names: anchored on its
49
+ # first line (ToolNames::Execute::HANDED_OVER), never a "task_id:" line
50
+ # anywhere, which the command's own output can print. The "[execute]"
51
+ # lead and a "ran as: …" line before it are optional.
52
+ # web/public/task_stop.js has the JS twin (handedOverId).
53
+ # @return [String, nil]
54
+ def handed_over_id(text)
55
+ text = text.to_s
56
+ # A regexp raises on bytes that aren't UTF-8 (a command's raw output).
57
+ text = text.scrub unless text.valid_encoding?
58
+ text[handed_over_pattern, 1]
59
+ end
60
+
61
+ def handed_over_pattern
62
+ @handed_over_pattern ||= /\A#{RAN_AS_LINE}(?:\[#{ToolNames::Execute::NAME}\]\n)?#{Regexp.escape(ToolNames::Execute::HANDED_OVER)} #{ToolNames::Execute::HANDED_OVER_AFTER}[^\n]*\ntask_id: (\S+)/
63
+ end
64
+
65
+ private_class_method :handed_over_pattern
66
+ end
67
+ end
@@ -4,6 +4,8 @@ require "time"
4
4
 
5
5
  require_relative "config"
6
6
  require_relative "log"
7
+ require_relative "task_transcript"
8
+ require_relative "tool_names"
7
9
  require_relative "tools/task_runtime"
8
10
 
9
11
  module Samagotchi
@@ -40,10 +42,10 @@ module Samagotchi
40
42
 
41
43
  # The first line of a task tool's result, after a gate-corrected call's
42
44
  # "ran as: …" line and the optional "[tool]" lead.
43
- TASK_LINE = /\A#{Tools::TaskRuntime::RAN_AS_LINE}(?:\[task_(?:wait|get|stop)\]\n)?task_id: (\S+)/
45
+ TASK_LINE = /\A#{TaskTranscript::RAN_AS_LINE}(?:\[task_(?:wait|get|stop)\]\n)?task_id: (\S+)/
44
46
  LOOK_TOOLS = %w[task_wait task_get task_stop].freeze
45
- HANDED_OVER_LIMIT = /#{Regexp.escape(Tools::Execute::HANDED_OVER)} (\d+)s limit/
46
- HANDED_OVER_EARLY = /#{Regexp.escape(Tools::Execute::HANDED_OVER)} (\d+)s mark/
47
+ HANDED_OVER_LIMIT = /#{Regexp.escape(ToolNames::Execute::HANDED_OVER)} (\d+)s limit/
48
+ HANDED_OVER_EARLY = /#{Regexp.escape(ToolNames::Execute::HANDED_OVER)} (\d+)s mark/
47
49
 
48
50
  MODE_KEY = "session.task_wakes"
49
51
  MODES = %w[wake queue off].freeze
@@ -77,8 +79,8 @@ module Samagotchi
77
79
  tool = event[:tool].to_s
78
80
  output = event[:output].to_s
79
81
  output = output.scrub unless output.valid_encoding?
80
- if (id = Tools::TaskRuntime.created_id(tool, output))
81
- executed = tool == Tools::Execute::NAME
82
+ if (id = TaskTranscript.created_id(tool, output))
83
+ executed = tool == ToolNames::Execute::NAME
82
84
  add(id, limit_sec: executed ? output[HANDED_OVER_LIMIT, 1]&.to_i : nil,
83
85
  early_sec: executed ? output[HANDED_OVER_EARLY, 1]&.to_i : nil)
84
86
  elsif LOOK_TOOLS.include?(tool) && (id = output[TASK_LINE, 1])
@@ -125,7 +127,7 @@ module Samagotchi
125
127
  # records ([Entry, TaskRecord], oldest first). One the user stopped (the
126
128
  # web's stop-task button) or whose record is gone is dropped, not
127
129
  # returned. +keep+: the ids the conversation still holds
128
- # (TaskRuntime.created_ids_in): a !rollback or a restored turn may have
130
+ # (TaskTranscript.created_ids_in): a !rollback or a restored turn may have
129
131
  # dropped the result that added one, which is dropped too. One `ps` per
130
132
  # running task (TaskRuntime.get_record); nothing with an empty watch.
131
133
  # @return [Array<Array(Entry, Tools::TaskRecord)>]
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "../../composer_route"
4
4
  require_relative "../../prompt_history"
5
- require_relative "../../session_commands"
5
+ require_relative "../../session_command_catalog"
6
6
  require_relative "../exit_lines"
7
7
  require_relative "../reline_seam"
8
8
 
@@ -112,7 +112,7 @@ module Samagotchi
112
112
  return submit(nil) if local == :detach
113
113
  return end_scratch if local == :exit && @scratch
114
114
  return refuse_scratch_archive if local == :archive && @scratch
115
- return SessionCommands.delete_on_exit?(text) ? exit_and_delete : exit_worker if local == :exit
115
+ return SessionCommandCatalog.delete_on_exit?(text) ? exit_and_delete : exit_worker if local == :exit
116
116
  return archive_after_turn if local == :archive && (@running || !@queued_ids.empty?)
117
117
  return exit_and_archive if local == :archive
118
118
 
@@ -173,7 +173,7 @@ module Samagotchi
173
173
  # The session's commands as its worker's snapshot names them (its
174
174
  # plugins' too); the built-ins until one arrives, or from a worker too
175
175
  # old to name them. An unknown /foo is a prompt.
176
- def command_registry = @command_registry || SessionCommands.builtin_registry
176
+ def command_registry = @command_registry || SessionCommandCatalog.builtin_registry
177
177
 
178
178
  # Ctrl-C cancels the running turn (whoever started it). At an idle
179
179
  # prompt it clears the line; on an empty one it says how to detach, and
@@ -2,7 +2,7 @@
2
2
 
3
3
  require_relative "../../client_id"
4
4
  require_relative "../exit_lines"
5
- require_relative "../../session_commands"
5
+ require_relative "../../session_command_catalog"
6
6
  require_relative "../question_prompt"
7
7
 
8
8
  module Samagotchi
@@ -29,7 +29,7 @@ module Samagotchi
29
29
 
30
30
  # A bare answer at the continue prompt (an empty one is yes).
31
31
  def continue_line(text)
32
- text.empty? ? SessionCommands::CONTINUE_COMMAND : "#{SessionCommands::CONTINUE_COMMAND} #{text}"
32
+ text.empty? ? SessionCommandCatalog::CONTINUE_COMMAND : "#{SessionCommandCatalog::CONTINUE_COMMAND} #{text}"
33
33
  end
34
34
 
35
35
  def offer_continue(event)
@@ -35,7 +35,7 @@ require_relative "../steer"
35
35
  require_relative "../turn_origin"
36
36
  require_relative "../model_profile"
37
37
  require_relative "../output_formatter"
38
- require_relative "../session_commands"
38
+ require_relative "../session_command_catalog"
39
39
  require_relative "../session_manager"
40
40
  require_relative "../session_metrics"
41
41
  require_relative "../children_status"
@@ -506,7 +506,7 @@ module Samagotchi
506
506
  def older_worker_command(reply, line)
507
507
  return nil unless reply.status == 400 && reply.json&.fetch("error", nil) == "unknown_command"
508
508
 
509
- SessionCommands.builtin_name(line)
509
+ SessionCommandCatalog.builtin_name(line)
510
510
  end
511
511
 
512
512
  def stale_worker(cant)
@@ -556,7 +556,7 @@ module Samagotchi
556
556
  @screen.commit(paint(version_line, 2)) if version_line
557
557
  end
558
558
  if snapshot[:commands]
559
- @command_registry = Commands::Registry.from_listing(snapshot[:commands], base: SessionCommands.builtin_registry)
559
+ @command_registry = Commands::Registry.from_listing(snapshot[:commands], base: SessionCommandCatalog.builtin_registry)
560
560
  end
561
561
  Array(snapshot[:init_tasks]).each { |task| @view.init_started(task) }
562
562
  cards = Array(snapshot[:cards])
@@ -9,7 +9,7 @@ require_relative "../degrade"
9
9
  require_relative "../memory_paths"
10
10
  require_relative "../prompt_history"
11
11
  require_relative "../tools/memory"
12
- require_relative "../session_commands"
12
+ require_relative "../session_command_catalog"
13
13
  require_relative "line_reader"
14
14
  require_relative "reline_history_hook"
15
15
 
@@ -36,6 +36,11 @@ module Samagotchi
36
36
  Allowed.new(name: "Samagotchi::Tools::Execute::TIMEOUT_PREFIX", file: "samagotchi/tools/execute"),
37
37
  Allowed.new(name: "Samagotchi::Tools::Execute::HANDED_OVER", file: "samagotchi/tools/execute"),
38
38
  Allowed.new(name: "Samagotchi::Tools::Execute.timeout_seconds", file: "samagotchi/tools/execute"),
39
+ Allowed.new(name: "Samagotchi::TaskFiles::TASKS_DIR", file: "samagotchi/task_files"),
40
+ Allowed.new(name: "Samagotchi::TaskFiles::EXIT_CODE_FILENAME", file: "samagotchi/task_files"),
41
+ # The two names' old home, forwarding to TaskFiles for one release:
42
+ # remove these entries with TaskRuntime's two constants in the
43
+ # release after the one that ships TaskFiles.
39
44
  Allowed.new(name: "Samagotchi::Tools::TaskRuntime::TASKS_DIR", file: "samagotchi/tools/task_runtime"),
40
45
  Allowed.new(name: "Samagotchi::Tools::TaskRuntime::EXIT_CODE_FILENAME", file: "samagotchi/tools/task_runtime"),
41
46
  Allowed.new(name: "Samagotchi::AtomicFile.write", file: "samagotchi/atomic_file"),
@@ -4,25 +4,9 @@ require "json"
4
4
  require_relative "log"
5
5
  require_relative "command_steps"
6
6
  require_relative "tool_view"
7
- require_relative "tools/execute"
7
+ require_relative "tool_names"
8
8
  require_relative "tools/registry"
9
- require_relative "tools/read"
10
- require_relative "tools/write"
11
- require_relative "tools/memory"
12
- require_relative "tools/edit"
13
- require_relative "tools/task_create"
14
- require_relative "tools/task_get"
15
- require_relative "tools/task_list"
16
- require_relative "tools/task_runtime"
17
- require_relative "tools/task_stop"
18
- require_relative "tools/task_wait"
19
- require_relative "tools/web_fetch"
20
- require_relative "tools/ask_user_question"
21
- require_relative "tools/list_sessions"
22
- require_relative "tools/send_note"
23
- require_relative "tools/context_read"
24
- require_relative "tools/delegate"
25
- require_relative "tools/delegate_result"
9
+ require_relative "task_files"
26
10
  require_relative "tools/forget_outputs"
27
11
  require_relative "tools/objectives_tool"
28
12
 
@@ -68,7 +52,7 @@ module Samagotchi
68
52
  }
69
53
  # A grep that found nothing: ok, and the web row says "no match".
70
54
  # Not a hand-over: an "exit: 1" in its output tail is the command's.
71
- if event[:status] == "ok" && tool_name == Tools::Execute::NAME && !handed_over?(tool_name, result) &&
55
+ if event[:status] == "ok" && tool_name == ToolNames::Execute::NAME && !handed_over?(tool_name, result) &&
72
56
  execute_failed?(result.to_s)
73
57
  event[:no_match] = true
74
58
  end
@@ -87,7 +71,7 @@ module Samagotchi
87
71
  event
88
72
  end
89
73
 
90
- def objectives_proposal?(call) = call[:action].to_s.strip.downcase == Tools::ObjectivesTool::PROPOSE
74
+ def objectives_proposal?(call) = call[:action].to_s.strip.downcase == ToolNames::ObjectivesTool::PROPOSE
91
75
 
92
76
  # A move's state=, before note= so a cut can't drop it: the page reads it from
93
77
  # the params (the title stays as an older page reads it). None for a
@@ -96,12 +80,12 @@ module Samagotchi
96
80
  return [] if objectives_proposal?(call)
97
81
 
98
82
  state = Tools::ObjectivesTool.state_of(call)
99
- [Tools::ObjectivesTool::BLOCKED, Tools::ObjectivesTool::DONE].include?(state) ? ["state=#{preview_tool_param(state)}"] : []
83
+ [ToolNames::ObjectivesTool::BLOCKED, ToolNames::ObjectivesTool::DONE].include?(state) ? ["state=#{preview_tool_param(state)}"] : []
100
84
  end
101
85
 
102
86
  # An objectives row's words: "O2 note · evidence", or "propose done O2 note".
103
87
  def objectives_title(call)
104
- return "#{Tools::ObjectivesTool::PROPOSE} #{call[:change]} #{call[:id]} #{call[:text]} · #{call[:note]}".squeeze(" ") if objectives_proposal?(call)
88
+ return "#{ToolNames::ObjectivesTool::PROPOSE} #{call[:change]} #{call[:id]} #{call[:text]} · #{call[:note]}".squeeze(" ") if objectives_proposal?(call)
105
89
 
106
90
  "#{call[:id]} #{call[:note]}#{" · #{call[:evidence]}" unless call[:evidence].to_s.strip.empty?}"
107
91
  end
@@ -124,22 +108,22 @@ module Samagotchi
124
108
  def tool_title(tool_name, call, cwd: nil)
125
109
  return task_title(tool_name, call, cwd) if TASK_TOOLS.include?(tool_name)
126
110
 
127
- if [Tools::Execute::NAME, Tools::TaskCreate::NAME].include?(tool_name)
111
+ if [ToolNames::Execute::NAME, ToolNames::TaskCreate::NAME].include?(tool_name)
128
112
  title = command_description(tool_name, call) || steps_title(call[:content])
129
113
  return title if title
130
114
  end
131
115
 
132
116
  text = case tool_name
133
- when Tools::Read::NAME, Tools::MemoryRead::NAME then call[:content]
134
- when Tools::Write::NAME, Tools::Edit::NAME, Tools::MemoryWrite::NAME then call[:path]
135
- when Tools::Execute::NAME, Tools::TaskCreate::NAME then command_title(call[:content])
136
- when Tools::ObjectivesTool::NAME then objectives_title(call)
117
+ when ToolNames::Read::NAME, ToolNames::MemoryRead::NAME then call[:content]
118
+ when ToolNames::Write::NAME, ToolNames::Edit::NAME, ToolNames::MemoryWrite::NAME then call[:path]
119
+ when ToolNames::Execute::NAME, ToolNames::TaskCreate::NAME then command_title(call[:content])
120
+ when ToolNames::ObjectivesTool::NAME then objectives_title(call)
137
121
  end
138
122
  text = text.to_s.strip
139
123
  return nil if text.empty?
140
124
 
141
125
  case tool_name
142
- when Tools::Read::NAME, Tools::Write::NAME, Tools::Edit::NAME
126
+ when ToolNames::Read::NAME, ToolNames::Write::NAME, ToolNames::Edit::NAME
143
127
  path = relative_path(text, cwd)
144
128
  path.length > TOOL_ACTIVITY_PREVIEW_LIMIT ? "…#{path[-(TOOL_ACTIVITY_PREVIEW_LIMIT - 1)..]}" : path
145
129
  else
@@ -148,20 +132,20 @@ module Samagotchi
148
132
  end
149
133
  end
150
134
 
151
- TASK_TOOLS = [Tools::TaskWait::NAME, Tools::TaskGet::NAME, Tools::TaskStop::NAME].freeze
135
+ TASK_TOOLS = [ToolNames::TaskWait::NAME, ToolNames::TaskGet::NAME, ToolNames::TaskStop::NAME].freeze
152
136
 
153
137
  # A task_wait / task_get / task_stop row's title: the task's command
154
138
  # (its record under +cwd+, the process's directory when nil) as a
155
139
  # command's title, and for a wait how long it waits ("npm test · up to
156
140
  # 600s"). nil when there is no record: the row keeps its id.
157
141
  def task_title(tool_name, call, cwd)
158
- command = Tools::TaskRuntime.command_of(call[:content], root: cwd)
142
+ command = TaskFiles.command_of(call[:content], root: cwd)
159
143
  return nil unless command
160
144
 
161
145
  suffix = ""
162
- if tool_name == Tools::TaskWait::NAME
146
+ if tool_name == ToolNames::TaskWait::NAME
163
147
  timeout = call[:timeout].to_s.strip
164
- suffix = " · up to #{timeout.empty? ? Tools::TaskWait::TIMEOUT_DEFAULT : timeout.to_i}s"
148
+ suffix = " · up to #{timeout.empty? ? ToolNames::TaskWait::TIMEOUT_DEFAULT : timeout.to_i}s"
165
149
  end
166
150
  room = TOOL_ACTIVITY_PREVIEW_LIMIT - suffix.length
167
151
  text = steps_title(command, room) || command_title(command).gsub(/\s+/, " ")
@@ -208,26 +192,26 @@ module Samagotchi
208
192
  # @param call [Hash, nil] the call, for a label that depends on its form
209
193
  def tool_activity_action(tool_name, registry: nil, call: nil)
210
194
  case tool_name
211
- when Tools::Execute::NAME then "running command"
212
- when Tools::Read::NAME then "reading file"
213
- when Tools::Write::NAME then "writing file"
214
- when Tools::Edit::NAME then "editing file"
215
- when Tools::MemoryRead::NAME then "reading memory"
216
- when Tools::MemoryWrite::NAME then memory_write_action(call)
217
- when Tools::TaskCreate::NAME then "starting background task"
218
- when Tools::TaskGet::NAME then "checking task"
219
- when Tools::TaskList::NAME then "listing tasks"
220
- when Tools::TaskStop::NAME then "stopping task"
221
- when Tools::TaskWait::NAME then "waiting for task"
222
- when Tools::WebFetch::NAME then "fetching URL"
223
- when Tools::AskUserQuestion::NAME then "asking user"
224
- when Tools::ListSessions::NAME then "listing sessions"
225
- when Tools::SendNote::NAME then "sending a note"
226
- when Tools::ContextRead::NAME then "reading attached context"
227
- when Tools::Delegate::NAME then "delegating"
228
- when Tools::DelegateResult::NAME then "waiting for a delegate"
229
- when Tools::ForgetOutputs::NAME then forget_outputs_action(call)
230
- when Tools::ObjectivesTool::NAME then objectives_proposal?(call) ? "proposing a change" : "recording a move"
195
+ when ToolNames::Execute::NAME then "running command"
196
+ when ToolNames::Read::NAME then "reading file"
197
+ when ToolNames::Write::NAME then "writing file"
198
+ when ToolNames::Edit::NAME then "editing file"
199
+ when ToolNames::MemoryRead::NAME then "reading memory"
200
+ when ToolNames::MemoryWrite::NAME then memory_write_action(call)
201
+ when ToolNames::TaskCreate::NAME then "starting background task"
202
+ when ToolNames::TaskGet::NAME then "checking task"
203
+ when ToolNames::TaskList::NAME then "listing tasks"
204
+ when ToolNames::TaskStop::NAME then "stopping task"
205
+ when ToolNames::TaskWait::NAME then "waiting for task"
206
+ when ToolNames::WebFetch::NAME then "fetching URL"
207
+ when ToolNames::AskUserQuestion::NAME then "asking user"
208
+ when ToolNames::ListSessions::NAME then "listing sessions"
209
+ when ToolNames::SendNote::NAME then "sending a note"
210
+ when ToolNames::ContextRead::NAME then "reading attached context"
211
+ when ToolNames::Delegate::NAME then "delegating"
212
+ when ToolNames::DelegateResult::NAME then "waiting for a delegate"
213
+ when ToolNames::ForgetOutputs::NAME then forget_outputs_action(call)
214
+ when ToolNames::ObjectivesTool::NAME then objectives_proposal?(call) ? "proposing a change" : "recording a move"
231
215
  else registry_entry(registry, tool_name)&.label || "calling tool"
232
216
  end
233
217
  end
@@ -265,19 +249,19 @@ module Samagotchi
265
249
  # can print any line, "status: stopped" included.
266
250
  def tool_activity_status(result, tool_name = nil, command: nil)
267
251
  text = result.to_s
268
- return "stopped" if [Tools::Execute::NOT_RUN_ON_STOP, *Tools::Execute::NOT_RUN.values].include?(text)
269
- if tool_name == Tools::Execute::NAME &&
270
- text.start_with?(Tools::Execute::STOPPED_BY_USER, *Tools::Execute::STOPPED_BY.values)
252
+ return "stopped" if [ToolNames::Execute::NOT_RUN_ON_STOP, *ToolNames::Execute::NOT_RUN.values].include?(text)
253
+ if tool_name == ToolNames::Execute::NAME &&
254
+ text.start_with?(ToolNames::Execute::STOPPED_BY_USER, *ToolNames::Execute::STOPPED_BY.values)
271
255
  return "stopped"
272
256
  end
273
257
  return "error" if text.start_with?("Error:")
274
258
  # Still running, as a task: its output tail may hold any "exit: N".
275
259
  return "ok" if handed_over?(tool_name, text)
276
- if tool_name == Tools::Execute::NAME && execute_failed?(text)
260
+ if tool_name == ToolNames::Execute::NAME && execute_failed?(text)
277
261
  return no_match?(text, command) ? "ok" : "error"
278
262
  end
279
263
 
280
- if tool_name == Tools::TaskWait::NAME
264
+ if tool_name == ToolNames::TaskWait::NAME
281
265
  # The header: every line before the log's tail (output_tail:), which can print any line.
282
266
  header = text.split(/^output_tail:$/, 2).first.to_s
283
267
  return "stopped" if header.match?(/^(wait_result: canceled|status: stopped)$/)
@@ -288,15 +272,15 @@ module Samagotchi
288
272
  end
289
273
 
290
274
  # An execute still running at its limit (or when a message from the user
291
- # waited), handed over as a task (Tools::Execute::HANDED_OVER).
275
+ # waited), handed over as a task (ToolNames::Execute::HANDED_OVER).
292
276
  def handed_over?(tool_name, text)
293
- return false unless tool_name == Tools::Execute::NAME && text.to_s.start_with?(Tools::Execute::HANDED_OVER)
277
+ return false unless tool_name == ToolNames::Execute::NAME && text.to_s.start_with?(ToolNames::Execute::HANDED_OVER)
294
278
 
295
279
  # The first line only, scrubbed: a regexp raises on bytes that aren't UTF-8.
296
280
  text.to_s.each_line.first.to_s.scrub.match?(HANDED_OVER_START)
297
281
  end
298
282
 
299
- HANDED_OVER_START = /\A#{Regexp.escape(Tools::Execute::HANDED_OVER)} #{Tools::Execute::HANDED_OVER_AFTER}/
283
+ HANDED_OVER_START = /\A#{Regexp.escape(ToolNames::Execute::HANDED_OVER)} #{ToolNames::Execute::HANDED_OVER_AFTER}/
300
284
 
301
285
  def execute_failed?(text)
302
286
  code = execute_exit_code(text)
@@ -375,28 +359,28 @@ module Samagotchi
375
359
  # turn (+registry+'s entry then).
376
360
  def tool_activity_params(tool_name, call, registry: nil, surface: nil)
377
361
  case tool_name
378
- when Tools::Execute::NAME
362
+ when ToolNames::Execute::NAME
379
363
  "command=#{preview_tool_param(call[:content])}"
380
- when Tools::Read::NAME
364
+ when ToolNames::Read::NAME
381
365
  parts = ["path=#{preview_tool_param(call[:content])}"]
382
366
  range = format_line_range(call)
383
367
  parts << "lines=#{range}" if range
384
368
  parts.join(" ")
385
- when Tools::Write::NAME
369
+ when ToolNames::Write::NAME
386
370
  "path=#{preview_tool_param(call[:path])}"
387
- when Tools::Edit::NAME
371
+ when ToolNames::Edit::NAME
388
372
  parts = ["path=#{preview_tool_param(call[:path])}"]
389
373
  range = format_line_range(call)
390
374
  parts << "lines=#{range}" if range
391
375
  parts.join(" ")
392
- when Tools::MemoryRead::NAME
376
+ when ToolNames::MemoryRead::NAME
393
377
  parts = []
394
378
  name = call[:content].to_s.strip
395
379
  parts << "name=#{preview_tool_param(name)}" unless name.empty?
396
380
  scope = call[:scope].to_s.strip
397
381
  parts << "scope=#{preview_tool_param(scope)}" unless scope.empty?
398
382
  parts.join(" ")
399
- when Tools::MemoryWrite::NAME
383
+ when ToolNames::MemoryWrite::NAME
400
384
  parts = []
401
385
  path = call[:path].to_s.strip
402
386
  parts << "name=#{preview_tool_param(path)}" unless path.empty?
@@ -406,16 +390,16 @@ module Samagotchi
406
390
  parts << "description=#{preview_tool_param(desc)}" unless desc.empty?
407
391
  parts << "remove=true" if call[:remove].to_s.strip.downcase == "true"
408
392
  parts.join(" ")
409
- when Tools::TaskCreate::NAME
393
+ when ToolNames::TaskCreate::NAME
410
394
  parts = ["command=#{preview_tool_param(call[:content])}"]
411
395
  cwd = call[:cwd].to_s.strip
412
396
  parts << "cwd=#{preview_tool_param(cwd)}" unless cwd.empty?
413
397
  env = call[:env].to_s.strip
414
398
  parts << "env=#{preview_tool_param(env)}" unless env.empty?
415
399
  parts.join(" ")
416
- when Tools::TaskGet::NAME, Tools::TaskStop::NAME
400
+ when ToolNames::TaskGet::NAME, ToolNames::TaskStop::NAME
417
401
  "id=#{preview_tool_param(call[:content])}"
418
- when Tools::TaskWait::NAME
402
+ when ToolNames::TaskWait::NAME
419
403
  parts = ["id=#{preview_tool_param(call[:content])}"]
420
404
  timeout = call[:timeout].to_s.strip
421
405
  tail_lines = call[:tail_lines].to_s.strip
@@ -424,21 +408,21 @@ module Samagotchi
424
408
  parts << "tail_lines=#{preview_tool_param(tail_lines)}" unless tail_lines.empty?
425
409
  parts << "done_pattern=#{preview_tool_param(done_pattern)}" unless done_pattern.empty?
426
410
  parts.join(" ")
427
- when Tools::TaskList::NAME
411
+ when ToolNames::TaskList::NAME
428
412
  nil
429
- when Tools::WebFetch::NAME
413
+ when ToolNames::WebFetch::NAME
430
414
  "url=#{preview_tool_param(call[:content])}"
431
- when Tools::ListSessions::NAME
415
+ when ToolNames::ListSessions::NAME
432
416
  call[:cwd].to_s.strip.empty? ? nil : "cwd=#{preview_tool_param(call[:cwd])}"
433
- when Tools::SendNote::NAME
417
+ when ToolNames::SendNote::NAME
434
418
  "session=#{preview_tool_param(call[:session])} text=#{preview_tool_param(call[:content])}"
435
- when Tools::ContextRead::NAME
419
+ when ToolNames::ContextRead::NAME
436
420
  parts = []
437
421
  parts << "name=#{preview_tool_param(call[:content])}" unless call[:content].to_s.strip.empty?
438
422
  parts << "offset=#{preview_tool_param(call[:offset])}" unless call[:offset].to_s.strip.empty?
439
423
  parts << "limit=#{preview_tool_param(call[:limit])}" unless call[:limit].to_s.strip.empty?
440
424
  parts.empty? ? nil : parts.join(" ")
441
- when Tools::Delegate::NAME
425
+ when ToolNames::Delegate::NAME
442
426
  parts = []
443
427
  parts << "session=#{preview_tool_param(call[:session])}" unless call[:session].to_s.strip.empty?
444
428
  parts << "model=#{preview_tool_param(call[:model])}" unless call[:model].to_s.strip.empty?
@@ -446,9 +430,9 @@ module Samagotchi
446
430
  parts << "task=#{preview_tool_param(call[:content])}"
447
431
  parts << "wait=#{preview_tool_param(call[:wait])}" unless call[:wait].nil? || call[:wait].to_s.strip.empty?
448
432
  parts.join(" ")
449
- when Tools::DelegateResult::NAME
433
+ when ToolNames::DelegateResult::NAME
450
434
  call[:session].to_s.strip.empty? ? nil : "session=#{preview_tool_param(call[:session])}"
451
- when Tools::ForgetOutputs::NAME
435
+ when ToolNames::ForgetOutputs::NAME
452
436
  request = Tools::ForgetOutputs.parse(call)
453
437
  parts = []
454
438
  parts << "ids=#{preview_tool_param(request.ids.join(","))}" unless request.ids.empty?
@@ -456,12 +440,12 @@ module Samagotchi
456
440
  parts << "restore=#{preview_tool_param(request.restore.join(","))}" if request.restore?
457
441
  parts << "note=#{preview_tool_param(request.note)}" unless request.note.empty?
458
442
  parts.empty? ? nil : parts.join(" ")
459
- when Tools::ObjectivesTool::NAME
443
+ when ToolNames::ObjectivesTool::NAME
460
444
  parts = ["id=#{preview_tool_param(call[:id])}", *objectives_state_param(call), "note=#{preview_tool_param(call[:note])}"]
461
445
  parts.unshift("change=#{preview_tool_param(call[:change])}") if objectives_proposal?(call)
462
446
  parts << "evidence=#{preview_tool_param(call[:evidence])}" unless call[:evidence].to_s.strip.empty?
463
447
  parts.join(" ")
464
- when Tools::AskUserQuestion::NAME
448
+ when ToolNames::AskUserQuestion::NAME
465
449
  parts = ["question=#{preview_tool_param(call[:question] || call[:content])}"]
466
450
  opts = call[:options]
467
451
  parts << "options=#{preview_tool_param(Array(opts).join(","))}" if opts && !Array(opts).empty?