samagotchi 0.160.0 → 0.162.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 +110 -1
- data/docs/architecture.md +8 -4
- data/docs/cli.md +6 -4
- data/docs/context.md +10 -1
- data/docs/guardrails.md +12 -3
- data/docs/internals/block-renderers.md +3 -2
- data/docs/internals/bridge.md +82 -17
- data/docs/internals/bundles.md +24 -5
- data/docs/internals/inline-widgets.md +6 -3
- data/docs/internals/objectives.md +8 -2
- data/docs/internals/remote-sessions.md +2 -1
- data/docs/internals/web-frontend.md +1 -1
- data/docs/memory.md +4 -2
- data/docs/plugins.md +13 -4
- data/docs/releasing.md +7 -4
- data/docs/scheduling.md +37 -5
- data/docs/sessions.md +15 -12
- data/docs/testing.md +16 -2
- data/lib/samagotchi/bridge/card_store.rb +54 -16
- data/lib/samagotchi/bridge/ring_buffer.rb +11 -1
- data/lib/samagotchi/bridge/sse_writer.rb +8 -5
- data/lib/samagotchi/bridge/turn_accumulator.rb +7 -1
- data/lib/samagotchi/bridge/turn_drawings/synth.rb +555 -0
- data/lib/samagotchi/bridge/turn_drawings.rb +103 -7
- data/lib/samagotchi/bridge/turn_ranges.rb +175 -0
- data/lib/samagotchi/bridge.rb +127 -54
- data/lib/samagotchi/bridge_client.rb +9 -6
- data/lib/samagotchi/broadcast_command.rb +24 -8
- data/lib/samagotchi/bundle_command.rb +56 -6
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/child_reports.rb +29 -23
- data/lib/samagotchi/client_id.rb +2 -0
- data/lib/samagotchi/compat.rb +32 -0
- data/lib/samagotchi/context_candidates_log.rb +10 -1
- data/lib/samagotchi/context_command.rb +1 -1
- data/lib/samagotchi/context_move.rb +6 -1
- data/lib/samagotchi/engine/events.rb +12 -0
- data/lib/samagotchi/engine/plugins.rb +1 -1
- data/lib/samagotchi/engine/steering.rb +2 -2
- data/lib/samagotchi/guardrails/child_boundary.rb +37 -4
- data/lib/samagotchi/guardrails/protected_paths.rb +8 -5
- data/lib/samagotchi/guardrails/targets.rb +25 -6
- data/lib/samagotchi/live_versions.rb +7 -2
- data/lib/samagotchi/memory_bundle/asset_installer.rb +59 -12
- data/lib/samagotchi/memory_bundle/file_state.rb +11 -0
- data/lib/samagotchi/memory_bundle/index_updater.rb +7 -4
- data/lib/samagotchi/memory_bundle/installer.rb +79 -12
- data/lib/samagotchi/memory_bundle/provenance.rb +158 -6
- data/lib/samagotchi/memory_bundle/shipped_update.rb +2 -1
- data/lib/samagotchi/memory_bundle/status.rb +1 -1
- data/lib/samagotchi/memory_bundle/system_bundle.rb +2 -0
- data/lib/samagotchi/memory_bundle/uninstaller.rb +27 -13
- data/lib/samagotchi/memory_orphans.rb +40 -9
- data/lib/samagotchi/note_command.rb +9 -1
- data/lib/samagotchi/note_delivery.rb +34 -24
- data/lib/samagotchi/note_move.rb +205 -0
- data/lib/samagotchi/objectives_command.rb +3 -1
- data/lib/samagotchi/owner_lock.rb +7 -1
- data/lib/samagotchi/plugin/sessions.rb +8 -1
- data/lib/samagotchi/project_id_clash.rb +1 -1
- data/lib/samagotchi/project_init.rb +3 -3
- data/lib/samagotchi/review_start.rb +5 -3
- data/lib/samagotchi/schedule_command.rb +12 -0
- data/lib/samagotchi/schedules.rb +286 -48
- data/lib/samagotchi/send_command.rb +2 -15
- data/lib/samagotchi/session.rb +42 -5
- data/lib/samagotchi/session_access/local.rb +45 -17
- data/lib/samagotchi/session_access/remote.rb +11 -4
- data/lib/samagotchi/session_access/web_bridge_client.rb +2 -2
- data/lib/samagotchi/session_access.rb +16 -3
- data/lib/samagotchi/session_continue.rb +155 -21
- data/lib/samagotchi/session_delete_command.rb +3 -1
- data/lib/samagotchi/session_inbox.rb +41 -1
- data/lib/samagotchi/session_inbox_delivery.rb +254 -0
- data/lib/samagotchi/session_manager/delivery.rb +66 -12
- data/lib/samagotchi/session_manager/emptiness.rb +7 -2
- data/lib/samagotchi/session_manager/lifecycle.rb +2 -1
- data/lib/samagotchi/session_manager/listing.rb +4 -1
- data/lib/samagotchi/session_manager/removal.rb +130 -26
- data/lib/samagotchi/session_manager/spawn.rb +164 -104
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_retention.rb +53 -23
- data/lib/samagotchi/session_setup.rb +27 -7
- data/lib/samagotchi/starting_marker.rb +93 -24
- data/lib/samagotchi/state_format.rb +18 -7
- data/lib/samagotchi/system_one/runs.rb +6 -1
- data/lib/samagotchi/system_one/runs_store.rb +18 -14
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +7 -4
- data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +5 -2
- data/lib/samagotchi/terminal_ui/attached_loop.rb +5 -0
- data/lib/samagotchi/terminal_ui/join_replay.rb +34 -128
- data/lib/samagotchi/terminal_ui/version_lines.rb +4 -2
- data/lib/samagotchi/terminal_ui.rb +14 -4
- data/lib/samagotchi/tools/delegate.rb +9 -4
- data/lib/samagotchi/tools/schedule_task.rb +5 -3
- data/lib/samagotchi/tools/send_note.rb +10 -4
- data/lib/samagotchi/turn_state.rb +108 -4
- data/lib/samagotchi/update_command.rb +7 -2
- data/lib/samagotchi/verify_fresh.rb +27 -9
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +1 -1
- data/lib/samagotchi/web/message_display.rb +5 -1
- data/lib/samagotchi/web/public/app.js +2 -7
- data/lib/samagotchi/web/public/block_frame.js +5 -1
- data/lib/samagotchi/web/public/chains.js +4 -4
- data/lib/samagotchi/web/public/data.js +18 -2
- data/lib/samagotchi/web/public/info_bar_model.js +1 -1
- data/lib/samagotchi/web/public/session_fields.js +3 -2
- data/lib/samagotchi/web/public/session_state.js +2 -0
- data/lib/samagotchi/web/public/timing.js +17 -6
- data/lib/samagotchi/web/public/turn_block.js +6 -2
- data/lib/samagotchi/web/public/update_notice.js +2 -2
- data/lib/samagotchi/web/public/widget_walk.js +4 -2
- data/lib/samagotchi/web/session_access_routes.rb +12 -8
- data/lib/samagotchi/web/session_control.rb +9 -3
- data/lib/samagotchi/web/session_create.rb +8 -4
- data/lib/samagotchi/web/session_hub.rb +50 -5
- data/lib/samagotchi/web/session_show.rb +22 -11
- data/lib/samagotchi/web/session_summary.rb +5 -2
- data/lib/samagotchi/worker.rb +45 -7
- data/lib/samagotchi/worker_boot.rb +22 -11
- data/lib/samagotchi/worker_inbound.rb +41 -8
- data/lib/samagotchi/worker_link.rb +4 -1
- data/lib/samagotchi/worker_presence.rb +106 -0
- data/lib/samagotchi/worker_sidecar.rb +3 -4
- metadata +7 -1
|
@@ -2,14 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
require_relative "../bridge/turn_accumulator"
|
|
4
4
|
require_relative "../bridge/turn_drawings"
|
|
5
|
+
require_relative "../bridge/turn_drawings/synth"
|
|
6
|
+
require_relative "../bridge/turn_ranges"
|
|
5
7
|
require_relative "../context_note"
|
|
6
8
|
require_relative "../image_store"
|
|
7
9
|
require_relative "../log"
|
|
8
|
-
require_relative "../message_parts"
|
|
9
10
|
require_relative "../output_formatter"
|
|
10
11
|
require_relative "../steer"
|
|
11
|
-
require_relative "../tool_activity"
|
|
12
|
-
require_relative "../tool_view"
|
|
13
12
|
require_relative "../turn_note"
|
|
14
13
|
require_relative "event_renderer"
|
|
15
14
|
require_relative "formatting"
|
|
@@ -33,11 +32,13 @@ module Samagotchi
|
|
|
33
32
|
@renderer = renderer
|
|
34
33
|
end
|
|
35
34
|
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
35
|
+
# The last prompt's turn is drawn from a drawing: the snapshot's
|
|
36
|
+
# last_turn_drawing (Bridge#snapshot: as live drew it) when it is that
|
|
37
|
+
# turn's, else one made from the snapshot's messages, the link's saved
|
|
38
|
+
# records and the snapshot's cards (Bridge::TurnDrawings::Synth: an
|
|
39
|
+
# older worker, a turn before drawings, a line over 1 MB).
|
|
39
40
|
def render_join(snapshot, saved)
|
|
40
|
-
render_join_header(Array(snapshot[:messages]), drawing: snapshot[:last_turn_drawing])
|
|
41
|
+
render_join_header(Array(snapshot[:messages]), drawing: snapshot[:last_turn_drawing], cards: Array(snapshot[:cards]))
|
|
41
42
|
render_join_gap(snapshot, saved)
|
|
42
43
|
end
|
|
43
44
|
|
|
@@ -106,7 +107,7 @@ module Samagotchi
|
|
|
106
107
|
@screen.commit(gap_divider(line)) if line
|
|
107
108
|
end
|
|
108
109
|
|
|
109
|
-
def render_join_header(messages, drawing: nil)
|
|
110
|
+
def render_join_header(messages, drawing: nil, cards: [])
|
|
110
111
|
exchange = messages.select { |m| %w[user model assistant].include?(m[:role].to_s) }
|
|
111
112
|
# The id stays findable: the detach line and chi sessions list show it.
|
|
112
113
|
Log.session_id = @link.client.session_id
|
|
@@ -119,36 +120,17 @@ module Samagotchi
|
|
|
119
120
|
render_join_notes(messages, from)
|
|
120
121
|
return unless last_user
|
|
121
122
|
|
|
122
|
-
|
|
123
|
+
prompt = exchange[last_user]
|
|
124
|
+
@screen.commit(join_prompt_line(prompt))
|
|
123
125
|
# An image-only turn's text names its images already.
|
|
124
|
-
unless ImageRef.placeholder_only?(
|
|
125
|
-
Array(
|
|
126
|
+
unless ImageRef.placeholder_only?(prompt[:content])
|
|
127
|
+
Array(prompt[:images]).each { |ref| @screen.commit(format_image_line(ref)) }
|
|
126
128
|
end
|
|
127
|
-
|
|
129
|
+
drawn = drawn_turn(drawing, prompt) || made_turn(messages, from, cards)
|
|
130
|
+
if drawn
|
|
128
131
|
render_drawn_turn(*drawn, exchange[(last_user + 1)..])
|
|
129
|
-
return render_join_notes(messages, from, after_exchange: true)
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
render_join_steps(messages, from, durations: join_tool_records(exchange[last_user]))
|
|
133
|
-
record = @link.saved_turn_record(exchange[last_user][:turn_id] || exchange[last_user]["turn_id"])
|
|
134
|
-
failed = join_failed_record(record)
|
|
135
|
-
# A turn that ended with no answer: its notice, as live.
|
|
136
|
-
empty = messages.drop(from + 1).filter_map { |m| TurnNote.empty_answer(m) }.last
|
|
137
|
-
if empty
|
|
138
|
-
@screen.commit(format_empty_answer_line(empty[:retries] || empty["retries"], empty[:refusal] || empty["refusal"]))
|
|
139
|
-
elsif failed
|
|
140
|
-
# A failed turn whose steps stayed: the line the live view left,
|
|
141
|
-
# and no step text as if it were the answer (the web keeps a
|
|
142
|
-
# failed turn's texts as steps too).
|
|
143
|
-
failure = failed["failure"]
|
|
144
|
-
@screen.commit(@view.turn_failed_line(failure["summary"] || failure["message"], failed["duration_ms"]))
|
|
145
|
-
@screen.commit(@view.turn_end_hint(ROLLBACK_HINT)) if failure["kept_steps"]
|
|
146
|
-
elsif record && record["status"] == "canceled"
|
|
147
|
-
# A canceled turn (a Stop, Ctrl-C): its line, as live, and no
|
|
148
|
-
# text it streamed before the cancel as if it were the answer.
|
|
149
|
-
@screen.commit(@view.turn_canceled_line(record["cancellation_reason"], record["duration_ms"], by: record["cancelled_by"]))
|
|
150
|
-
@screen.commit(@view.turn_end_hint(ROLLBACK_HINT)) unless record["continue"]
|
|
151
132
|
else
|
|
133
|
+
# No turn ran for it (a `!cmd`'s record): what came after it.
|
|
152
134
|
render_join_answer(exchange[(last_user + 1)..])
|
|
153
135
|
end
|
|
154
136
|
render_join_notes(messages, from, after_exchange: true)
|
|
@@ -179,6 +161,24 @@ module Samagotchi
|
|
|
179
161
|
[turn, EventRenderer.symbolize(Bridge::TurnAccumulator.from_wire(drawing[:end]))]
|
|
180
162
|
end
|
|
181
163
|
|
|
164
|
+
# The prompt's turn (its opening at +from+ in +messages+) drawn from
|
|
165
|
+
# its messages (TurnRanges, Synth.safe_line) with the link's saved
|
|
166
|
+
# records of it (this machine's, or chi web's for --remote) and the
|
|
167
|
+
# snapshot's +cards+ (its CardStore rows), as [turn, end event]; nil
|
|
168
|
+
# when no turn opens there.
|
|
169
|
+
def made_turn(messages, from, cards)
|
|
170
|
+
# Its opening, or the range that holds it (a wake turn's report, its note the opening).
|
|
171
|
+
range = Bridge::TurnRanges.of(messages).find { |r| r.opening == from || r.range.cover?(from) }
|
|
172
|
+
return nil unless range
|
|
173
|
+
|
|
174
|
+
# Tool records first: a remote link reads both in one request then
|
|
175
|
+
# (RemoteLink#saved_tool_records keeps it for #saved_turn_record).
|
|
176
|
+
tools = range.turn_id ? Array(@link.saved_tool_records(range.turn_id)) : []
|
|
177
|
+
record = range.turn_id && @link.saved_turn_record(range.turn_id)
|
|
178
|
+
line = Bridge::TurnDrawings::Synth.safe_line(messages, range, record: record, tools: tools, entries: cards)
|
|
179
|
+
[Bridge::TurnAccumulator.from_wire(line.drawing), EventRenderer.symbolize(Bridge::TurnAccumulator.from_wire(line.end))]
|
|
180
|
+
end
|
|
181
|
+
|
|
182
182
|
# The last turn from its drawing, through the renderer as live drew it
|
|
183
183
|
# (the running turn's join path, AttachedLoop#render_current_turn): its
|
|
184
184
|
# rows with their status and duration, a retry's line, a hook's notice,
|
|
@@ -202,100 +202,6 @@ module Samagotchi
|
|
|
202
202
|
@view.finish_thinking_spinner
|
|
203
203
|
end
|
|
204
204
|
|
|
205
|
-
# What the last exchange did, as the live turn drew it: each saved
|
|
206
|
-
# call's tool row (action, params, status from its result, and its
|
|
207
|
-
# duration from +durations+, the turn's saved tool records) and a
|
|
208
|
-
# plugin's steer and the user's input merged into the turn where they
|
|
209
|
-
# came. The step texts stay out, as live (the ticker's).
|
|
210
|
-
def render_join_steps(messages, from, durations: [])
|
|
211
|
-
records = durations.dup
|
|
212
|
-
messages.each_with_index.drop(from + 1).each do |m, i|
|
|
213
|
-
if Steer.steer?(m)
|
|
214
|
-
@screen.commit(format_steer_line(source: m[:source], text: m[:content]))
|
|
215
|
-
elsif Steer.input?(m) && !Steer.turn_prompt?(m)
|
|
216
|
-
@screen.commit("input> #{m[:content]}")
|
|
217
|
-
elsif %w[model assistant].include?(m[:role].to_s)
|
|
218
|
-
responses = messages.drop(i + 1).take_while { |r| r[:role].to_s == "tool_response" }
|
|
219
|
-
Array(MessageParts.for_message(m, responses)&.dig(:tools)).each do |part|
|
|
220
|
-
record = join_record(records, part)
|
|
221
|
-
duration = record&.dig("duration_ms")
|
|
222
|
-
@screen.commit(join_tool_line(part, duration_ms: duration.is_a?(Numeric) ? duration : nil,
|
|
223
|
-
saved_status: record&.dig("status"),
|
|
224
|
-
auto_allowed: record&.dig("guardrail")))
|
|
225
|
-
end
|
|
226
|
-
end
|
|
227
|
-
end
|
|
228
|
-
end
|
|
229
|
-
|
|
230
|
-
# The prompt's turn's saved tool records (AttachLink: SessionMetrics,
|
|
231
|
-
# in this machine's state dir, as #attach_images): none for a prompt
|
|
232
|
-
# with no turn id (an older session) or a session with no analytics.
|
|
233
|
-
def join_tool_records(prompt)
|
|
234
|
-
@link.saved_tool_records(prompt[:turn_id] || prompt["turn_id"])
|
|
235
|
-
end
|
|
236
|
-
|
|
237
|
-
# The turn's saved +record+ when it failed with the fields its line is
|
|
238
|
-
# made of (SessionMetrics#failure_fields), else nil: a completed or
|
|
239
|
-
# older turn — or one saved before the failure field — draws its
|
|
240
|
-
# answer as before (a canceled one its line).
|
|
241
|
-
def join_failed_record(record)
|
|
242
|
-
return nil unless record && record["status"] == "failed" && record["failure"].is_a?(Hash)
|
|
243
|
-
|
|
244
|
-
failure = record["failure"]
|
|
245
|
-
return nil if failure["summary"].to_s.empty? && failure["message"].to_s.empty?
|
|
246
|
-
|
|
247
|
-
record
|
|
248
|
-
end
|
|
249
|
-
|
|
250
|
-
# The saved record of +part+'s call (its duration and status): the
|
|
251
|
-
# next record of its tool, in call order, consumed with the ones it
|
|
252
|
-
# skipped. By tool and order, not by iteration: an empty retry spends
|
|
253
|
-
# an iteration that left no message (as the web's timing.js
|
|
254
|
-
# takeRecord). nil for a call that left none: it never finished, or a
|
|
255
|
-
# worker before turn ids saved it.
|
|
256
|
-
def join_record(records, part)
|
|
257
|
-
return nil if part[:output].nil?
|
|
258
|
-
|
|
259
|
-
at = records.index { |r| r["tool"].to_s == part[:tool].to_s }
|
|
260
|
-
return nil unless at
|
|
261
|
-
|
|
262
|
-
records.shift(at + 1).last
|
|
263
|
-
end
|
|
264
|
-
|
|
265
|
-
# A saved call's row; "no result" for a call the turn never answered
|
|
266
|
-
# (it ended first). Its status is the saved record's, as live and the
|
|
267
|
-
# web's reload (a guardrail's "blocked" is no error), else read from
|
|
268
|
-
# its result (a guardrail's deny text reads blocked too). A record that
|
|
269
|
-
# System 1 allowed once (+auto_allowed+, its saved {rule, p}) says so,
|
|
270
|
-
# as the live row does.
|
|
271
|
-
def join_tool_line(part, duration_ms: nil, saved_status: nil, auto_allowed: nil)
|
|
272
|
-
output = part[:output]
|
|
273
|
-
command = part.dig(:view, :truncated) ? nil : part.dig(:view, :command)
|
|
274
|
-
text = ToolActivity.saved_result_text(output) unless output.nil?
|
|
275
|
-
status = if output.nil?
|
|
276
|
-
"no result"
|
|
277
|
-
elsif !saved_status.to_s.empty?
|
|
278
|
-
saved_status.to_s
|
|
279
|
-
elsif ToolActivity.guardrail_denied?(text)
|
|
280
|
-
"blocked"
|
|
281
|
-
else
|
|
282
|
-
ToolActivity.tool_activity_status(text, part[:tool], command: command)
|
|
283
|
-
end
|
|
284
|
-
activity = { action: part[:label] || ToolActivity.tool_activity_action(part[:tool]), tool: part[:tool],
|
|
285
|
-
params: part[:params], status: status,
|
|
286
|
-
description: ToolView.description_title(part[:view] || {}) }
|
|
287
|
-
"#{format_tool_activity_line(activity, duration_ms: duration_ms)}" \
|
|
288
|
-
"#{format_tool_image_suffix(part[:images])}#{format_tool_diff_suffix(part[:diff])}#{format_tool_cut_suffix(part[:output_truncated])}" \
|
|
289
|
-
"#{auto_allowed_suffix(auto_allowed)}"
|
|
290
|
-
end
|
|
291
|
-
|
|
292
|
-
# The live row's System 1 suffix for a saved record's {rule, p}.
|
|
293
|
-
def auto_allowed_suffix(saved)
|
|
294
|
-
return "" unless saved.is_a?(Hash)
|
|
295
|
-
|
|
296
|
-
format_tool_guardrail_suffix({ decided_by: "system one", p: saved["p"] || saved[:p] })
|
|
297
|
-
end
|
|
298
|
-
|
|
299
205
|
# The context notes that came since the last prompt (+from+, its index
|
|
300
206
|
# in +messages+; all of them in a session with none), after the
|
|
301
207
|
# exchange the header shows.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "../installed_versions"
|
|
4
|
+
require_relative "../compat"
|
|
4
5
|
require_relative "../version"
|
|
5
6
|
require_relative "../chi_invocation"
|
|
6
7
|
|
|
@@ -24,7 +25,7 @@ module Samagotchi
|
|
|
24
25
|
|
|
25
26
|
newest = [installed, terminal].select { |v| Gem::Version.correct?(v.to_s) && !v.to_s.empty? }
|
|
26
27
|
.max_by { |v| Gem::Version.new(v) }
|
|
27
|
-
worker_old =
|
|
28
|
+
worker_old = Compat.verdict(worker_version: worker, version: newest) == :restart_suggested
|
|
28
29
|
terminal_old = InstalledVersions.newer?(newest, terminal)
|
|
29
30
|
return nil unless worker_old || terminal_old
|
|
30
31
|
|
|
@@ -51,7 +52,8 @@ module Samagotchi
|
|
|
51
52
|
def across_machines(worker:, installed:, session_id:, terminal:, via:)
|
|
52
53
|
there = [installed, worker].select { |v| Gem::Version.correct?(v.to_s) && !v.to_s.empty? }
|
|
53
54
|
.max_by { |v| Gem::Version.new(v) }
|
|
54
|
-
worker_old =
|
|
55
|
+
worker_old = installed && !installed.to_s.empty? &&
|
|
56
|
+
Compat.verdict(worker_version: worker, version: installed) == :restart_suggested
|
|
55
57
|
terminal_old = InstalledVersions.newer?(there, terminal)
|
|
56
58
|
return nil unless worker_old || terminal_old
|
|
57
59
|
|
|
@@ -14,6 +14,8 @@ require_relative "host_registry"
|
|
|
14
14
|
require_relative "session"
|
|
15
15
|
require_relative "owner_lock"
|
|
16
16
|
require_relative "bridge/card_store"
|
|
17
|
+
require_relative "bridge/turn_accumulator"
|
|
18
|
+
require_relative "bridge/turn_drawings"
|
|
17
19
|
require_relative "bridge/pending_card"
|
|
18
20
|
require_relative "engine"
|
|
19
21
|
require_relative "turn_flow"
|
|
@@ -189,12 +191,11 @@ module Samagotchi
|
|
|
189
191
|
mode: "assist",
|
|
190
192
|
model_name: @effective_model_name,
|
|
191
193
|
working_directory: Dir.pwd,
|
|
192
|
-
preloaded_memory_names: @requested_memories,
|
|
193
|
-
muted_memory_names: @muted_memory_names,
|
|
194
194
|
scratch: @scratch,
|
|
195
195
|
setup: SessionSetup.new(appended_prompt_files: @appended_prompt_files, toolkit: @toolkit || Toolkit.configured,
|
|
196
196
|
toolkit_origin: @toolkit ? "chosen" : "default", settings: @session_settings,
|
|
197
|
-
parent_answers: @parent_answers
|
|
197
|
+
parent_answers: @parent_answers, preloaded_memory_names: @requested_memories,
|
|
198
|
+
muted_memory_names: @muted_memory_names)
|
|
198
199
|
))
|
|
199
200
|
# --llm-context*: the session's own values, over a resumed one's.
|
|
200
201
|
# Saved at once on a resumed one: leaving before a turn keeps them.
|
|
@@ -254,11 +255,20 @@ module Samagotchi
|
|
|
254
255
|
end
|
|
255
256
|
ArchiveStore.user_input(session.id, state_dir: Session.default_state_dir)
|
|
256
257
|
sink = OneShotSink.new
|
|
258
|
+
# The turn's drawing, as a worker's Bridge writes it: this process is
|
|
259
|
+
# the session's owner (OwnerLock), the file's one writer meanwhile.
|
|
260
|
+
drawings = Bridge::TurnDrawings.new(path: File.join(Session.session_dir(session.id), Bridge::TurnDrawings::FILE),
|
|
261
|
+
messages: -> { session.messages })
|
|
262
|
+
accumulator = Bridge::TurnAccumulator.new(on_turn_end: drawings.method(:append))
|
|
263
|
+
events = lambda do |event|
|
|
264
|
+
sink.call(event)
|
|
265
|
+
accumulator.call(event)
|
|
266
|
+
end
|
|
257
267
|
save_before_first_turn(session)
|
|
258
268
|
attach_mentioned_context(session, prompt)
|
|
259
269
|
max_iterations = IterationLimit.for(no_interrupt: true)
|
|
260
270
|
begin
|
|
261
|
-
result = @engine.run_turn(session, prompt, on_event:
|
|
271
|
+
result = @engine.run_turn(session, prompt, on_event: events, max_iterations: max_iterations,
|
|
262
272
|
cancel_controller: nil, images: ImageInput.extract(prompt))
|
|
263
273
|
rescue LLM::ProviderError, ImageStore::Error => e
|
|
264
274
|
return one_shot_failed(session, e)
|
|
@@ -13,6 +13,10 @@ require_relative "../child_reports"
|
|
|
13
13
|
require_relative "../worker_link"
|
|
14
14
|
|
|
15
15
|
module Samagotchi
|
|
16
|
+
# Loaded on first use (a delegate call): keeps the bundle test kit's load budget
|
|
17
|
+
# (spec/samagotchi/testing/sandbox_spec.rb).
|
|
18
|
+
autoload :WorkerPresence, File.expand_path("../worker_presence", __dir__)
|
|
19
|
+
|
|
16
20
|
# Loaded on the first task (a leaf; the bundle test kit's load budget,
|
|
17
21
|
# spec/samagotchi/testing/sandbox_spec.rb).
|
|
18
22
|
autoload :CommandIntake, File.expand_path("../command_intake", __dir__)
|
|
@@ -109,7 +113,7 @@ module Samagotchi
|
|
|
109
113
|
# the boot limit), :timeout (+timeout+ ran out first) or :canceled
|
|
110
114
|
def self.await_worker(child_id, state_dir:, timeout:, cancelled: -> { false })
|
|
111
115
|
dir = Session.session_dir(child_id, state_dir: state_dir)
|
|
112
|
-
return :up if
|
|
116
|
+
return :up if WorkerPresence.read(child_id, state_dir: state_dir).owned?
|
|
113
117
|
|
|
114
118
|
limit = [WorkerLink::START_LIMIT, timeout.to_f].min
|
|
115
119
|
link = WorkerLink.await(child_id, session_dir: dir, timeout: limit, unlink: false,
|
|
@@ -175,10 +179,11 @@ module Samagotchi
|
|
|
175
179
|
|
|
176
180
|
# The parent's own parent_answers, as it is now (a snapshot): the
|
|
177
181
|
# child's answers are held at least as tight.
|
|
178
|
-
setup = SessionSetup.new(
|
|
182
|
+
setup = SessionSetup.new(preloaded_memory_names: CHILD_MEMORIES,
|
|
183
|
+
toolkit: toolkit.empty? ? nil : Toolkit.new(name: toolkit),
|
|
179
184
|
parent_answers: parent.parent_answers)
|
|
180
185
|
child = SessionManager.spawn_session(prompt: task, working_directory: folder,
|
|
181
|
-
model_name: child_model(model, parent),
|
|
186
|
+
model_name: child_model(model, parent),
|
|
182
187
|
parent_id: parent.id, delegate: true, setup: setup, state_dir: state_dir,
|
|
183
188
|
client_id: ClientId.delegate(parent.id))
|
|
184
189
|
DelegateWait.mark_started(parent.id, child, state_dir: state_dir)
|
|
@@ -372,7 +377,7 @@ module Samagotchi
|
|
|
372
377
|
when :accepted
|
|
373
378
|
DelegateWait.mark_turn(parent.id, id, follow_up_turn(id, delivered.enqueued_id, state_dir: state_dir), state_dir: state_dir)
|
|
374
379
|
id
|
|
375
|
-
when :refused then "Error: session #{id[0, 8]}
|
|
380
|
+
when :refused then "Error: session #{id[0, 8]} #{delivered.refusal_text}"
|
|
376
381
|
when :timeout then "Error: the worker of session #{id[0, 8]} did not answer in time, so the message was not sent"
|
|
377
382
|
else "Error: the message to session #{id[0, 8]} could not be written"
|
|
378
383
|
end
|
|
@@ -28,9 +28,11 @@ module Samagotchi
|
|
|
28
28
|
mode: mode.nil? || mode.to_s.strip.empty? ? Schedules::CHAIN : mode.to_s.strip,
|
|
29
29
|
keep_links: keep_links
|
|
30
30
|
)
|
|
31
|
-
"Scheduled #{entry.id}: #{entry.target_words} · #{entry.recurrence_words} · next #{entry.next_due}. " \
|
|
32
|
-
|
|
33
|
-
|
|
31
|
+
result = "Scheduled #{entry.id}: #{entry.target_words} · #{entry.recurrence_words} · next #{entry.next_due}. " \
|
|
32
|
+
"It fires outside this worker (chi schedule tick, run by cron or chi web's hub); " \
|
|
33
|
+
"chi schedule list and cancel_schedule show and stop it."
|
|
34
|
+
warning = schedules.tick_warning
|
|
35
|
+
warning ? "#{result}\n#{warning}" : result
|
|
34
36
|
rescue Schedules::Usage, Session::NotFound, Session::AmbiguousId => e
|
|
35
37
|
"Error: #{e.message}"
|
|
36
38
|
end
|
|
@@ -26,13 +26,19 @@ module Samagotchi
|
|
|
26
26
|
Session.load(id, state_dir: peers.state_dir)
|
|
27
27
|
return "Error: #{id[0, 8]} is this session; send_note is for other sessions" if id == peers.session_id
|
|
28
28
|
|
|
29
|
+
# A link continued into this one: the note would come back here.
|
|
30
|
+
head = SessionInbox::Head.resolve(:note, id, state_dir: peers.state_dir)
|
|
31
|
+
return "Error: #{id[0, 8]} was continued into this session; send_note is for other sessions" if head.id == peers.session_id
|
|
32
|
+
|
|
29
33
|
result = NoteDelivery.deliver(id, text: content, source: "session", from_session: peers.session_id,
|
|
30
|
-
from_cwd: peers.cwd, state_dir: peers.state_dir)
|
|
31
|
-
|
|
32
|
-
return "Error: session #{
|
|
34
|
+
from_cwd: peers.cwd, state_dir: peers.state_dir, resolved: head)
|
|
35
|
+
target = result.id[0, 8]
|
|
36
|
+
return "Error: session #{target}: #{result.refusal}" if result.scratch
|
|
37
|
+
return "Error: session #{target} is #{OwnerLock::TUI_BUSY}, which can't take notes" unless result.delivered?
|
|
33
38
|
|
|
34
39
|
where = result.status == :queued ? "its worker adds it before its next turn" : "it has no worker now, so it waits for its next start"
|
|
35
|
-
|
|
40
|
+
via = result.continued? ? " (#{id[0, 8]} was continued: #{target} is its chain's newest link)" : ""
|
|
41
|
+
"Queued a note for session #{target}#{via}: #{where}. It does not start a turn there."
|
|
36
42
|
rescue Session::NotFound => e
|
|
37
43
|
"Error: #{e.message} (list_sessions shows them)"
|
|
38
44
|
rescue ArgumentError => e # AmbiguousId, SessionInbox::NoteRejected, a file that won't load
|
|
@@ -15,6 +15,8 @@ module Samagotchi
|
|
|
15
15
|
# so any lock may be held around a call into it, and it is never held
|
|
16
16
|
# around anything else.
|
|
17
17
|
class TurnState
|
|
18
|
+
ENQUEUED_ID_HISTORY = 256
|
|
19
|
+
|
|
18
20
|
# @param clock [#call] monotonic seconds
|
|
19
21
|
def initialize(clock:)
|
|
20
22
|
@clock = clock
|
|
@@ -32,6 +34,80 @@ module Samagotchi
|
|
|
32
34
|
@last_context = nil
|
|
33
35
|
@steers = []
|
|
34
36
|
@carried_steers = []
|
|
37
|
+
@enqueued_ids = {}
|
|
38
|
+
@enqueued_order = []
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Reserve an id for a Bridge-enqueued input. False means this id was
|
|
42
|
+
# already accepted in this worker.
|
|
43
|
+
def reserve_enqueued_id(id)
|
|
44
|
+
id = id.to_s
|
|
45
|
+
return false if id.empty?
|
|
46
|
+
|
|
47
|
+
@lock.synchronize do
|
|
48
|
+
next false if @enqueued_ids.key?(id)
|
|
49
|
+
|
|
50
|
+
remember_enqueued_id(id, :queued)
|
|
51
|
+
true
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
# Claim an input file. A Bridge reservation permits its first file claim;
|
|
56
|
+
# a duplicate file with the same id is refused. Unreserved files are
|
|
57
|
+
# accepted once too.
|
|
58
|
+
def claim_enqueued_id(id)
|
|
59
|
+
id = id.to_s
|
|
60
|
+
return true if id.empty?
|
|
61
|
+
|
|
62
|
+
@lock.synchronize do
|
|
63
|
+
case @enqueued_ids[id]
|
|
64
|
+
when :queued
|
|
65
|
+
@enqueued_ids[id] = :running
|
|
66
|
+
true
|
|
67
|
+
when nil
|
|
68
|
+
remember_enqueued_id(id, :running)
|
|
69
|
+
true
|
|
70
|
+
else
|
|
71
|
+
false
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def finish_enqueued_id(id)
|
|
77
|
+
id = id.to_s
|
|
78
|
+
return if id.empty?
|
|
79
|
+
|
|
80
|
+
@lock.synchronize do
|
|
81
|
+
@enqueued_ids[id] = :ended if @enqueued_ids[id] == :running
|
|
82
|
+
prune_enqueued_ids
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
def forget_enqueued_id(id)
|
|
87
|
+
id = id.to_s
|
|
88
|
+
return if id.empty?
|
|
89
|
+
|
|
90
|
+
@lock.synchronize do
|
|
91
|
+
next unless @enqueued_ids[id] == :queued
|
|
92
|
+
|
|
93
|
+
@enqueued_ids.delete(id)
|
|
94
|
+
@enqueued_order.delete(id)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def drop_enqueued_id(id)
|
|
99
|
+
id = id.to_s
|
|
100
|
+
return if id.empty?
|
|
101
|
+
|
|
102
|
+
@lock.synchronize do
|
|
103
|
+
@enqueued_ids[id] = :ended if @enqueued_ids[id] == :queued
|
|
104
|
+
prune_enqueued_ids
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def enqueued_id?(id)
|
|
109
|
+
id = id.to_s
|
|
110
|
+
!id.empty? && @lock.synchronize { @enqueued_ids.key?(id) }
|
|
35
111
|
end
|
|
36
112
|
|
|
37
113
|
# ── Activity clock ──────────────────────────────────────────────────
|
|
@@ -179,11 +255,22 @@ module Samagotchi
|
|
|
179
255
|
@lock.synchronize { !@input_waiting_warned.tap { @input_waiting_warned = true } }
|
|
180
256
|
end
|
|
181
257
|
|
|
182
|
-
# Cancel the running turn, if any (
|
|
183
|
-
#
|
|
184
|
-
|
|
185
|
-
|
|
258
|
+
# Cancel the running turn, if any (the controller is taken, and the turn
|
|
259
|
+
# id compared, under the lock; it is cancelled outside it).
|
|
260
|
+
# @param turn_id [String, nil] the turn the caller means: another turn
|
|
261
|
+
# running (a Stop that arrived as its turn ended) is left alone; nil
|
|
262
|
+
# cancels whichever runs
|
|
263
|
+
# @return [Boolean, Symbol] whether a cancellation was triggered, or
|
|
264
|
+
# :turn_mismatch when +turn_id+ is not the running turn's
|
|
265
|
+
def cancel!(reason, turn_id: nil)
|
|
266
|
+
ctrl = @lock.synchronize do
|
|
267
|
+
next nil unless @controller
|
|
268
|
+
next :mismatch if turn_id && @context&.id != turn_id
|
|
269
|
+
|
|
270
|
+
@controller
|
|
271
|
+
end
|
|
186
272
|
return false unless ctrl
|
|
273
|
+
return :turn_mismatch if ctrl == :mismatch
|
|
187
274
|
|
|
188
275
|
ctrl.cancel!(reason)
|
|
189
276
|
end
|
|
@@ -238,5 +325,22 @@ module Samagotchi
|
|
|
238
325
|
def take_steers
|
|
239
326
|
@lock.synchronize { @steers.tap { @steers = [] } }
|
|
240
327
|
end
|
|
328
|
+
|
|
329
|
+
private
|
|
330
|
+
|
|
331
|
+
def remember_enqueued_id(id, status)
|
|
332
|
+
@enqueued_ids[id] = status
|
|
333
|
+
@enqueued_order << id
|
|
334
|
+
prune_enqueued_ids
|
|
335
|
+
end
|
|
336
|
+
|
|
337
|
+
def prune_enqueued_ids
|
|
338
|
+
while @enqueued_ids.size > ENQUEUED_ID_HISTORY
|
|
339
|
+
index = @enqueued_order.index { |id| @enqueued_ids[id] == :ended }
|
|
340
|
+
break unless index
|
|
341
|
+
|
|
342
|
+
@enqueued_ids.delete(@enqueued_order.delete_at(index))
|
|
343
|
+
end
|
|
344
|
+
end
|
|
241
345
|
end
|
|
242
346
|
end
|
|
@@ -310,7 +310,9 @@ module Samagotchi
|
|
|
310
310
|
when :skipped then skipped(row, result.error)
|
|
311
311
|
else failed(row, result.error)
|
|
312
312
|
end
|
|
313
|
-
system.clear_merge_notes(earlier, files: result.merged)
|
|
313
|
+
if read_by_someone? && (error = system.clear_merge_notes(earlier, files: result.merged)).is_a?(String)
|
|
314
|
+
row.status = [row.status, error].compact.join("; ")
|
|
315
|
+
end
|
|
314
316
|
row
|
|
315
317
|
end
|
|
316
318
|
|
|
@@ -363,7 +365,10 @@ module Samagotchi
|
|
|
363
365
|
else failed(row, [update.note, *notes].compact.join("; "))
|
|
364
366
|
end
|
|
365
367
|
# A failed row keeps them: the run didn't finish, and the next says what stands.
|
|
366
|
-
|
|
368
|
+
if read_by_someone? && update.status != :failed
|
|
369
|
+
error = MemoryBundle::Provenance.new(name: update.name).clear_merge_notes(earlier, files: row.merged)
|
|
370
|
+
row.status = [row.status, error].compact.join("; ") if error.is_a?(String)
|
|
371
|
+
end
|
|
367
372
|
row
|
|
368
373
|
end
|
|
369
374
|
|
|
@@ -6,6 +6,7 @@ require "time"
|
|
|
6
6
|
require_relative "atomic_file"
|
|
7
7
|
require_relative "log"
|
|
8
8
|
require_relative "session"
|
|
9
|
+
require_relative "session_inbox"
|
|
9
10
|
require_relative "session_metrics"
|
|
10
11
|
require_relative "session_row"
|
|
11
12
|
require_relative "worker_link"
|
|
@@ -86,8 +87,10 @@ module Samagotchi
|
|
|
86
87
|
refuse_busy!(parent.id, of, author, state_dir)
|
|
87
88
|
|
|
88
89
|
child = spawn.call(prompt: prompt_for(anchor.quote), working_directory: parent.working_directory,
|
|
89
|
-
model_name: parent.model_name,
|
|
90
|
-
|
|
90
|
+
model_name: parent.model_name,
|
|
91
|
+
setup: SessionSetup.new(preloaded_memory_names: [Session::DELEGATE_MEMORY]),
|
|
92
|
+
parent_id: parent.id, delegate: true, verify_fresh: origin, state_dir: state_dir,
|
|
93
|
+
client_id: ClientId.delegate(parent.id))
|
|
91
94
|
append_ledger(dir, { "child_id" => child.id, "of" => of, "author" => author, "at" => Time.now.utc.iso8601(3) })
|
|
92
95
|
child
|
|
93
96
|
end
|
|
@@ -160,28 +163,43 @@ module Samagotchi
|
|
|
160
163
|
|
|
161
164
|
# The child's new verdicts (+records+, Engine's verdict records) into
|
|
162
165
|
# its parent's INBOX, marked fresh: true with the child's id and the
|
|
163
|
-
# verdict they check again (of). Best effort.
|
|
166
|
+
# verdict they check again (of). Best effort. A session inbox delivery
|
|
167
|
+
# (SessionInbox.deliver :fresh_verdict): pinned to the parent the child
|
|
168
|
+
# names (its original verdict's card is there, a continued one's too),
|
|
169
|
+
# never a worker started for it; a parent that is gone gets nothing.
|
|
164
170
|
# @param child [Session] a Verify fresh child
|
|
165
171
|
# @return [Integer] how many were written
|
|
166
172
|
def forward(child, records, state_dir: Session.default_state_dir)
|
|
167
173
|
fresh = child&.verify_fresh
|
|
168
|
-
return 0 unless fresh&.of && child.parent_id
|
|
174
|
+
return 0 unless fresh&.of && child.parent_id
|
|
169
175
|
|
|
170
|
-
|
|
176
|
+
written = 0
|
|
177
|
+
SessionInbox.deliver(:fresh_verdict, child.parent_id, state_dir: state_dir, wake: false) do |at|
|
|
178
|
+
written = write_verdicts(Session.session_dir(at, state_dir: state_dir), child, fresh.of, records)
|
|
179
|
+
false
|
|
180
|
+
end
|
|
181
|
+
written
|
|
182
|
+
rescue StandardError => e
|
|
183
|
+
Log.warn(:worker, "fresh_forward_failed", parent: child&.parent_id.to_s[0, 8], error: e.class.name, msg: e.message)
|
|
184
|
+
0
|
|
185
|
+
end
|
|
186
|
+
|
|
187
|
+
# +records+ into +session_dir+'s INBOX, one file per check turn.
|
|
188
|
+
# @return [Integer] how many were written
|
|
189
|
+
def write_verdicts(session_dir, child, of, records)
|
|
190
|
+
dir = File.join(session_dir, INBOX)
|
|
171
191
|
FileUtils.mkdir_p(dir)
|
|
172
192
|
Array(records).count do |record|
|
|
173
193
|
check = field(record, :check_turn_id).to_s
|
|
174
194
|
next false unless Session.valid_id?(check)
|
|
175
195
|
|
|
176
196
|
out = record.to_h { |key, value| [key.to_s, value.is_a?(Hash) ? value.transform_keys(&:to_s) : value] }
|
|
177
|
-
out.merge!("fresh" => true, "of" =>
|
|
197
|
+
out.merge!("fresh" => true, "of" => of, "child_id" => child.id)
|
|
178
198
|
AtomicFile.write(File.join(dir, "#{check}.json"), JSON.generate(out))
|
|
179
199
|
true
|
|
180
200
|
end
|
|
181
|
-
rescue StandardError => e
|
|
182
|
-
Log.warn(:worker, "fresh_forward_failed", parent: child&.parent_id.to_s[0, 8], error: e.class.name, msg: e.message)
|
|
183
|
-
0
|
|
184
201
|
end
|
|
202
|
+
private_class_method :write_verdicts
|
|
185
203
|
|
|
186
204
|
# The verdicts waiting in +session_dir+'s INBOX: [[record, file]],
|
|
187
205
|
# oldest first.
|
data/lib/samagotchi/version.rb
CHANGED
data/lib/samagotchi/web/app.rb
CHANGED
|
@@ -1896,7 +1896,7 @@ module Samagotchi
|
|
|
1896
1896
|
def session_to_json(s, status: s.status, owner: nil)
|
|
1897
1897
|
SessionSummary.build(s, status: status, owner: owner,
|
|
1898
1898
|
session_dir: @session_class.session_dir(s.id, state_dir: default_state_dir),
|
|
1899
|
-
registry: host_registry)
|
|
1899
|
+
registry: host_registry, compat_version: @hub&.installed || VERSION)
|
|
1900
1900
|
end
|
|
1901
1901
|
|
|
1902
1902
|
def parse_json(str)
|
|
@@ -231,11 +231,15 @@ module Samagotchi
|
|
|
231
231
|
# draws (the opening, steps, steers, merged lines, the empty answer):
|
|
232
232
|
# a context note, a `!cmd`'s record and anything else keep today's
|
|
233
233
|
# place. +origins+: each item's message index.
|
|
234
|
+
# One walk: +drawings+ are sorted apart by range, +origins+ never go
|
|
235
|
+
# back.
|
|
234
236
|
def stamp_drawn(items, origins, drawings, strip:)
|
|
235
237
|
return items if drawings.empty?
|
|
236
238
|
|
|
239
|
+
at = 0
|
|
237
240
|
items.each_with_index.map do |item, i|
|
|
238
|
-
|
|
241
|
+
at += 1 while at < drawings.size && drawings[at].to <= origins[i]
|
|
242
|
+
line = drawings[at] if at < drawings.size && drawings[at].range.cover?(origins[i])
|
|
239
243
|
next item unless line && drawn_kind?(item)
|
|
240
244
|
|
|
241
245
|
stamped = item.merge(drawn: line.turn_id)
|