samagotchi 0.201.0 → 0.202.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 +57 -1
- data/docs/architecture.md +47 -7
- data/docs/cli.md +20 -1
- data/docs/configuration.md +2 -1
- data/docs/internals/bridge.md +4 -4
- data/docs/internals/engine.md +9 -6
- data/docs/internals/knowledge.md +26 -6
- data/docs/internals/objectives.md +2 -2
- data/docs/internals/pages.md +4 -2
- data/docs/internals/participants.md +1 -1
- data/docs/internals/peers.md +9 -7
- data/docs/internals/remote-sessions.md +6 -6
- data/docs/internals/session-lifecycle.md +37 -15
- data/docs/internals/web-frontend.md +18 -7
- data/docs/pages.md +6 -3
- data/docs/plugins.md +2 -2
- data/docs/scheduling.md +40 -12
- data/docs/sessions.md +48 -6
- data/docs/sub-agent.md +1 -2
- data/docs/testing.md +92 -8
- data/lib/samagotchi/answer_display.rb +3 -2
- data/lib/samagotchi/answer_tail.rb +4 -4
- data/lib/samagotchi/bridge/card_places.rb +5 -5
- data/lib/samagotchi/bridge/turn_accumulator.rb +5 -5
- data/lib/samagotchi/bridge/turn_drawings/synth.rb +5 -5
- data/lib/samagotchi/bridge/turn_drawings.rb +2 -2
- data/lib/samagotchi/bridge/turn_ranges.rb +15 -8
- data/lib/samagotchi/bridge.rb +5 -5
- data/lib/samagotchi/broadcast_command.rb +3 -3
- data/lib/samagotchi/bundle_command/try/trace.rb +18 -6
- data/lib/samagotchi/bundles/knowledge/embedder.rb +2 -0
- data/lib/samagotchi/bundles/knowledge/github.rb +19 -0
- data/lib/samagotchi/bundles/knowledge/manifest.yml +6 -6
- data/lib/samagotchi/bundles/knowledge/plugin.rb +26 -54
- data/lib/samagotchi/bundles/knowledge/refresh.rb +89 -14
- data/lib/samagotchi/bundles/knowledge/store.rb +36 -2
- data/lib/samagotchi/bundles/schedules/manifest.yml +2 -2
- data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +24 -3
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +1 -1
- data/lib/samagotchi/bundles/system/manifest.yml +3 -3
- data/lib/samagotchi/bundles/system/skill_pages.md +1 -1
- data/lib/samagotchi/chi_invocation.rb +1 -1
- data/lib/samagotchi/child_move.rb +6 -3
- data/lib/samagotchi/child_reports.rb +3 -3
- data/lib/samagotchi/children_status.rb +4 -2
- data/lib/samagotchi/client_id.rb +2 -2
- data/lib/samagotchi/compat.rb +4 -6
- data/lib/samagotchi/config.rb +3 -0
- data/lib/samagotchi/context_note.rb +1 -1
- data/lib/samagotchi/continue_intent.rb +1 -1
- data/lib/samagotchi/continue_offer.rb +2 -2
- data/lib/samagotchi/continue_repair_backoff.rb +71 -0
- data/lib/samagotchi/delivery.rb +1 -4
- data/lib/samagotchi/engine/participants.rb +1 -1
- data/lib/samagotchi/engine/recap.rb +1 -1
- data/lib/samagotchi/engine/turn_end.rb +1 -1
- data/lib/samagotchi/grants.rb +2 -2
- data/lib/samagotchi/guardrail_wiring.rb +1 -1
- data/lib/samagotchi/guardrails/approval.rb +3 -3
- data/lib/samagotchi/guardrails/pair_boundary.rb +3 -3
- data/lib/samagotchi/guardrails/participant_refusal.rb +2 -2
- data/lib/samagotchi/idle_recap.rb +6 -6
- data/lib/samagotchi/kernel_loop.rb +1 -1
- data/lib/samagotchi/last_turn.rb +9 -6
- data/lib/samagotchi/llm/chat_loop.rb +5 -2
- data/lib/samagotchi/mcp_command.rb +2 -2
- data/lib/samagotchi/memory_usage.rb +7 -6
- data/lib/samagotchi/note_command.rb +1 -1
- data/lib/samagotchi/note_delivery.rb +5 -5
- data/lib/samagotchi/objectives_backing.rb +3 -3
- data/lib/samagotchi/objectives_record.rb +2 -2
- data/lib/samagotchi/objectives_track.rb +2 -2
- data/lib/samagotchi/owner_lock.rb +28 -2
- data/lib/samagotchi/owner_prompt.rb +3 -3
- data/lib/samagotchi/pages/runtime.rb +9 -4
- data/lib/samagotchi/participant_revise.rb +6 -6
- data/lib/samagotchi/peer_receipts.rb +11 -4
- data/lib/samagotchi/plugin/side_question.rb +1 -1
- data/lib/samagotchi/recap_place.rb +5 -5
- data/lib/samagotchi/reply_for.rb +3 -3
- data/lib/samagotchi/reply_wait/liveness.rb +2 -2
- data/lib/samagotchi/reply_wait/poll.rb +3 -3
- data/lib/samagotchi/schedule_command.rb +1 -1
- data/lib/samagotchi/schedule_display.rb +4 -3
- data/lib/samagotchi/schedules/busy_part.rb +29 -5
- data/lib/samagotchi/schedules/history_part.rb +47 -7
- data/lib/samagotchi/schedules/marks_part.rb +52 -29
- data/lib/samagotchi/schedules/notices_part.rb +31 -4
- data/lib/samagotchi/schedules/protection_part.rb +69 -11
- data/lib/samagotchi/schedules/prune_part.rb +3 -2
- data/lib/samagotchi/schedules.rb +37 -47
- data/lib/samagotchi/send_command.rb +12 -12
- data/lib/samagotchi/session.rb +71 -59
- data/lib/samagotchi/session_access/contract.rb +7 -0
- data/lib/samagotchi/session_access/exchange.rb +1 -1
- data/lib/samagotchi/session_access/local.rb +9 -9
- data/lib/samagotchi/session_access/remote.rb +20 -24
- data/lib/samagotchi/session_access.rb +1 -1
- data/lib/samagotchi/session_chain/delete.rb +15 -8
- data/lib/samagotchi/session_chain/delete_preview.rb +3 -3
- data/lib/samagotchi/session_chain/links.rb +18 -4
- data/lib/samagotchi/session_chain/successor.rb +1 -1
- data/lib/samagotchi/session_chain.rb +3 -3
- data/lib/samagotchi/session_continue.rb +83 -32
- data/lib/samagotchi/session_inbox.rb +6 -17
- data/lib/samagotchi/session_inbox_delivery.rb +36 -13
- data/lib/samagotchi/session_lifecycle/allowed.rb +5 -5
- data/lib/samagotchi/session_manager/delegate_rule.rb +2 -2
- data/lib/samagotchi/session_manager/delivery.rb +13 -38
- data/lib/samagotchi/session_manager/emptiness.rb +35 -15
- data/lib/samagotchi/session_manager/lifecycle.rb +3 -2
- data/lib/samagotchi/session_manager/listing.rb +35 -10
- data/lib/samagotchi/session_manager/removal.rb +16 -0
- data/lib/samagotchi/session_manager/spawn.rb +9 -6
- data/lib/samagotchi/session_manager.rb +6 -4
- data/lib/samagotchi/session_messages.rb +602 -0
- data/lib/samagotchi/session_metrics.rb +7 -4
- data/lib/samagotchi/session_participants.rb +4 -4
- data/lib/samagotchi/session_protection.rb +16 -2
- data/lib/samagotchi/session_retention.rb +26 -12
- data/lib/samagotchi/session_row.rb +1 -1
- data/lib/samagotchi/session_stats.rb +17 -8
- data/lib/samagotchi/session_stores.rb +5 -0
- data/lib/samagotchi/session_transition.rb +3 -2
- data/lib/samagotchi/sessions_command.rb +55 -3
- data/lib/samagotchi/steer.rb +16 -50
- data/lib/samagotchi/storage_migration.rb +234 -0
- data/lib/samagotchi/terminal_ui/attached_loop.rb +3 -3
- data/lib/samagotchi/terminal_ui/formatting.rb +5 -5
- data/lib/samagotchi/terminal_ui/join_replay.rb +3 -3
- data/lib/samagotchi/terminal_ui/question_prompt.rb +2 -2
- data/lib/samagotchi/toolkit_report_command.rb +1 -1
- data/lib/samagotchi/tools/ask_session.rb +2 -2
- data/lib/samagotchi/tools/send_note.rb +1 -1
- data/lib/samagotchi/turn_context.rb +3 -3
- data/lib/samagotchi/turn_endings.rb +17 -0
- data/lib/samagotchi/turn_excerpts.rb +7 -4
- data/lib/samagotchi/turn_flow.rb +1 -1
- data/lib/samagotchi/turn_note.rb +30 -2
- data/lib/samagotchi/turn_origin.rb +7 -7
- data/lib/samagotchi/turn_outcome.rb +11 -0
- data/lib/samagotchi/turn_stages/verify_anchors.rb +4 -4
- data/lib/samagotchi/verify_fresh.rb +6 -6
- data/lib/samagotchi/verify_fresh_origin.rb +4 -4
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +4 -3
- data/lib/samagotchi/web/last_ask.rb +2 -2
- data/lib/samagotchi/web/message_display.rb +12 -12
- data/lib/samagotchi/web/page_routes.rb +9 -3
- data/lib/samagotchi/web/participant_view.rb +4 -4
- data/lib/samagotchi/web/participants_admin.rb +1 -1
- data/lib/samagotchi/web/public/bubbles.js +1 -1
- data/lib/samagotchi/web/public/composer_fold.js +0 -3
- data/lib/samagotchi/web/public/composer_grip.js +0 -2
- data/lib/samagotchi/web/public/page_view.js +29 -4
- data/lib/samagotchi/web/public/participant_model.js +1 -1
- data/lib/samagotchi/web/public/peer_wait.js +2 -4
- data/lib/samagotchi/web/public/schedule_marks.js +2 -5
- data/lib/samagotchi/web/public/schedule_name.js +14 -0
- data/lib/samagotchi/web/public/session_actions.js +5 -0
- data/lib/samagotchi/web/public/start_page.js +1 -1
- data/lib/samagotchi/web/public/stop_sheet.js +2 -4
- data/lib/samagotchi/web/public/working_set_rail.js +0 -3
- data/lib/samagotchi/web/push/attention.rb +1 -1
- data/lib/samagotchi/web/session_create.rb +1 -1
- data/lib/samagotchi/web/session_hub.rb +68 -20
- data/lib/samagotchi/web/session_summary.rb +18 -14
- data/lib/samagotchi/web/verify_fresh_routes.rb +4 -4
- data/lib/samagotchi/worker/turn_drain.rb +158 -0
- data/lib/samagotchi/worker.rb +8 -144
- data/lib/samagotchi/worker_boot.rb +21 -5
- data/lib/samagotchi/worker_participant_gate.rb +5 -5
- data/lib/samagotchi/worker_presence.rb +24 -1
- metadata +6 -1
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "fileutils"
|
|
4
|
+
require "json"
|
|
5
|
+
require_relative "atomic_file"
|
|
6
|
+
require_relative "owner_lock"
|
|
7
|
+
require_relative "session"
|
|
8
|
+
require_relative "session_messages"
|
|
9
|
+
require_relative "session_paths"
|
|
10
|
+
require_relative "session_transition"
|
|
11
|
+
require_relative "starting_marker"
|
|
12
|
+
require_relative "worker_presence"
|
|
13
|
+
|
|
14
|
+
module Samagotchi
|
|
15
|
+
# Saved sessions moved from one storage format to the next, one named,
|
|
16
|
+
# versioned step at a time (`chi sessions migrate`). A step reads each
|
|
17
|
+
# session file and says whether it is :pending, :done or skipped
|
|
18
|
+
# ([:skip, reason]), converts a pending one and checks what it wrote,
|
|
19
|
+
# putting the file back when the check fails. No ledger: a step detects
|
|
20
|
+
# the format, so running again converts only what is left.
|
|
21
|
+
#
|
|
22
|
+
# A session someone holds is left for later: a worker or a one-turn run
|
|
23
|
+
# owns it (it converts at its own next save), a worker is being started,
|
|
24
|
+
# or a transition holds it (a continue, a delete, a send). A conversion
|
|
25
|
+
# runs under the session's transition lock (kind :migrate, one try) and
|
|
26
|
+
# its owner lock (OwnerLock::MIGRATING, one try), for the milliseconds it
|
|
27
|
+
# takes. Meanwhile a delivery that holds the transition lock shared (a
|
|
28
|
+
# send, a push, a peer's say, a turn sender) is refused "being migrated";
|
|
29
|
+
# a stop waits for the lock (its lock wait), else is refused the same;
|
|
30
|
+
# a note, a ring, a fresh verdict and a leaving worker's respawn are
|
|
31
|
+
# written and wait for it (WorkerPresence.settled) before deciding on a
|
|
32
|
+
# worker; a resume spawns the worker, and a worker started meanwhile
|
|
33
|
+
# waits for it to take the lock (WorkerBoot). Nothing signals the
|
|
34
|
+
# migrating process. A dry run reads only: no lock, no folder made.
|
|
35
|
+
module StorageMigration
|
|
36
|
+
# A step's outcome over the sessions: the ids converted (or, in a dry
|
|
37
|
+
# run, to convert: +pending+), how many were done already, the ids
|
|
38
|
+
# skipped by reason and the ones that failed with why; the session
|
|
39
|
+
# files' bytes before (pending ones) and after (converted ones).
|
|
40
|
+
StepReport = Data.define(:id, :version, :pending, :converted, :done, :skipped, :failed, :bytes_before, :bytes_after)
|
|
41
|
+
|
|
42
|
+
# +dry_run+: nothing was written.
|
|
43
|
+
Report = Data.define(:dry_run, :steps) do
|
|
44
|
+
def to_h = { dry_run: dry_run, steps: steps.map(&:to_h) }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# session.split_messages: a session's messages in their own file beside
|
|
48
|
+
# <id>.json (SessionMessages, split), converted with
|
|
49
|
+
# SessionMessages.split_file!.
|
|
50
|
+
module SplitMessages
|
|
51
|
+
# What a conversion replaced: the file's bytes and what it held, and
|
|
52
|
+
# the generation it wrote.
|
|
53
|
+
Converted = Data.define(:bytes, :data, :messages, :generation)
|
|
54
|
+
|
|
55
|
+
module_function
|
|
56
|
+
|
|
57
|
+
def id = "session-split-messages"
|
|
58
|
+
def version = 1
|
|
59
|
+
|
|
60
|
+
# :done for a split file; :pending for one with inline messages (an
|
|
61
|
+
# older chi's beside a log too); [:skip, "unreadable"] for one that
|
|
62
|
+
# doesn't parse or whose messages can't be read, [:skip, "not a
|
|
63
|
+
# session"] for other JSON (its id isn't its name: schedules.json),
|
|
64
|
+
# [:skip, "messages not a list"] for a "messages" that isn't one.
|
|
65
|
+
def detect(path)
|
|
66
|
+
data = JSON.parse(File.read(path))
|
|
67
|
+
return [:skip, "unreadable"] unless data.is_a?(Hash)
|
|
68
|
+
return [:skip, "not a session"] unless data["id"] == File.basename(path, SessionPaths::FILE_EXT)
|
|
69
|
+
return :done if data.key?(SessionMessages::LOG_KEY) && !data.key?(SessionMessages::KEY)
|
|
70
|
+
return [:skip, "messages not a list"] if data.key?(SessionMessages::KEY) && !data[SessionMessages::KEY].is_a?(Array)
|
|
71
|
+
|
|
72
|
+
SessionMessages.of_file(data, path: path)
|
|
73
|
+
:pending
|
|
74
|
+
rescue JSON::ParserError, SystemCallError
|
|
75
|
+
[:skip, "unreadable"]
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @return [Converted]
|
|
79
|
+
def convert(path)
|
|
80
|
+
bytes = File.binread(path)
|
|
81
|
+
data = JSON.parse(bytes)
|
|
82
|
+
messages = SessionMessages.of_file(data, path: path)
|
|
83
|
+
Converted.new(bytes: bytes, data: data, messages: messages, generation: SessionMessages.split_file!(path))
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# @return [String, nil] what doesn't match, nil when all does
|
|
87
|
+
def verify(path, converted)
|
|
88
|
+
data = JSON.parse(File.read(path))
|
|
89
|
+
kept = converted.data.except(SessionMessages::KEY, SessionMessages::LOG_KEY)
|
|
90
|
+
return "the session file's other keys changed" unless data.except(SessionMessages::LOG_KEY) == kept
|
|
91
|
+
return "the messages read back differ" unless SessionMessages.read(path) == converted.messages
|
|
92
|
+
|
|
93
|
+
nil
|
|
94
|
+
rescue JSON::ParserError, SystemCallError => e
|
|
95
|
+
"the session file can't be read back: #{e.message}"
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# The file as it was, the generation gone.
|
|
99
|
+
def undo(path, converted)
|
|
100
|
+
AtomicFile.write(path, converted.bytes)
|
|
101
|
+
FileUtils.rm_f(File.join(SessionMessages.dir_of(path), converted.generation))
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
STEPS = [SplitMessages].freeze
|
|
106
|
+
# Beside <id>.json: its bytes from before a conversion whose check
|
|
107
|
+
# failed and whose undo failed too (#undone).
|
|
108
|
+
PREMIGRATE_EXT = ".premigrate"
|
|
109
|
+
# The transition hold a conversion takes ("being migrated"; not one a
|
|
110
|
+
# stop refuses at once: Busy::TRANSITIONS, it is short).
|
|
111
|
+
HOLD_KIND = :migrate
|
|
112
|
+
|
|
113
|
+
module_function
|
|
114
|
+
|
|
115
|
+
# Run each of +steps+ over the session files in +state_dir+, archived
|
|
116
|
+
# ones included (or only +ids+).
|
|
117
|
+
# @return [Report]
|
|
118
|
+
def run(steps: STEPS, state_dir: Session.default_state_dir, dry_run: false, ids: nil)
|
|
119
|
+
paths = session_paths(state_dir, ids)
|
|
120
|
+
Report.new(dry_run: dry_run, steps: steps.map { |step| run_step(step, paths, state_dir, dry_run) })
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
# Every session file (named by a valid id), or +ids+' files.
|
|
124
|
+
def session_paths(state_dir, ids)
|
|
125
|
+
return ids.map { |id| Session.session_file(id, state_dir: state_dir) } if ids
|
|
126
|
+
return [] unless Dir.exist?(state_dir)
|
|
127
|
+
|
|
128
|
+
Dir.glob(File.join(state_dir, "*#{SessionPaths::FILE_EXT}"))
|
|
129
|
+
.select { |path| Session.valid_id?(File.basename(path, SessionPaths::FILE_EXT)) }.sort
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
def run_step(step, paths, state_dir, dry_run)
|
|
133
|
+
tally = { pending: [], converted: [], done: 0, skipped: Hash.new { |h, k| h[k] = [] }, failed: {},
|
|
134
|
+
bytes_before: 0, bytes_after: 0 }
|
|
135
|
+
paths.each do |path|
|
|
136
|
+
id = File.basename(path, SessionPaths::FILE_EXT)
|
|
137
|
+
outcome = File.exist?(path) ? migrate(step, path, id, state_dir, dry_run) : [:skip, "missing"]
|
|
138
|
+
record(tally, id, path, outcome)
|
|
139
|
+
end
|
|
140
|
+
StepReport.new(id: step.id, version: step.version, pending: tally[:pending], converted: tally[:converted],
|
|
141
|
+
done: tally[:done], skipped: tally[:skipped].to_h, failed: tally[:failed],
|
|
142
|
+
bytes_before: tally[:bytes_before], bytes_after: dry_run ? nil : tally[:bytes_after])
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# @return [Symbol, Array] :done, [:pending, bytes] (a dry run),
|
|
146
|
+
# [:converted, bytes before], [:skip, reason], [:failed, why]
|
|
147
|
+
def migrate(step, path, id, state_dir, dry_run)
|
|
148
|
+
state = step.detect(path)
|
|
149
|
+
return state unless state == :pending
|
|
150
|
+
|
|
151
|
+
held = held_by(id, state_dir)
|
|
152
|
+
return [:skip, held] if held
|
|
153
|
+
return [:pending, File.size(path)] if dry_run
|
|
154
|
+
|
|
155
|
+
convert(step, path, id, state_dir)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
# Why +id+ is held now (read only): "owned", "starting", "held"; nil
|
|
159
|
+
# when nobody holds it.
|
|
160
|
+
def held_by(id, state_dir)
|
|
161
|
+
return "owned" if WorkerPresence.read(id, state_dir: state_dir).owned?
|
|
162
|
+
return "starting" if StartingMarker.fresh?(id, state_dir: state_dir)
|
|
163
|
+
|
|
164
|
+
"held" if SessionTransition.held?(id, state_dir: state_dir)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
# The conversion under the session's locks, its state read again there.
|
|
168
|
+
# A session deleted since the first look is skipped, the transition
|
|
169
|
+
# lock's folder taken back (SessionTransition.forget): no skeleton.
|
|
170
|
+
def convert(step, path, id, state_dir)
|
|
171
|
+
SessionTransition.exclusive([id], kind: HOLD_KIND, state_dir: state_dir, deadline: 0) do
|
|
172
|
+
next [:skip, "missing"] unless File.exist?(path)
|
|
173
|
+
|
|
174
|
+
lock = OwnerLock.acquire(Session.session_dir(id, state_dir: state_dir), kind: OwnerLock::MIGRATING, wait: 0)
|
|
175
|
+
next [:skip, "owned"] unless lock
|
|
176
|
+
|
|
177
|
+
begin
|
|
178
|
+
next [:skip, "starting"] if StartingMarker.fresh?(id, state_dir: state_dir)
|
|
179
|
+
|
|
180
|
+
state = step.detect(path)
|
|
181
|
+
next state unless state == :pending
|
|
182
|
+
|
|
183
|
+
before = File.size(path)
|
|
184
|
+
converted = step.convert(path)
|
|
185
|
+
error = step.verify(path, converted)
|
|
186
|
+
error ? [:failed, undone(step, path, converted, error)] : [:converted, before]
|
|
187
|
+
ensure
|
|
188
|
+
lock.release
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
rescue SessionTransition::Busy
|
|
192
|
+
[:skip, "held"]
|
|
193
|
+
rescue StandardError => e
|
|
194
|
+
[:failed, "#{e.class}: #{e.message}"[0, 300]]
|
|
195
|
+
ensure
|
|
196
|
+
SessionTransition.forget(id, state_dir: state_dir)
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# The file put back after a failed check (+error+, the failure); when
|
|
200
|
+
# that fails too, its bytes from before go beside it
|
|
201
|
+
# (<id>.json.premigrate), named in the failure (and, when that fails
|
|
202
|
+
# as well, the failure says the file is left converted).
|
|
203
|
+
# @return [String] the failure
|
|
204
|
+
def undone(step, path, converted, error)
|
|
205
|
+
step.undo(path, converted)
|
|
206
|
+
error
|
|
207
|
+
rescue StandardError => e
|
|
208
|
+
failed = "#{error}; putting the file back failed (#{e.class}: #{e.message})"
|
|
209
|
+
kept = "#{path}#{PREMIGRATE_EXT}"
|
|
210
|
+
begin
|
|
211
|
+
File.binwrite(kept, converted.bytes)
|
|
212
|
+
"#{failed}, its original is in #{kept}"
|
|
213
|
+
rescue StandardError => e
|
|
214
|
+
"#{failed}, and keeping its original in #{kept} failed (#{e.class}: #{e.message}): the file is left converted"
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
|
|
218
|
+
def record(tally, id, path, outcome)
|
|
219
|
+
kind, value = outcome
|
|
220
|
+
case kind
|
|
221
|
+
when :done then tally[:done] += 1
|
|
222
|
+
when :skip then tally[:skipped][value] << id
|
|
223
|
+
when :failed then tally[:failed][id] = value
|
|
224
|
+
when :pending
|
|
225
|
+
tally[:pending] << id
|
|
226
|
+
tally[:bytes_before] += value
|
|
227
|
+
when :converted
|
|
228
|
+
tally[:converted] << id
|
|
229
|
+
tally[:bytes_before] += value
|
|
230
|
+
tally[:bytes_after] += File.size(path)
|
|
231
|
+
end
|
|
232
|
+
end
|
|
233
|
+
end
|
|
234
|
+
end
|
|
@@ -31,7 +31,7 @@ require_relative "../bridge_client"
|
|
|
31
31
|
require_relative "../delivery"
|
|
32
32
|
require_relative "../log"
|
|
33
33
|
require_relative "../context_note"
|
|
34
|
-
require_relative "../
|
|
34
|
+
require_relative "../speaker"
|
|
35
35
|
require_relative "../turn_origin"
|
|
36
36
|
require_relative "../model_profile"
|
|
37
37
|
require_relative "../output_formatter"
|
|
@@ -705,14 +705,14 @@ module Samagotchi
|
|
|
705
705
|
end
|
|
706
706
|
|
|
707
707
|
def learn_participants(rows)
|
|
708
|
-
Array(rows).each { |row| @participant_names[
|
|
708
|
+
Array(rows).each { |row| @participant_names[Speaker.author_for(row[:id])] = row[:name] if row.is_a?(Hash) && row[:id] }
|
|
709
709
|
end
|
|
710
710
|
|
|
711
711
|
# The prompt line's author keywords for the +author+ of an event, an
|
|
712
712
|
# origin or a queued row ("p:<id>"): their name as the worker named
|
|
713
713
|
# them; {} for the owner's.
|
|
714
714
|
def author_of(author)
|
|
715
|
-
author =
|
|
715
|
+
author = Speaker.sender_author(author) or return {}
|
|
716
716
|
|
|
717
717
|
{ author: author, author_name: @participant_names[author] }
|
|
718
718
|
end
|
|
@@ -277,9 +277,9 @@ module Samagotchi
|
|
|
277
277
|
# author). The name cleaned as everywhere (ParticipantName: one line,
|
|
278
278
|
# no control characters), the id when it has none.
|
|
279
279
|
def participant_prefix(author, name)
|
|
280
|
-
return "" unless
|
|
280
|
+
return "" unless Speaker.participant_author?(author)
|
|
281
281
|
|
|
282
|
-
id =
|
|
282
|
+
id = Speaker.participant_id(author)
|
|
283
283
|
"#{paint("#{participant_display_name(id, name)} ▸", participant_colour(id))} "
|
|
284
284
|
end
|
|
285
285
|
|
|
@@ -324,10 +324,10 @@ module Samagotchi
|
|
|
324
324
|
def join_prompt_line(message)
|
|
325
325
|
source = (message[:source] || message["source"]).to_s
|
|
326
326
|
content = (message[:content] || message["content"]).to_s
|
|
327
|
-
author =
|
|
327
|
+
author = Speaker.sender_author(message[:author] || message["author"])
|
|
328
328
|
return prompt_line(ClientId::PARTICIPANT_PREFIX, content, author: author, author_name: message[:author_name] || message["author_name"]) if author
|
|
329
329
|
return prompt_line(source, content) if Steer.opener_header(message)
|
|
330
|
-
return prompt_line(nil, content) unless source ==
|
|
330
|
+
return prompt_line(nil, content) unless source == Speaker::DELEGATE_REPORT
|
|
331
331
|
|
|
332
332
|
content.split(/\n\n(?=session: )/).map { |report| report_line(report) }.join("\n")
|
|
333
333
|
end
|
|
@@ -703,7 +703,7 @@ module Samagotchi
|
|
|
703
703
|
return nil unless rows.is_a?(Hash) && !rows.empty?
|
|
704
704
|
|
|
705
705
|
rows.map do |author, row|
|
|
706
|
-
name = row[:name].to_s.empty? ?
|
|
706
|
+
name = row[:name].to_s.empty? ? Speaker.participant_id(author) : row[:name]
|
|
707
707
|
turns = row[:turns].to_i
|
|
708
708
|
format("%<name>s $%<cost>.2f (%<turns>d turn%<s>s)", name: name, cost: row[:cost].to_f, turns: turns,
|
|
709
709
|
s: turns == 1 ? "" : "s")
|
|
@@ -8,7 +8,7 @@ require_relative "../context_note"
|
|
|
8
8
|
require_relative "../image_store"
|
|
9
9
|
require_relative "../log"
|
|
10
10
|
require_relative "../output_formatter"
|
|
11
|
-
require_relative "../
|
|
11
|
+
require_relative "../speaker"
|
|
12
12
|
require_relative "../turn_note"
|
|
13
13
|
require_relative "event_renderer"
|
|
14
14
|
require_relative "formatting"
|
|
@@ -113,9 +113,9 @@ module Samagotchi
|
|
|
113
113
|
Log.session_id = @link.client.session_id
|
|
114
114
|
Log.info(:attached, "joined", session: @link.client.session_id, messages: exchange.size,
|
|
115
115
|
notes: messages.count { |m| ContextNote.note?(m) })
|
|
116
|
-
# The last turn's own prompt (
|
|
116
|
+
# The last turn's own prompt (Speaker.turn_prompt?, as the page's
|
|
117
117
|
# turns): not Qwen's recovery line, nor input merged into the turn.
|
|
118
|
-
last_user = exchange.rindex { |m|
|
|
118
|
+
last_user = exchange.rindex { |m| Speaker.turn_prompt?(m) }
|
|
119
119
|
from = last_user && messages.index { |m| m.equal?(exchange[last_user]) }
|
|
120
120
|
render_join_notes(messages, from)
|
|
121
121
|
return unless last_user
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
require_relative "question_slot"
|
|
4
4
|
require_relative "../turn_flow"
|
|
5
5
|
require_relative "../question_desk"
|
|
6
|
-
require_relative "../
|
|
6
|
+
require_relative "../speaker"
|
|
7
7
|
|
|
8
8
|
module Samagotchi
|
|
9
9
|
class TerminalUI
|
|
@@ -241,7 +241,7 @@ module Samagotchi
|
|
|
241
241
|
return nil if author.empty?
|
|
242
242
|
|
|
243
243
|
name = get.call(:author_name).to_s
|
|
244
|
-
name.empty? ? author.delete_prefix(
|
|
244
|
+
name.empty? ? author.delete_prefix(Speaker::PARTICIPANT_PREFIX) : name
|
|
245
245
|
end
|
|
246
246
|
|
|
247
247
|
def parse_approval(raw)
|
|
@@ -108,7 +108,7 @@ module Samagotchi
|
|
|
108
108
|
return refused
|
|
109
109
|
end
|
|
110
110
|
|
|
111
|
-
head =
|
|
111
|
+
head = InboxDelivery::Head.resolve(:send, Session.resolve!(given, state_dir: @peers.state_dir), state_dir: @peers.state_dir)
|
|
112
112
|
asker = SessionAccess::Ask.new(@access, exchange: exchange)
|
|
113
113
|
reply = asker.call(given, prompt: question, client_id: ClientId.session(@peers.session_id), queue: true,
|
|
114
114
|
timeout: @slice, cap: @cap, cancelled: @cancelled,
|
|
@@ -124,7 +124,7 @@ module Samagotchi
|
|
|
124
124
|
Session.load(id, state_dir: @peers.state_dir)
|
|
125
125
|
return "Error: #{id[0, 8]} is this session; ask_session is for other sessions" if id == @peers.session_id
|
|
126
126
|
|
|
127
|
-
head =
|
|
127
|
+
head = InboxDelivery::Head.resolve(:send, id, state_dir: @peers.state_dir)
|
|
128
128
|
return "Error: #{id[0, 8]} was continued into this session; ask_session is for other sessions" if head.id == @peers.session_id
|
|
129
129
|
return "Error: a question can't start with ! or /: ask it in words" if question.start_with?("!", "/")
|
|
130
130
|
|
|
@@ -28,7 +28,7 @@ module Samagotchi
|
|
|
28
28
|
return "Error: #{id[0, 8]} is this session; send_note is for other sessions" if id == peers.session_id
|
|
29
29
|
|
|
30
30
|
# A link continued into this one: the note would come back here.
|
|
31
|
-
head =
|
|
31
|
+
head = InboxDelivery::Head.resolve(:note, id, state_dir: peers.state_dir)
|
|
32
32
|
return "Error: #{id[0, 8]} was continued into this session; send_note is for other sessions" if head.id == peers.session_id
|
|
33
33
|
|
|
34
34
|
result = NoteDelivery.deliver(id, text: content, source: "session", from_session: peers.session_id,
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "turn_origin"
|
|
4
|
-
require_relative "
|
|
4
|
+
require_relative "speaker"
|
|
5
5
|
require_relative "guardrails/context"
|
|
6
6
|
require_relative "llm/turn_settings"
|
|
7
7
|
|
|
@@ -52,8 +52,8 @@ module Samagotchi
|
|
|
52
52
|
**fields)
|
|
53
53
|
end
|
|
54
54
|
|
|
55
|
-
# Whether a participant wrote the turn (
|
|
56
|
-
def participant? =
|
|
55
|
+
# Whether a participant wrote the turn (Speaker.participant_author?).
|
|
56
|
+
def participant? = Speaker.participant_author?(author)
|
|
57
57
|
|
|
58
58
|
# Whether the turn's controller is cancelled (a Stop, a hook's stop_turn).
|
|
59
59
|
def cancelled? = !!controller&.cancelled?
|
|
@@ -15,6 +15,9 @@ module Samagotchi
|
|
|
15
15
|
class TurnEndings
|
|
16
16
|
# A failed turn's error_kind when chi itself failed (FailureLine).
|
|
17
17
|
CHI_ERROR = FailureLine::CHI_ERROR
|
|
18
|
+
# A failed turn's error_reason when its host doesn't serve the model
|
|
19
|
+
# asked for (LLM::BadRequest#model_not_served?).
|
|
20
|
+
MODEL_NOT_SERVED = :model_not_served
|
|
18
21
|
|
|
19
22
|
def self.chi_error?(error) = FailureLine.chi_error?(error)
|
|
20
23
|
|
|
@@ -44,6 +47,17 @@ module Samagotchi
|
|
|
44
47
|
end
|
|
45
48
|
end
|
|
46
49
|
|
|
50
|
+
# What else a failed turn's LastTurn keeps (not a stop fact: no parent's
|
|
51
|
+
# wait reads them): the provider error's finer code (error_reason, e.g.
|
|
52
|
+
# model_not_served) and its host (error_host), for a schedule's firing
|
|
53
|
+
# (Schedules: a configuration failure stops it at once, naming both).
|
|
54
|
+
# Only what is known.
|
|
55
|
+
def failure_facts(event)
|
|
56
|
+
return {} unless event&.dig(:type) == :turn_failed
|
|
57
|
+
|
|
58
|
+
{ error_reason: event[:error_reason]&.to_s, error_host: event[:host]&.to_s }.compact
|
|
59
|
+
end
|
|
60
|
+
|
|
47
61
|
# What the session keeps of a turn that ended normally (nil: nothing to
|
|
48
62
|
# replace). Runs under the event lock: it also adds the turn's note to
|
|
49
63
|
# result.conversation, which the session keeps as-is.
|
|
@@ -122,6 +136,9 @@ module Samagotchi
|
|
|
122
136
|
# A provider error says what kind it is, for one line per kind in the UIs.
|
|
123
137
|
if error.is_a?(LLM::ProviderError)
|
|
124
138
|
failed.merge!(error_kind: error.kind, retryable: error.retryable?, host: error.host, summary: failure_summary(error))
|
|
139
|
+
# The host doesn't serve the model asked for: a code beside the kind
|
|
140
|
+
# (bad_request covers more), so a schedule stops at once (Schedules::HistoryPart::CONFIG_FAILURES).
|
|
141
|
+
failed[:error_reason] = MODEL_NOT_SERVED if error.is_a?(LLM::BadRequest) && error.model_not_served?
|
|
125
142
|
elsif self.class.chi_error?(error)
|
|
126
143
|
failed.merge!(error_kind: CHI_ERROR, summary: self.class.failure_line(error))
|
|
127
144
|
end
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
require "json"
|
|
4
4
|
require_relative "session_paths"
|
|
5
|
+
require_relative "session_messages"
|
|
5
6
|
require_relative "analytics_file"
|
|
6
7
|
require_relative "redact"
|
|
7
8
|
require_relative "speaker"
|
|
@@ -129,8 +130,10 @@ module Samagotchi
|
|
|
129
130
|
return none unless File.exist?(path)
|
|
130
131
|
|
|
131
132
|
unreadable = 0
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
# The file and its messages (wherever it keeps them): either unreadable
|
|
134
|
+
# counts the session as one.
|
|
135
|
+
messages = begin
|
|
136
|
+
SessionMessages.parse(path) { |data| SessionMessages.of_file(data, path: path) if data.is_a?(Hash) }
|
|
134
137
|
rescue JSON::ParserError, SystemCallError
|
|
135
138
|
unreadable += 1
|
|
136
139
|
nil
|
|
@@ -138,9 +141,9 @@ module Samagotchi
|
|
|
138
141
|
dir = SessionPaths.session_dir(session_id)
|
|
139
142
|
analytics = AnalyticsFile.read(dir)
|
|
140
143
|
unreadable += 1 if analytics.nil? && File.exist?(AnalyticsFile.path(dir))
|
|
141
|
-
return none.with(unreadable: unreadable) unless
|
|
144
|
+
return none.with(unreadable: unreadable) unless messages && analytics
|
|
142
145
|
|
|
143
|
-
Files.new(messages: hashes(
|
|
146
|
+
Files.new(messages: hashes(messages), turn_records: hashes(analytics["turn_records"]),
|
|
144
147
|
tool_records: hashes(analytics["tool_records"]), unreadable: unreadable)
|
|
145
148
|
end
|
|
146
149
|
|
data/lib/samagotchi/turn_flow.rb
CHANGED
|
@@ -178,7 +178,7 @@ module Samagotchi
|
|
|
178
178
|
# after a Ctrl-C or a new prompt (!rollback still erases it), and the
|
|
179
179
|
# model reads that it wasn't continued: a note, or the user's reason
|
|
180
180
|
# (with a summary of that turn). The reason's line opens a turn on the
|
|
181
|
-
# page (
|
|
181
|
+
# page (Speaker.turn_opening?) that no turn runs: it carries an
|
|
182
182
|
# +opening_id+ of its own, never a turn_id (a claim link, a fold, Revise
|
|
183
183
|
# and Apply read a turn_id as a turn), so a card shown after it finds its
|
|
184
184
|
# place by that id (Bridge::CardPlaces#opening_of).
|
data/lib/samagotchi/turn_note.rb
CHANGED
|
@@ -27,6 +27,14 @@ module Samagotchi
|
|
|
27
27
|
TASK_COMMAND_CHARS = 60
|
|
28
28
|
RETRY_NUDGE = :retry_nudge
|
|
29
29
|
EMPTY_ANSWER = :empty_answer
|
|
30
|
+
# A failed-turn note's marker keys (#failed, #failed_after): +failed+ its
|
|
31
|
+
# failure summary, +kept+ the tool steps its work kept (failed_after
|
|
32
|
+
# only). The readers (#failure, #failure_summary, Bridge::TurnRanges)
|
|
33
|
+
# read them, the words only for a note an older chi wrote. Display only,
|
|
34
|
+
# never sent (AnswerDisplay::DISPLAY_ONLY; the payload builders send a
|
|
35
|
+
# system message's content): the model's text is unchanged.
|
|
36
|
+
FAILED_KEY = :failed
|
|
37
|
+
KEPT_KEY = :kept
|
|
30
38
|
# An empty step's thinking kept on the marker: its tail, so a model
|
|
31
39
|
# that thought for minutes doesn't put megabytes in the session file.
|
|
32
40
|
# The same cap as ThinkingTails' saved tails.
|
|
@@ -59,7 +67,7 @@ module Samagotchi
|
|
|
59
67
|
elsif continued then "The continued turn stopped there."
|
|
60
68
|
else "The user's last message was not answered."
|
|
61
69
|
end
|
|
62
|
-
message("#{FAILED}#{one_line(summary)}. #{tail}")
|
|
70
|
+
message("#{FAILED}#{one_line(summary)}. #{tail}").merge(FAILED_KEY => one_line(summary))
|
|
63
71
|
end
|
|
64
72
|
|
|
65
73
|
# A turn that failed partway, its work kept: "the previous turn failed
|
|
@@ -74,16 +82,36 @@ module Samagotchi
|
|
|
74
82
|
"the user's last message is not answered yet."
|
|
75
83
|
end
|
|
76
84
|
message("#{FAILED_AFTER}#{count}: #{one_line(summary)}#{KEPT_WORK}#{tail}")
|
|
85
|
+
.merge(FAILED_KEY => one_line(summary), KEPT_KEY => steps)
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
# A failed-turn note's marker (#failed, #failed_after: either key type)
|
|
89
|
+
# as {summary:, kept:} (kept: the steps its work kept, nil for one that
|
|
90
|
+
# kept nothing), or nil: not a note, or one an older chi wrote without
|
|
91
|
+
# the marker (its words are the fallback, #failure_summary).
|
|
92
|
+
# @return [Hash, nil]
|
|
93
|
+
def failure(entry)
|
|
94
|
+
return nil unless note?(entry)
|
|
95
|
+
|
|
96
|
+
summary = entry[FAILED_KEY] || entry[FAILED_KEY.to_s]
|
|
97
|
+
return nil unless summary.is_a?(String)
|
|
98
|
+
|
|
99
|
+
kept = entry[KEPT_KEY] || entry[KEPT_KEY.to_s]
|
|
100
|
+
{ summary: summary, kept: kept.is_a?(Integer) ? kept : nil }
|
|
77
101
|
end
|
|
78
102
|
|
|
79
103
|
# The failure summary of the failed-turn note at the tail of +messages+
|
|
80
104
|
# (behind context notes at most), either kind (rolled back, or its work
|
|
81
|
-
# kept), or nil without one
|
|
105
|
+
# kept), or nil without one: its marker (#failure), the words for an
|
|
106
|
+
# older chi's note.
|
|
82
107
|
def failure_summary(messages)
|
|
83
108
|
list = Array(messages)
|
|
84
109
|
index = trailing_index(list)
|
|
85
110
|
return nil unless index
|
|
86
111
|
|
|
112
|
+
marked = failure(list[index])
|
|
113
|
+
return marked[:summary] if marked
|
|
114
|
+
|
|
87
115
|
text = (list[index][:content] || list[index]["content"]).to_s
|
|
88
116
|
if text.start_with?("#{OPEN}#{FAILED}")
|
|
89
117
|
text["#{OPEN}#{FAILED}".length..].sub(/\. [^.]*\.\]\z/, "")
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "client_id"
|
|
4
|
-
require_relative "
|
|
4
|
+
require_relative "speaker"
|
|
5
5
|
|
|
6
6
|
module Samagotchi
|
|
7
7
|
# Who queued a turn: the client that sent it (+client_id+, ClientId), the
|
|
@@ -40,16 +40,16 @@ module Samagotchi
|
|
|
40
40
|
# @return [Hash, nil]
|
|
41
41
|
def wire = empty? ? nil : to_h
|
|
42
42
|
|
|
43
|
-
# Whether a participant queued the turn (
|
|
43
|
+
# Whether a participant queued the turn (Speaker.participant_author?: any
|
|
44
44
|
# author but none or the owner's).
|
|
45
|
-
def participant? =
|
|
45
|
+
def participant? = Speaker.participant_author?(author)
|
|
46
46
|
|
|
47
|
-
# The author when a participant's (
|
|
48
|
-
def participant_author =
|
|
47
|
+
# The author when a participant's (Speaker.sender_author), else nil.
|
|
48
|
+
def participant_author = Speaker.sender_author(author)
|
|
49
49
|
|
|
50
|
-
# The participant's id (
|
|
50
|
+
# The participant's id (Speaker.participant_id: its author without "p:"),
|
|
51
51
|
# nil for the owner's.
|
|
52
|
-
def participant_id = participant_author &&
|
|
52
|
+
def participant_id = participant_author && Speaker.participant_id(participant_author)
|
|
53
53
|
|
|
54
54
|
# What sent the turn, by its client (ClientId.origin): "client",
|
|
55
55
|
# "reminder", "delegate", "participant"...
|
|
@@ -37,5 +37,16 @@ module Samagotchi
|
|
|
37
37
|
reason = [turn.outcome, turn.error_kind, turn.cancel_reason, turn.stopped_by].compact.join(": ")
|
|
38
38
|
"turn #{reason.empty? ? "did not complete" : reason}"
|
|
39
39
|
end
|
|
40
|
+
|
|
41
|
+
# A settled turn that didn't complete, as a code: its finer error code
|
|
42
|
+
# (LastTurn#error_reason), else its error kind, else its cancel reason;
|
|
43
|
+
# nil for a reply, a completed turn, one still running or one that says
|
|
44
|
+
# none.
|
|
45
|
+
# @return [String, nil]
|
|
46
|
+
def code
|
|
47
|
+
return nil if replied || turn.nil? || completed?
|
|
48
|
+
|
|
49
|
+
[turn.error_reason, turn.error_kind, turn.cancel_reason].compact.first&.to_s
|
|
50
|
+
end
|
|
40
51
|
end
|
|
41
52
|
end
|
|
@@ -8,7 +8,7 @@ require_relative "../participant_revise"
|
|
|
8
8
|
require_relative "../redact"
|
|
9
9
|
require_relative "../revise_anchor"
|
|
10
10
|
require_relative "../session"
|
|
11
|
-
require_relative "../
|
|
11
|
+
require_relative "../speaker"
|
|
12
12
|
require_relative "../verdict"
|
|
13
13
|
require_relative "../verify_anchor"
|
|
14
14
|
|
|
@@ -61,7 +61,7 @@ module Samagotchi
|
|
|
61
61
|
return if fields.empty?
|
|
62
62
|
|
|
63
63
|
opening = messages.reverse.find do |message|
|
|
64
|
-
|
|
64
|
+
Speaker.turn_prompt?(message) && (message[:turn_id] || message["turn_id"]).to_s == turn.id
|
|
65
65
|
end
|
|
66
66
|
return unless opening
|
|
67
67
|
|
|
@@ -105,7 +105,7 @@ module Samagotchi
|
|
|
105
105
|
|
|
106
106
|
target = turn.revises.turn_id
|
|
107
107
|
author = turn.origin&.author
|
|
108
|
-
if
|
|
108
|
+
if Speaker.participant_author?(author) &&
|
|
109
109
|
(refused = ParticipantRevise.refusal(messages, turn_id: target, author: author, before: turn.id))
|
|
110
110
|
return reject_revise(turn.revises, refused)
|
|
111
111
|
end
|
|
@@ -116,7 +116,7 @@ module Samagotchi
|
|
|
116
116
|
|
|
117
117
|
seen << target
|
|
118
118
|
newer = messages.reverse.find do |message|
|
|
119
|
-
|
|
119
|
+
Speaker.turn_prompt?(message) && ReviseAnchor.from(message[:revises] || message["revises"])&.turn_id == target
|
|
120
120
|
end
|
|
121
121
|
return ReviseAnchor.new(turn_id: target) unless newer
|
|
122
122
|
|