samagotchi 0.171.0 → 0.172.1

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 (209) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -1
  3. data/bin/chi +6 -4
  4. data/docs/architecture.md +24 -7
  5. data/docs/bundles.md +4 -21
  6. data/docs/cli.md +50 -80
  7. data/docs/configuration.md +11 -13
  8. data/docs/context.md +5 -6
  9. data/docs/guardrails.md +57 -96
  10. data/docs/hooks.md +3 -4
  11. data/docs/internals/attached-context.md +6 -7
  12. data/docs/internals/background-tasks.md +4 -3
  13. data/docs/internals/bridge.md +29 -3
  14. data/docs/internals/engine.md +21 -9
  15. data/docs/internals/objectives.md +3 -5
  16. data/docs/internals/participants.md +118 -143
  17. data/docs/internals/prompt-caching.md +4 -5
  18. data/docs/internals/web-frontend.md +70 -27
  19. data/docs/internals/working-set.md +7 -5
  20. data/docs/memory.md +1 -1
  21. data/docs/plugins.md +2 -16
  22. data/docs/scheduling.md +1 -1
  23. data/docs/sessions.md +15 -49
  24. data/docs/sub-agent.md +2 -2
  25. data/docs/system-one.md +2 -2
  26. data/docs/testing.md +81 -8
  27. data/lib/samagotchi/bridge/card_store.rb +1 -0
  28. data/lib/samagotchi/bridge/sse_writer.rb +1 -0
  29. data/lib/samagotchi/bridge/turn_accumulator.rb +0 -1
  30. data/lib/samagotchi/bridge.rb +26 -9
  31. data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
  32. data/lib/samagotchi/bundle_command/test_run.rb +1 -0
  33. data/lib/samagotchi/bundle_command/try/run.rb +1 -0
  34. data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
  35. data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
  36. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
  37. data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
  38. data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
  39. data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
  40. data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
  41. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  42. data/lib/samagotchi/client_id.rb +126 -67
  43. data/lib/samagotchi/command_intake.rb +15 -3
  44. data/lib/samagotchi/compat.rb +6 -1
  45. data/lib/samagotchi/context_candidate_choice.rb +1 -3
  46. data/lib/samagotchi/context_candidates.rb +3 -4
  47. data/lib/samagotchi/continue_offer.rb +9 -2
  48. data/lib/samagotchi/delivery.rb +23 -12
  49. data/lib/samagotchi/empty_answer_error.rb +5 -0
  50. data/lib/samagotchi/engine/cards.rb +3 -0
  51. data/lib/samagotchi/engine/context_judge.rb +5 -3
  52. data/lib/samagotchi/engine/events.rb +4 -0
  53. data/lib/samagotchi/engine/hooks.rb +3 -0
  54. data/lib/samagotchi/engine/messages.rb +5 -0
  55. data/lib/samagotchi/engine/model_settings.rb +2 -0
  56. data/lib/samagotchi/engine/models.rb +10 -0
  57. data/lib/samagotchi/engine/objectives.rb +6 -0
  58. data/lib/samagotchi/engine/participants.rb +18 -14
  59. data/lib/samagotchi/engine/plugins.rb +8 -0
  60. data/lib/samagotchi/engine/questions.rb +3 -0
  61. data/lib/samagotchi/engine/recap.rb +1 -0
  62. data/lib/samagotchi/engine/session.rb +4 -0
  63. data/lib/samagotchi/engine/side_requests.rb +9 -0
  64. data/lib/samagotchi/engine/steering.rb +2 -0
  65. data/lib/samagotchi/engine/toolkit.rb +5 -0
  66. data/lib/samagotchi/engine/turn_end.rb +7 -0
  67. data/lib/samagotchi/engine/turn_helpers.rb +8 -0
  68. data/lib/samagotchi/engine/verify.rb +2 -0
  69. data/lib/samagotchi/engine.rb +3 -2
  70. data/lib/samagotchi/grants.rb +28 -22
  71. data/lib/samagotchi/guardrail_wiring.rb +23 -18
  72. data/lib/samagotchi/guardrails/approval_use.rb +59 -0
  73. data/lib/samagotchi/guardrails/approvals.rb +9 -11
  74. data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
  75. data/lib/samagotchi/guardrails/gate.rb +10 -5
  76. data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
  77. data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
  78. data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
  79. data/lib/samagotchi/guardrails/review_paths.rb +2 -3
  80. data/lib/samagotchi/guardrails.rb +1 -1
  81. data/lib/samagotchi/llm_context_strategy.rb +2 -0
  82. data/lib/samagotchi/objectives.rb +1 -4
  83. data/lib/samagotchi/objectives_audit.rb +1 -1
  84. data/lib/samagotchi/objectives_track.rb +1 -1
  85. data/lib/samagotchi/owner_prompt.rb +3 -6
  86. data/lib/samagotchi/parent_report.rb +1 -0
  87. data/lib/samagotchi/participant_messages.rb +9 -4
  88. data/lib/samagotchi/participant_turn_mode.rb +50 -0
  89. data/lib/samagotchi/participants.rb +21 -12
  90. data/lib/samagotchi/participants_watch.rb +2 -2
  91. data/lib/samagotchi/plugin/api.rb +2 -2
  92. data/lib/samagotchi/plugin/attached_context.rb +1 -1
  93. data/lib/samagotchi/plugin/context.rb +0 -4
  94. data/lib/samagotchi/plugin/sessions.rb +1 -25
  95. data/lib/samagotchi/run_options.rb +2 -3
  96. data/lib/samagotchi/send_command.rb +10 -10
  97. data/lib/samagotchi/session.rb +0 -3
  98. data/lib/samagotchi/session_access/contract.rb +1 -10
  99. data/lib/samagotchi/session_access/local.rb +10 -23
  100. data/lib/samagotchi/session_access/remote.rb +9 -20
  101. data/lib/samagotchi/session_access.rb +12 -16
  102. data/lib/samagotchi/session_chain.rb +4 -6
  103. data/lib/samagotchi/session_commands.rb +6 -3
  104. data/lib/samagotchi/session_continue.rb +1 -2
  105. data/lib/samagotchi/session_manager/delivery.rb +25 -5
  106. data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
  107. data/lib/samagotchi/session_manager/listing.rb +5 -0
  108. data/lib/samagotchi/session_manager/spawn.rb +2 -2
  109. data/lib/samagotchi/session_manager/worker_command.rb +9 -0
  110. data/lib/samagotchi/session_participants.rb +12 -8
  111. data/lib/samagotchi/session_review.rb +5 -7
  112. data/lib/samagotchi/session_stores.rb +3 -3
  113. data/lib/samagotchi/share_command.rb +10 -12
  114. data/lib/samagotchi/share_terms.rb +41 -0
  115. data/lib/samagotchi/steer.rb +8 -14
  116. data/lib/samagotchi/system_prompt.rb +16 -25
  117. data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
  118. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
  119. data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
  120. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
  121. data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
  122. data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
  123. data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
  124. data/lib/samagotchi/terminal_ui.rb +1 -2
  125. data/lib/samagotchi/testing/sessions.rb +2 -10
  126. data/lib/samagotchi/tool_runner.rb +8 -9
  127. data/lib/samagotchi/toolkit.rb +4 -22
  128. data/lib/samagotchi/toolkit_suggest.rb +3 -3
  129. data/lib/samagotchi/tools/deferred_tools.rb +1 -0
  130. data/lib/samagotchi/tools/execute.rb +11 -45
  131. data/lib/samagotchi/tools/leftovers.rb +126 -0
  132. data/lib/samagotchi/tools/memory.rb +70 -26
  133. data/lib/samagotchi/tools/task_get.rb +1 -0
  134. data/lib/samagotchi/tools/task_list.rb +3 -2
  135. data/lib/samagotchi/tools/task_record.rb +6 -4
  136. data/lib/samagotchi/tools/task_runtime.rb +199 -20
  137. data/lib/samagotchi/tools/task_wait.rb +4 -3
  138. data/lib/samagotchi/turn_context.rb +1 -1
  139. data/lib/samagotchi/turn_origin.rb +1 -1
  140. data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
  141. data/lib/samagotchi/turn_stages/objectives.rb +2 -2
  142. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
  143. data/lib/samagotchi/update_hint.rb +3 -1
  144. data/lib/samagotchi/verify_fresh.rb +1 -1
  145. data/lib/samagotchi/verify_fresh_origin.rb +13 -11
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +3 -5
  148. data/lib/samagotchi/web/participant_access.rb +14 -18
  149. data/lib/samagotchi/web/participant_review.rb +3 -4
  150. data/lib/samagotchi/web/participant_show.rb +4 -3
  151. data/lib/samagotchi/web/participant_view.rb +3 -3
  152. data/lib/samagotchi/web/participants_admin.rb +21 -40
  153. data/lib/samagotchi/web/public/app.js +7 -48
  154. data/lib/samagotchi/web/public/card_kinds.js +51 -0
  155. data/lib/samagotchi/web/public/cards_place.js +13 -2
  156. data/lib/samagotchi/web/public/data.js +2 -10
  157. data/lib/samagotchi/web/public/delivery_line.js +17 -60
  158. data/lib/samagotchi/web/public/index.html +10 -30
  159. data/lib/samagotchi/web/public/live_turn.js +1 -1
  160. data/lib/samagotchi/web/public/owner_claim.js +2 -2
  161. data/lib/samagotchi/web/public/participant_bar.js +4 -5
  162. data/lib/samagotchi/web/public/participant_names.js +14 -10
  163. data/lib/samagotchi/web/public/participants_panel.js +29 -62
  164. data/lib/samagotchi/web/public/question_cards.js +29 -14
  165. data/lib/samagotchi/web/public/reload_check.js +2 -3
  166. data/lib/samagotchi/web/public/send_turn.js +4 -23
  167. data/lib/samagotchi/web/public/session_actions.js +4 -45
  168. data/lib/samagotchi/web/public/session_fields.js +0 -1
  169. data/lib/samagotchi/web/public/session_state.js +0 -3
  170. data/lib/samagotchi/web/public/session_sync.js +4 -2
  171. data/lib/samagotchi/web/public/session_view.js +2 -3
  172. data/lib/samagotchi/web/public/settings_panel.js +4 -18
  173. data/lib/samagotchi/web/public/stage_answer.js +56 -0
  174. data/lib/samagotchi/web/public/stage_asking.js +30 -11
  175. data/lib/samagotchi/web/public/stage_cloud.js +93 -0
  176. data/lib/samagotchi/web/public/stage_dom.js +100 -0
  177. data/lib/samagotchi/web/public/stage_handoff.js +154 -0
  178. data/lib/samagotchi/web/public/stage_pager.js +59 -0
  179. data/lib/samagotchi/web/public/stage_place.js +144 -0
  180. data/lib/samagotchi/web/public/stage_probe.js +64 -0
  181. data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
  182. data/lib/samagotchi/web/public/stage_view.js +133 -506
  183. data/lib/samagotchi/web/public/stage_waiting.js +30 -13
  184. data/lib/samagotchi/web/public/start_page.js +12 -33
  185. data/lib/samagotchi/web/public/start_settings.js +24 -78
  186. data/lib/samagotchi/web/public/start_toolkit.js +6 -10
  187. data/lib/samagotchi/web/public/stream_handlers.js +1 -9
  188. data/lib/samagotchi/web/public/turn_block.js +3 -7
  189. data/lib/samagotchi/web/public/turn_events.js +15 -6
  190. data/lib/samagotchi/web/public/turn_pieces.js +3 -4
  191. data/lib/samagotchi/web/public/viewer_self.js +8 -2
  192. data/lib/samagotchi/web/qr.rb +1 -1
  193. data/lib/samagotchi/web/session_control.rb +3 -1
  194. data/lib/samagotchi/web/session_create.rb +5 -71
  195. data/lib/samagotchi/web/session_summary.rb +1 -4
  196. data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
  197. data/lib/samagotchi/web/viewer.rb +3 -3
  198. data/lib/samagotchi/worker.rb +16 -14
  199. data/lib/samagotchi/worker_inbound.rb +4 -3
  200. data/lib/samagotchi/worker_link.rb +17 -0
  201. data/lib/samagotchi/working_set.rb +87 -28
  202. metadata +16 -8
  203. data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
  204. data/lib/samagotchi/participant_mode.rb +0 -86
  205. data/lib/samagotchi/review_apply.rb +0 -131
  206. data/lib/samagotchi/review_command.rb +0 -123
  207. data/lib/samagotchi/review_start.rb +0 -196
  208. data/lib/samagotchi/web/public/review_apply.js +0 -34
  209. data/lib/samagotchi/web/public/review_start.js +0 -55
@@ -10,6 +10,7 @@ require_relative "../bundler_env"
10
10
  require_relative "../child_env"
11
11
  require_relative "../log"
12
12
  require_relative "../process_group"
13
+ require_relative "leftovers"
13
14
  require_relative "shell"
14
15
  require_relative "task_record"
15
16
 
@@ -38,6 +39,29 @@ module Samagotchi
38
39
  # not a number, or a pid another process reuses): nothing is signalled.
39
40
  NOT_CHIS_PROCESS = "the task's process is gone or isn't the one chi started"
40
41
  USER_STOP_NOTE = "note: the user stopped this task; don't restart it unless they ask."
42
+ # What a shell leaves holding the task's log when it exits (`cmd &`
43
+ # with no `wait`): the waiter of the chi that started it gives it
44
+ # LEFTOVER_GRACE_SEC, then stops the group (TERM,
45
+ # LEFTOVER_STOP_GRACE_SEC, KILL), as execute does (Leftovers). A
46
+ # refresh that finds one no waiter here watches (that chi is gone)
47
+ # stops it at once, with a shorter TERM grace and KILL wait: it blocks
48
+ # that read. HELD_RECHECK_SEC: the refresh looks twice before it
49
+ # signals, since two held? probes at once read each other as holders.
50
+ LEFTOVER_GRACE_SEC = Leftovers::GRACE_SEC
51
+ LEFTOVER_STOP_GRACE_SEC = 1.0
52
+ REFRESH_STOP_GRACE_SEC = 0.5
53
+ REFRESH_KILL_WAIT_SEC = 0.5
54
+ HELD_RECHECK_SEC = 0.05
55
+ # Set on a waiter thread (record_exit_status) while it settles the
56
+ # task's leftovers: a refresh in this process then only reads.
57
+ SETTLING = :samagotchi_task_settling
58
+
59
+ # This process's live waiters by task id (record_exit_status), and the
60
+ # lock a waiter's last write and task_stop's flag take, so neither
61
+ # writes over the other in this process.
62
+ @waiters = {}
63
+ @waiters_lock = Mutex.new
64
+ @settle_lock = Mutex.new
41
65
 
42
66
  module_function
43
67
 
@@ -66,6 +90,10 @@ module Samagotchi
66
90
  # The child writes to it; the ensure below closes the parent's copy.
67
91
  output_io = File.open(output_path, "a") # rubocop:disable Style/FileOpen
68
92
  output_io.sync = true
93
+ # Shared across the spawn, as Execute.spawn_logged holds its logs:
94
+ # the shell and every process it starts inherit it, so a holder left
95
+ # when the shell exits shows (Leftovers.held?).
96
+ output_io.flock(File::LOCK_SH)
69
97
 
70
98
  wrapped_command = wrapped_shell_command(normalized_command, exit_code_path)
71
99
  # Non-login, the shell Execute uses too (Shell).
@@ -100,6 +128,11 @@ module Samagotchi
100
128
  # status. Only while this chi runs. A record a refresh finished in
101
129
  # between (failed, no exit code) is corrected; a stopped task (its
102
130
  # stop flag on disk) is left as task_stop records it.
131
+ # Then the task's leftovers (leftovers_after_exit): a process the
132
+ # shell left holding the log gets the grace and is stopped, and the
133
+ # record is finished in one write with its +leftovers+. While this
134
+ # thread lives, it is the task's one settler in this process
135
+ # (waiter_for): a refresh here leaves a held record running.
103
136
  # The thread is named EXIT_STATUS_THREAD (a spec joins them with
104
137
  # #quiesce! before it removes the tasks' folder) here, before it
105
138
  # returns: named from inside, a thread not yet scheduled was missed by
@@ -109,20 +142,96 @@ module Samagotchi
109
142
  # Absolute now: safe against a later chdir.
110
143
  record_path = File.expand_path(metadata_path(task_id))
111
144
  path = File.expand_path(path)
145
+ started = record_at(record_path)
146
+ logs = log_paths(started).map { |log| File.expand_path(log) }
112
147
  thread = Thread.new do
113
148
  Thread.current.report_on_exception = false
114
149
  code = status_code(waiter.value)
115
150
  next if code.nil? || record_at(record_path)&.stop_requested_by
116
151
 
117
152
  File.write(path, code.to_s) if !File.exist?(path) && Dir.exist?(File.dirname(path))
118
- correct_finished_record(record_path, read_exit_code(path))
153
+ found = leftovers_after_exit(record_path, logs, started&.pid)
154
+ settle(record_path, read_exit_code(path) || code, found)
119
155
  rescue StandardError => e
120
156
  Log.debug(:turn, "task_exit_code_failed", id: task_id, error: e.class.name, msg: e.message)
157
+ ensure
158
+ forget_waiter(task_id, Thread.current)
121
159
  end
122
160
  thread.name = EXIT_STATUS_THREAD
161
+ remember_waiter(task_id, thread, started&.pid)
123
162
  thread
124
163
  end
125
164
 
165
+ # This process's waiter of +task_id+ while it lives, else nil (none
166
+ # started here, done, killed, or a fork's copy of the table). With
167
+ # +pid+, only a waiter of that pid (a record whose pid changed since
168
+ # isn't the process it waits on).
169
+ # @return [Thread, nil]
170
+ def waiter_for(task_id, pid: nil)
171
+ @waiters_lock.synchronize do
172
+ thread, waited = @waiters[task_id]
173
+ next (pid.nil? || pid == waited ? thread : nil) if thread&.alive?
174
+
175
+ @waiters.delete(task_id)
176
+ nil
177
+ end
178
+ end
179
+
180
+ def remember_waiter(task_id, thread, pid)
181
+ @waiters_lock.synchronize { @waiters[task_id] = [thread, pid] if thread.alive? }
182
+ end
183
+
184
+ def forget_waiter(task_id, thread)
185
+ @waiters_lock.synchronize { @waiters.delete(task_id) if @waiters[task_id]&.first == thread }
186
+ end
187
+
188
+ # The waiter's look once the shell is reaped: a process still holding
189
+ # +logs+ gets LEFTOVER_GRACE_SEC, then its group is stopped
190
+ # (Leftovers.check), unless a task_stop flags the record first.
191
+ # @return [Symbol, nil] :stopped, :detached, or nil (nothing held)
192
+ # +pgid+ is the pid this waiter waited on (read as it started), never
193
+ # the record's pid read now: an edited task.json can't steer a signal.
194
+ def leftovers_after_exit(record_path, logs, pgid)
195
+ return nil if logs.empty? || !ProcessGroup.signalable?(pgid)
196
+
197
+ Thread.current[SETTLING] = true
198
+ outcome = Leftovers.check(pgid, logs, grace: LEFTOVER_GRACE_SEC, stop_grace: LEFTOVER_STOP_GRACE_SEC,
199
+ poll: Leftovers::POLL_SEC, deadline: nil,
200
+ cancelled: -> { record_at(record_path)&.stop_requested_by })
201
+ Thread.current[SETTLING] = false unless outcome.leftovers
202
+ outcome.leftovers
203
+ end
204
+
205
+ # The waiter's one write: a record still running (held through the
206
+ # grace) is finished with the shell's code and +found+; one a refresh
207
+ # finished meanwhile is corrected. A stop's flag wins.
208
+ def settle(record_path, code, found)
209
+ @settle_lock.synchronize do
210
+ record = record_at(record_path)
211
+ next if record.nil? || record.stop_requested_by
212
+ next correct_finished_record(record_path, code, leftovers: found) unless record.running?
213
+ next unless found && code
214
+
215
+ AtomicFile.write(record_path, record_json(finished_with(record, code).with(leftovers: found.to_s,
216
+ finished_at: timestamp)))
217
+ end
218
+ end
219
+
220
+ # The task's logs as its record names them: output_path, and a
221
+ # handed-over execute's stderr_path.
222
+ def log_paths(record)
223
+ return [] unless record
224
+
225
+ [record.output_path, record.stderr_path].filter_map { |log| log.to_s unless log.to_s.empty? }
226
+ end
227
+
228
+ # The one line task_get, task_wait (and the finish note) and task_list
229
+ # add for a record with +leftovers+; nil without.
230
+ def leftovers_note(record)
231
+ kind = record.leftovers.to_s.to_sym
232
+ Leftovers::TASK_ADVICE.key?(kind) ? Leftovers.task_hint(kind) : nil
233
+ end
234
+
126
235
  # A Process::Status as a shell reports one: its exit status, or
127
236
  # 128 + the signal that killed it.
128
237
  def status_code(status)
@@ -151,14 +260,20 @@ module Samagotchi
151
260
  MISSING_CODE_REASONS = ["process_ended_without_exit_code", NOT_CHIS_PROCESS].freeze
152
261
 
153
262
  # A record a refresh finished before the exit code was there (failed,
154
- # exit_code nil, no stop) gets +code+; finished_at is kept.
155
- def correct_finished_record(record_path, code)
263
+ # exit_code nil, no stop) gets +code+; finished_at is kept. With
264
+ # +leftovers+, a finished record without the key gets it too.
265
+ def correct_finished_record(record_path, code, leftovers: nil)
156
266
  record = record_at(record_path)
157
- return unless code && record && !record.running? && record.exit_code.nil? && !record.stop_requested_by
158
- return unless MISSING_CODE_REASONS.include?(record.stop_reason)
267
+ return unless record && !record.running? && !record.stop_requested_by
268
+
269
+ updates = {}
270
+ if code && record.exit_code.nil? && MISSING_CODE_REASONS.include?(record.stop_reason)
271
+ updates.merge!(status: code.zero? ? "completed" : "failed", exit_code: code, stop_reason: nil)
272
+ end
273
+ updates[:leftovers] = leftovers.to_s if leftovers && record.leftovers.nil?
274
+ return if updates.empty?
159
275
 
160
- status = code.zero? ? "completed" : "failed"
161
- AtomicFile.write(record_path, record_json(record.with(status: status, exit_code: code, stop_reason: nil)))
276
+ AtomicFile.write(record_path, record_json(record.with(**updates)))
162
277
  end
163
278
 
164
279
  def record_at(path)
@@ -327,14 +442,26 @@ module Samagotchi
327
442
  refreshed = refresh_record(record)
328
443
  return [refreshed, nil] unless refreshed.running?
329
444
 
330
- # Checked again just before the signal: never a group chi didn't start.
331
- return [mark_not_chis(refreshed), nil] unless chis_group?(refreshed, started: refreshed.pid_started)
445
+ # Checked again just before the signal: never a group chi didn't
446
+ # start. A shell already gone whose leftovers still hold the log
447
+ # (a waiter's grace) is stopped by its group.
448
+ leftover = !chis_group?(refreshed, started: refreshed.pid_started)
449
+ return [mark_not_chis(refreshed), nil] if leftover && !leftover_group?(refreshed)
332
450
 
333
- flagged = refreshed.with(stop_requested_by: by.to_s)
334
- write_record(flagged)
451
+ flagged = @settle_lock.synchronize do
452
+ current = current_record(refreshed)
453
+ next current unless current.running?
335
454
 
336
- # A group that isn't ours (EPERM) is an error, not a stop.
337
- ProcessGroup.stop(flagged.pid, grace: STOP_GRACE_SEC, poll: STOP_POLL_INTERVAL_SEC, quiet: [Errno::ESRCH])
455
+ current.with(stop_requested_by: by.to_s).tap { |with_flag| write_record(with_flag) }
456
+ end
457
+ return [flagged, nil] unless flagged.running?
458
+
459
+ if leftover
460
+ Leftovers.stop_group(flagged.pid, log_paths(flagged), grace: STOP_GRACE_SEC, poll: STOP_POLL_INTERVAL_SEC)
461
+ else
462
+ # A group that isn't ours (EPERM) is an error, not a stop.
463
+ ProcessGroup.stop(flagged.pid, grace: STOP_GRACE_SEC, poll: STOP_POLL_INTERVAL_SEC, quiet: [Errno::ESRCH])
464
+ end
338
465
 
339
466
  updated = mark_stopped(refresh_record(flagged))
340
467
  write_record(updated)
@@ -344,15 +471,62 @@ module Samagotchi
344
471
  [nil, "Error: #{e.message}"]
345
472
  end
346
473
 
474
+ # A running record whose shell is dead and reaped while processes of
475
+ # its group still hold its logs: the group is the task's (a pgid isn't
476
+ # reused while it has members).
477
+ def leftover_group?(record)
478
+ pid = record.pid
479
+ ProcessGroup.signalable?(pid) && (waiter_for(record.id, pid: pid) || !ProcessGroup.alive?(pid)) &&
480
+ ProcessGroup.group_alive?(pid) &&
481
+ Leftovers.held?(log_paths(record))
482
+ end
483
+
347
484
  def refresh_record(record)
348
485
  return record unless record.running?
349
486
 
350
487
  pid = record.pid
351
488
  return record if chis_group?(record, started: record.pid_started)
352
- # Gone: its exit code says how it ended.
353
- return mark_finished_without_exit_code(record) if ProcessGroup.signalable?(pid) && !ProcessGroup.alive?(pid)
354
489
 
355
- mark_not_chis(record)
490
+ # This process's waiter of this pid not done yet: the pid was its
491
+ # shell. A zombie keeps the pid until the waiter reaps it; after
492
+ # that (the waiter's grace) the pid can't be reused while the group
493
+ # still has members (POSIX), and with the group empty a reused pid
494
+ # is only read here: every signal below first checks the group.
495
+ waiter = waiter_for(record.id, pid: pid)
496
+ return mark_not_chis(record) unless ProcessGroup.signalable?(pid) && (waiter || !ProcessGroup.alive?(pid))
497
+
498
+ # Gone: its exit code says how it ended, once nothing it left holds
499
+ # the log. This process's waiter settles a held one (a refresh only
500
+ # reads it); with none here, the refresh does.
501
+ logs = log_paths(record)
502
+ return current_record(record) if waiter && (waiter[SETTLING] || Leftovers.held?(logs))
503
+ return settle_from_refresh(record, logs) if !waiter && Leftovers.held?(logs)
504
+
505
+ mark_finished_without_exit_code(record)
506
+ end
507
+
508
+ # A record whose shell is gone, its log still held, and no waiter here
509
+ # (the chi that started it is gone, or another process reads): a
510
+ # second look after HELD_RECHECK_SEC, then the group is stopped
511
+ # without the waiter's grace (the shell may be long gone) and the
512
+ # record finished with what was found. A group already empty at the
513
+ # first look is never signalled: a log still held then is a holder
514
+ # outside it (detached). Blocks this read up to
515
+ # REFRESH_STOP_GRACE_SEC + REFRESH_KILL_WAIT_SEC and a ps.
516
+ def settle_from_refresh(record, logs)
517
+ return mark_finished_without_exit_code(record) if current_record(record).stop_requested_by
518
+
519
+ group = ProcessGroup.group_alive?(record.pid)
520
+ sleep(HELD_RECHECK_SEC)
521
+ return mark_finished_without_exit_code(record) unless Leftovers.held?(logs)
522
+
523
+ found = if group
524
+ Leftovers.stop_group(record.pid, logs, grace: REFRESH_STOP_GRACE_SEC, poll: Leftovers::POLL_SEC,
525
+ kill_wait: REFRESH_KILL_WAIT_SEC)
526
+ else
527
+ :detached
528
+ end
529
+ mark_finished_without_exit_code(record, leftovers: found)
356
530
  end
357
531
 
358
532
  # Whether the record's pid still leads its own group. With +started+
@@ -494,7 +668,8 @@ module Samagotchi
494
668
  filtered.join(" ")
495
669
  end
496
670
 
497
- def mark_finished_without_exit_code(record, missing_reason: "process_ended_without_exit_code")
671
+ # @param leftovers [Symbol, nil] what a refresh found left holding the log
672
+ def mark_finished_without_exit_code(record, missing_reason: "process_ended_without_exit_code", leftovers: nil)
498
673
  record = current_record(record)
499
674
  return record unless record.running?
500
675
 
@@ -504,16 +679,20 @@ module Samagotchi
504
679
  mark_stopped(record.with(exit_code: exit_code))
505
680
  elsif exit_code.nil?
506
681
  record.with(status: "failed", exit_code: nil, stop_reason: record.stop_reason || missing_reason)
507
- elsif exit_code.zero?
508
- record.with(status: "completed", exit_code: 0)
509
682
  else
510
- record.with(status: "failed", exit_code: exit_code)
683
+ finished_with(record, exit_code)
511
684
  end
685
+ record = record.with(leftovers: leftovers.to_s) if leftovers && !record.stop_requested_by
512
686
  record = record.with(finished_at: record.finished_at || timestamp)
513
687
  write_record(record)
514
688
  record
515
689
  end
516
690
 
691
+ # +record+ ended with the shell's +code+: completed on 0, else failed.
692
+ def finished_with(record, code)
693
+ code.zero? ? record.with(status: "completed", exit_code: 0) : record.with(status: "failed", exit_code: code)
694
+ end
695
+
517
696
  # The disk copy when it is newer than +record+: already finished, or
518
697
  # flagged by a stop that started after +record+ was read.
519
698
  def current_record(record)
@@ -108,12 +108,13 @@ module Samagotchi
108
108
  # A finished task's text, as task_wait gives it: its status lines and
109
109
  # always the log's last lines (a test run's summary, an error), then a
110
110
  # stderr_tail when the record has a non-empty one. A task the user
111
- # stopped also says so. Public: a task finish wake's note
112
- # (TaskFinishNote) carries the same text.
111
+ # stopped also says so, and one whose shell left processes holding
112
+ # its log (TaskRuntime.leftovers_note). Public: a task finish wake's
113
+ # note (TaskFinishNote) carries the same text.
113
114
  # @param record [TaskRecord] a finished one
114
115
  # @return [String] a RefText of its output_path when it has one
115
116
  def self.finished_text(record, tail_lines: TAIL_LINES_DEFAULT)
116
- extra = record.stopped_by_user? ? [TaskRuntime::USER_STOP_NOTE] : []
117
+ extra = [TaskRuntime.leftovers_note(record), (TaskRuntime::USER_STOP_NOTE if record.stopped_by_user?)].compact
117
118
  format_response(record, output_tail: output_tail(record, tail_lines), tail_lines: tail_lines, extra: extra)
118
119
  end
119
120
 
@@ -19,7 +19,7 @@ module Samagotchi
19
19
  # +session+ the Session it runs in (live: its working-set pins are read at call time)
20
20
  # +origin+ its TurnOrigin (TurnOrigin::NONE for the owner's)
21
21
  # +author+ the participant's author ("p:<id>") when one wrote it, else nil
22
- # +participant_mode+ a participant's turn's mode (ParticipantMode: :review, :pair), nil for the owner's
22
+ # +participant_mode+ a participant's turn's mode (GuardrailWiring::PAIR, or REFUSED: no current invite), nil for the owner's
23
23
  # +participant+ that participant's Participants::Entry (participants.json), nil for none
24
24
  # +controller+ the turn's CancellationController (live: its cancel state); a thread
25
25
  # off the turn cancels through TurnState#controller, never through this
@@ -52,7 +52,7 @@ module Samagotchi
52
52
  def participant_id = participant_author && Steer.participant_id(participant_author)
53
53
 
54
54
  # What sent the turn, by its client (ClientId.origin): "client",
55
- # "reminder", "delegate", "review"...
55
+ # "reminder", "delegate", "participant"...
56
56
  def source = ClientId.origin(client_id)
57
57
 
58
58
  # Whether a human sent the turn (ClientId.human?: a missing client is).
@@ -48,13 +48,13 @@ module Samagotchi
48
48
  # ask settled: the session's candidates' status changes are logged
49
49
  # (ContextCandidatesLog#look) and, unless context.candidates.mode is
50
50
  # off, the proposed candidates due an ask are asked about the owner's
51
- # prompts so far, this one included. Never a participant's turn, a
52
- # review session or a Continue (no new prompt). The sessions dir is
51
+ # prompts so far, this one included. Never a participant's turn or a
52
+ # Continue (no new prompt). The sessions dir is
53
53
  # the turn's (TurnContext#state_dir).
54
54
  # @param recorder [SystemOne::Recorder] the asks' runs (built for this call)
55
55
  def start(turn, recorder:)
56
56
  session = turn.session
57
- return unless turn.prompt.is_a?(String) && OwnerPrompt.turn?(session: session, author: turn.origin.author)
57
+ return unless turn.prompt.is_a?(String) && OwnerPrompt.turn?(author: turn.origin.author)
58
58
 
59
59
  pass(session, ContextCandidates.owner_texts(session.messages) + [turn.prompt], phase: "turn",
60
60
  state_dir: turn.context.state_dir, recorder: recorder)
@@ -64,7 +64,7 @@ module Samagotchi
64
64
  # #candidate_proposed: github-pr's init task runs beside the first
65
65
  # turn): judged on the owner's prompts so far, the running turn's too.
66
66
  # The caller (Engine#judge_late_candidates) gives the session the
67
- # Engine is on, never nil nor a review session.
67
+ # Engine is on, never nil.
68
68
  # @param messages [Array<Hash>] the owner's conversation so far (Engine#plugin_messages)
69
69
  # @param recorder [SystemOne::Recorder] the asks' runs (built for this call)
70
70
  # @param state_dir [String] the sessions dir
@@ -41,7 +41,7 @@ module Samagotchi
41
41
  # reminder's or a delegate's prompt isn't one.
42
42
  # @param recorder [SystemOne::Recorder] the audit's runs (built for this call)
43
43
  def incoming(turn, recorder:)
44
- return unless turn.prompt.is_a?(String) && turn.origin.human? && OwnerPrompt.turn?(session: turn.session, author: turn.origin.author)
44
+ return unless turn.prompt.is_a?(String) && turn.origin.human? && OwnerPrompt.turn?(author: turn.origin.author)
45
45
 
46
46
  audit(turn.context.state_dir, recorder).incoming(turn.session, turn.prompt)
47
47
  end
@@ -51,7 +51,7 @@ module Samagotchi
51
51
  # @param recorder [SystemOne::Recorder] the audit's runs (built for this
52
52
  # call: after a before_turn hook's model switch, the new model)
53
53
  def outgoing(turn, result, recorder:)
54
- return unless OwnerPrompt.turn?(session: turn.session, author: turn.origin.author)
54
+ return unless OwnerPrompt.turn?(author: turn.origin.author)
55
55
 
56
56
  audit(turn.context.state_dir, recorder).outgoing(turn.session, result.text)
57
57
  end
@@ -113,12 +113,12 @@ module Samagotchi
113
113
  # never logged and never waited for, so System 1's server has the
114
114
  # question's prefix cached for the first prompt. Only for a session a
115
115
  # first prompt could be asked for: origin default, no model reply, not
116
- # a delegate child or a review session, at least two candidates; and
116
+ # a delegate child, at least two candidates; and
117
117
  # not when the worker starts on a first prompt (that ask comes at once).
118
118
  def warm!(session)
119
119
  return unless Config.get("toolkit.suggest.mode").to_s == "on"
120
120
 
121
- return if session.nil? || session.toolkit_origin != "default" || session.delegate? || session.review?
121
+ return if session.nil? || session.toolkit_origin != "default" || session.delegate?
122
122
  return if ToolkitSuggest.model_reply?(session.messages) || !session.last_prompt.to_s.strip.empty?
123
123
 
124
124
  default = Toolkit.of(session.toolkit)
@@ -2,7 +2,7 @@
2
2
 
3
3
  require "fileutils"
4
4
  require_relative "desktop"
5
- require_relative "memory_bundle/shipped_update"
5
+ require_relative "memory_bundle/source"
6
6
  require_relative "session"
7
7
  require_relative "version"
8
8
 
@@ -43,6 +43,8 @@ module Samagotchi
43
43
  end
44
44
 
45
45
  def message(version, shipped_dir:, helper:)
46
+ require_relative "memory_bundle/shipped_update"
47
+
46
48
  # A retry that would merge is an update too; one that only records a reason isn't.
47
49
  bundles = MemoryBundle::ShippedUpdate.plan(shipped_dir: shipped_dir).count do |r|
48
50
  r.status == :would_update || (r.status == :would_merge && r.merged.any?)
@@ -69,7 +69,7 @@ module Samagotchi
69
69
  # Starts a fresh child for the parent's unclear verdict +of+ (its
70
70
  # check_turn_id). +author+: the requester ("p:<id>"), nil for the
71
71
  # owner; +mode+: a participant's grant's mode in the parent
72
- # (ParticipantMode.live_for), saved on the child.
72
+ # (Grants::PAIR_MODE), saved on the child.
73
73
  # @return [Session] the child
74
74
  # @raise [Refused] no such verdict, not unclear, or a fresh check of it
75
75
  # still runs
@@ -3,14 +3,15 @@
3
3
  require_relative "steer"
4
4
  require_relative "turn_origin"
5
5
  require_relative "verify_anchor"
6
- require_relative "participant_mode"
6
+ require_relative "participant_turn_mode"
7
7
 
8
8
  module Samagotchi
9
9
  # A Verify fresh child's own field (Session#verify_fresh; VerifyFresh
10
10
  # starts one): who asked for it (+author+, a participant's "p:<id>", nil
11
11
  # for the owner), the rules a participant's turns run under in it
12
- # (+mode+, "review" or "pair": their grant's mode in the parent when it
13
- # started, ParticipantMode.live_for), the parent's verdict it checks again
12
+ # (+mode+, "pair": their grant's when it started; "review" in a child an
13
+ # older chi saved for a read-only turn, whose turns are refused), the
14
+ # parent's verdict it checks again
14
15
  # (+of+, that verdict's check_turn_id) and the claim (+anchor+, a
15
16
  # VerifyAnchor). Every turn of a participant's child is theirs
16
17
  # (#origin_for): their author, their boundary, their cap. A field that is
@@ -41,9 +42,9 @@ module Samagotchi
41
42
  # delegate child).
42
43
  def participant? = Steer.participant_author?(author)
43
44
 
44
- # The boundary a participant's turn runs under here: :pair only when
45
- # saved so, else :review (fail closed).
46
- def turn_mode = mode == ParticipantMode::PAIR.to_s ? ParticipantMode::PAIR : ParticipantMode::REVIEW
45
+ # Whether a participant's turns run here, under the owner's rules: only
46
+ # when saved so (fail closed: ParticipantTurnMode).
47
+ def pair? = mode == ParticipantTurnMode::PAIR_MODE
47
48
 
48
49
  # +origin+ with this child's requester as its author: every turn of a
49
50
  # participant's child is theirs, whoever typed it; the owner's child
@@ -59,12 +60,13 @@ module Samagotchi
59
60
  def to_file = { "author" => author, "mode" => mode, "of" => of, "anchor" => anchor&.to_file }.compact
60
61
  end
61
62
 
62
- # A verify_fresh field that doesn't read: a participant author nobody
63
- # has, so the worker refuses its turns ("revoked"), under the review
64
- # boundary.
65
- # The modes a participant's child may be saved with (a constant of
63
+ # The modes a participant's child may be saved with: "pair", and "review"
64
+ # (an older chi's, read so it is refused, never run) (a constant of
66
65
  # VerifyFreshOrigin: one inside the Data.define block would be Samagotchi's).
67
- VerifyFreshOrigin::MODES = [ParticipantMode::REVIEW.to_s, ParticipantMode::PAIR.to_s].freeze
66
+ VerifyFreshOrigin::MODES = %w[review pair].freeze
67
+
68
+ # A verify_fresh field that doesn't read: a participant author nobody
69
+ # has, so the worker refuses its turns ("revoked").
68
70
 
69
71
  VerifyFreshOrigin::INVALID = VerifyFreshOrigin.new(author: "#{Steer::PARTICIPANT_PREFIX}invalid", mode: "review",
70
72
  of: nil, anchor: nil)
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Samagotchi
4
- VERSION = "0.171.0"
4
+ VERSION = "0.172.1"
5
5
  # Sent as the User-Agent of every HTTP request chi makes.
6
6
  USER_AGENT = "chi/#{VERSION}"
7
7
  end
@@ -55,7 +55,6 @@ require_relative "../system_one/client"
55
55
  require_relative "../turn_flow"
56
56
  require_relative "../session_chain"
57
57
  require_relative "../session_access"
58
- require_relative "../review_start"
59
58
  require_relative "../parent_answers_command"
60
59
  require_relative "../guardrails/parent_policy"
61
60
  require_relative "markdown_renderer"
@@ -331,7 +330,6 @@ module Samagotchi
331
330
  ["POST", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_invite, :owner, :session],
332
331
  ["DELETE", %r{\A/api/sessions/([^/]+)/participants\z}, :handle_participants_revoke, :owner, :session],
333
332
  ["POST", %r{\A/api/sessions/([^/]+)/participants/([^/]+)/cap\z}, :handle_participants_cap, :owner, :session],
334
- ["GET", %r{\A/api/sessions/([^/]+)/apply\z}, :handle_apply, :owner, :session],
335
333
  ["GET", %r{\A/api/participants\z}, :handle_participants_list, :owner],
336
334
  ["DELETE", %r{\A/api/participants\z}, :handle_participants_revoke, :owner],
337
335
  ["GET", %r{\A/api/push\z}, :handle_push_info, :owner],
@@ -1154,9 +1152,9 @@ module Samagotchi
1154
1152
  json_response(200, payload)
1155
1153
  end
1156
1154
 
1157
- # Kits the web never offers: the participants' boundary, a delegate
1158
- # child's, and ToolkitSuggest's defer sentinel.
1159
- PICKER_HIDDEN_KITS = [Toolkit::REVIEW, Toolkit::CHILD, ToolkitSuggest::UNDECIDED].freeze
1155
+ # Kits the web never offers: a delegate child's, and ToolkitSuggest's
1156
+ # defer sentinel.
1157
+ PICKER_HIDDEN_KITS = [Toolkit::CHILD, ToolkitSuggest::UNDECIDED].freeze
1160
1158
 
1161
1159
  # The start page's toolkit picker: {default:, suggest:, kits: [{name,
1162
1160
  # description?}]}. Built-in and config kits only (a bundle's load in