samagotchi 0.170.0 → 0.172.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/bin/chi +6 -4
- data/docs/architecture.md +26 -9
- data/docs/bundles.md +4 -21
- data/docs/cli.md +81 -97
- data/docs/configuration.md +12 -14
- data/docs/context.md +5 -6
- data/docs/guardrails.md +57 -96
- data/docs/hooks.md +3 -4
- data/docs/internals/attached-context.md +6 -7
- data/docs/internals/background-tasks.md +4 -3
- data/docs/internals/block-renderers.md +2 -2
- data/docs/internals/bridge.md +34 -7
- data/docs/internals/context-telemetry.md +13 -1
- data/docs/internals/engine.md +25 -13
- data/docs/internals/inline-widgets.md +1 -1
- data/docs/internals/objectives.md +4 -6
- data/docs/internals/participants.md +340 -249
- data/docs/internals/prompt-caching.md +4 -5
- data/docs/internals/web-frontend.md +185 -35
- data/docs/internals/working-set.md +71 -5
- data/docs/memory.md +1 -1
- data/docs/plugins.md +2 -16
- data/docs/releasing.md +2 -1
- data/docs/scheduling.md +1 -1
- data/docs/sessions.md +17 -48
- data/docs/sub-agent.md +2 -2
- data/docs/system-one.md +2 -2
- data/docs/testing.md +56 -7
- data/lib/samagotchi/bridge/card_store.rb +1 -0
- data/lib/samagotchi/bridge/sse_writer.rb +1 -0
- data/lib/samagotchi/bridge/turn_accumulator.rb +8 -5
- data/lib/samagotchi/bridge.rb +64 -20
- data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
- data/lib/samagotchi/bundle_command/test_run.rb +1 -0
- data/lib/samagotchi/bundle_command/try/run.rb +1 -0
- data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
- data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
- data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
- data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
- data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +126 -67
- data/lib/samagotchi/command_intake.rb +15 -3
- data/lib/samagotchi/compat.rb +6 -1
- data/lib/samagotchi/context_candidate_choice.rb +1 -3
- data/lib/samagotchi/context_candidates.rb +3 -4
- data/lib/samagotchi/continue_offer.rb +9 -2
- data/lib/samagotchi/delivery.rb +36 -12
- data/lib/samagotchi/empty_answer_error.rb +5 -0
- data/lib/samagotchi/engine/cards.rb +3 -0
- data/lib/samagotchi/engine/context_judge.rb +5 -3
- data/lib/samagotchi/engine/events.rb +4 -0
- data/lib/samagotchi/engine/hooks.rb +3 -0
- data/lib/samagotchi/engine/messages.rb +5 -0
- data/lib/samagotchi/engine/model_settings.rb +2 -0
- data/lib/samagotchi/engine/models.rb +10 -0
- data/lib/samagotchi/engine/objectives.rb +6 -0
- data/lib/samagotchi/engine/participants.rb +18 -14
- data/lib/samagotchi/engine/plugins.rb +8 -0
- data/lib/samagotchi/engine/questions.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -0
- data/lib/samagotchi/engine/session.rb +4 -0
- data/lib/samagotchi/engine/side_requests.rb +9 -0
- data/lib/samagotchi/engine/steering.rb +2 -0
- data/lib/samagotchi/engine/toolkit.rb +5 -0
- data/lib/samagotchi/engine/turn_end.rb +7 -0
- data/lib/samagotchi/engine/turn_helpers.rb +8 -0
- data/lib/samagotchi/engine/verify.rb +2 -0
- data/lib/samagotchi/engine.rb +8 -3
- data/lib/samagotchi/event_fanout.rb +22 -1
- data/lib/samagotchi/grants.rb +33 -22
- data/lib/samagotchi/guardrail_wiring.rb +23 -18
- data/lib/samagotchi/guardrails/approval_use.rb +59 -0
- data/lib/samagotchi/guardrails/approvals.rb +9 -11
- data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
- data/lib/samagotchi/guardrails/gate.rb +10 -5
- data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
- data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
- data/lib/samagotchi/guardrails/review_paths.rb +2 -3
- data/lib/samagotchi/guardrails.rb +1 -1
- data/lib/samagotchi/image_store.rb +51 -12
- data/lib/samagotchi/kernel_loop.rb +7 -1
- data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
- data/lib/samagotchi/llm_context_stale.rb +4 -1
- data/lib/samagotchi/llm_context_strategy.rb +2 -0
- data/lib/samagotchi/objectives.rb +1 -4
- data/lib/samagotchi/objectives_audit.rb +1 -1
- data/lib/samagotchi/objectives_track.rb +1 -1
- data/lib/samagotchi/owner_prompt.rb +3 -6
- data/lib/samagotchi/parent_report.rb +1 -0
- data/lib/samagotchi/participant_messages.rb +48 -4
- data/lib/samagotchi/participant_revise.rb +61 -0
- data/lib/samagotchi/participant_turn_mode.rb +50 -0
- data/lib/samagotchi/participant_uploads.rb +98 -0
- data/lib/samagotchi/participants.rb +21 -12
- data/lib/samagotchi/participants_watch.rb +2 -2
- data/lib/samagotchi/plugin/api.rb +2 -2
- data/lib/samagotchi/plugin/attached_context.rb +1 -1
- data/lib/samagotchi/plugin/context.rb +0 -4
- data/lib/samagotchi/plugin/sessions.rb +1 -25
- data/lib/samagotchi/reply_wait/poll.rb +5 -1
- data/lib/samagotchi/revise_anchor.rb +9 -0
- data/lib/samagotchi/run_options.rb +2 -3
- data/lib/samagotchi/send_command.rb +15 -10
- data/lib/samagotchi/session.rb +0 -3
- data/lib/samagotchi/session_access/contract.rb +1 -10
- data/lib/samagotchi/session_access/local.rb +10 -23
- data/lib/samagotchi/session_access/remote.rb +9 -20
- data/lib/samagotchi/session_access.rb +12 -16
- data/lib/samagotchi/session_chain.rb +4 -6
- data/lib/samagotchi/session_commands.rb +6 -3
- data/lib/samagotchi/session_continue.rb +1 -2
- data/lib/samagotchi/session_inbox.rb +20 -12
- data/lib/samagotchi/session_manager/delivery.rb +32 -5
- data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
- data/lib/samagotchi/session_manager/listing.rb +5 -0
- data/lib/samagotchi/session_manager/spawn.rb +2 -2
- data/lib/samagotchi/session_manager/worker_command.rb +9 -0
- data/lib/samagotchi/session_participants.rb +12 -8
- data/lib/samagotchi/session_review.rb +5 -7
- data/lib/samagotchi/session_stores.rb +3 -1
- data/lib/samagotchi/share_command.rb +10 -12
- data/lib/samagotchi/share_terms.rb +41 -0
- data/lib/samagotchi/steer.rb +8 -14
- data/lib/samagotchi/system_prompt.rb +16 -25
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
- data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
- data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
- data/lib/samagotchi/terminal_ui.rb +1 -2
- data/lib/samagotchi/testing/sessions.rb +2 -10
- data/lib/samagotchi/tool_runner.rb +42 -9
- data/lib/samagotchi/toolkit.rb +4 -22
- data/lib/samagotchi/toolkit_suggest.rb +3 -3
- data/lib/samagotchi/tools/deferred_tools.rb +1 -0
- data/lib/samagotchi/tools/execute.rb +11 -45
- data/lib/samagotchi/tools/leftovers.rb +122 -0
- data/lib/samagotchi/tools/memory.rb +88 -25
- data/lib/samagotchi/tools/task_get.rb +1 -0
- data/lib/samagotchi/tools/task_list.rb +3 -2
- data/lib/samagotchi/tools/task_record.rb +6 -4
- data/lib/samagotchi/tools/task_runtime.rb +199 -20
- data/lib/samagotchi/tools/task_wait.rb +4 -3
- data/lib/samagotchi/turn_anchors.rb +0 -3
- data/lib/samagotchi/turn_context.rb +1 -1
- data/lib/samagotchi/turn_origin.rb +1 -1
- data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
- data/lib/samagotchi/turn_stages/objectives.rb +2 -2
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
- data/lib/samagotchi/turn_stages/verify_anchors.rb +13 -1
- data/lib/samagotchi/update_hint.rb +3 -1
- data/lib/samagotchi/verify_anchor.rb +6 -0
- data/lib/samagotchi/verify_fresh.rb +1 -1
- data/lib/samagotchi/verify_fresh_origin.rb +13 -11
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +45 -82
- data/lib/samagotchi/web/participant_access.rb +139 -40
- data/lib/samagotchi/web/participant_images.rb +185 -0
- data/lib/samagotchi/web/participant_review.rb +22 -142
- data/lib/samagotchi/web/participant_show.rb +241 -0
- data/lib/samagotchi/web/participant_stream_filter.rb +25 -2
- data/lib/samagotchi/web/participant_view.rb +334 -27
- data/lib/samagotchi/web/participants_admin.rb +21 -40
- data/lib/samagotchi/web/public/annotate_bar.css +1 -2
- data/lib/samagotchi/web/public/annotate_bar.js +1 -2
- data/lib/samagotchi/web/public/app.js +126 -132
- data/lib/samagotchi/web/public/block_host.js +7 -4
- data/lib/samagotchi/web/public/bubbles.js +9 -1
- data/lib/samagotchi/web/public/cards_offscreen.js +69 -0
- data/lib/samagotchi/web/public/chip_popover.js +5 -4
- data/lib/samagotchi/web/public/claim_link.js +4 -4
- data/lib/samagotchi/web/public/composer.css +4 -6
- data/lib/samagotchi/web/public/composer_mode.js +11 -6
- data/lib/samagotchi/web/public/composer_mount.js +4 -4
- data/lib/samagotchi/web/public/ctx.js +12 -3
- data/lib/samagotchi/web/public/data.js +2 -10
- data/lib/samagotchi/web/public/delivery_line.js +49 -63
- data/lib/samagotchi/web/public/history_shape.js +5 -1
- data/lib/samagotchi/web/public/images.js +7 -1
- data/lib/samagotchi/web/public/index.html +50 -34
- data/lib/samagotchi/web/public/info_bar_model.js +2 -1
- data/lib/samagotchi/web/public/live_turn.js +1 -1
- data/lib/samagotchi/web/public/markdown_place.js +5 -4
- data/lib/samagotchi/web/public/owner_claim.js +5 -5
- data/lib/samagotchi/web/public/owner_verify.js +0 -2
- data/lib/samagotchi/web/public/participant_bar.js +107 -0
- data/lib/samagotchi/web/public/participant_main.js +289 -0
- data/lib/samagotchi/web/public/participant_model.js +5 -292
- data/lib/samagotchi/web/public/participant_names.js +17 -8
- data/lib/samagotchi/web/public/participant_revise.js +66 -0
- data/lib/samagotchi/web/public/participants_panel.js +32 -74
- data/lib/samagotchi/web/public/question_cards.js +14 -3
- data/lib/samagotchi/web/public/refs.js +53 -36
- data/lib/samagotchi/web/public/reload_check.js +70 -14
- data/lib/samagotchi/web/public/revise_stack.js +21 -10
- data/lib/samagotchi/web/public/send_turn.js +28 -29
- data/lib/samagotchi/web/public/session_actions.js +4 -45
- data/lib/samagotchi/web/public/session_fields.js +1 -2
- data/lib/samagotchi/web/public/session_state.js +12 -7
- data/lib/samagotchi/web/public/session_sync.js +9 -2
- data/lib/samagotchi/web/public/session_view.js +14 -5
- data/lib/samagotchi/web/public/settings_panel.js +4 -18
- data/lib/samagotchi/web/public/stage_answer.js +56 -0
- data/lib/samagotchi/web/public/stage_asking.js +7 -5
- data/lib/samagotchi/web/public/stage_cloud.js +93 -0
- data/lib/samagotchi/web/public/stage_dom.js +100 -0
- data/lib/samagotchi/web/public/stage_handoff.js +154 -0
- data/lib/samagotchi/web/public/stage_owner_wait.js +35 -0
- data/lib/samagotchi/web/public/stage_pager.js +59 -0
- data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
- data/lib/samagotchi/web/public/stage_view.js +103 -410
- data/lib/samagotchi/web/public/stage_waiting.js +74 -6
- data/lib/samagotchi/web/public/start_page.js +12 -33
- data/lib/samagotchi/web/public/start_settings.js +24 -78
- data/lib/samagotchi/web/public/start_toolkit.js +6 -10
- data/lib/samagotchi/web/public/storage.js +22 -0
- data/lib/samagotchi/web/public/stream_handlers.js +3 -10
- data/lib/samagotchi/web/public/tokens.css +1 -1
- data/lib/samagotchi/web/public/turn_block.js +2 -5
- data/lib/samagotchi/web/public/turn_events.js +30 -12
- data/lib/samagotchi/web/public/turn_html.js +9 -2
- data/lib/samagotchi/web/public/turn_pieces.js +3 -4
- data/lib/samagotchi/web/public/turn_range.js +1 -2
- data/lib/samagotchi/web/public/verify_target.js +2 -4
- data/lib/samagotchi/web/public/viewer.js +59 -0
- data/lib/samagotchi/web/public/viewer_self.js +59 -0
- data/lib/samagotchi/web/public/working_set.js +11 -5
- data/lib/samagotchi/web/public/working_set_view.js +53 -8
- data/lib/samagotchi/web/public/ws_cards.js +89 -0
- data/lib/samagotchi/web/public/ws_peek.js +106 -0
- data/lib/samagotchi/web/public/ws_preview.css +126 -0
- data/lib/samagotchi/web/public/ws_reader.js +319 -0
- data/lib/samagotchi/web/public/ws_timeline.js +200 -0
- data/lib/samagotchi/web/public/ws_way_in.js +25 -0
- data/lib/samagotchi/web/qr.rb +1 -1
- data/lib/samagotchi/web/session_control.rb +42 -15
- data/lib/samagotchi/web/session_create.rb +6 -72
- data/lib/samagotchi/web/session_show.rb +75 -28
- data/lib/samagotchi/web/session_summary.rb +1 -4
- data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
- data/lib/samagotchi/web/viewer.rb +70 -0
- data/lib/samagotchi/web/working_set_routes.rb +132 -0
- data/lib/samagotchi/worker.rb +35 -28
- data/lib/samagotchi/worker_inbound.rb +4 -3
- data/lib/samagotchi/worker_link.rb +17 -0
- data/lib/samagotchi/working_set.rb +322 -11
- metadata +32 -13
- data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
- data/lib/samagotchi/participant_mode.rb +0 -86
- data/lib/samagotchi/review_apply.rb +0 -131
- data/lib/samagotchi/review_command.rb +0 -123
- data/lib/samagotchi/review_start.rb +0 -196
- data/lib/samagotchi/web/public/participant.html +0 -218
- data/lib/samagotchi/web/public/participant.js +0 -719
- data/lib/samagotchi/web/public/participant_annotate.js +0 -76
- data/lib/samagotchi/web/public/review_apply.js +0 -34
- data/lib/samagotchi/web/public/review_history.js +0 -72
- data/lib/samagotchi/web/public/review_start.js +0 -55
- data/lib/samagotchi/web/public/tools_overview.js +0 -42
|
@@ -9,21 +9,19 @@ module Samagotchi
|
|
|
9
9
|
|
|
10
10
|
# ctx.sessions recorded (Plugin::Sessions' methods, same signatures:
|
|
11
11
|
# the contract spec checks): each call lands in #calls and returns a
|
|
12
|
-
# scripted answer (#on), or by default: fork a new id ("fork-1"…),
|
|
13
|
-
# review a new id ("review-1"…), send
|
|
12
|
+
# scripted answer (#on), or by default: fork a new id ("fork-1"…), send
|
|
14
13
|
# and stop the id they got, children [], read raises
|
|
15
14
|
# Plugin::Sessions::Error (no such session).
|
|
16
15
|
#
|
|
17
16
|
# bundle.sessions.on(:children) { |all:| [{ id: "c1", status: "running" }] }
|
|
18
17
|
# bundle.sessions.on(:send) { |id, text, delivery:| raise Samagotchi::Plugin::Sessions::Error, "busy" }
|
|
19
18
|
class Sessions
|
|
20
|
-
METHODS = %i[fork
|
|
19
|
+
METHODS = %i[fork send read children stop].freeze
|
|
21
20
|
|
|
22
21
|
def initialize
|
|
23
22
|
@calls = []
|
|
24
23
|
@scripts = {}
|
|
25
24
|
@forks = 0
|
|
26
|
-
@reviews = 0
|
|
27
25
|
@mutex = Mutex.new
|
|
28
26
|
end
|
|
29
27
|
|
|
@@ -49,12 +47,6 @@ module Samagotchi
|
|
|
49
47
|
end
|
|
50
48
|
end
|
|
51
49
|
|
|
52
|
-
def review(subjects: [], seed: nil, recap_wait: 0)
|
|
53
|
-
answer(:review, [], { subjects: subjects, seed: seed, recap_wait: recap_wait }) do
|
|
54
|
-
"review-#{@mutex.synchronize { @reviews += 1 }}"
|
|
55
|
-
end
|
|
56
|
-
end
|
|
57
|
-
|
|
58
50
|
def send(id, text, delivery: nil)
|
|
59
51
|
answer(:send, [id, text], { delivery: delivery }) { id }
|
|
60
52
|
end
|
|
@@ -20,7 +20,6 @@ require_relative "config"
|
|
|
20
20
|
require_relative "memory_bundle/index_sync"
|
|
21
21
|
require_relative "memory_bundle/index_size"
|
|
22
22
|
require_relative "memory_bundle/entry_size"
|
|
23
|
-
require_relative "participant_mode"
|
|
24
23
|
|
|
25
24
|
module Samagotchi
|
|
26
25
|
# One call's result from ToolRunner#run, read by ToolResponse (the loops'
|
|
@@ -29,7 +28,9 @@ module Samagotchi
|
|
|
29
28
|
# loaded, stamp) are nil. +loaded+: the tools a find_tools hit loads
|
|
30
29
|
# (Tools::ToolIndex::FoundText#loaded), by their wire names. +stamp+: an
|
|
31
30
|
# ok read/edit/write's file right after it ran ({path:, mtime_ns:,
|
|
32
|
-
# size:}), what WorkingSet tells a file changed since by
|
|
31
|
+
# size:}), what WorkingSet tells a file changed since by; for an ok
|
|
32
|
+
# memory_read, one entry per name it read, with the file that answered and
|
|
33
|
+
# the model overlay read with it (ToolRunner#memory_stamp).
|
|
33
34
|
ToolRun = Data.define(:output, :capped_output, :truncated, :activity, :images, :shown_params, :shown_label, :refs, :diff,
|
|
34
35
|
:output_file, :loaded, :stamp) do
|
|
35
36
|
def initialize(output: nil, capped_output: nil, truncated: nil, activity: nil, images: nil, shown_params: nil,
|
|
@@ -205,7 +206,8 @@ module Samagotchi
|
|
|
205
206
|
images: images&.any? ? images : nil,
|
|
206
207
|
shown_params: params && plugin_tool?(call[:name]) ? params : nil,
|
|
207
208
|
shown_label: label, refs: refs.empty? ? nil : refs, diff: diff, output_file: output_file,
|
|
208
|
-
loaded: result[:loaded],
|
|
209
|
+
loaded: result[:loaded],
|
|
210
|
+
stamp: status == "ok" ? file_stamp(call, context) || memory_stamp(result) : nil)
|
|
209
211
|
end
|
|
210
212
|
|
|
211
213
|
private
|
|
@@ -216,12 +218,46 @@ module Samagotchi
|
|
|
216
218
|
# can't place and a stat that fails or isn't a file's.
|
|
217
219
|
def file_stamp(call, context)
|
|
218
220
|
ref = FileRef.for(call[:name], call, cwd: session_cwd(context))
|
|
219
|
-
|
|
221
|
+
ref ? stat_entry(ref.path) : nil
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# A memory_read's stamp, one entry per name it read: the file that
|
|
225
|
+
# answered it, and the model overlay read with it when there was one, as
|
|
226
|
+
# they are right after the call:
|
|
227
|
+
# {memories: {name => {path:, mtime_ns:, size:, overlay: {…},
|
|
228
|
+
# requested_scope:, model_key:, fallback_model_key:, project_dir:}}}
|
|
229
|
+
# (no +overlay+ key when no overlay was read). Request metadata lets the
|
|
230
|
+
# working set re-resolve the answer; older path-only stamps compare those
|
|
231
|
+
# paths as before. Nothing at the top carries mtime_ns, so a reader that
|
|
232
|
+
# knows only file stamps (WorkingSet.stamp_of) sees none here, and a
|
|
233
|
+
# session saved before memory stamps has none (compat N-1).
|
|
234
|
+
# nil when the read answered no name (the index listing, every name
|
|
235
|
+
# missing), or none of its files can be stat'd.
|
|
236
|
+
def memory_stamp(result)
|
|
237
|
+
reads = result[:memory_reads]
|
|
238
|
+
return nil unless reads.is_a?(Hash)
|
|
239
|
+
|
|
240
|
+
entries = reads.filter_map do |name, read|
|
|
241
|
+
entry = stat_entry(read[:path])
|
|
242
|
+
next if entry.nil?
|
|
243
|
+
|
|
244
|
+
overlay = stat_entry(read[:overlay_path])
|
|
245
|
+
entry[:overlay] = overlay if overlay
|
|
246
|
+
entry.merge!(read.slice(:requested_scope, :model_key, :fallback_model_key, :project_dir))
|
|
247
|
+
[name, entry]
|
|
248
|
+
end
|
|
249
|
+
entries.empty? ? nil : { memories: entries.to_h }
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
# A file as it is now: {path:, mtime_ns:, size:}; nil for a path that is
|
|
253
|
+
# not a file or cannot be stat'd.
|
|
254
|
+
def stat_entry(path)
|
|
255
|
+
return nil if path.to_s.empty?
|
|
220
256
|
|
|
221
|
-
stat = File.stat(
|
|
257
|
+
stat = File.stat(path)
|
|
222
258
|
return nil unless stat.file?
|
|
223
259
|
|
|
224
|
-
{ path:
|
|
260
|
+
{ path: path, mtime_ns: (stat.mtime.to_i * 1_000_000_000) + stat.mtime.nsec, size: stat.size }
|
|
225
261
|
rescue SystemCallError
|
|
226
262
|
nil
|
|
227
263
|
end
|
|
@@ -473,9 +509,6 @@ module Samagotchi
|
|
|
473
509
|
end
|
|
474
510
|
|
|
475
511
|
def unreached_group_refusal(call, context)
|
|
476
|
-
# A review turn's boundary denies it first (and words why); a pair
|
|
477
|
-
# turn gets the owner's refusal.
|
|
478
|
-
return if context.participant? && context.participant_mode != ParticipantMode::PAIR
|
|
479
512
|
return if tools.nil?
|
|
480
513
|
|
|
481
514
|
surface = surface(context)
|
data/lib/samagotchi/toolkit.rb
CHANGED
|
@@ -63,9 +63,6 @@ module Samagotchi
|
|
|
63
63
|
FULL = "full"
|
|
64
64
|
LEAN = "lean"
|
|
65
65
|
CHILD = "child"
|
|
66
|
-
# A participant's turn (Guardrails::ReviewBoundary): the name is
|
|
67
|
-
# reserved, so config and plugins can't redefine the boundary.
|
|
68
|
-
REVIEW = "review"
|
|
69
66
|
|
|
70
67
|
# A name that can't be resolved (unknown, a cycle).
|
|
71
68
|
class Unresolved < StandardError; end
|
|
@@ -167,7 +164,7 @@ module Samagotchi
|
|
|
167
164
|
|
|
168
165
|
name = toolkit.name
|
|
169
166
|
seen = []
|
|
170
|
-
until seen.include?(name) ||
|
|
167
|
+
until seen.include?(name) || BUILTIN_KITS.key?(name)
|
|
171
168
|
seen << name
|
|
172
169
|
spec = config[name] || config[name.to_sym]
|
|
173
170
|
break unless spec.is_a?(Hash)
|
|
@@ -233,20 +230,13 @@ module Samagotchi
|
|
|
233
230
|
ask_user_question web_fetch context_read
|
|
234
231
|
].freeze
|
|
235
232
|
|
|
236
|
-
# The built-ins a participant's turn may call (REVIEW): reads only.
|
|
237
|
-
# ReviewBoundary denies everything else, whatever the session declares
|
|
238
|
-
# (D3: the declared tools stay the session's), the forget layer's
|
|
239
|
-
# forget_outputs too (a layer tool, declared with every kit).
|
|
240
|
-
REVIEW_TOOLS = %w[read memory_read].freeze
|
|
241
|
-
|
|
242
233
|
BUILTIN_KITS = [
|
|
243
234
|
Kit.new(name: FULL, tools: :all, groups: :all),
|
|
244
235
|
Kit.new(name: LEAN, tools: LEAN_TOOLS, groups: :all),
|
|
245
236
|
# A delegate child: it reports to its parent (send_note; the
|
|
246
237
|
# delegated-by line names it) and leaves memory writes to it, and
|
|
247
238
|
# starts no children of its own (removed: not even through call_tool).
|
|
248
|
-
Kit.new(name: CHILD, base: LEAN, add: %w[send_note], remove: %w[memory_write delegate delegate_result])
|
|
249
|
-
Kit.new(name: REVIEW, tools: REVIEW_TOOLS, groups: [])
|
|
239
|
+
Kit.new(name: CHILD, base: LEAN, add: %w[send_note], remove: %w[memory_write delegate delegate_result])
|
|
250
240
|
].to_h { |kit| [kit.name, kit] }.freeze
|
|
251
241
|
|
|
252
242
|
# What a toolkit declares, the Registry's filter: every registered tool,
|
|
@@ -365,18 +355,12 @@ module Samagotchi
|
|
|
365
355
|
.with(name: name, prompt: prompt.to_s.empty? ? nil : prompt, groups: groups)
|
|
366
356
|
end
|
|
367
357
|
|
|
368
|
-
# config.yml's toolkit.kits.review, refused: the review kit is the
|
|
369
|
-
# participants' boundary, which config may not widen.
|
|
370
|
-
RESERVED_REVIEW = "toolkit.kits.review: review is reserved for participants' turns (chi share); " \
|
|
371
|
-
"name your kit something else"
|
|
372
|
-
|
|
373
358
|
# The keys a config kit entry takes.
|
|
374
359
|
KIT_KEYS = %w[base add remove prompt groups description].freeze
|
|
375
360
|
|
|
376
361
|
# The kits known: the built-ins, then config's toolkit.kits, then the
|
|
377
362
|
# plugins' (chi.toolkit). A config kit with a built-in's name is left
|
|
378
|
-
# out (one it can't redefine
|
|
379
|
-
# reserved) with a line in +errors+, and so is one
|
|
363
|
+
# out (one it can't redefine) with a line in +errors+, and so is one
|
|
380
364
|
# that isn't a {base:, add:, remove:, prompt:, groups:, description:} map. One with another key
|
|
381
365
|
# (a typo, a tools: list) still loads, with a line naming the key, and
|
|
382
366
|
# so does one whose prompt isn't text or is over PROMPT_MAX characters:
|
|
@@ -392,9 +376,7 @@ module Samagotchi
|
|
|
392
376
|
kits = BUILTIN_KITS.dup
|
|
393
377
|
(config.is_a?(Hash) ? config : {}).each do |name, spec|
|
|
394
378
|
name = name.to_s
|
|
395
|
-
if name
|
|
396
|
-
errors << RESERVED_REVIEW
|
|
397
|
-
elsif kits.key?(name)
|
|
379
|
+
if kits.key?(name)
|
|
398
380
|
errors << "toolkit.kits.#{name}: #{name} is a built-in toolkit"
|
|
399
381
|
elsif !spec.is_a?(Hash)
|
|
400
382
|
errors << "toolkit.kits.#{name}: expected {base:, add:, remove:, prompt:, groups:, description:}"
|
|
@@ -45,12 +45,12 @@ module Samagotchi
|
|
|
45
45
|
Judged = Data.define(:suggestion, :would, :fields)
|
|
46
46
|
|
|
47
47
|
# The candidates with classifier descriptions, excluding the configured
|
|
48
|
-
# default, the
|
|
48
|
+
# default, the defer sentinel, and a
|
|
49
49
|
# kit that doesn't resolve (a missing base, a cycle): it is left out,
|
|
50
50
|
# not the whole ask, and the ≥ 2 clause counts what is left.
|
|
51
51
|
def self.candidates(kits, default_name:)
|
|
52
52
|
kits.values.filter_map do |kit|
|
|
53
|
-
next if [default_name.to_s,
|
|
53
|
+
next if [default_name.to_s, UNDECIDED].include?(kit.name)
|
|
54
54
|
next if kit.description.to_s.empty?
|
|
55
55
|
next unless resolves?(kit.name, kits)
|
|
56
56
|
|
|
@@ -83,7 +83,7 @@ module Samagotchi
|
|
|
83
83
|
# path, when the session already has a model reply.
|
|
84
84
|
def self.eligible?(session:, prompt:, author:, candidate_count:, last_suggestion:, defer_turns:, defer_tokens:)
|
|
85
85
|
return false unless session&.toolkit_origin == "default"
|
|
86
|
-
return false if session.delegate? || !OwnerPrompt.turn?(
|
|
86
|
+
return false if session.delegate? || !OwnerPrompt.turn?(author: author)
|
|
87
87
|
return false unless prompt.is_a?(String) && !prompt.strip.empty?
|
|
88
88
|
return false if value(candidate_count).to_i < 2
|
|
89
89
|
|
|
@@ -5,6 +5,7 @@ require_relative "../config"
|
|
|
5
5
|
require_relative "../log"
|
|
6
6
|
require_relative "../paths"
|
|
7
7
|
require_relative "../process_group"
|
|
8
|
+
require_relative "leftovers"
|
|
8
9
|
require_relative "output_guardrails"
|
|
9
10
|
require_relative "shell"
|
|
10
11
|
require_relative "task_runtime"
|
|
@@ -43,13 +44,11 @@ module Samagotchi
|
|
|
43
44
|
# mode a command at the limit is handed over (HANDED_OVER).
|
|
44
45
|
TIMEOUT_HINT = "(killed at the limit; for a long command use task_create, then task_wait)"
|
|
45
46
|
# How long the output may stay open once the shell has exited: a
|
|
46
|
-
# `server &` child keeps the
|
|
47
|
-
BACKGROUND_GRACE_SEC =
|
|
48
|
-
BACKGROUND_HINT =
|
|
49
|
-
"to keep a server or long job running, start it with task_create)"
|
|
47
|
+
# `server &` child keeps the logs (Leftovers).
|
|
48
|
+
BACKGROUND_GRACE_SEC = Leftovers::GRACE_SEC
|
|
49
|
+
BACKGROUND_HINT = Leftovers.execute_hint(:stopped)
|
|
50
50
|
# It had left the process group (setsid), so it is still running.
|
|
51
|
-
BACKGROUND_DETACHED_HINT =
|
|
52
|
-
"to keep a server or long job running, start it with task_create)"
|
|
51
|
+
BACKGROUND_DETACHED_HINT = Leftovers.execute_hint(:detached)
|
|
53
52
|
# What a command still running at the limit gets (execute.on_timeout):
|
|
54
53
|
# :task hands it over as a task (HANDED_OVER), :kill stops it.
|
|
55
54
|
ON_TIMEOUT = %i[task kill].freeze
|
|
@@ -321,51 +320,18 @@ module Samagotchi
|
|
|
321
320
|
# (`cmd &`) gets BACKGROUND_GRACE_SEC, then its group is stopped
|
|
322
321
|
# (BACKGROUND_HINT), unless it left the group (setsid) and holds them
|
|
323
322
|
# still (BACKGROUND_DETACHED_HINT, left running). One that holds none
|
|
324
|
-
# (`nohup cmd >f 2>&1 &`) is left alone, with no note.
|
|
323
|
+
# (`nohup cmd >f 2>&1 &`) is left alone, with no note (Leftovers.check).
|
|
325
324
|
# @return [Array(Symbol, Symbol|false)] the outcome (:done, :grace,
|
|
326
325
|
# :timeout, :cancelled) and the hint (:stopped, :detached or false)
|
|
327
326
|
def self.background_check(pgid, logs, deadline:, cancelled:)
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
outcome
|
|
331
|
-
return [outcome, false] if outcome == :done
|
|
332
|
-
|
|
333
|
-
# Only the group: the leader's pid is reaped, so it may name another process.
|
|
334
|
-
ProcessGroup.stop(pgid, grace: STOP_GRACE_SEC, poll: STOP_POLL_INTERVAL_SEC, stopped: -> { !held?(logs) })
|
|
335
|
-
return [outcome, false] unless outcome == :grace
|
|
336
|
-
|
|
337
|
-
[outcome, held?(logs) ? :detached : :stopped]
|
|
327
|
+
outcome = Leftovers.check(pgid, logs, grace: BACKGROUND_GRACE_SEC, stop_grace: STOP_GRACE_SEC,
|
|
328
|
+
poll: STOP_POLL_INTERVAL_SEC, deadline: deadline, cancelled: cancelled)
|
|
329
|
+
[outcome.wait, outcome.leftovers || false]
|
|
338
330
|
end
|
|
339
331
|
private_class_method :background_check
|
|
340
332
|
|
|
341
|
-
# Whether some process still has one of +paths+ open from the spawn
|
|
342
|
-
|
|
343
|
-
# fails while any inherited copy is open (~0.2 ms; no lsof, no /proc).
|
|
344
|
-
def self.held?(paths)
|
|
345
|
-
paths.any? do |path|
|
|
346
|
-
File.open(path, "r") do |file|
|
|
347
|
-
got = file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
348
|
-
file.flock(File::LOCK_UN) if got
|
|
349
|
-
!got
|
|
350
|
-
end
|
|
351
|
-
rescue Errno::ENOENT
|
|
352
|
-
false
|
|
353
|
-
end
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
# Polls the logs until no one holds them (+until_time+), the overall
|
|
357
|
-
# +deadline+ or a Stop. Returns :done, :grace, :timeout or :cancelled.
|
|
358
|
-
def self.await_release(paths, until_time:, deadline:, cancelled:)
|
|
359
|
-
loop do
|
|
360
|
-
return :done unless held?(paths)
|
|
361
|
-
return :cancelled if cancelled.call
|
|
362
|
-
return :timeout if deadline && monotonic_time > deadline
|
|
363
|
-
return :grace if monotonic_time > until_time
|
|
364
|
-
|
|
365
|
-
sleep(STOP_POLL_INTERVAL_SEC)
|
|
366
|
-
end
|
|
367
|
-
end
|
|
368
|
-
private_class_method :await_release
|
|
333
|
+
# Whether some process still has one of +paths+ open from the spawn (Leftovers.held?).
|
|
334
|
+
def self.held?(paths) = Leftovers.held?(paths)
|
|
369
335
|
|
|
370
336
|
# The whole log when it is under execute.truncate_at_bytes, else its
|
|
371
337
|
# head and tail only, read by seek (never the whole 100 MB).
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../process_group"
|
|
4
|
+
|
|
5
|
+
module Samagotchi
|
|
6
|
+
module Tools
|
|
7
|
+
# What a command's shell left running once it exited (`server &`), the
|
|
8
|
+
# rule execute applies (and task_create's tasks): chi holds the
|
|
9
|
+
# command's logs with a shared flock across the spawn, so every process
|
|
10
|
+
# the command started inherits them, and a fresh exclusive flock fails
|
|
11
|
+
# while any of them still has one open (held?). A holder gets a grace,
|
|
12
|
+
# then the command's group is stopped; one that left the group (setsid)
|
|
13
|
+
# and still holds them is out of reach and left running; one that holds
|
|
14
|
+
# none (`nohup cmd >f 2>&1 &`) was never in question.
|
|
15
|
+
module Leftovers
|
|
16
|
+
# How long the logs may stay held once the shell has exited.
|
|
17
|
+
GRACE_SEC = 1.5
|
|
18
|
+
POLL_SEC = 0.05
|
|
19
|
+
|
|
20
|
+
# What a check found and did. +wait+: :done (nothing held, or released
|
|
21
|
+
# within the grace), :grace (the grace ran out: the group was
|
|
22
|
+
# stopped), :timeout (the caller's deadline), :cancelled (its Stop).
|
|
23
|
+
# +leftovers+: :stopped, :detached (a holder outside the group, left
|
|
24
|
+
# running) or nil (nothing to say).
|
|
25
|
+
Outcome = Data.define(:wait, :leftovers)
|
|
26
|
+
|
|
27
|
+
# The hint's two endings, by what happened to the holder.
|
|
28
|
+
WHAT = { stopped: "was stopped", detached: "was left running" }.freeze
|
|
29
|
+
# What each tool's hint tells the model to do instead. Never just
|
|
30
|
+
# "with task_create": a `server &` there is stopped too. The task
|
|
31
|
+
# side never says "use task_create" (that is how it got there), and a
|
|
32
|
+
# holder left running is told how to end it.
|
|
33
|
+
EXECUTE_ADVICE = "to keep a server or long job running, start it with task_create in the foreground (no `&`)"
|
|
34
|
+
TASK_ADVICE = {
|
|
35
|
+
stopped: "to keep one running, run it in the foreground or end the command with `wait`",
|
|
36
|
+
detached: "it left the task's process group, so task_stop can't reach it: kill it if it shouldn't run; " \
|
|
37
|
+
"next time, run it in the foreground or end the command with `wait`"
|
|
38
|
+
}.freeze
|
|
39
|
+
|
|
40
|
+
module_function
|
|
41
|
+
|
|
42
|
+
# A tool's hint for +kind+ (:stopped, :detached): +held+ names what
|
|
43
|
+
# the process kept open ("the output", "the task's log"), +advice+
|
|
44
|
+
# what to do instead. The wording lives here, for every tool.
|
|
45
|
+
def hint(kind, held:, advice:) = "(a background process kept #{held} open and #{WHAT.fetch(kind)}; #{advice})"
|
|
46
|
+
|
|
47
|
+
# execute's hint (Execute::BACKGROUND_HINT, BACKGROUND_DETACHED_HINT).
|
|
48
|
+
def execute_hint(kind) = hint(kind, held: "the output", advice: EXECUTE_ADVICE)
|
|
49
|
+
|
|
50
|
+
# A task's hint (TaskRuntime.leftovers_note: task_get, task_wait, the
|
|
51
|
+
# finish note, task_list).
|
|
52
|
+
def task_hint(kind) = hint(kind, held: "the task's log", advice: TASK_ADVICE.fetch(kind))
|
|
53
|
+
|
|
54
|
+
# After the shell exited: a holder of +logs+ gets +grace+, then the
|
|
55
|
+
# group +pgid+ is stopped (stop_group), unless the deadline or
|
|
56
|
+
# +cancelled+ came first.
|
|
57
|
+
# @return [Outcome]
|
|
58
|
+
def check(pgid, logs, grace:, stop_grace:, poll:, deadline:, cancelled:)
|
|
59
|
+
return Outcome.new(wait: :done, leftovers: nil) unless held?(logs)
|
|
60
|
+
|
|
61
|
+
wait = await_release(logs, until_time: monotonic + grace, deadline: deadline, cancelled: cancelled, poll: poll)
|
|
62
|
+
return Outcome.new(wait: wait, leftovers: nil) if wait == :done
|
|
63
|
+
|
|
64
|
+
found = stop_group(pgid, logs, grace: stop_grace, poll: poll)
|
|
65
|
+
Outcome.new(wait: wait, leftovers: wait == :grace ? found : nil)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# Stops what is left of the group +pgid+ once its leader is dead and
|
|
69
|
+
# reaped: TERM, +grace+, KILL (ProcessGroup.stop, the groups its
|
|
70
|
+
# members started too). Only while the group still has members: a pgid
|
|
71
|
+
# isn't reused while its group has any (POSIX), so they are the
|
|
72
|
+
# command's; logs held with the group empty mean a holder that left it
|
|
73
|
+
# (setsid), and nothing is signalled. Done once the group is empty or
|
|
74
|
+
# nothing holds the logs, so no KILL goes out to an emptied group.
|
|
75
|
+
# @param kill_wait [Numeric, nil] ProcessGroup.stop's (nil: its default)
|
|
76
|
+
# @return [Symbol, nil] :stopped, :detached when the logs are still
|
|
77
|
+
# held, nil when the group was empty and nothing holds them (an
|
|
78
|
+
# earlier held? that read another probe's lock: nothing to say)
|
|
79
|
+
def stop_group(pgid, logs, grace:, poll:, kill_wait: nil)
|
|
80
|
+
alive = ProcessGroup.group_alive?(pgid)
|
|
81
|
+
if alive
|
|
82
|
+
options = kill_wait ? { kill_wait: kill_wait } : {}
|
|
83
|
+
ProcessGroup.stop(pgid, grace: grace, poll: poll, quiet: ProcessGroup::QUIET,
|
|
84
|
+
stopped: -> { !ProcessGroup.group_alive?(pgid) || !held?(logs) }, **options)
|
|
85
|
+
end
|
|
86
|
+
return :detached if held?(logs)
|
|
87
|
+
|
|
88
|
+
alive ? :stopped : nil
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Whether some process still has one of +paths+ open from the spawn:
|
|
92
|
+
# a flock belongs to the open file description, so an exclusive try
|
|
93
|
+
# fails while any inherited copy is open (~0.2 ms; no lsof, no /proc).
|
|
94
|
+
def held?(paths)
|
|
95
|
+
paths.any? do |path|
|
|
96
|
+
File.open(path, "r") do |file|
|
|
97
|
+
got = file.flock(File::LOCK_EX | File::LOCK_NB)
|
|
98
|
+
file.flock(File::LOCK_UN) if got
|
|
99
|
+
!got
|
|
100
|
+
end
|
|
101
|
+
rescue Errno::ENOENT
|
|
102
|
+
false
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# Polls the logs until no one holds them (+until_time+), the overall
|
|
107
|
+
# +deadline+ or a Stop. Returns :done, :grace, :timeout or :cancelled.
|
|
108
|
+
def await_release(paths, until_time:, deadline:, cancelled:, poll: POLL_SEC)
|
|
109
|
+
loop do
|
|
110
|
+
return :done unless held?(paths)
|
|
111
|
+
return :cancelled if cancelled.call
|
|
112
|
+
return :timeout if deadline && monotonic > deadline
|
|
113
|
+
return :grace if monotonic > until_time
|
|
114
|
+
|
|
115
|
+
sleep(poll)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
@@ -25,67 +25,130 @@ module Samagotchi
|
|
|
25
25
|
|
|
26
26
|
SEPARATOR = "\n\n---\n\n"
|
|
27
27
|
|
|
28
|
+
# A memory_read's text, and which file answered each name it read
|
|
29
|
+
# (+reads+: name => {path:, overlay_path:, requested_scope:, model_key:,
|
|
30
|
+
# fallback_model_key:, project_dir:}, absolute paths, the overlay only
|
|
31
|
+
# when one was read with the entry). A String, so every reader of the
|
|
32
|
+
# result reads the same bytes the model does; ToolRunner turns +reads+
|
|
33
|
+
# into the run's stamp (ToolRunner#memory_stamp).
|
|
34
|
+
class Result < String
|
|
35
|
+
attr_reader :reads
|
|
36
|
+
|
|
37
|
+
def initialize(text, reads = {})
|
|
38
|
+
super(text)
|
|
39
|
+
@reads = reads
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
Resolved = Data.define(:scope, :path, :overlay_path, :overlay_key)
|
|
44
|
+
|
|
45
|
+
# The entry and overlay that would answer +name+ from these explicit
|
|
46
|
+
# memory roots. Nil when no entry exists.
|
|
47
|
+
def self.resolve(name, project_dir:, system_dir:, scope: nil, model_key: nil, fallback_model_key: nil)
|
|
48
|
+
name = name.to_s.strip
|
|
49
|
+
return nil if name.empty? || invalid_name?(name)
|
|
50
|
+
|
|
51
|
+
scope = normalize_scope(scope)
|
|
52
|
+
roots = { "project" => project_dir, "system" => system_dir }
|
|
53
|
+
scopes = scope ? [scope] : %w[project system]
|
|
54
|
+
scopes.each do |resolved_scope|
|
|
55
|
+
root = roots[resolved_scope]
|
|
56
|
+
next unless root
|
|
57
|
+
|
|
58
|
+
path = File.join(root, "#{name}.md")
|
|
59
|
+
next unless File.exist?(path)
|
|
60
|
+
|
|
61
|
+
overlay_path = overlay_key = nil
|
|
62
|
+
[model_key, fallback_model_key].compact.each do |candidate|
|
|
63
|
+
key = candidate.to_s.strip
|
|
64
|
+
next if key.empty?
|
|
65
|
+
|
|
66
|
+
candidate_path = File.join(root, "#{name}.#{key}.md")
|
|
67
|
+
next unless File.exist?(candidate_path)
|
|
68
|
+
|
|
69
|
+
overlay_path = candidate_path
|
|
70
|
+
overlay_key = key
|
|
71
|
+
break
|
|
72
|
+
end
|
|
73
|
+
return Resolved.new(scope: resolved_scope, path: path, overlay_path: overlay_path, overlay_key: overlay_key)
|
|
74
|
+
end
|
|
75
|
+
nil
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.overlay_prefix(key) = "#{SEPARATOR}Model-specific guidance (#{key}):\n"
|
|
79
|
+
|
|
80
|
+
def self.append_overlay(body, overlay_text, key) = "#{body}#{overlay_prefix(key)}#{overlay_text}"
|
|
81
|
+
|
|
82
|
+
def self.overlay_key(name, path)
|
|
83
|
+
basename = File.basename(path.to_s)
|
|
84
|
+
prefix = "#{name}."
|
|
85
|
+
return nil unless basename.start_with?(prefix) && basename.end_with?(".md")
|
|
86
|
+
|
|
87
|
+
basename.delete_prefix(prefix).delete_suffix(".md")
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def self.overlay_tail(text, key)
|
|
91
|
+
at = text.to_s.rindex(overlay_prefix(key))
|
|
92
|
+
at ? text[at..] : ""
|
|
93
|
+
end
|
|
94
|
+
|
|
28
95
|
# @param fallback_model_key [String, nil] read when +model_key+ has no overlay
|
|
29
96
|
def self.call(entry_name, scope: nil, model_key: nil, fallback_model_key: nil)
|
|
30
97
|
entry_name = entry_name.to_s.strip
|
|
31
98
|
scope = normalize_scope(scope)
|
|
32
99
|
|
|
33
100
|
if entry_name.empty?
|
|
34
|
-
return scoped_index(scope) + (scope == "project" ? orphan_hint : "") if scope
|
|
101
|
+
return Result.new(scoped_index(scope) + (scope == "project" ? orphan_hint : "")) if scope
|
|
35
102
|
|
|
36
103
|
project = scoped_index("project") + orphan_hint
|
|
37
104
|
system = scoped_index("system")
|
|
38
|
-
return [
|
|
105
|
+
return Result.new([
|
|
39
106
|
"Project memories:",
|
|
40
107
|
project,
|
|
41
108
|
"",
|
|
42
109
|
"System memories:",
|
|
43
110
|
system
|
|
44
|
-
].join("\n")
|
|
111
|
+
].join("\n"))
|
|
45
112
|
end
|
|
46
113
|
|
|
47
114
|
names = parse_names(entry_name)
|
|
48
115
|
|
|
49
116
|
if names.empty?
|
|
50
|
-
return "Error: no memory names provided"
|
|
117
|
+
return Result.new("Error: no memory names provided")
|
|
51
118
|
end
|
|
52
119
|
|
|
53
120
|
bad = names.find { |name| invalid_name?(name) }
|
|
54
|
-
return invalid_name_error(bad) if bad
|
|
121
|
+
return Result.new(invalid_name_error(bad)) if bad
|
|
55
122
|
|
|
56
123
|
resolved_scopes = scope ? [scope] : %w[project system]
|
|
57
124
|
|
|
58
125
|
results = []
|
|
126
|
+
reads = {}
|
|
59
127
|
missing = []
|
|
128
|
+
project_dir = memories_dir("project") unless scope == "system"
|
|
129
|
+
system_dir = memories_dir("system") unless scope == "project"
|
|
60
130
|
|
|
61
131
|
names.each do |name|
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
body = File.read(path, encoding: "UTF-8")
|
|
68
|
-
# Append model-specific overlay if key is provided and overlay exists.
|
|
69
|
-
[model_key, fallback_model_key].compact.each do |key|
|
|
70
|
-
overlay_path = ModelOverlay.overlay_path_for(name, key, resolved_scope)
|
|
71
|
-
next unless overlay_path && File.exist?(overlay_path)
|
|
72
|
-
|
|
73
|
-
body += SEPARATOR + "Model-specific guidance (#{key}):\n" + File.read(overlay_path, encoding: "UTF-8")
|
|
74
|
-
break
|
|
75
|
-
end
|
|
76
|
-
results << body
|
|
77
|
-
found = true
|
|
78
|
-
break
|
|
132
|
+
resolved = resolve(name, scope: scope, model_key: model_key, fallback_model_key: fallback_model_key,
|
|
133
|
+
project_dir: project_dir, system_dir: system_dir)
|
|
134
|
+
unless resolved
|
|
135
|
+
missing << name
|
|
136
|
+
next
|
|
79
137
|
end
|
|
80
|
-
|
|
138
|
+
|
|
139
|
+
body = File.read(resolved.path, encoding: "UTF-8")
|
|
140
|
+
body = append_overlay(body, File.read(resolved.overlay_path, encoding: "UTF-8"), resolved.overlay_key) if resolved.overlay_path
|
|
141
|
+
results << body
|
|
142
|
+
reads[name] = { path: resolved.path, overlay_path: resolved.overlay_path, requested_scope: scope,
|
|
143
|
+
model_key: model_key, fallback_model_key: fallback_model_key, project_dir: project_dir }
|
|
81
144
|
end
|
|
82
145
|
|
|
83
|
-
return results.join(SEPARATOR) if missing.empty?
|
|
146
|
+
return Result.new(results.join(SEPARATOR), reads) if missing.empty?
|
|
84
147
|
|
|
85
148
|
# The hint goes after the first line: WorkingSet reads the missing
|
|
86
149
|
# names off it (WorkingSet::MISSING) and a failure off its "Error".
|
|
87
150
|
miss = "Error: memory not found: #{missing.join(", ")}#{orphan_hint if resolved_scopes.include?("project")}"
|
|
88
|
-
results.empty? ? miss : results.join(SEPARATOR) + SEPARATOR + miss
|
|
151
|
+
results.empty? ? Result.new(miss) : Result.new(results.join(SEPARATOR) + SEPARATOR + miss, reads)
|
|
89
152
|
rescue Errno::ENOENT => e
|
|
90
153
|
"Error: #{e.message}"
|
|
91
154
|
rescue StandardError => e
|
|
@@ -33,6 +33,7 @@ module Samagotchi
|
|
|
33
33
|
"output_path: #{record.output_path}",
|
|
34
34
|
# A handed-over execute's stderr has a log of its own.
|
|
35
35
|
("stderr_path: #{record.stderr_path}" if record.stderr_path),
|
|
36
|
+
TaskRuntime.leftovers_note(record),
|
|
36
37
|
(TaskRuntime::USER_STOP_NOTE if record.stopped_by_user?)
|
|
37
38
|
].compact.join("\n")
|
|
38
39
|
end
|
|
@@ -26,8 +26,9 @@ module Samagotchi
|
|
|
26
26
|
"pid: #{record.pid}",
|
|
27
27
|
"created_at: #{record.created_at}",
|
|
28
28
|
"command: #{preview(record.command)}",
|
|
29
|
-
"output_path: #{record.output_path}"
|
|
30
|
-
|
|
29
|
+
"output_path: #{record.output_path}",
|
|
30
|
+
TaskRuntime.leftovers_note(record)
|
|
31
|
+
].compact.join("\n")
|
|
31
32
|
end
|
|
32
33
|
private_class_method :format_entry
|
|
33
34
|
|
|
@@ -11,14 +11,16 @@ module Samagotchi
|
|
|
11
11
|
# The file keeps its key order (the members' order) and leaves out the
|
|
12
12
|
# optional keys while unset: +stderr_path+ and +handed_over_by+ (only a
|
|
13
13
|
# handed-over execute has them) and +stop_requested_by+ (only once a
|
|
14
|
-
# stop started)
|
|
14
|
+
# stop started) and +leftovers+ ("stopped" or "detached": what its
|
|
15
|
+
# shell left holding the log when it exited, Leftovers). bundles/slow-commands
|
|
16
|
+
# reads these files on its own.
|
|
15
17
|
TaskRecord = Data.define(
|
|
16
18
|
:metadata_version, :id, :command, :cwd, :workspace_root, :status, :pid, :pid_started,
|
|
17
19
|
:created_at, :started_at, :finished_at, :exit_code, :stop_reason,
|
|
18
|
-
:output_path, :stderr_path, :exit_code_path, :handed_over_by, :stop_requested_by
|
|
20
|
+
:output_path, :stderr_path, :exit_code_path, :handed_over_by, :stop_requested_by, :leftovers
|
|
19
21
|
) do
|
|
20
22
|
self::METADATA_VERSION = 1
|
|
21
|
-
self::OPTIONAL = %i[stderr_path handed_over_by stop_requested_by].freeze
|
|
23
|
+
self::OPTIONAL = %i[stderr_path handed_over_by stop_requested_by leftovers].freeze
|
|
22
24
|
|
|
23
25
|
# A record for a task that just started (task_create), or that an
|
|
24
26
|
# execute hands over (TaskRuntime.adopt fills in +pid_started+ then).
|
|
@@ -28,7 +30,7 @@ module Samagotchi
|
|
|
28
30
|
new(metadata_version: self::METADATA_VERSION, id: id, command: command, cwd: cwd, workspace_root: Dir.pwd,
|
|
29
31
|
status: "running", pid: pid, pid_started: pid_started, created_at: started_at, started_at: started_at,
|
|
30
32
|
finished_at: nil, exit_code: nil, stop_reason: nil, output_path: output_path, stderr_path: stderr_path,
|
|
31
|
-
exit_code_path: exit_code_path, handed_over_by: handed_over_by, stop_requested_by: nil)
|
|
33
|
+
exit_code_path: exit_code_path, handed_over_by: handed_over_by, stop_requested_by: nil, leftovers: nil)
|
|
32
34
|
end
|
|
33
35
|
|
|
34
36
|
# @param data [Hash] the parsed task.json; a key it lacks reads nil
|