samagotchi 0.172.1 → 0.174.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 +61 -1
- data/docs/architecture.md +16 -5
- data/docs/cli.md +11 -3
- data/docs/configuration.md +1 -1
- data/docs/internals/background-tasks.md +16 -6
- data/docs/internals/bridge.md +2 -2
- data/docs/internals/engine.md +2 -1
- data/docs/internals/participants.md +69 -9
- data/docs/internals/remote-sessions.md +9 -1
- data/docs/internals/web-frontend.md +34 -8
- data/docs/plugins.md +1 -1
- data/docs/scheduling.md +9 -3
- data/docs/sessions.md +5 -3
- data/docs/testing.md +32 -10
- data/lib/samagotchi/bridge.rb +4 -3
- data/lib/samagotchi/bundle_command/try/sandbox.rb +2 -2
- data/lib/samagotchi/bundles/core/manifest.yml +2 -2
- data/lib/samagotchi/bundles/slow-commands/manifest.yml +3 -3
- data/lib/samagotchi/bundles/slow-commands/plugin.rb +3 -3
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/child_move.rb +11 -4
- data/lib/samagotchi/continued_from.rb +51 -0
- data/lib/samagotchi/engine/participants.rb +2 -2
- data/lib/samagotchi/engine.rb +1 -2
- data/lib/samagotchi/file_ref.rb +3 -5
- data/lib/samagotchi/guardrails/participant_refusal.rb +1 -1
- data/lib/samagotchi/memory_usage.rb +2 -1
- data/lib/samagotchi/message_parts.rb +5 -3
- data/lib/samagotchi/note_move.rb +11 -4
- data/lib/samagotchi/objectives_backing.rb +6 -10
- data/lib/samagotchi/parent_report.rb +1 -1
- data/lib/samagotchi/participant_prompts.rb +101 -0
- data/lib/samagotchi/participant_turn_mode.rb +1 -1
- data/lib/samagotchi/project_env.rb +7 -5
- data/lib/samagotchi/prompt_history.rb +2 -2
- data/lib/samagotchi/schedules.rb +13 -16
- data/lib/samagotchi/send_command/parent_gate.rb +2 -2
- data/lib/samagotchi/send_command.rb +4 -4
- data/lib/samagotchi/session_access/contract.rb +1 -0
- data/lib/samagotchi/session_access/local.rb +9 -1
- data/lib/samagotchi/session_access/remote.rb +26 -3
- data/lib/samagotchi/session_chain/delete.rb +405 -0
- data/lib/samagotchi/session_chain/delete_preview.rb +96 -0
- data/lib/samagotchi/session_chain/links.rb +38 -0
- data/lib/samagotchi/session_command_catalog.rb +210 -0
- data/lib/samagotchi/session_commands.rb +6 -187
- data/lib/samagotchi/session_continue.rb +51 -13
- data/lib/samagotchi/session_delete_command.rb +82 -17
- data/lib/samagotchi/session_manager/lifecycle.rb +5 -4
- data/lib/samagotchi/session_manager/removal.rb +137 -31
- data/lib/samagotchi/session_manager.rb +2 -1
- data/lib/samagotchi/session_stores.rb +4 -0
- data/lib/samagotchi/spawn_env.rb +75 -0
- data/lib/samagotchi/task_files.rb +51 -0
- data/lib/samagotchi/task_transcript.rb +67 -0
- data/lib/samagotchi/task_watch.rb +8 -6
- data/lib/samagotchi/terminal_ui/attached_loop/composer.rb +3 -3
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +2 -2
- data/lib/samagotchi/terminal_ui/attached_loop/questions.rb +2 -2
- data/lib/samagotchi/terminal_ui/attached_loop.rb +4 -4
- data/lib/samagotchi/terminal_ui/exit_lines.rb +16 -0
- data/lib/samagotchi/terminal_ui/input_support.rb +1 -1
- data/lib/samagotchi/terminal_ui/local_link.rb +7 -1
- data/lib/samagotchi/terminal_ui.rb +3 -3
- data/lib/samagotchi/testing/boundary.rb +2 -2
- data/lib/samagotchi/testing/bundle.rb +1 -1
- data/lib/samagotchi/tool_activity.rb +63 -79
- data/lib/samagotchi/tool_names.rb +179 -0
- data/lib/samagotchi/tool_view.rb +3 -5
- data/lib/samagotchi/tools/ask_user_question.rb +2 -1
- data/lib/samagotchi/tools/builtins.rb +1 -1
- data/lib/samagotchi/tools/cancel_reminder.rb +2 -1
- data/lib/samagotchi/tools/cancel_schedule.rb +3 -1
- data/lib/samagotchi/tools/context_read.rb +2 -1
- data/lib/samagotchi/tools/delegate.rb +2 -1
- data/lib/samagotchi/tools/delegate_result.rb +2 -1
- data/lib/samagotchi/tools/edit.rb +2 -1
- data/lib/samagotchi/tools/execute.rb +22 -28
- data/lib/samagotchi/tools/forget_outputs.rb +2 -1
- data/lib/samagotchi/tools/list_reminders.rb +2 -1
- data/lib/samagotchi/tools/list_schedules.rb +3 -1
- data/lib/samagotchi/tools/list_sessions.rb +2 -1
- data/lib/samagotchi/tools/memory.rb +3 -2
- data/lib/samagotchi/tools/objectives_tool.rb +5 -4
- data/lib/samagotchi/tools/read.rb +2 -1
- data/lib/samagotchi/tools/register_reminder.rb +2 -1
- data/lib/samagotchi/tools/schedule_task.rb +3 -1
- data/lib/samagotchi/tools/send_note.rb +2 -1
- data/lib/samagotchi/tools/task_create.rb +2 -1
- data/lib/samagotchi/tools/task_get.rb +2 -1
- data/lib/samagotchi/tools/task_list.rb +2 -1
- data/lib/samagotchi/tools/task_runtime.rb +29 -154
- data/lib/samagotchi/tools/task_stop.rb +2 -1
- data/lib/samagotchi/tools/task_wait.rb +3 -2
- data/lib/samagotchi/tools/web_fetch.rb +2 -1
- data/lib/samagotchi/tools/write.rb +2 -1
- data/lib/samagotchi/turn_checkpoint.rb +1 -1
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +45 -37
- data/lib/samagotchi/web/message_display.rb +1 -7
- data/lib/samagotchi/web/participant_access.rb +12 -4
- data/lib/samagotchi/web/participant_history.rb +68 -0
- data/lib/samagotchi/web/participant_images.rb +2 -1
- data/lib/samagotchi/web/participant_show.rb +5 -5
- data/lib/samagotchi/web/participants_admin.rb +1 -1
- data/lib/samagotchi/web/public/app.js +24 -12
- data/lib/samagotchi/web/public/card_registry.js +248 -0
- data/lib/samagotchi/web/public/cards_offscreen.js +3 -3
- data/lib/samagotchi/web/public/cards_place.js +8 -2
- data/lib/samagotchi/web/public/chains.js +3 -3
- data/lib/samagotchi/web/public/data.js +12 -0
- data/lib/samagotchi/web/public/delete_sheet.js +344 -0
- data/lib/samagotchi/web/public/format.js +1 -1
- data/lib/samagotchi/web/public/history_shape.js +1 -2
- data/lib/samagotchi/web/public/index.html +27 -0
- data/lib/samagotchi/web/public/info_bar.js +3 -2
- data/lib/samagotchi/web/public/lists_view.js +2 -2
- data/lib/samagotchi/web/public/more_sheet.js +42 -5
- data/lib/samagotchi/web/public/participant_history.js +27 -0
- data/lib/samagotchi/web/public/question_cards.js +1 -15
- data/lib/samagotchi/web/public/reload_check.js +90 -72
- data/lib/samagotchi/web/public/session_actions.js +50 -6
- data/lib/samagotchi/web/public/session_view.js +6 -1
- data/lib/samagotchi/web/public/stage_asking.js +6 -20
- data/lib/samagotchi/web/public/stage_view.js +2 -2
- data/lib/samagotchi/web/public/stage_waiting.js +4 -10
- data/lib/samagotchi/web/public/task_stop.js +1 -1
- data/lib/samagotchi/web/push_routes.rb +1 -1
- data/lib/samagotchi/web/reload_check_route.rb +5 -2
- data/lib/samagotchi/web/request_body.rb +72 -0
- data/lib/samagotchi/web/server.rb +70 -0
- data/lib/samagotchi/web/session_access_routes.rb +13 -11
- data/lib/samagotchi/web/session_control.rb +79 -20
- data/lib/samagotchi/web/session_create.rb +2 -1
- data/lib/samagotchi/web/session_show.rb +8 -8
- data/lib/samagotchi/web/viewer.rb +7 -2
- data/lib/samagotchi/web/working_set_routes.rb +3 -3
- data/lib/samagotchi/worker.rb +49 -182
- data/lib/samagotchi/worker_exit_claim.rb +102 -0
- data/lib/samagotchi/worker_participant_gate.rb +156 -0
- data/lib/samagotchi/worker_task_wakes.rb +2 -2
- metadata +17 -1
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "turn_flow"
|
|
4
|
+
require_relative "commands/registry"
|
|
5
|
+
require_relative "llm_context_command"
|
|
6
|
+
require_relative "thinking_command"
|
|
7
|
+
require_relative "toolkit_command"
|
|
8
|
+
require_relative "parent_answers_command"
|
|
9
|
+
require_relative "title_command"
|
|
10
|
+
require_relative "reasoning_history_command"
|
|
11
|
+
require_relative "working_set_command"
|
|
12
|
+
require_relative "recap_command"
|
|
13
|
+
|
|
14
|
+
module Samagotchi
|
|
15
|
+
# The session commands' catalogue: their names, the registry of the
|
|
16
|
+
# built-ins (SessionCommands' handlers, which #run instance_execs on a
|
|
17
|
+
# SessionCommands, so the blocks name methods only the runner has) and
|
|
18
|
+
# the class-level reads the UIs make of it (builtin_name, ui_only_entry,
|
|
19
|
+
# delete_on_exit?…). No Engine, no tool: the attached TUI, chi web,
|
|
20
|
+
# chi send and the prompt history require this file, not
|
|
21
|
+
# session_commands (which loads tools/execute and tools/builtins for
|
|
22
|
+
# !cmd and /context). Every caller of the class methods names this module
|
|
23
|
+
# directly; SessionCommands only includes it, for its constants.
|
|
24
|
+
module SessionCommandCatalog
|
|
25
|
+
MODEL_COMMAND = "/model"
|
|
26
|
+
MODELS_COMMAND = "/models"
|
|
27
|
+
GUARDRAILS_COMMAND = "/guardrails"
|
|
28
|
+
CONTEXT_COMMAND = "/context"
|
|
29
|
+
HELP_COMMAND = "/help"
|
|
30
|
+
LLM_CONTEXT_COMMAND = LLMContextCommand::NAME
|
|
31
|
+
THINKING_COMMAND = ThinkingCommand::NAME
|
|
32
|
+
TOOLKIT_COMMAND = ToolkitCommand::NAME
|
|
33
|
+
PARENT_ANSWERS_COMMAND = ParentAnswersCommand::NAME
|
|
34
|
+
TITLE_COMMAND = TitleCommand::NAME
|
|
35
|
+
REASONING_HISTORY_COMMAND = ReasoningHistoryCommand::NAME
|
|
36
|
+
WORKING_SET_COMMAND = WorkingSetCommand::NAME
|
|
37
|
+
RECAP_COMMAND = RecapCommand::NAME
|
|
38
|
+
# A literal: ObjectivesCommand loads on first use (SessionCommands autoloads it).
|
|
39
|
+
OBJECTIVES_COMMAND = "/objectives"
|
|
40
|
+
ROLLBACK_COMMAND = "!rollback"
|
|
41
|
+
CONTINUE_COMMAND = TurnFlow::CONTINUE_COMMAND
|
|
42
|
+
SHELL_BANG_PREFIX = "!"
|
|
43
|
+
# /exit --delete: delete the session on the way out.
|
|
44
|
+
EXIT_DELETE_FLAG = "--delete"
|
|
45
|
+
|
|
46
|
+
# Puts the built-in commands into +registry+, in lookup order (!rollback
|
|
47
|
+
# before !cmd): the ones #run runs, then the UIs' own (local: Tab and
|
|
48
|
+
# help only).
|
|
49
|
+
#
|
|
50
|
+
# While a turn runs, a show form (the name alone) runs at once beside
|
|
51
|
+
# it (it only reads); setting forms and !cmd run after the turn; !rollback
|
|
52
|
+
# and /continue are refused (Commands::Registry::MID_TURN).
|
|
53
|
+
def self.register_builtins(registry)
|
|
54
|
+
registry.register(ROLLBACK_COMMAND, "discard the interrupted turn and restore the pre-turn state",
|
|
55
|
+
id: :rollback) { |_text| rollback }
|
|
56
|
+
registry.register("!", "run a shell command; its output goes into the conversation",
|
|
57
|
+
id: :shell, mid_turn: :queue, match: ->(text) { text.match?(/\A!\s*\S/) }) { |text| shell(text) }
|
|
58
|
+
# #run passes who sent it (as_parent:) per call.
|
|
59
|
+
registry.register(CONTINUE_COMMAND, "answer the continue offer (yes, no or no, <reason>)") { |text| continue_command(text) }
|
|
60
|
+
# It reads the hosts only (the registry is mutex-guarded): mid-turn too.
|
|
61
|
+
registry.register(MODELS_COMMAND, "list the hosts' models (/models <text> filters)", anytime: true) do |text|
|
|
62
|
+
reply(models_listing(text.delete_prefix(MODELS_COMMAND).strip))
|
|
63
|
+
end
|
|
64
|
+
registry.register(GUARDRAILS_COMMAND, "list guardrails, revoke an approval or flag a System 1 auto-allow",
|
|
65
|
+
mid_turn: show_mid_turn(GUARDRAILS_COMMAND)) do |text|
|
|
66
|
+
guardrails(text.delete_prefix(GUARDRAILS_COMMAND).strip)
|
|
67
|
+
end
|
|
68
|
+
# The list only reads the store; attach and decline only write it (the
|
|
69
|
+
# note waits for the next between-turn delivery): mid-turn too.
|
|
70
|
+
registry.register(CONTEXT_COMMAND, "list the attached context and its candidates (chi context); attach NAME, " \
|
|
71
|
+
"decline NAME: a candidate", anytime: true,
|
|
72
|
+
match: ->(text) { text.match?(%r{\A/context(?:\s+.*)?\z}i) }) do |text|
|
|
73
|
+
context_command(text.strip.sub(%r{\A/context}i, "").strip)
|
|
74
|
+
end
|
|
75
|
+
registry.register(MODEL_COMMAND, "show or switch the model", mid_turn: show_mid_turn(MODEL_COMMAND),
|
|
76
|
+
match: ->(text) { text.match?(%r{\A/model(?:\s+.*)?\z}) }) { |text| model(text) }
|
|
77
|
+
# Setting it saves the session; it takes effect at the next turn's start.
|
|
78
|
+
registry.register(LLM_CONTEXT_COMMAND, "show or set this session's LLM context strategy, apply rule and budget",
|
|
79
|
+
id: :llm_context, mid_turn: show_mid_turn(LLM_CONTEXT_COMMAND), match: ->(text) { text.match?(%r{\A/llm-context(?:\s+.*)?\z}) }) { |text| llm_context(text) }
|
|
80
|
+
registry.register(THINKING_COMMAND, "show or set this session's thinking level (off|low|medium|high|default)",
|
|
81
|
+
id: :thinking, mid_turn: show_mid_turn(THINKING_COMMAND), match: ->(text) { text.match?(%r{\A/thinking(?:\s+.*)?\z}) }) { |text| thinking(text) }
|
|
82
|
+
registry.register(REASONING_HISTORY_COMMAND, "show or set which of its reasoning this session's model gets back (drop|turn|all|default)",
|
|
83
|
+
id: :reasoning_history, mid_turn: show_mid_turn(REASONING_HISTORY_COMMAND),
|
|
84
|
+
match: ->(text) { text.match?(%r{\A/reasoning-history(?:\s+.*)?\z}) }) { |text| reasoning_history(text) }
|
|
85
|
+
registry.register(TOOLKIT_COMMAND, "show or change the tools this session declares (NAME, add TOOL, remove TOOL)",
|
|
86
|
+
id: :toolkit, mid_turn: show_mid_turn(TOOLKIT_COMMAND),
|
|
87
|
+
match: ->(text) { text.match?(%r{\A/toolkit(?:\s+.*)?\z}) }) { |text| toolkit(text) }
|
|
88
|
+
# The list reads only: mid-turn too; an op waits for the turn's end
|
|
89
|
+
# (the next turn's first request is the first with it).
|
|
90
|
+
registry.register(WORKING_SET_COMMAND, "list the working set; drop, undrop, pin or unpin an item (drop KEY…)",
|
|
91
|
+
id: :working_set, mid_turn: show_mid_turn(WORKING_SET_COMMAND),
|
|
92
|
+
match: ->(text) { text.match?(%r{\A/working-set(?:\s+.*)?\z}) }) { |text| working_set(text) }
|
|
93
|
+
# Tighten-only from anyone; loosening is a user's at a UI (#run's user_ui:).
|
|
94
|
+
registry.register(PARENT_ANSWERS_COMMAND, "show or set what a parent agent may answer here (hard|standard|soft|default; " \
|
|
95
|
+
"never looser than config.yml's)",
|
|
96
|
+
id: :parent_answers, mid_turn: show_mid_turn(PARENT_ANSWERS_COMMAND),
|
|
97
|
+
match: ->(text) { text.match?(%r{\A/parent-answers(?:\s+.*)?\z}) }) { |text| parent_answers(text) }
|
|
98
|
+
# Only a name: renaming a running session doesn't wait for its turn.
|
|
99
|
+
registry.register(TITLE_COMMAND, "show or set this session's title (--clear: back to its first message)",
|
|
100
|
+
id: :title, anytime: true, match: ->(text) { text.match?(%r{\A/title(?:\s+.*)?\z}) }) { |text| title(text) }
|
|
101
|
+
# An edit only writes a note file (it goes in between turns): mid-turn too.
|
|
102
|
+
registry.register(OBJECTIVES_COMMAND, "show or edit what this session is to achieve (set A | B, add TEXT, edit ID TEXT, done ID…, drop ID…, undo ID…, check ID, track)",
|
|
103
|
+
id: :objectives, anytime: true) { |text| objectives(text) }
|
|
104
|
+
registry.register(HELP_COMMAND, "list the commands, the bundles' too", anytime: true) { |_text| reply(help_listing) }
|
|
105
|
+
registry.register("/stats", "show the session's stats", local: true)
|
|
106
|
+
# /recap with words (on|off|default, or a slip: the usage) runs here:
|
|
107
|
+
# it toggles the idle job only, so it doesn't wait for a running turn.
|
|
108
|
+
# The bare /recap shows the recap: the UIs' own, under the same name
|
|
109
|
+
# (Registry#register); /help lists the name once, with this entry's
|
|
110
|
+
# description.
|
|
111
|
+
registry.register(RECAP_COMMAND, "show the session's recap (in a terminal); on|off|default sets this session's own",
|
|
112
|
+
id: :recap_setting, anytime: true, match: ->(text) { text.match?(%r{\A/recap\s+\S}) }) { |text| recap_setting(text) }
|
|
113
|
+
registry.register(RECAP_COMMAND, "show the session's recap", local: true, match: ->(text) { text == RECAP_COMMAND })
|
|
114
|
+
# Bare `exit` too; any case; --delete after it.
|
|
115
|
+
registry.register("/exit", "leave (--delete also deletes the session)", local: true,
|
|
116
|
+
match: exit_match("/?exit"))
|
|
117
|
+
registry.register("/quit", "leave, like /exit", id: :exit, local: true, match: exit_match("/quit"))
|
|
118
|
+
registry.register("/archive", "leave and archive the session: hidden from the lists, kept for good", local: true,
|
|
119
|
+
match: ->(text) { text.casecmp?("/archive") })
|
|
120
|
+
# The terminal's: the web has no worker to leave running.
|
|
121
|
+
registry.register("/detach", "leave and keep the worker running", local: true, uis: [:attached],
|
|
122
|
+
match: ->(text) { text.casecmp?("/detach") })
|
|
123
|
+
# /cut TEXT and /queue TEXT while a turn runs: a delivery the UIs parse
|
|
124
|
+
# themselves (ComposerRoute.steer_choice) before any command check; the
|
|
125
|
+
# entries exist for Tab completion and /help. The web composer sends
|
|
126
|
+
# /cut too (its queue is Alt+Enter). The attached TUI queues in its
|
|
127
|
+
# worker's input files.
|
|
128
|
+
registry.register("/cut", "cut the running generation and go in now (/cut TEXT)",
|
|
129
|
+
local: true, uis: %i[attached web], match: ->(text) { text.casecmp?("/cut") || text.match?(%r{\A/cut\s+\S}) })
|
|
130
|
+
registry.register("/queue", "run as the next turn after this one (/queue TEXT)",
|
|
131
|
+
local: true, uis: [:attached], match: ->(text) { text.casecmp?("/queue") || text.match?(%r{\A/queue\s+\S}) })
|
|
132
|
+
# Takes back the newest message this attached TUI queued, before it
|
|
133
|
+
# runs (the worker's POST turn/dequeue); the web has × on the bubble.
|
|
134
|
+
registry.register("/unqueue", "remove your newest queued message before it runs", local: true, uis: [:attached],
|
|
135
|
+
match: ->(text) { text.casecmp?("/unqueue") })
|
|
136
|
+
# chi share for the attached session (TerminalUI::ParticipantCommands):
|
|
137
|
+
# a participant's turns run on its worker.
|
|
138
|
+
registry.register("/share", "invite someone into this session (/share NAME [--expires 3d] [--cap 2])",
|
|
139
|
+
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/share(?:\s.*)?\z}i) })
|
|
140
|
+
registry.register("/participants", "list this session's participants (revoke [P], cap P DOLLARS)",
|
|
141
|
+
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/participants(?:\s.*)?\z}i) })
|
|
142
|
+
registry
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# @return [#call] line → :anytime for +name+ alone (it shows), else
|
|
146
|
+
# :queue (it sets what the next turn runs under)
|
|
147
|
+
def self.show_mid_turn(name)
|
|
148
|
+
->(text) { text == name ? :anytime : :queue }
|
|
149
|
+
end
|
|
150
|
+
private_class_method :show_mid_turn
|
|
151
|
+
|
|
152
|
+
# @param word [String] the regexp source of the exit word
|
|
153
|
+
def self.exit_match(word)
|
|
154
|
+
pattern = /\A#{word}(?:\s+#{EXIT_DELETE_FLAG})?\z/i
|
|
155
|
+
->(text) { text.match?(pattern) }
|
|
156
|
+
end
|
|
157
|
+
private_class_method :exit_match
|
|
158
|
+
|
|
159
|
+
# @return [Boolean] an exit line (/exit, /quit, exit) that deletes the session too
|
|
160
|
+
def self.delete_on_exit?(line) = line.to_s.split.last.to_s.casecmp?(EXIT_DELETE_FLAG)
|
|
161
|
+
|
|
162
|
+
# The built-ins alone, for callers without an Engine (an attached TUI
|
|
163
|
+
# before its snapshot names the session's commands, specs).
|
|
164
|
+
# @return [Commands::Registry] frozen
|
|
165
|
+
def self.builtin_registry
|
|
166
|
+
@builtin_registry ||= register_builtins(Commands::Registry.new).freeze
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# The UI-only built-in command +text+ is a line of (/stats, /exit, …), or
|
|
170
|
+
# nil: neither a worker nor a one-shot run (chi send, chi -p
|
|
171
|
+
# --non-interactive) runs it, so they refuse it rather than send it to
|
|
172
|
+
# the model as a prompt. What the UIs run: the line as the entry's match
|
|
173
|
+
# takes it — bare "exit" too (/exit's match), but not with arguments
|
|
174
|
+
# ("/exit --delete" is a UI line; "/stats please" is a prompt), as in a
|
|
175
|
+
# terminal.
|
|
176
|
+
# @return [Commands::Registry::Entry, nil]
|
|
177
|
+
def self.ui_only_entry(text)
|
|
178
|
+
line = text.to_s.strip
|
|
179
|
+
return nil unless line.start_with?("/", "!") || line.match?(/\Aexit\z/i)
|
|
180
|
+
|
|
181
|
+
entry = builtin_registry.lookup_local(line)
|
|
182
|
+
return nil unless entry
|
|
183
|
+
return entry if line == entry.name
|
|
184
|
+
|
|
185
|
+
line.match?(/\Aexit\z/i) ? entry : nil
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# Why a UI-only command isn't run by +runner+ ("chi send"): /stats points
|
|
189
|
+
# at its model-free reader (naming the session +id+ when there is one),
|
|
190
|
+
# which reads the session's machine: under chi --remote NAME (+remote+)
|
|
191
|
+
# it runs there.
|
|
192
|
+
# @return [String]
|
|
193
|
+
def self.ui_only_refusal(name, id, runner:, remote: nil)
|
|
194
|
+
return "#{name} is a terminal and web command; #{runner} doesn't run it" unless name == "/stats"
|
|
195
|
+
|
|
196
|
+
"#{name} is a terminal and web command; use chi sessions stats #{id ? id[0, 8] : "ID"}#{" on #{remote}" if remote}"
|
|
197
|
+
end
|
|
198
|
+
|
|
199
|
+
# The name of this chi's own session command +line+ is (one a worker
|
|
200
|
+
# runs), or nil. A worker that answers such a line unknown_command runs
|
|
201
|
+
# an older chi that doesn't have it yet (/llm-context against 0.39.0):
|
|
202
|
+
# the UIs say so (BridgeClient.stale_worker_message) instead of "not a
|
|
203
|
+
# session command".
|
|
204
|
+
# @return [String, nil]
|
|
205
|
+
def self.builtin_name(line) = builtin_registry.lookup(line)&.name
|
|
206
|
+
|
|
207
|
+
# How help and errors name an entry (the shell's "!" is "!<cmd>").
|
|
208
|
+
def self.display_name(entry) = entry.name == SHELL_BANG_PREFIX ? "#{SHELL_BANG_PREFIX}<cmd>" : entry.name
|
|
209
|
+
end
|
|
210
|
+
end
|
|
@@ -14,6 +14,7 @@ require_relative "context_candidates"
|
|
|
14
14
|
require_relative "tools/context_read"
|
|
15
15
|
require_relative "tools/peers"
|
|
16
16
|
require_relative "commands/registry"
|
|
17
|
+
require_relative "session_command_catalog"
|
|
17
18
|
require_relative "llm_context_command"
|
|
18
19
|
require_relative "thinking_command"
|
|
19
20
|
require_relative "toolkit_command"
|
|
@@ -42,30 +43,12 @@ module Samagotchi
|
|
|
42
43
|
# both TUIs read the same words (Registry#lookup_local gives the entry,
|
|
43
44
|
# its id says what to do), but #run never runs them.
|
|
44
45
|
class SessionCommands
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
GUARDRAILS_COMMAND = "/guardrails"
|
|
48
|
-
CONTEXT_COMMAND = "/context"
|
|
49
|
-
HELP_COMMAND = "/help"
|
|
50
|
-
LLM_CONTEXT_COMMAND = LLMContextCommand::NAME
|
|
51
|
-
THINKING_COMMAND = ThinkingCommand::NAME
|
|
52
|
-
TOOLKIT_COMMAND = ToolkitCommand::NAME
|
|
53
|
-
PARENT_ANSWERS_COMMAND = ParentAnswersCommand::NAME
|
|
54
|
-
TITLE_COMMAND = TitleCommand::NAME
|
|
55
|
-
REASONING_HISTORY_COMMAND = ReasoningHistoryCommand::NAME
|
|
56
|
-
WORKING_SET_COMMAND = WorkingSetCommand::NAME
|
|
57
|
-
RECAP_COMMAND = RecapCommand::NAME
|
|
58
|
-
# A literal: ObjectivesCommand loads on first use (the autoload above).
|
|
59
|
-
OBJECTIVES_COMMAND = "/objectives"
|
|
46
|
+
include SessionCommandCatalog
|
|
47
|
+
|
|
60
48
|
# A remote catalog has hundreds of ids (OpenRouter ~380): plain /models
|
|
61
49
|
# shows this many per host; /models <text> lists every match.
|
|
62
50
|
MODELS_PER_HOST = 20
|
|
63
|
-
ROLLBACK_COMMAND = "!rollback"
|
|
64
|
-
CONTINUE_COMMAND = TurnFlow::CONTINUE_COMMAND
|
|
65
|
-
SHELL_BANG_PREFIX = "!"
|
|
66
51
|
ALIAS_USAGE = "usage /model <model> --alias <name> [--default]"
|
|
67
|
-
# /exit --delete: delete the session on the way out.
|
|
68
|
-
EXIT_DELETE_FLAG = "--delete"
|
|
69
52
|
|
|
70
53
|
# /help's note per listed mid_turn (the web's command list says the same).
|
|
71
54
|
MID_TURN_NOTES = { "anytime" => "mid-turn too", "depends" => "show: mid-turn too" }.freeze
|
|
@@ -86,167 +69,6 @@ module Samagotchi
|
|
|
86
69
|
# first (nothing changed): the line that applies it
|
|
87
70
|
Result = Struct.new(:status, :output, :changed, :model_name, :resume, :shell, :decision, :confirm, keyword_init: true)
|
|
88
71
|
|
|
89
|
-
# Puts the built-in commands into +registry+, in lookup order (!rollback
|
|
90
|
-
# before !cmd): the ones #run runs, then the UIs' own (local: Tab and
|
|
91
|
-
# help only).
|
|
92
|
-
#
|
|
93
|
-
# While a turn runs, a show form (the name alone) runs at once beside
|
|
94
|
-
# it (it only reads); setting forms and !cmd run after the turn; !rollback
|
|
95
|
-
# and /continue are refused (Commands::Registry::MID_TURN).
|
|
96
|
-
def self.register_builtins(registry)
|
|
97
|
-
registry.register(ROLLBACK_COMMAND, "discard the interrupted turn and restore the pre-turn state",
|
|
98
|
-
id: :rollback) { |_text| rollback }
|
|
99
|
-
registry.register("!", "run a shell command; its output goes into the conversation",
|
|
100
|
-
id: :shell, mid_turn: :queue, match: ->(text) { text.match?(/\A!\s*\S/) }) { |text| shell(text) }
|
|
101
|
-
# #run passes who sent it (as_parent:) per call.
|
|
102
|
-
registry.register(CONTINUE_COMMAND, "answer the continue offer (yes, no or no, <reason>)") { |text| continue_command(text) }
|
|
103
|
-
# It reads the hosts only (the registry is mutex-guarded): mid-turn too.
|
|
104
|
-
registry.register(MODELS_COMMAND, "list the hosts' models (/models <text> filters)", anytime: true) do |text|
|
|
105
|
-
reply(models_listing(text.delete_prefix(MODELS_COMMAND).strip))
|
|
106
|
-
end
|
|
107
|
-
registry.register(GUARDRAILS_COMMAND, "list guardrails, revoke an approval or flag a System 1 auto-allow",
|
|
108
|
-
mid_turn: show_mid_turn(GUARDRAILS_COMMAND)) do |text|
|
|
109
|
-
guardrails(text.delete_prefix(GUARDRAILS_COMMAND).strip)
|
|
110
|
-
end
|
|
111
|
-
# The list only reads the store; attach and decline only write it (the
|
|
112
|
-
# note waits for the next between-turn delivery): mid-turn too.
|
|
113
|
-
registry.register(CONTEXT_COMMAND, "list the attached context and its candidates (chi context); attach NAME, " \
|
|
114
|
-
"decline NAME: a candidate", anytime: true,
|
|
115
|
-
match: ->(text) { text.match?(%r{\A/context(?:\s+.*)?\z}i) }) do |text|
|
|
116
|
-
context_command(text.strip.sub(%r{\A/context}i, "").strip)
|
|
117
|
-
end
|
|
118
|
-
registry.register(MODEL_COMMAND, "show or switch the model", mid_turn: show_mid_turn(MODEL_COMMAND),
|
|
119
|
-
match: ->(text) { text.match?(%r{\A/model(?:\s+.*)?\z}) }) { |text| model(text) }
|
|
120
|
-
# Setting it saves the session; it takes effect at the next turn's start.
|
|
121
|
-
registry.register(LLM_CONTEXT_COMMAND, "show or set this session's LLM context strategy, apply rule and budget",
|
|
122
|
-
id: :llm_context, mid_turn: show_mid_turn(LLM_CONTEXT_COMMAND), match: ->(text) { text.match?(%r{\A/llm-context(?:\s+.*)?\z}) }) { |text| llm_context(text) }
|
|
123
|
-
registry.register(THINKING_COMMAND, "show or set this session's thinking level (off|low|medium|high|default)",
|
|
124
|
-
id: :thinking, mid_turn: show_mid_turn(THINKING_COMMAND), match: ->(text) { text.match?(%r{\A/thinking(?:\s+.*)?\z}) }) { |text| thinking(text) }
|
|
125
|
-
registry.register(REASONING_HISTORY_COMMAND, "show or set which of its reasoning this session's model gets back (drop|turn|all|default)",
|
|
126
|
-
id: :reasoning_history, mid_turn: show_mid_turn(REASONING_HISTORY_COMMAND),
|
|
127
|
-
match: ->(text) { text.match?(%r{\A/reasoning-history(?:\s+.*)?\z}) }) { |text| reasoning_history(text) }
|
|
128
|
-
registry.register(TOOLKIT_COMMAND, "show or change the tools this session declares (NAME, add TOOL, remove TOOL)",
|
|
129
|
-
id: :toolkit, mid_turn: show_mid_turn(TOOLKIT_COMMAND),
|
|
130
|
-
match: ->(text) { text.match?(%r{\A/toolkit(?:\s+.*)?\z}) }) { |text| toolkit(text) }
|
|
131
|
-
# The list reads only: mid-turn too; an op waits for the turn's end
|
|
132
|
-
# (the next turn's first request is the first with it).
|
|
133
|
-
registry.register(WORKING_SET_COMMAND, "list the working set; drop, undrop, pin or unpin an item (drop KEY…)",
|
|
134
|
-
id: :working_set, mid_turn: show_mid_turn(WORKING_SET_COMMAND),
|
|
135
|
-
match: ->(text) { text.match?(%r{\A/working-set(?:\s+.*)?\z}) }) { |text| working_set(text) }
|
|
136
|
-
# Tighten-only from anyone; loosening is a user's at a UI (#run's user_ui:).
|
|
137
|
-
registry.register(PARENT_ANSWERS_COMMAND, "show or set what a parent agent may answer here (hard|standard|soft|default; " \
|
|
138
|
-
"never looser than config.yml's)",
|
|
139
|
-
id: :parent_answers, mid_turn: show_mid_turn(PARENT_ANSWERS_COMMAND),
|
|
140
|
-
match: ->(text) { text.match?(%r{\A/parent-answers(?:\s+.*)?\z}) }) { |text| parent_answers(text) }
|
|
141
|
-
# Only a name: renaming a running session doesn't wait for its turn.
|
|
142
|
-
registry.register(TITLE_COMMAND, "show or set this session's title (--clear: back to its first message)",
|
|
143
|
-
id: :title, anytime: true, match: ->(text) { text.match?(%r{\A/title(?:\s+.*)?\z}) }) { |text| title(text) }
|
|
144
|
-
# An edit only writes a note file (it goes in between turns): mid-turn too.
|
|
145
|
-
registry.register(OBJECTIVES_COMMAND, "show or edit what this session is to achieve (set A | B, add TEXT, edit ID TEXT, done ID…, drop ID…, undo ID…, check ID, track)",
|
|
146
|
-
id: :objectives, anytime: true) { |text| objectives(text) }
|
|
147
|
-
registry.register(HELP_COMMAND, "list the commands, the bundles' too", anytime: true) { |_text| reply(help_listing) }
|
|
148
|
-
registry.register("/stats", "show the session's stats", local: true)
|
|
149
|
-
# /recap with words (on|off|default, or a slip: the usage) runs here:
|
|
150
|
-
# it toggles the idle job only, so it doesn't wait for a running turn.
|
|
151
|
-
# The bare /recap shows the recap: the UIs' own, under the same name
|
|
152
|
-
# (Registry#register); /help lists the name once, with this entry's
|
|
153
|
-
# description.
|
|
154
|
-
registry.register(RECAP_COMMAND, "show the session's recap (in a terminal); on|off|default sets this session's own",
|
|
155
|
-
id: :recap_setting, anytime: true, match: ->(text) { text.match?(%r{\A/recap\s+\S}) }) { |text| recap_setting(text) }
|
|
156
|
-
registry.register(RECAP_COMMAND, "show the session's recap", local: true, match: ->(text) { text == RECAP_COMMAND })
|
|
157
|
-
# Bare `exit` too; any case; --delete after it.
|
|
158
|
-
registry.register("/exit", "leave (--delete also deletes the session)", local: true,
|
|
159
|
-
match: exit_match("/?exit"))
|
|
160
|
-
registry.register("/quit", "leave, like /exit", id: :exit, local: true, match: exit_match("/quit"))
|
|
161
|
-
registry.register("/archive", "leave and archive the session: hidden from the lists, kept for good", local: true,
|
|
162
|
-
match: ->(text) { text.casecmp?("/archive") })
|
|
163
|
-
# The terminal's: the web has no worker to leave running.
|
|
164
|
-
registry.register("/detach", "leave and keep the worker running", local: true, uis: [:attached],
|
|
165
|
-
match: ->(text) { text.casecmp?("/detach") })
|
|
166
|
-
# /cut TEXT and /queue TEXT while a turn runs: a delivery the UIs parse
|
|
167
|
-
# themselves (ComposerRoute.steer_choice) before any command check; the
|
|
168
|
-
# entries exist for Tab completion and /help. The web composer sends
|
|
169
|
-
# /cut too (its queue is Alt+Enter). The attached TUI queues in its
|
|
170
|
-
# worker's input files.
|
|
171
|
-
registry.register("/cut", "cut the running generation and go in now (/cut TEXT)",
|
|
172
|
-
local: true, uis: %i[attached web], match: ->(text) { text.casecmp?("/cut") || text.match?(%r{\A/cut\s+\S}) })
|
|
173
|
-
registry.register("/queue", "run as the next turn after this one (/queue TEXT)",
|
|
174
|
-
local: true, uis: [:attached], match: ->(text) { text.casecmp?("/queue") || text.match?(%r{\A/queue\s+\S}) })
|
|
175
|
-
# Takes back the newest message this attached TUI queued, before it
|
|
176
|
-
# runs (the worker's POST turn/dequeue); the web has × on the bubble.
|
|
177
|
-
registry.register("/unqueue", "remove your newest queued message before it runs", local: true, uis: [:attached],
|
|
178
|
-
match: ->(text) { text.casecmp?("/unqueue") })
|
|
179
|
-
# chi share for the attached session (TerminalUI::ParticipantCommands):
|
|
180
|
-
# a participant's turns run on its worker.
|
|
181
|
-
registry.register("/share", "invite someone into this session (/share NAME [--expires 3d] [--cap 2])",
|
|
182
|
-
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/share(?:\s.*)?\z}i) })
|
|
183
|
-
registry.register("/participants", "list this session's participants (revoke [P], cap P DOLLARS)",
|
|
184
|
-
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/participants(?:\s.*)?\z}i) })
|
|
185
|
-
registry
|
|
186
|
-
end
|
|
187
|
-
|
|
188
|
-
# @return [#call] line → :anytime for +name+ alone (it shows), else
|
|
189
|
-
# :queue (it sets what the next turn runs under)
|
|
190
|
-
def self.show_mid_turn(name)
|
|
191
|
-
->(text) { text == name ? :anytime : :queue }
|
|
192
|
-
end
|
|
193
|
-
private_class_method :show_mid_turn
|
|
194
|
-
|
|
195
|
-
# @param word [String] the regexp source of the exit word
|
|
196
|
-
def self.exit_match(word)
|
|
197
|
-
pattern = /\A#{word}(?:\s+#{EXIT_DELETE_FLAG})?\z/i
|
|
198
|
-
->(text) { text.match?(pattern) }
|
|
199
|
-
end
|
|
200
|
-
private_class_method :exit_match
|
|
201
|
-
|
|
202
|
-
# @return [Boolean] an exit line (/exit, /quit, exit) that deletes the session too
|
|
203
|
-
def self.delete_on_exit?(line) = line.to_s.split.last.to_s.casecmp?(EXIT_DELETE_FLAG)
|
|
204
|
-
|
|
205
|
-
# The built-ins alone, for callers without an Engine (an attached TUI
|
|
206
|
-
# before its snapshot names the session's commands, specs).
|
|
207
|
-
# @return [Commands::Registry] frozen
|
|
208
|
-
def self.builtin_registry
|
|
209
|
-
@builtin_registry ||= register_builtins(Commands::Registry.new).freeze
|
|
210
|
-
end
|
|
211
|
-
|
|
212
|
-
# The UI-only built-in command +text+ is a line of (/stats, /exit, …), or
|
|
213
|
-
# nil: neither a worker nor a one-shot run (chi send, chi -p
|
|
214
|
-
# --non-interactive) runs it, so they refuse it rather than send it to
|
|
215
|
-
# the model as a prompt. What the UIs run: the line as the entry's match
|
|
216
|
-
# takes it — bare "exit" too (/exit's match), but not with arguments
|
|
217
|
-
# ("/exit --delete" is a UI line; "/stats please" is a prompt), as in a
|
|
218
|
-
# terminal.
|
|
219
|
-
# @return [Commands::Registry::Entry, nil]
|
|
220
|
-
def self.ui_only_entry(text)
|
|
221
|
-
line = text.to_s.strip
|
|
222
|
-
return nil unless line.start_with?("/", "!") || line.match?(/\Aexit\z/i)
|
|
223
|
-
|
|
224
|
-
entry = builtin_registry.lookup_local(line)
|
|
225
|
-
return nil unless entry
|
|
226
|
-
return entry if line == entry.name
|
|
227
|
-
|
|
228
|
-
line.match?(/\Aexit\z/i) ? entry : nil
|
|
229
|
-
end
|
|
230
|
-
|
|
231
|
-
# Why a UI-only command isn't run by +runner+ ("chi send"): /stats points
|
|
232
|
-
# at its model-free reader (naming the session +id+ when there is one),
|
|
233
|
-
# which reads the session's machine: under chi --remote NAME (+remote+)
|
|
234
|
-
# it runs there.
|
|
235
|
-
# @return [String]
|
|
236
|
-
def self.ui_only_refusal(name, id, runner:, remote: nil)
|
|
237
|
-
return "#{name} is a terminal and web command; #{runner} doesn't run it" unless name == "/stats"
|
|
238
|
-
|
|
239
|
-
"#{name} is a terminal and web command; use chi sessions stats #{id ? id[0, 8] : "ID"}#{" on #{remote}" if remote}"
|
|
240
|
-
end
|
|
241
|
-
|
|
242
|
-
# The name of this chi's own session command +line+ is (one a worker
|
|
243
|
-
# runs), or nil. A worker that answers such a line unknown_command runs
|
|
244
|
-
# an older chi that doesn't have it yet (/llm-context against 0.39.0):
|
|
245
|
-
# the UIs say so (BridgeClient.stale_worker_message) instead of "not a
|
|
246
|
-
# session command".
|
|
247
|
-
# @return [String, nil]
|
|
248
|
-
def self.builtin_name(line) = builtin_registry.lookup(line)&.name
|
|
249
|
-
|
|
250
72
|
# @return [String, nil] the model /model clear goes back to; nil when none
|
|
251
73
|
attr_reader :default_model
|
|
252
74
|
|
|
@@ -256,7 +78,7 @@ module Samagotchi
|
|
|
256
78
|
# @param save [#call] saves a session (a worker passes its state dir)
|
|
257
79
|
# @param registry [Commands::Registry] the commands #run looks lines up in
|
|
258
80
|
def initialize(engine:, turn_flow:, default_model:, save: ->(session) { session.save },
|
|
259
|
-
registry:
|
|
81
|
+
registry: SessionCommandCatalog.builtin_registry)
|
|
260
82
|
@engine = engine
|
|
261
83
|
@turn_flow = turn_flow
|
|
262
84
|
@default_model = default_model
|
|
@@ -275,13 +97,13 @@ module Samagotchi
|
|
|
275
97
|
# A name with a UI's entry and the session's (/recap): listed once,
|
|
276
98
|
# as the session's (registered first).
|
|
277
99
|
entries = @registry.entries.uniq(&:name).sort_by { |entry| [help_group(entry), entry.name] }
|
|
278
|
-
width = entries.map { |entry|
|
|
100
|
+
width = entries.map { |entry| SessionCommandCatalog.display_name(entry).length }.max
|
|
279
101
|
lines = entries.map do |entry|
|
|
280
102
|
notes = []
|
|
281
103
|
notes << entry.source unless entry.source == "core"
|
|
282
104
|
notes << MID_TURN_NOTES[entry.mid_turn_label] if MID_TURN_NOTES[entry.mid_turn_label]
|
|
283
105
|
notes << "#{ui_words(entry.uis)} only" if entry.local
|
|
284
|
-
line = " #{
|
|
106
|
+
line = " #{SessionCommandCatalog.display_name(entry).ljust(width)} #{entry.description}"
|
|
285
107
|
notes.empty? ? line : "#{line} (#{notes.join("; ")})"
|
|
286
108
|
end
|
|
287
109
|
"commands:\n#{lines.join("\n")}"
|
|
@@ -300,9 +122,6 @@ module Samagotchi
|
|
|
300
122
|
words.join(" and ")
|
|
301
123
|
end
|
|
302
124
|
|
|
303
|
-
# How help and errors name an entry (the shell's "!" is "!<cmd>").
|
|
304
|
-
def self.display_name(entry) = entry.name == SHELL_BANG_PREFIX ? "#{SHELL_BANG_PREFIX}<cmd>" : entry.name
|
|
305
|
-
|
|
306
125
|
# @param confirm [Boolean] a costly setting change asks first
|
|
307
126
|
# (CacheConfirm): true for a person at a web tab or
|
|
308
127
|
# a TUI; false applies it at once (chi send, a delegate, a plugin,
|
|
@@ -15,6 +15,7 @@ require_relative "context_move"
|
|
|
15
15
|
require_relative "note_move"
|
|
16
16
|
require_relative "archive_store"
|
|
17
17
|
require_relative "continue_intent"
|
|
18
|
+
require_relative "continued_from"
|
|
18
19
|
require_relative "recap_store"
|
|
19
20
|
require_relative "bridge_client"
|
|
20
21
|
require_relative "worker_link"
|
|
@@ -83,8 +84,12 @@ module Samagotchi
|
|
|
83
84
|
CarryContext = Data.define(:previous, :to, :state_dir, :move)
|
|
84
85
|
# What an undo works on: the previous link's id, the failed link (nil in
|
|
85
86
|
# :moved_back, which looks it up itself), the delegates the plan moved,
|
|
86
|
-
#
|
|
87
|
-
|
|
87
|
+
# what the forward carry kept (#run's +carried+, by step name), and why
|
|
88
|
+
# (ChildMove.apply's undo: :failed_continue, or :deleted_link for
|
|
89
|
+
# #undo_deleted_link).
|
|
90
|
+
UndoContext = Data.define(:from, :link, :state_dir, :moved, :carried, :reason) do
|
|
91
|
+
def initialize(from:, link:, state_dir:, moved:, carried:, reason: :failed_continue) = super
|
|
92
|
+
end
|
|
88
93
|
|
|
89
94
|
# When a failed continue undoes each step, in time order: :moved_back
|
|
90
95
|
# while the failed link's worker may still run (before
|
|
@@ -240,7 +245,8 @@ module Samagotchi
|
|
|
240
245
|
carried = {}
|
|
241
246
|
begin
|
|
242
247
|
link = spawn_continuation(previous, prompt: prompt, preview: preview, choice: checked.choice, state_dir: state_dir,
|
|
243
|
-
move: plan.move, client_id: client_id, carried: carried,
|
|
248
|
+
move: plan.move, archived: archived, client_id: client_id, carried: carried,
|
|
249
|
+
session_id: successor_id)
|
|
244
250
|
rescue StandardError, Interrupt
|
|
245
251
|
# Nothing started: the delegates go back, what this archived goes
|
|
246
252
|
# back to the lists, and a new link saved before its worker failed
|
|
@@ -345,13 +351,15 @@ module Samagotchi
|
|
|
345
351
|
end
|
|
346
352
|
|
|
347
353
|
# The delegates a failed #run moved, or began to (its
|
|
348
|
-
# intent names them), go back to the previous link
|
|
354
|
+
# intent names them), go back to the previous link; a deleted link's
|
|
355
|
+
# (#undo_deleted_link) the ones its caller names.
|
|
349
356
|
private_class_method def self.undo_move(ctx)
|
|
350
357
|
id = ctx.from
|
|
351
358
|
state_dir = ctx.state_dir
|
|
352
|
-
link = SessionChain.next_of(id, state_dir: state_dir) or return
|
|
353
|
-
|
|
354
|
-
|
|
359
|
+
link = ctx.link || SessionChain.next_of(id, state_dir: state_dir) or return
|
|
360
|
+
# A deleted link's caller names exactly the ones still under it.
|
|
361
|
+
ids = (ctx.reason == :failed_continue && ChildMove.read_intent(link, state_dir: state_dir)&.ids) || ctx.moved
|
|
362
|
+
ChildMove.apply(ids, from: link, to: id, state_dir: state_dir, undo: ctx.reason)
|
|
355
363
|
ChildMove.clear_intent(link, state_dir: state_dir)
|
|
356
364
|
rescue StandardError => e
|
|
357
365
|
Log.warn(:worker, "continue_undo_move_failed", sid: id, error: e.class.name, msg: e.message)
|
|
@@ -388,17 +396,43 @@ module Samagotchi
|
|
|
388
396
|
end
|
|
389
397
|
|
|
390
398
|
ctx = UndoContext.new(from: id, link: orphan, state_dir: state_dir, moved: moved, carried: carried)
|
|
391
|
-
|
|
392
|
-
FileUtils.rm_f(Session.session_file(orphan, state_dir: state_dir))
|
|
393
|
-
undo_carry(:link_unlinked, ctx)
|
|
394
|
-
SessionManager.delete_session(orphan, state_dir: state_dir, allow_scheduled: true)
|
|
395
|
-
undo_carry(:link_deleted, ctx)
|
|
399
|
+
unlink(ctx) { SessionManager.delete_session(orphan, state_dir: state_dir, allow_scheduled: true) }
|
|
396
400
|
rescue SessionManager::DeleteRefused
|
|
397
401
|
raise
|
|
398
402
|
rescue StandardError => e
|
|
399
403
|
Log.warn(:worker, "continue_cleanup_failed", sid: id, error: e.class.name, msg: e.message)
|
|
400
404
|
end
|
|
401
405
|
|
|
406
|
+
# The undo's phases once the link's worker is stopped, shared by a
|
|
407
|
+
# failed continue (#discard_failed_link) and a deleted link
|
|
408
|
+
# (#undo_deleted_link): :link_stopped, its session file goes (out of the
|
|
409
|
+
# chain), :link_unlinked, the block removes the rest (neither caller
|
|
410
|
+
# recurses: each passes its own removal), :link_deleted.
|
|
411
|
+
private_class_method def self.unlink(ctx)
|
|
412
|
+
undo_carry(:link_stopped, ctx)
|
|
413
|
+
FileUtils.rm_f(Session.session_file(ctx.link, state_dir: ctx.state_dir))
|
|
414
|
+
undo_carry(:link_unlinked, ctx)
|
|
415
|
+
yield
|
|
416
|
+
undo_carry(:link_deleted, ctx)
|
|
417
|
+
end
|
|
418
|
+
|
|
419
|
+
# Deleting +link+, the chain's newest link, undoes the continue +from+ →
|
|
420
|
+
# +link+ (SessionChain::Delete, D5) once +link+'s worker is stopped:
|
|
421
|
+
# +moved+ go back to +from+ (:moved_back, their note says the link was
|
|
422
|
+
# deleted), then #unlink: the rings and every note waiting in +link+
|
|
423
|
+
# but the continue's own go to +from+ (NoteMove.give_back, moved: []),
|
|
424
|
+
# and the block removes +link+'s files. +from+ is not woken (unlike a
|
|
425
|
+
# failed continue): the reports wait for its next start. Unarchiving
|
|
426
|
+
# is the caller's.
|
|
427
|
+
# @yieldreturn the block's value is returned
|
|
428
|
+
def self.undo_deleted_link(from:, link:, moved:, state_dir:)
|
|
429
|
+
ctx = UndoContext.new(from: from, link: link, state_dir: state_dir, moved: moved, carried: {}, reason: :deleted_link)
|
|
430
|
+
undo_carry(:moved_back, ctx)
|
|
431
|
+
removed = nil
|
|
432
|
+
unlink(ctx) { removed = yield }
|
|
433
|
+
removed
|
|
434
|
+
end
|
|
435
|
+
|
|
402
436
|
# A failed link's folder an older chi's note brought back after its
|
|
403
437
|
# delete: the note goes back to +id+, the folder goes again.
|
|
404
438
|
private_class_method def self.give_back_strays(ctx)
|
|
@@ -425,12 +459,16 @@ module Samagotchi
|
|
|
425
459
|
# it (spawn_session marks it starting before its file is saved:
|
|
426
460
|
# StartingMarker.fresh?).
|
|
427
461
|
# @param carried [Hash] gets what each CARRY step took to it, by name
|
|
462
|
+
# @param archived [Array<String>] what the archive of +previous+ hid
|
|
463
|
+
# (ContinuedFrom's record, written first in before_spawn: no crash
|
|
464
|
+
# window between the link's save and the record)
|
|
428
465
|
private_class_method def self.spawn_continuation(previous, prompt:, preview:, choice:, state_dir:, move:, carried:,
|
|
429
|
-
session_id:, client_id: nil)
|
|
466
|
+
session_id:, archived: [], client_id: nil)
|
|
430
467
|
recap = RecapStore.read(Session.session_dir(previous.id, state_dir: state_dir))
|
|
431
468
|
start = prompt.to_s.strip.empty? ? nil : prompt
|
|
432
469
|
preview = previous.first_preview if preview.to_s.strip.empty? && start.nil?
|
|
433
470
|
before_spawn = lambda do |link|
|
|
471
|
+
ContinuedFrom.write(link.id, previous_id: previous.id, archived_ids: archived, moved_ids: move, state_dir: state_dir)
|
|
434
472
|
ctx = CarryContext.new(previous: previous, to: link.id, state_dir: state_dir, move: move)
|
|
435
473
|
CARRY.each { |step| carried[step.name] = send(step.carry, ctx) }
|
|
436
474
|
end
|