samagotchi 0.177.0 → 0.177.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +18 -1
- data/docs/architecture.md +5 -4
- data/docs/cli.md +4 -1
- data/docs/internals/bundles.md +4 -3
- data/docs/internals/participants.md +8 -5
- data/docs/internals/session-lifecycle.md +167 -0
- data/docs/internals/web-frontend.md +7 -3
- data/docs/internals/working-set.md +2 -1
- data/docs/plugins.md +1 -1
- data/docs/releasing.md +3 -2
- data/docs/sessions.md +7 -5
- data/docs/testing.md +4 -2
- data/lib/samagotchi/bundle_command/check.rb +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/compat.rb +1 -1
- data/lib/samagotchi/engine/session.rb +7 -0
- data/lib/samagotchi/memory_bundle/shipped_update.rb +1 -1
- data/lib/samagotchi/note_delivery.rb +9 -4
- data/lib/samagotchi/plugin/sessions.rb +2 -1
- data/lib/samagotchi/revise_anchor.rb +2 -0
- data/lib/samagotchi/schedules.rb +3 -1
- data/lib/samagotchi/session_chain/delete.rb +28 -5
- data/lib/samagotchi/session_chain/delete_preview.rb +2 -2
- data/lib/samagotchi/session_inbox.rb +29 -0
- data/lib/samagotchi/session_inbox_delivery.rb +13 -5
- data/lib/samagotchi/session_lifecycle/allowed.rb +66 -0
- data/lib/samagotchi/session_lifecycle/canonical.rb +49 -0
- data/lib/samagotchi/session_lifecycle/reading.rb +65 -0
- data/lib/samagotchi/session_lifecycle.rb +59 -0
- data/lib/samagotchi/session_manager/delivery.rb +29 -6
- data/lib/samagotchi/session_manager/lifecycle.rb +22 -14
- data/lib/samagotchi/session_manager/removal.rb +16 -8
- data/lib/samagotchi/session_manager/spawn.rb +2 -2
- data/lib/samagotchi/sessions_command.rb +1 -1
- data/lib/samagotchi/system_prompt.rb +22 -2
- data/lib/samagotchi/tool_names.rb +1 -1
- data/lib/samagotchi/tools/memory.rb +29 -0
- data/lib/samagotchi/turn_anchors.rb +5 -0
- data/lib/samagotchi/verify_anchor.rb +2 -0
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/participant_show.rb +3 -4
- data/lib/samagotchi/web/participant_view.rb +4 -6
- data/lib/samagotchi/web/public/data.js +6 -2
- data/lib/samagotchi/web/public/delete_sheet.js +42 -5
- data/lib/samagotchi/web/public/ws_sections.js +1 -0
- data/lib/samagotchi/web/session_control.rb +4 -3
- data/lib/samagotchi/web/session_summary.rb +7 -0
- data/lib/samagotchi/worker_boot.rb +9 -6
- data/lib/samagotchi/worker_presence.rb +4 -0
- data/lib/samagotchi/working_set_command.rb +2 -2
- data/lib/samagotchi/working_set_steer.rb +3 -10
- metadata +6 -1
|
@@ -10,6 +10,7 @@ require_relative "../process_group"
|
|
|
10
10
|
require_relative "../reminder_carry"
|
|
11
11
|
require_relative "../session"
|
|
12
12
|
require_relative "../session_inbox"
|
|
13
|
+
require_relative "../session_lifecycle"
|
|
13
14
|
require_relative "../task_files"
|
|
14
15
|
require_relative "../tools/task_record"
|
|
15
16
|
require_relative "../task_transcript"
|
|
@@ -23,7 +24,9 @@ module Samagotchi
|
|
|
23
24
|
# WorkerIdleExit#hold_for_restart hold (:turn_running, :reminders, ...),
|
|
24
25
|
# :fallback_held (disk-visible work blocks an unresponsive-worker restart),
|
|
25
26
|
# :stopped (its worker, +pid+, is SIGSTOPped: neither the ask nor a TERM
|
|
26
|
-
# reaches it until it is continued),
|
|
27
|
+
# reaches it until it is continued), :stop_marked (the session is stopped,
|
|
28
|
+
# or being stopped: `chi sessions stop`, no successor would come), or
|
|
29
|
+
# :failed (the worker answered something else or didn't release).
|
|
27
30
|
class RestartRefused < StandardError
|
|
28
31
|
attr_reader :session_id, :reason, :version, :holds, :pid
|
|
29
32
|
|
|
@@ -58,6 +61,8 @@ module Samagotchi
|
|
|
58
61
|
where = via.remote? ? " on #{via.remote}" : ""
|
|
59
62
|
"session #{session_id}'s worker#{" (pid #{pid})" if pid} is stopped (SIGSTOP or Ctrl-Z) and can't take " \
|
|
60
63
|
"a restart: continue it#{" with `kill -CONT #{pid}`" if pid}#{where}, then restart again"
|
|
64
|
+
when :stop_marked
|
|
65
|
+
"session #{session_id} can't restart: #{DeliverResult::STOPPED["detail"]} (on the newest chi)"
|
|
61
66
|
when :not_running
|
|
62
67
|
"session #{session_id} has no running worker; the next prompt starts one on the newest chi"
|
|
63
68
|
when :unsupported
|
|
@@ -107,24 +112,27 @@ module Samagotchi
|
|
|
107
112
|
def restart_session(session_id, state_dir: nil, wait: RESTART_WAIT, client_id: ClientId::CLI_RESTART)
|
|
108
113
|
sd = state_dir || Session.default_state_dir
|
|
109
114
|
Session.load(session_id, state_dir: sd) # ArgumentError for none
|
|
110
|
-
|
|
115
|
+
# The lifecycle precheck is ALLOWED[:restart] for the state read now
|
|
116
|
+
# (a stop marked refuses before the worker is asked); the op-local
|
|
117
|
+
# checks (SIGSTOP, features, the Bridge's reply, the fallback) are here.
|
|
118
|
+
reading = SessionLifecycle.read(session_id, state_dir: sd, owner: session_owner(session_id, state_dir: sd))
|
|
119
|
+
cell = SessionLifecycle.allowed(:restart, reading.state)
|
|
120
|
+
raise OwnedByTUI, session_id if cell == :owned_by_tui
|
|
121
|
+
raise Session::NotFound, session_id if cell == :gone
|
|
122
|
+
|
|
123
|
+
owner = reading.presence.owner
|
|
111
124
|
# A stopped worker answers neither the ask (READ_TIMEOUT) nor the
|
|
112
125
|
# fallback's TERM (FALLBACK_STOP_WAIT): say so now, not after ~70 s.
|
|
113
126
|
raise RestartRefused.new(session_id, :stopped, pid: owner.pid) if owner&.worker? && ProcessGroup.stopped?(owner.pid)
|
|
127
|
+
raise RestartRefused.new(session_id, cell) if SessionLifecycle.verdict(:restart, cell) == :refused
|
|
114
128
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
return restart_fallback(session_id, owner: owner, sidecar: link.sidecar, state_dir: sd, wait: wait,
|
|
129
|
+
link = reading.presence.link
|
|
130
|
+
if cell == :fallback
|
|
131
|
+
skew = link.is_a?(WorkerLink::Skew)
|
|
132
|
+
return restart_fallback(session_id, owner: owner, sidecar: (link.sidecar if skew), state_dir: sd, wait: wait,
|
|
133
|
+
why: skew ? :skew : :starting_stuck)
|
|
119
134
|
end
|
|
120
135
|
|
|
121
|
-
if link.is_a?(WorkerLink::Starting)
|
|
122
|
-
return restart_fallback(session_id, owner: owner, sidecar: nil, state_dir: sd, wait: wait, why: :starting_stuck) if link.stuck
|
|
123
|
-
|
|
124
|
-
raise RestartRefused.new(session_id, :starting)
|
|
125
|
-
end
|
|
126
|
-
raise RestartRefused.new(session_id, :not_running) unless link.live?
|
|
127
|
-
|
|
128
136
|
sidecar = link.sidecar
|
|
129
137
|
unless sidecar.features.include?("restart")
|
|
130
138
|
raise RestartRefused.new(session_id, :unsupported, version: sidecar.version)
|
|
@@ -143,7 +151,7 @@ module Samagotchi
|
|
|
143
151
|
raise RestartRefused.new(session_id, :failed) unless reply.status == 200
|
|
144
152
|
|
|
145
153
|
Log.info(:worker, "restart", sid: session_id, from: sidecar.version, by: client_id)
|
|
146
|
-
fresh = WorkerLink.await(session_id, session_dir:
|
|
154
|
+
fresh = WorkerLink.await(session_id, session_dir: Session.session_dir(session_id, state_dir: sd), timeout: wait, unlink: false, interval: 0.1,
|
|
147
155
|
until: ->(live) { live.epoch != sidecar.epoch || live.started_at != sidecar.started_at })
|
|
148
156
|
# Timed out: the last outcome, the old worker's Live too, is no successor.
|
|
149
157
|
changed = fresh.live? && (fresh.epoch != sidecar.epoch || fresh.started_at != sidecar.started_at)
|
|
@@ -13,7 +13,8 @@ module Samagotchi
|
|
|
13
13
|
# the previous one a delete would bring back), :delegate_unmovable (a
|
|
14
14
|
# delegate the deleted link's continue moved can't go back: +ids+,
|
|
15
15
|
# +detail+ says why), :chain_scheduled (a schedule is on the chain a
|
|
16
|
-
# chain delete would take: +schedule_id+)
|
|
16
|
+
# chain delete would take: +schedule_id+), :plan_changed (the plan no
|
|
17
|
+
# longer matches the digest the caller confirmed).
|
|
17
18
|
class DeleteRefused < StandardError
|
|
18
19
|
attr_reader :session_id, :reason, :schedule_id, :ids
|
|
19
20
|
|
|
@@ -40,6 +41,7 @@ module Samagotchi
|
|
|
40
41
|
"#{@ids.map { |id| id[0, 8] }.join(", ")}, moved here by the continue, can't go back to the previous link" \
|
|
41
42
|
"#{": #{detail}" if detail}"
|
|
42
43
|
when :chain_scheduled then "an enabled schedule runs on this chain#{cancel}"
|
|
44
|
+
when :plan_changed then "what this delete would do changed since it was shown; review the new plan and try again"
|
|
43
45
|
else "session #{@session_id} can't be deleted now (#{@reason})"
|
|
44
46
|
end
|
|
45
47
|
end
|
|
@@ -193,8 +195,12 @@ module Samagotchi
|
|
|
193
195
|
# (:continuing) while a continue of the session is in flight
|
|
194
196
|
# @raise [DeleteRefused] also :scheduled for a schedule's anchor or
|
|
195
197
|
# head, and with the keywords :continuing and :delegate_unmovable
|
|
198
|
+
# @param still [#call, nil] the caller's own check, read again under
|
|
199
|
+
# the deleting lock (#remove_files; DeleteRefused :changed when it no
|
|
200
|
+
# longer holds): an empty session's discard. Not with the chain keywords.
|
|
196
201
|
def delete_session(id_or_prefix, state_dir: nil, stop: false, wait: STOP_WAIT, purge_runs: true,
|
|
197
|
-
allow_scheduled: false, revive_previous: false, refuse_continuing: false
|
|
202
|
+
allow_scheduled: false, revive_previous: false, refuse_continuing: false, expect: nil,
|
|
203
|
+
still: nil)
|
|
198
204
|
sd = state_dir || Session.default_state_dir
|
|
199
205
|
given = id_or_prefix.to_s
|
|
200
206
|
# Only a plain id: anything else could name a path outside the state dir.
|
|
@@ -209,10 +215,11 @@ module Samagotchi
|
|
|
209
215
|
if revive_previous || refuse_continuing
|
|
210
216
|
require_relative "../session_chain/delete"
|
|
211
217
|
return SessionChain::Delete.link(id, state_dir: sd, stop: stop, wait: wait, revive_previous: revive_previous,
|
|
212
|
-
refuse_continuing: refuse_continuing)
|
|
218
|
+
refuse_continuing: refuse_continuing, expect: expect)
|
|
213
219
|
end
|
|
214
220
|
|
|
215
|
-
delete_resolved(id, state_dir: sd, stop: stop, wait: wait, purge_runs: purge_runs, allow_scheduled: allow_scheduled
|
|
221
|
+
delete_resolved(id, state_dir: sd, stop: stop, wait: wait, purge_runs: purge_runs, allow_scheduled: allow_scheduled,
|
|
222
|
+
still: still)
|
|
216
223
|
end
|
|
217
224
|
|
|
218
225
|
# Delete every link of +id+'s chain with their delegates
|
|
@@ -223,7 +230,7 @@ module Samagotchi
|
|
|
223
230
|
# @return [Hash] SessionChain::Delete.chain's
|
|
224
231
|
# @raise [ArgumentError] unknown id (Session::AmbiguousId for a prefix of several)
|
|
225
232
|
# @raise [DeleteRefused, OwnedByTUI] nothing was deleted
|
|
226
|
-
def delete_chain(id_or_prefix, state_dir: nil, stop: false, wait: STOP_WAIT)
|
|
233
|
+
def delete_chain(id_or_prefix, state_dir: nil, stop: false, wait: STOP_WAIT, expect: nil)
|
|
227
234
|
sd = state_dir || Session.default_state_dir
|
|
228
235
|
given = id_or_prefix.to_s
|
|
229
236
|
raise Session::NotFound, given unless Session.valid_id?(given)
|
|
@@ -232,21 +239,22 @@ module Samagotchi
|
|
|
232
239
|
raise Session::NotFound, given unless Session.exist?(id, state_dir: sd)
|
|
233
240
|
|
|
234
241
|
require_relative "../session_chain/delete"
|
|
235
|
-
SessionChain::Delete.chain(id, state_dir: sd, stop: stop, wait: wait)
|
|
242
|
+
SessionChain::Delete.chain(id, state_dir: sd, stop: stop, wait: wait, expect: expect)
|
|
236
243
|
end
|
|
237
244
|
|
|
238
245
|
# #delete_session's checks and removal for a full id that exists:
|
|
239
246
|
# the schedule refusal, the one-turn run, the worker (stopped with
|
|
240
247
|
# +stop+), then #remove_files. SessionChain::Delete's last step.
|
|
241
248
|
# @return [Hash] {id:, removed:, stopped:}
|
|
242
|
-
def delete_resolved(id, state_dir:, stop: false, wait: STOP_WAIT, purge_runs: true, allow_scheduled: false
|
|
249
|
+
def delete_resolved(id, state_dir:, stop: false, wait: STOP_WAIT, purge_runs: true, allow_scheduled: false,
|
|
250
|
+
still: nil)
|
|
243
251
|
sd = state_dir
|
|
244
252
|
path = Session.session_file(id, state_dir: sd)
|
|
245
253
|
dir = Session.session_dir(id, state_dir: sd)
|
|
246
254
|
refuse_scheduled!(id, sd) unless allow_scheduled
|
|
247
255
|
|
|
248
256
|
held = release_for_delete!(id, state_dir: sd, stop: stop, wait: wait)
|
|
249
|
-
removed = remove_files(id, path, dir, state_dir: sd, purge_runs: purge_runs)
|
|
257
|
+
removed = remove_files(id, path, dir, state_dir: sd, purge_runs: purge_runs, still: still)
|
|
250
258
|
{ id: id, removed: removed, stopped: held }
|
|
251
259
|
end
|
|
252
260
|
|
|
@@ -195,8 +195,8 @@ module Samagotchi
|
|
|
195
195
|
# landing between the check and the resume is a small window, left as
|
|
196
196
|
# is. An archived parent with a later link wakes that link (one hop:
|
|
197
197
|
# ChildReports reads its predecessor's rings, no further).
|
|
198
|
-
# @return [Symbol] :woken, or why not (:gone, :
|
|
199
|
-
# :scratch, :owned, :starting)
|
|
198
|
+
# @return [Symbol] :woken, or why not (:gone, :deleting: a deleter
|
|
199
|
+
# holds its lock, :stopped, :archived, :scratch, :owned, :starting)
|
|
200
200
|
def wake_for_report(session_id, state_dir: nil)
|
|
201
201
|
sd = state_dir || Session.default_state_dir
|
|
202
202
|
outcome = SessionInbox.wake(:ring, session_id, state_dir: sd, owner: ->(id) { session_owner(id, state_dir: sd) }).wake
|
|
@@ -325,7 +325,7 @@ module Samagotchi
|
|
|
325
325
|
next false
|
|
326
326
|
rescue Samagotchi::SessionManager::RestartRefused => e
|
|
327
327
|
@stdout.flush
|
|
328
|
-
@stderr.puts %i[not_running unsupported fallback_held stopped failed].include?(e.reason) ? e.message : "session #{id}: #{e.message}"
|
|
328
|
+
@stderr.puts %i[not_running unsupported fallback_held stopped stop_marked failed].include?(e.reason) ? e.message : "session #{id}: #{e.message}"
|
|
329
329
|
next false
|
|
330
330
|
rescue ArgumentError => e
|
|
331
331
|
@stdout.flush
|
|
@@ -103,7 +103,7 @@ module Samagotchi
|
|
|
103
103
|
# prompt_parts: the same as sizes, one PromptPart per memory, file and
|
|
104
104
|
# plugin section, the tools part first (#prompt_parts).
|
|
105
105
|
Built = Data.define(:prompt, :memory_index, :notes, :files, :sections, :stable_length, :surface, :memory_names, :parts,
|
|
106
|
-
:prompt_parts)
|
|
106
|
+
:prompt_parts, :prompt_memories)
|
|
107
107
|
|
|
108
108
|
# One part of the prompt: its kind (KINDS), what it was made from (the
|
|
109
109
|
# memory names of identity and preloads, the files of agents and
|
|
@@ -137,6 +137,7 @@ module Samagotchi
|
|
|
137
137
|
@memory_index = built.memory_index
|
|
138
138
|
@last = built
|
|
139
139
|
@last_prompt_memory_names = built.memory_names
|
|
140
|
+
@last_prompt_memories = built.prompt_memories
|
|
140
141
|
built.prompt
|
|
141
142
|
end
|
|
142
143
|
|
|
@@ -167,6 +168,11 @@ module Samagotchi
|
|
|
167
168
|
# @return [Array<String>]
|
|
168
169
|
def prompt_memory_names = @last_prompt_memory_names || []
|
|
169
170
|
|
|
171
|
+
# The identity and preloaded memories in the last built prompt, with the
|
|
172
|
+
# scope each memory answered from; retained across reset! like the names.
|
|
173
|
+
# @return [Array<Array(String, String, nil)>]
|
|
174
|
+
def prompt_memories = @last_prompt_memories || []
|
|
175
|
+
|
|
170
176
|
# The files whose text the prompt #build returned last holds, as
|
|
171
177
|
# absolute paths in prompt order: the AGENTS.md chain, then the
|
|
172
178
|
# appended files (append_system_prompt:, --append-system-prompt). [] before a
|
|
@@ -240,10 +246,24 @@ module Samagotchi
|
|
|
240
246
|
prompt_parts = Degrade.call(:prompt, "prompt_parts_failed", fallback: []) do
|
|
241
247
|
prompt_parts_for(@section_parts, chat: chat, surface: surface)
|
|
242
248
|
end
|
|
249
|
+
prompt_memories = prompt_memories_for(@section_parts)
|
|
243
250
|
Built.new(prompt: prompt, memory_index: @measured_index, notes: @section_notes || [],
|
|
244
251
|
files: @section_files.freeze, sections: @section_plugins.freeze, stable_length: @measured_stable_length,
|
|
245
252
|
surface: surface, memory_names: [@section_identity_memory_name, *@section_activated_memory_names].compact.uniq.freeze,
|
|
246
|
-
parts: @section_parts.freeze, prompt_parts: prompt_parts.freeze)
|
|
253
|
+
parts: @section_parts.freeze, prompt_parts: prompt_parts.freeze, prompt_memories: prompt_memories)
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
def prompt_memories_for(sections)
|
|
257
|
+
sections.flat_map do |section|
|
|
258
|
+
case section.kind
|
|
259
|
+
when :identity
|
|
260
|
+
section.ref ? [[section.ref, "system"]] : []
|
|
261
|
+
when :preloads
|
|
262
|
+
Array(section_items(section)).map { |_, name, _, scope| [name, scope] }
|
|
263
|
+
else
|
|
264
|
+
[]
|
|
265
|
+
end
|
|
266
|
+
end.map(&:freeze).uniq.freeze
|
|
247
267
|
end
|
|
248
268
|
|
|
249
269
|
# ── The parts' sizes ───────────────────────────────────────────────────────
|
|
@@ -4,7 +4,7 @@ module Samagotchi
|
|
|
4
4
|
# chi's built-in tools' names, and the fixed texts some of them write into
|
|
5
5
|
# their results, apart from the tool classes: the code that only names a
|
|
6
6
|
# tool or matches a result to draw a row (ToolActivity, ToolView, FileRef,
|
|
7
|
-
#
|
|
7
|
+
# Objectives::Backing) reads them here and loads no tool class (Tools::Execute
|
|
8
8
|
# alone brings TaskRuntime, Shell, Leftovers, the process group…).
|
|
9
9
|
#
|
|
10
10
|
# Each tool class keeps its own constants, read from here
|
|
@@ -76,6 +76,35 @@ module Samagotchi
|
|
|
76
76
|
nil
|
|
77
77
|
end
|
|
78
78
|
|
|
79
|
+
# Whether +target+ names a memory held in the prompt. A bare target
|
|
80
|
+
# matches by name; an explicitly scoped target also matches an
|
|
81
|
+
# unscoped prompt entry by name for older prompt data.
|
|
82
|
+
def self.prompt_memory?(target, memories)
|
|
83
|
+
target_scope, target_name = prompt_memory_target(target)
|
|
84
|
+
return false if target_name.empty?
|
|
85
|
+
|
|
86
|
+
Array(memories).any? do |memory|
|
|
87
|
+
prompt_name, prompt_scope = memory.is_a?(Array) ? memory : [memory, nil]
|
|
88
|
+
prompt_name = prompt_memory_name(prompt_name)
|
|
89
|
+
prompt_scope = normalize_scope(prompt_scope)
|
|
90
|
+
prompt_name == target_name && (!target_scope || !prompt_scope || target_scope == prompt_scope)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def self.prompt_memory_target(value)
|
|
95
|
+
name = value.to_s.strip
|
|
96
|
+
scope, name = name.split("/", 2) if name.include?("/")
|
|
97
|
+
[normalize_scope(scope), scope && VALID_SCOPES.include?(scope) ? name : value.to_s.strip]
|
|
98
|
+
end
|
|
99
|
+
private_class_method :prompt_memory_target
|
|
100
|
+
|
|
101
|
+
def self.prompt_memory_name(value)
|
|
102
|
+
name = value.to_s.strip
|
|
103
|
+
scope, suffix = name.split("/", 2) if name.include?("/")
|
|
104
|
+
scope && VALID_SCOPES.include?(scope) ? suffix : name
|
|
105
|
+
end
|
|
106
|
+
private_class_method :prompt_memory_name
|
|
107
|
+
|
|
79
108
|
def self.overlay_prefix(key) = "#{SEPARATOR}Model-specific guidance (#{key}):\n"
|
|
80
109
|
|
|
81
110
|
def self.append_overlay(body, overlay_text, key) = "#{body}#{overlay_prefix(key)}#{overlay_text}"
|
|
@@ -40,6 +40,11 @@ module Samagotchi
|
|
|
40
40
|
# A :prompt_restored event's fields.
|
|
41
41
|
def to_h = { verify: verify&.to_h, revises: revises&.to_h }.compact
|
|
42
42
|
|
|
43
|
+
# What a participant sees of each anchor.
|
|
44
|
+
def participant_view
|
|
45
|
+
self.class.members.to_h { |member| [member.to_s, public_send(member)&.participant_view] }.compact
|
|
46
|
+
end
|
|
47
|
+
|
|
43
48
|
# Engine#run_turn's keywords.
|
|
44
49
|
def kwargs = { verify:, revises: }.compact
|
|
45
50
|
end
|
data/lib/samagotchi/version.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "json"
|
|
4
4
|
require_relative "participant_view"
|
|
5
5
|
require_relative "viewer"
|
|
6
|
+
require_relative "session_summary"
|
|
6
7
|
require_relative "../bridge/turn_accumulator"
|
|
7
8
|
require_relative "../worker_link"
|
|
8
9
|
require_relative "../verify_anchor"
|
|
@@ -89,8 +90,7 @@ module Samagotchi
|
|
|
89
90
|
# delivery line, the stale-worker badge), its verdicts (filtered) and whether Verify fresh can run.
|
|
90
91
|
def participant_session_json(reads, features, viewer)
|
|
91
92
|
session = reads.session
|
|
92
|
-
status =
|
|
93
|
-
status = Session::STATUS_IDLE if status == Session::STATUS_RUNNING && reads.turn_snapshot && reads.current_turn.nil?
|
|
93
|
+
status = SessionSummary.shown_status(session, reads.state, reads.turn_snapshot, owner: session_owner(session.id))
|
|
94
94
|
dir = Session.session_dir(reads.id, state_dir: default_state_dir)
|
|
95
95
|
{ id: session.id, title: participant_title(session), status: status,
|
|
96
96
|
bridge_up: !reads.turn_snapshot.nil?, worker_features: Array(features),
|
|
@@ -197,8 +197,7 @@ module Samagotchi
|
|
|
197
197
|
shown = messages_for_display(raw, parts: true, cwd: session.working_directory, markdown: false)
|
|
198
198
|
messages = ParticipantView.messages(shown, viewer)
|
|
199
199
|
answer = participant_answer(messages, turn_id.empty? ? nil : turn_id)
|
|
200
|
-
status =
|
|
201
|
-
status = Session::STATUS_IDLE if status == Session::STATUS_RUNNING && reads.turn_snapshot && reads.current_turn.nil?
|
|
200
|
+
status = SessionSummary.shown_status(session, reads.state, reads.turn_snapshot, owner: session_owner(session.id))
|
|
202
201
|
json_response(200, {
|
|
203
202
|
tail: true,
|
|
204
203
|
session: { id: id, status: status, used_memory_names: [], prompt_notes: [], prompt_parts: [] },
|
|
@@ -4,8 +4,7 @@ require_relative "../output_formatter"
|
|
|
4
4
|
require_relative "../model_markup"
|
|
5
5
|
require_relative "../steer"
|
|
6
6
|
require_relative "../turn_origin"
|
|
7
|
-
require_relative "../
|
|
8
|
-
require_relative "../verify_anchor"
|
|
7
|
+
require_relative "../turn_anchors"
|
|
9
8
|
require_relative "../redact"
|
|
10
9
|
require_relative "../file_ref"
|
|
11
10
|
require_relative "../image_store"
|
|
@@ -138,8 +137,8 @@ module Samagotchi
|
|
|
138
137
|
# their image refs (#image_refs), the Revise draft's turn id and the
|
|
139
138
|
# origin's ids the page matches it by (restore.js restoreAction). nil
|
|
140
139
|
# for anyone else's, the owner's or an automatic turn (decided by the
|
|
141
|
-
# origin's author, which chi web stamped).
|
|
142
|
-
#
|
|
140
|
+
# origin's author, which chi web stamped). Only a parsed Verify claim is
|
|
141
|
+
# carried beyond those fields.
|
|
143
142
|
def prompt_restored(event, viewer)
|
|
144
143
|
origin = field(event, :origin)
|
|
145
144
|
return nil unless origin.is_a?(Hash)
|
|
@@ -150,8 +149,7 @@ module Samagotchi
|
|
|
150
149
|
out = { "type" => "prompt_restored", **seq(event), "prompt" => field(event, :prompt).to_s }
|
|
151
150
|
images = image_refs(field(event, :images), author, viewer)
|
|
152
151
|
out["images"] = images if images
|
|
153
|
-
|
|
154
|
-
out["revises"] = { "turn_id" => revises.turn_id } if revises
|
|
152
|
+
out.merge!(TurnAnchors.new(verify: field(event, :verify), revises: field(event, :revises)).participant_view)
|
|
155
153
|
out["origin"] = { "client_id" => field(origin, :client_id).to_s, "enqueued_id" => field(origin, :enqueued_id).to_s,
|
|
156
154
|
"author" => author.to_s }
|
|
157
155
|
out
|
|
@@ -282,13 +282,17 @@ export function unarchiveSession(id, opts = {}) {
|
|
|
282
282
|
// Deletes the session for good; force=true has the server stop a live
|
|
283
283
|
// worker first (the page's confirm says so; without it, 409 worker_running).
|
|
284
284
|
export function deleteSession(id, opts = {}) {
|
|
285
|
-
|
|
285
|
+
const { expect, ...apiOpts } = opts;
|
|
286
|
+
const plan = expect == null ? "" : `&expect=${encodeURIComponent(expect)}`;
|
|
287
|
+
return api(`/api/sessions/${encodeURIComponent(id)}?force=true${plan}`, { method: "DELETE", ...apiOpts });
|
|
286
288
|
}
|
|
287
289
|
|
|
288
290
|
// Deletes every link of the session's chain with their delegates (running
|
|
289
291
|
// ones are left running); force: the links' workers are stopped first.
|
|
290
292
|
export function deleteChain(id, opts = {}) {
|
|
291
|
-
|
|
293
|
+
const { expect, ...apiOpts } = opts;
|
|
294
|
+
const plan = expect == null ? "" : `&expect=${encodeURIComponent(expect)}`;
|
|
295
|
+
return api(`/api/sessions/${encodeURIComponent(id)}/chain?force=true${plan}`, { method: "DELETE", ...apiOpts });
|
|
292
296
|
}
|
|
293
297
|
|
|
294
298
|
// What deleting a chained session would do, for the delete sheet
|
|
@@ -7,8 +7,9 @@
|
|
|
7
7
|
// The sheet's words come from GET /api/sessions/:id/delete_plan, the plan
|
|
8
8
|
// and refusals the server's delete itself runs (SessionChain::Delete::
|
|
9
9
|
// Preview), fetched as the sheet opens and only for a chained session: a
|
|
10
|
-
// session in no chain keeps today's confirm (session_actions.js).
|
|
11
|
-
//
|
|
10
|
+
// session in no chain keeps today's confirm (session_actions.js). The
|
|
11
|
+
// confirmed digest is checked by the delete; a changed plan is re-read and
|
|
12
|
+
// shown for a fresh confirmation.
|
|
12
13
|
//
|
|
13
14
|
// Where it is drawn (a host): the ⋯ More panel turns into it, with ‹ More to
|
|
14
15
|
// go back (more_sheet.js' subview); a card's ✕ opens it at the card, on
|
|
@@ -111,6 +112,19 @@ export function deleteChoices(session, plan) {
|
|
|
111
112
|
};
|
|
112
113
|
}
|
|
113
114
|
|
|
115
|
+
function unchainedChoices(session, plan) {
|
|
116
|
+
const id = session?.id ?? plan?.session_id;
|
|
117
|
+
const name = sessionName(session);
|
|
118
|
+
return {
|
|
119
|
+
title: `Delete ${short(id)}${name ? ` — "${name}"` : ""}?`,
|
|
120
|
+
sub: "This session is no longer in a chain. This can't be undone.",
|
|
121
|
+
banner: null,
|
|
122
|
+
choices: [{ act: "this", label: "Delete this session", lines: ["Its turns and session data will be permanently deleted."],
|
|
123
|
+
primary: true }],
|
|
124
|
+
foot: "This can't be undone.",
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
114
128
|
function choiceTemplate(choice) {
|
|
115
129
|
const cls = `ds-choice${choice.primary ? " primary" : ""}`;
|
|
116
130
|
if (choice.disabled) {
|
|
@@ -132,11 +146,12 @@ function headTemplate(back) {
|
|
|
132
146
|
|
|
133
147
|
// The choices (a lit-html template). +busy+: a delete under way (every
|
|
134
148
|
// button off); +back+: drawn in the More panel.
|
|
135
|
-
export function deleteSheetTemplate(model, { back = false, busy = false } = {}) {
|
|
149
|
+
export function deleteSheetTemplate(model, { back = false, busy = false, notice = null } = {}) {
|
|
136
150
|
const allOff = model.choices.every((c) => c.disabled);
|
|
137
151
|
return html`<div class=${`delete-sheet${busy ? " busy" : ""}`}>${headTemplate(back)}
|
|
138
152
|
<div class="ds-title">${model.title}</div>
|
|
139
153
|
<div class="ds-sub">${model.sub}</div>
|
|
154
|
+
${notice ? html`<div class="ds-banner">${notice}</div>` : nothing}
|
|
140
155
|
${model.banner ? html`<div class="ds-banner">${model.banner}</div>` : nothing}
|
|
141
156
|
<div class="ds-choices" ?inert=${busy}>${model.choices.map(choiceTemplate)}</div>
|
|
142
157
|
<div class="ds-foot">${allOff ? html`<span></span>` : html`<span class="ds-note">${busy ? "Deleting…" : model.foot}</span>`}
|
|
@@ -224,6 +239,7 @@ export function listAfterDelete(list, result) {
|
|
|
224
239
|
export function runDeleteFlow({ id, session, host, api, done, plain, failed }) {
|
|
225
240
|
let plan = null;
|
|
226
241
|
let model = null;
|
|
242
|
+
let notice = null;
|
|
227
243
|
let refusal = null;
|
|
228
244
|
let busy = false;
|
|
229
245
|
const back = !!host.back;
|
|
@@ -233,7 +249,7 @@ export function runDeleteFlow({ id, session, host, api, done, plain, failed }) {
|
|
|
233
249
|
const live = () => (host.live ? host.live() : true);
|
|
234
250
|
const show = (tpl) => { if (live()) host.show(tpl); };
|
|
235
251
|
|
|
236
|
-
const showChoices = () => show(deleteSheetTemplate(model, { back, busy }));
|
|
252
|
+
const showChoices = () => show(deleteSheetTemplate(model, { back, busy, notice }));
|
|
237
253
|
const showRefusal = () => show(refusalTemplate(refusalModel(refusal, plan?.link?.previous_id), { back }));
|
|
238
254
|
|
|
239
255
|
show(loadingTemplate({ back }));
|
|
@@ -258,14 +274,35 @@ export function runDeleteFlow({ id, session, host, api, done, plain, failed }) {
|
|
|
258
274
|
busy = true;
|
|
259
275
|
showChoices();
|
|
260
276
|
try {
|
|
261
|
-
const
|
|
277
|
+
const digest = which === "chain" ? plan?.chain?.digest : plan?.link?.digest;
|
|
278
|
+
const result = which === "chain" ? await api.deleteChain(id, { expect: digest }) : await api.deleteSession(id, { expect: digest });
|
|
262
279
|
if (live()) host.close();
|
|
263
280
|
done(which, result, plan);
|
|
264
281
|
} catch (error) {
|
|
282
|
+
if (error.code === "plan_changed") {
|
|
283
|
+
await refreshPlan();
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
265
286
|
refused(error);
|
|
266
287
|
}
|
|
267
288
|
}
|
|
268
289
|
|
|
290
|
+
async function refreshPlan() {
|
|
291
|
+
show(loadingTemplate({ back }));
|
|
292
|
+
try {
|
|
293
|
+
const read = await api.deletePlan(id);
|
|
294
|
+
if (!live()) return;
|
|
295
|
+
plan = read;
|
|
296
|
+
model = read?.chained ? deleteChoices(session, plan) : unchainedChoices(session, plan);
|
|
297
|
+
notice = "What this deletes changed; check again";
|
|
298
|
+
busy = false;
|
|
299
|
+
showChoices();
|
|
300
|
+
} catch (error) {
|
|
301
|
+
busy = false;
|
|
302
|
+
if (live()) { host.close(); failed(error); }
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
|
|
269
306
|
// "Stop its worker, then delete": the delete again only once each worker
|
|
270
307
|
// reads stopped (the stop route's released, after its bounded wait);
|
|
271
308
|
// still running, the refusal says so, and nothing loops.
|
|
@@ -52,6 +52,7 @@ const sameScope = (a, b) => !a || !b || a === b;
|
|
|
52
52
|
|
|
53
53
|
// Whether +item+ (a set item) is the memory +part+ names: its name, and its
|
|
54
54
|
// scope when both know theirs.
|
|
55
|
+
// The server-side twin is Tools::MemoryRead.prompt_memory? in tools/memory.rb.
|
|
55
56
|
export const same = (part, item) => item.kind === "memory" && memoryName(item.key) === memoryName(part.ref) && sameScope(part.scope, item.scope);
|
|
56
57
|
|
|
57
58
|
// A part's row: never an item the set or a steer line knows.
|
|
@@ -480,7 +480,7 @@ module Samagotchi
|
|
|
480
480
|
# {status: "restarted", session_id, from_version, version} (version nil:
|
|
481
481
|
# the new worker is still starting; its bridge_up follows); 409
|
|
482
482
|
# {error: "held" | "not_running" | "unsupported" | "failed", reason, detail} in words (a stopped
|
|
483
|
-
# worker: held, reason stopped, its pid); 409
|
|
483
|
+
# worker: held, reason stopped, its pid; a stop marked: held, reason stop_marked); 409
|
|
484
484
|
# owned_by_tui; 404.
|
|
485
485
|
def handle_restart(_req, id)
|
|
486
486
|
result = @manager.restart_session(id, state_dir: @state_dir, client_id: ClientId::WEB_RESTART)
|
|
@@ -524,7 +524,8 @@ module Samagotchi
|
|
|
524
524
|
# session is always refused.
|
|
525
525
|
def handle_delete(req, id)
|
|
526
526
|
result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
|
|
527
|
-
wait: STOP_WAIT_SECONDS, revive_previous: true, refuse_continuing: true
|
|
527
|
+
wait: STOP_WAIT_SECONDS, revive_previous: true, refuse_continuing: true,
|
|
528
|
+
expect: req.GET["expect"])
|
|
528
529
|
touch_deleted(result[:id], result)
|
|
529
530
|
json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped],
|
|
530
531
|
unarchived: Array(result[:unarchived]), moved_back: Array(result[:moved_back]),
|
|
@@ -539,7 +540,7 @@ module Samagotchi
|
|
|
539
540
|
# their delegates (SessionManager.delete_chain). Query-only.
|
|
540
541
|
def handle_delete_chain(req, id)
|
|
541
542
|
result = @manager.delete_chain(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
|
|
542
|
-
wait: STOP_WAIT_SECONDS)
|
|
543
|
+
wait: STOP_WAIT_SECONDS, expect: req.GET["expect"])
|
|
543
544
|
touch_deleted(id, result)
|
|
544
545
|
json_response(200, { status: "deleted", session_id: id, deleted: result[:deleted], left: result[:left],
|
|
545
546
|
left_running: result[:left_running], unarchived: result[:unarchived],
|
|
@@ -166,6 +166,13 @@ module Samagotchi
|
|
|
166
166
|
session.displayed_status(owner: owner)
|
|
167
167
|
end
|
|
168
168
|
|
|
169
|
+
def shown_status(session, state, turn_snapshot, owner:)
|
|
170
|
+
status = displayed_status(session, state, owner: owner)
|
|
171
|
+
return Session::STATUS_IDLE if status == Session::STATUS_RUNNING && turn_snapshot && !turn_snapshot["current_turn"]
|
|
172
|
+
|
|
173
|
+
status
|
|
174
|
+
end
|
|
175
|
+
|
|
169
176
|
def first_preview_for(session)
|
|
170
177
|
raw = session.first_preview || session.last_prompt || ""
|
|
171
178
|
norm = raw.to_s.gsub(/\s+/, " ").strip
|
|
@@ -165,14 +165,17 @@ module Samagotchi
|
|
|
165
165
|
end
|
|
166
166
|
|
|
167
167
|
# Delete a session its worker left empty. Checked again now the lock is
|
|
168
|
-
# free
|
|
169
|
-
#
|
|
170
|
-
#
|
|
171
|
-
#
|
|
168
|
+
# free, and once more under the deleting lock (delete_session's
|
|
169
|
+
# still:): a note or input may have come in since the worker looked
|
|
170
|
+
# (one that went in before the lock keeps the session), and a worker
|
|
171
|
+
# woken meanwhile owns it (delete_session refuses). A delivery during
|
|
172
|
+
# the deleting lock is refused (DeliverResult::DELETING, its file taken
|
|
173
|
+
# back), one after removal finds no session.
|
|
172
174
|
private def discard_left_session(session_id, state_dir:, default_model:)
|
|
173
|
-
|
|
175
|
+
empty = -> { SessionManager.discardable?(session_id, state_dir: state_dir, default_model: default_model) }
|
|
176
|
+
return unless empty.call
|
|
174
177
|
|
|
175
|
-
SessionManager.delete_session(session_id, state_dir: state_dir, purge_runs: false)
|
|
178
|
+
SessionManager.delete_session(session_id, state_dir: state_dir, purge_runs: false, still: empty)
|
|
176
179
|
Log.info(:worker, "discarded_empty", sid: session_id)
|
|
177
180
|
rescue SessionManager::DeleteRefused, SessionManager::OwnedByTUI, ArgumentError, SystemCallError => e
|
|
178
181
|
Log.info(:worker, "kept_session", sid: session_id, error: e.class.name, msg: e.message)
|
|
@@ -78,6 +78,10 @@ module Samagotchi
|
|
|
78
78
|
# nothing else may resume, stop or delete it.
|
|
79
79
|
def tui_owned? = owner&.tui? == true
|
|
80
80
|
|
|
81
|
+
# A deleter holds it (OwnerLock kind deleting: SessionManager's
|
|
82
|
+
# delete, an empty session's discard) while the session's files go.
|
|
83
|
+
def deleting? = owner&.deleting? == true
|
|
84
|
+
|
|
81
85
|
# The `<id>/stopped` marker: a user's stop, not the turn's status.
|
|
82
86
|
def stop_marked? = stop_marked
|
|
83
87
|
|
|
@@ -33,7 +33,7 @@ module Samagotchi
|
|
|
33
33
|
WorkingSetSteer::ADD => "added", WorkingSetSteer::UNADD => "unadded" }.freeze
|
|
34
34
|
PIN_OPS = [WorkingSetSteer::PIN, WorkingSetSteer::UNPIN].freeze
|
|
35
35
|
|
|
36
|
-
# @param engine [Engine] #session, #working_set_strategy, #
|
|
36
|
+
# @param engine [Engine] #session, #working_set_strategy, #prompt_memories,
|
|
37
37
|
# #clone_messages, #replace_messages
|
|
38
38
|
# @param save [#call] saves the session after a change
|
|
39
39
|
def initialize(engine:, save:)
|
|
@@ -111,7 +111,7 @@ module Samagotchi
|
|
|
111
111
|
when WorkingSetSteer::UNPIN then WorkingSetSteer.unpin(pins, target)
|
|
112
112
|
when WorkingSetSteer::ADD
|
|
113
113
|
WorkingSetSteer.add(messages, target, strategy: strategy, cwd: cwd, found: method(:missing),
|
|
114
|
-
in_prompt: @engine.
|
|
114
|
+
in_prompt: @engine.prompt_memories)
|
|
115
115
|
else WorkingSetSteer.unadd(messages, target)
|
|
116
116
|
end
|
|
117
117
|
end
|
|
@@ -262,7 +262,7 @@ module Samagotchi
|
|
|
262
262
|
# of it sent), a dropped one (undrop it), a prompt-only memory, and one
|
|
263
263
|
# added already.
|
|
264
264
|
# @param found [#call] target → nil when it exists, else a Refused reason
|
|
265
|
-
# @param in_prompt [Array<String>] memory names the system prompt holds
|
|
265
|
+
# @param in_prompt [Array<Array(String, String, nil)>] memory names and scopes the system prompt holds
|
|
266
266
|
# @return [Done]
|
|
267
267
|
# @raise [Refused]
|
|
268
268
|
def add(conversation, target, strategy:, cwd:, found: ->(_) {}, in_prompt: [])
|
|
@@ -373,14 +373,7 @@ module Samagotchi
|
|
|
373
373
|
def prompt_memory?(target, names)
|
|
374
374
|
return false unless target.kind == WorkingSet::MEMORY
|
|
375
375
|
|
|
376
|
-
|
|
377
|
-
Array(names).any? { |prompt_name| normalized_memory_name(prompt_name) == name }
|
|
378
|
-
end
|
|
379
|
-
|
|
380
|
-
def normalized_memory_name(raw)
|
|
381
|
-
value = raw.to_s.strip
|
|
382
|
-
scope, name = value.split("/", 2)
|
|
383
|
-
Tools::VALID_SCOPES.include?(scope) ? name : value
|
|
376
|
+
Tools::MemoryRead.prompt_memory?(target.key, names)
|
|
384
377
|
end
|
|
385
378
|
|
|
386
379
|
# The note listing +ops+: a tail system message, escaped like a context
|
|
@@ -426,7 +419,7 @@ module Samagotchi
|
|
|
426
419
|
tokens >= 1000 ? format("%.1fk tokens", tokens / 1000.0) : "#{tokens} tokens"
|
|
427
420
|
end
|
|
428
421
|
|
|
429
|
-
private_class_method :relative, :shared_with, :drop_of, :known?, :live?, :layers, :same?, :prompt_memory?,
|
|
422
|
+
private_class_method :relative, :shared_with, :drop_of, :known?, :live?, :layers, :same?, :prompt_memory?,
|
|
430
423
|
:labels, :call_of
|
|
431
424
|
end
|
|
432
425
|
end
|