samagotchi 0.105.0 → 0.106.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 +48 -1
- data/docs/architecture.md +6 -3
- data/docs/cli.md +49 -8
- data/docs/configuration.md +9 -5
- data/docs/guardrails.md +56 -0
- data/docs/hooks.md +9 -0
- data/docs/internals/engine.md +22 -4
- data/docs/internals/participants.md +206 -28
- data/docs/internals/prompt-caching.md +2 -1
- data/docs/internals/working-set.md +8 -2
- data/docs/plugins.md +2 -0
- data/docs/testing.md +3 -1
- data/lib/samagotchi/bridge/turn_accumulator.rb +13 -3
- data/lib/samagotchi/bridge.rb +35 -1
- data/lib/samagotchi/bridge_client.rb +4 -1
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +21 -9
- data/lib/samagotchi/config.rb +16 -1
- data/lib/samagotchi/continue_offer.rb +13 -3
- data/lib/samagotchi/delivery.rb +10 -0
- data/lib/samagotchi/engine.rb +62 -6
- data/lib/samagotchi/guardrail_wiring.rb +13 -3
- data/lib/samagotchi/guardrails/review_boundary.rb +148 -0
- data/lib/samagotchi/guardrails.rb +1 -0
- data/lib/samagotchi/hook_routes.rb +5 -2
- data/lib/samagotchi/hooks/registry.rb +12 -3
- data/lib/samagotchi/idle_recap.rb +9 -0
- data/lib/samagotchi/parent_report.rb +17 -5
- data/lib/samagotchi/participant_messages.rb +16 -0
- data/lib/samagotchi/participants.rb +29 -6
- data/lib/samagotchi/participants_watch.rb +90 -0
- data/lib/samagotchi/plugin/context.rb +7 -1
- data/lib/samagotchi/plugin/side_question.rb +9 -1
- data/lib/samagotchi/reply_wait/poll.rb +18 -3
- data/lib/samagotchi/run_options.rb +1 -1
- data/lib/samagotchi/send_command.rb +41 -3
- data/lib/samagotchi/session_access/contract.rb +2 -0
- data/lib/samagotchi/session_access/local.rb +31 -9
- data/lib/samagotchi/session_access/remote.rb +19 -2
- data/lib/samagotchi/session_access.rb +6 -3
- data/lib/samagotchi/session_commands.rb +6 -0
- data/lib/samagotchi/session_inbox.rb +31 -5
- data/lib/samagotchi/session_manager.rb +43 -3
- data/lib/samagotchi/session_metrics/side_totals.rb +21 -7
- data/lib/samagotchi/session_metrics.rb +88 -1
- data/lib/samagotchi/share_command.rb +61 -22
- data/lib/samagotchi/side_usage.rb +4 -2
- data/lib/samagotchi/steer.rb +37 -1
- data/lib/samagotchi/terminal_ui/attach_link.rb +5 -0
- data/lib/samagotchi/terminal_ui/attached_loop.rb +54 -7
- data/lib/samagotchi/terminal_ui/formatting.rb +66 -5
- data/lib/samagotchi/terminal_ui/local_link.rb +7 -0
- data/lib/samagotchi/terminal_ui/participant_commands.rb +77 -0
- data/lib/samagotchi/terminal_ui/remote_link.rb +6 -2
- data/lib/samagotchi/terminal_ui/status_row.rb +11 -2
- data/lib/samagotchi/terminal_ui.rb +5 -0
- data/lib/samagotchi/testing/host.rb +4 -2
- data/lib/samagotchi/testing/recorder.rb +3 -1
- data/lib/samagotchi/toolkit.rb +21 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +45 -20
- data/lib/samagotchi/web/grants.rb +85 -5
- data/lib/samagotchi/web/participant_access.rb +206 -35
- data/lib/samagotchi/web/participant_review.rb +165 -0
- data/lib/samagotchi/web/participant_stream_filter.rb +157 -0
- data/lib/samagotchi/web/participant_view.rb +415 -0
- data/lib/samagotchi/web/public/app.js +15 -1
- data/lib/samagotchi/web/public/bubbles.js +3 -3
- data/lib/samagotchi/web/public/data.js +20 -0
- data/lib/samagotchi/web/public/index.html +71 -27
- data/lib/samagotchi/web/public/info_bar.js +12 -5
- data/lib/samagotchi/web/public/info_bar_model.js +8 -5
- data/lib/samagotchi/web/public/participant.html +119 -0
- data/lib/samagotchi/web/public/participant.js +300 -0
- data/lib/samagotchi/web/public/participant_model.js +160 -0
- data/lib/samagotchi/web/public/participant_names.js +21 -0
- data/lib/samagotchi/web/public/participants_panel.js +311 -0
- data/lib/samagotchi/web/public/stream_handlers.js +8 -1
- data/lib/samagotchi/web/public/tokens.css +12 -0
- data/lib/samagotchi/web/public/turn_events.js +16 -5
- data/lib/samagotchi/web/public/turn_html.js +8 -4
- data/lib/samagotchi/web/public/turn_view.js +2 -1
- data/lib/samagotchi/web/public/working_set_view.js +3 -3
- data/lib/samagotchi/web/qr.rb +14 -0
- data/lib/samagotchi/web/running.rb +52 -0
- data/lib/samagotchi/web/server.rb +13 -0
- data/lib/samagotchi/web/session_access_routes.rb +3 -0
- data/lib/samagotchi/worker.rb +107 -5
- data/lib/samagotchi/worker_inbound.rb +12 -4
- metadata +15 -1
data/lib/samagotchi/config.rb
CHANGED
|
@@ -311,6 +311,11 @@ module Samagotchi
|
|
|
311
311
|
Entry.new(key: "web.annotate_presets", yaml_path: %w[web annotate_presets], type: :string, default: "Agreed|Could you please elaborate?", expose: %i[env config cli], lifecycle: :web_start),
|
|
312
312
|
# What a file ref (a tool row's file, a diff line) opens on this machine: a preset (vscode, vscode-insiders, cursor, zed, none) or a URL template with {path} and an optional {line} (Web::Editor).
|
|
313
313
|
Entry.new(key: "web.editor", yaml_path: %w[web editor], type: :string, default: "vscode", expose: %i[env config cli], lifecycle: :web_start),
|
|
314
|
+
# Participants (chi share): a new grant's lifetime (Nh or Nd, 1h to 30d), and what a grant without its own
|
|
315
|
+
# takes: its spending cap in dollars ($0 to $1000) and how many of its turns may wait (0 to 20). Read when used.
|
|
316
|
+
Entry.new(key: "web.participants.expires", yaml_path: %w[web participants expires], type: :string, default: "7d", expose: %i[env config], lifecycle: :next_matching_turn_display_poll_write),
|
|
317
|
+
Entry.new(key: "web.participants.cap", yaml_path: %w[web participants cap], type: :float, default: 2.0, expose: %i[env config], lifecycle: :next_matching_turn_display_poll_write),
|
|
318
|
+
Entry.new(key: "web.participants.queue", yaml_path: %w[web participants queue], type: :integer, default: 3, expose: %i[env config], lifecycle: :next_matching_turn_display_poll_write),
|
|
314
319
|
|
|
315
320
|
Entry.new(key: "no_interrupt", yaml_path: %w[no_interrupt], type: :bool, default: false, expose: %i[env config cli], lifecycle: :next_cli_run),
|
|
316
321
|
Entry.new(key: "no_default_input", yaml_path: %w[no_default_input], type: :bool, default: false, expose: %i[env config cli], lifecycle: :next_cli_run),
|
|
@@ -609,7 +614,7 @@ module Samagotchi
|
|
|
609
614
|
elsif MAP_ENTRY_KEYS.key?(key)
|
|
610
615
|
map_entry_problems(key, value)
|
|
611
616
|
elsif value.is_a?(Hash) && (known_sections.include?(key) || ENTRIES.any? { |e| e.yaml_path.size > 1 && e.section == key })
|
|
612
|
-
value.
|
|
617
|
+
value.flat_map { |leaf, inner| leaf_problems("#{key}.#{leaf}", inner) }
|
|
613
618
|
elsif known_sections.include?(key)
|
|
614
619
|
# An all-commented section is nil; recap: false turns recaps off.
|
|
615
620
|
value.nil? || (key == "recap" && value == false) ? [] : ["config: '#{key}' must be a mapping of settings; ignored"]
|
|
@@ -619,6 +624,16 @@ module Samagotchi
|
|
|
619
624
|
end + model_ref_problems(data)
|
|
620
625
|
end
|
|
621
626
|
|
|
627
|
+
# A section's leaf: a key the registry knows, or a mapping of a
|
|
628
|
+
# third level (web.participants.cap) whose keys it knows.
|
|
629
|
+
def leaf_problems(path, value)
|
|
630
|
+
if value.is_a?(Hash) && ENTRIES.any? { |e| e.key.start_with?("#{path}.") }
|
|
631
|
+
value.keys.filter_map { |leaf| key_problem("#{path}.#{leaf}") }
|
|
632
|
+
else
|
|
633
|
+
[key_problem(path)].compact
|
|
634
|
+
end
|
|
635
|
+
end
|
|
636
|
+
|
|
622
637
|
# What a model ref in config.yml won't do as written: an alias whose
|
|
623
638
|
# target is another alias (aliases apply once, ModelRef), a ref that
|
|
624
639
|
# starts with "<host>/" (only ':' names a host).
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require "time"
|
|
4
4
|
require_relative "last_turn"
|
|
5
5
|
require_relative "log"
|
|
6
|
+
require_relative "steer"
|
|
6
7
|
|
|
7
8
|
module Samagotchi
|
|
8
9
|
# A session worker's continue offer: what it announces when a turn runs out
|
|
@@ -34,12 +35,15 @@ module Samagotchi
|
|
|
34
35
|
# @param max_iterations [#call] (no_interrupt) → Integer
|
|
35
36
|
# @param queue_command [#call] (line, client_id:) queues a session
|
|
36
37
|
# command as a card's (Bridge#queue_command, card: true)
|
|
37
|
-
|
|
38
|
+
# +participant_refused+ (author → Boolean): whether a participant's
|
|
39
|
+
# turn may no longer run (revoked, expired, at the cap: Worker's check).
|
|
40
|
+
def initialize(engine:, turn_flow:, run_turn:, max_iterations:, queue_command: nil, participant_refused: ->(_) { false })
|
|
38
41
|
@engine = engine
|
|
39
42
|
@turn_flow = turn_flow
|
|
40
43
|
@run_turn = run_turn
|
|
41
44
|
@max_iterations = max_iterations
|
|
42
45
|
@queue_command = queue_command
|
|
46
|
+
@participant_refused = participant_refused
|
|
43
47
|
# A question was posted since the last close (it may have been
|
|
44
48
|
# answered or superseded meanwhile).
|
|
45
49
|
@asked = false
|
|
@@ -124,12 +128,18 @@ module Samagotchi
|
|
|
124
128
|
# never began leaves it to no other.
|
|
125
129
|
# The continue turn is the offer turn's: its origin carries that turn's
|
|
126
130
|
# enqueued_id, so its last_turn, its reply file and its checkpoint mark
|
|
127
|
-
# do too, and a wait on that turn (chi answer) gets its reply
|
|
131
|
+
# do too, and a wait on that turn (chi answer) gets its reply; and its
|
|
132
|
+
# author, so a participant's turn continues as theirs, inside the
|
|
133
|
+
# review boundary and on their spend (never as the owner's). One whose
|
|
134
|
+
# participant may no longer run a turn isn't continued.
|
|
128
135
|
def run_continue_turn(command)
|
|
129
136
|
offer = @turn_flow.offer
|
|
130
137
|
resume_from = @turn_flow.resume_from
|
|
131
138
|
enqueued_id = offer_turn_id
|
|
139
|
+
author = Steer.sender_author(@engine.session&.last_turn&.author)
|
|
132
140
|
close("answered")
|
|
141
|
+
return if author && @participant_refused.call(author)
|
|
142
|
+
|
|
133
143
|
steer = @continue_steer
|
|
134
144
|
@continue_steer = nil
|
|
135
145
|
begin
|
|
@@ -140,7 +150,7 @@ module Samagotchi
|
|
|
140
150
|
Log.warn(:worker, "turn_not_begun", continue: true, error: e.class.name, msg: e.message)
|
|
141
151
|
return announce_offer(offer)
|
|
142
152
|
end
|
|
143
|
-
origin = { client_id: command.client_id, enqueued_id: enqueued_id }.compact
|
|
153
|
+
origin = { client_id: command.client_id, enqueued_id: enqueued_id, author: author }.compact
|
|
144
154
|
@run_turn.call(nil, continue: true, origin: origin, resumed: true, resume_from: resume_from,
|
|
145
155
|
max_iterations: @max_iterations.call(offer[:no_interrupt])) do |result, error|
|
|
146
156
|
if error
|
data/lib/samagotchi/delivery.rb
CHANGED
|
@@ -48,6 +48,16 @@ module Samagotchi
|
|
|
48
48
|
"restart it (chi sessions restart ID, or the web's update badge) and send again"
|
|
49
49
|
}.freeze
|
|
50
50
|
|
|
51
|
+
# A worker that runs participants' turns inside the review boundary
|
|
52
|
+
# (Bridge::FEATURES): one without it would run theirs with the owner's
|
|
53
|
+
# tools, so none is sent to it.
|
|
54
|
+
REVIEW_FEATURE = "review_turns"
|
|
55
|
+
REVIEW_UNAVAILABLE = {
|
|
56
|
+
"error" => "review_unavailable",
|
|
57
|
+
"detail" => "this session's worker predates participants' turns, so it has no review boundary; " \
|
|
58
|
+
"the owner must restart it (chi sessions restart ID, or the web's update badge)"
|
|
59
|
+
}.freeze
|
|
60
|
+
|
|
51
61
|
# Whether it is the one every sender defaulting to a step boundary uses.
|
|
52
62
|
def self.next_step?(value) = parse(value) == NEXT_STEP
|
|
53
63
|
|
data/lib/samagotchi/engine.rb
CHANGED
|
@@ -85,6 +85,7 @@ require_relative "guardrail_wiring"
|
|
|
85
85
|
require_relative "plugin_tasks"
|
|
86
86
|
require_relative "prompt_warmup"
|
|
87
87
|
require_relative "working_set"
|
|
88
|
+
require_relative "participants_watch"
|
|
88
89
|
require_relative "session_participants"
|
|
89
90
|
|
|
90
91
|
module Samagotchi
|
|
@@ -224,6 +225,8 @@ module Samagotchi
|
|
|
224
225
|
@participants = SessionParticipants.new(state_dir: -> { session_state_dir })
|
|
225
226
|
# Whether the system prompt carries the participants line.
|
|
226
227
|
@participants_line = false
|
|
228
|
+
# Who is in the review now, and who came or went (#announce_participants).
|
|
229
|
+
@participants_watch = ParticipantsWatch.new(session_id: -> { @session&.id }, state_dir: -> { session_state_dir })
|
|
227
230
|
@session = nil
|
|
228
231
|
@session_observer = SessionObserver.new
|
|
229
232
|
@metrics = SessionMetrics.new
|
|
@@ -595,9 +598,12 @@ module Samagotchi
|
|
|
595
598
|
# recreate a deleted session's folder; the folder itself may not exist
|
|
596
599
|
# yet before a first turn). Safe from any thread: the recap's, a
|
|
597
600
|
# plugin's, the turn's.
|
|
601
|
+
# A side request counts against the participant who caused it
|
|
602
|
+
# (#side_author), its usage's own author when it names one.
|
|
598
603
|
# @param usage [SideUsage]
|
|
599
604
|
# @return [void]
|
|
600
605
|
def note_side_usage(usage)
|
|
606
|
+
usage = usage.with(author: side_author(usage.kind)) if usage.author.nil?
|
|
601
607
|
synchronize_events do
|
|
602
608
|
block = @metrics.add_side(usage)
|
|
603
609
|
@session_observer.notify(type: :side_usage, kind: usage.kind, side: block)
|
|
@@ -609,6 +615,24 @@ module Samagotchi
|
|
|
609
615
|
@metrics.persist(state_dir: session_state_dir)
|
|
610
616
|
end
|
|
611
617
|
|
|
618
|
+
# Who caused a side request of +kind+ now: the running turn's author (a
|
|
619
|
+
# plugin's ctx.ask_model in it), the last turn's for the idle recap
|
|
620
|
+
# (it recaps that turn); nil, the owner's, for anything else (an owner
|
|
621
|
+
# command's ask_model between turns).
|
|
622
|
+
# @return [String, nil]
|
|
623
|
+
def side_author(kind)
|
|
624
|
+
return nil unless turn_running? || kind.to_s == RECAP_SIDE_KIND
|
|
625
|
+
|
|
626
|
+
Steer.sender_author(@guardrail_wiring.turn_author)
|
|
627
|
+
end
|
|
628
|
+
|
|
629
|
+
RECAP_SIDE_KIND = "recap"
|
|
630
|
+
|
|
631
|
+
# The running turn's author when a participant wrote it, else nil
|
|
632
|
+
# (ctx.turn_author, the hooks' author).
|
|
633
|
+
# @return [String, nil]
|
|
634
|
+
def turn_author = turn_running? ? Steer.sender_author(@guardrail_wiring.turn_author) : nil
|
|
635
|
+
|
|
612
636
|
# @return [SessionMetrics] the per-session analytics collector
|
|
613
637
|
attr_reader :metrics
|
|
614
638
|
|
|
@@ -797,7 +821,7 @@ module Samagotchi
|
|
|
797
821
|
# UIs need in the event log, emitted outside any turn's stream.
|
|
798
822
|
ANNOUNCEABLE_EVENTS = %i[turn_enqueued turn_dequeued input_merged prompt_restored continue_offered continue_resolved
|
|
799
823
|
command_queued command_ran context_added card hook_notice guardrail_warning
|
|
800
|
-
plugin_init_started plugin_init_finished working_set_updated].freeze
|
|
824
|
+
plugin_init_started plugin_init_finished working_set_updated participants_changed].freeze
|
|
801
825
|
|
|
802
826
|
# Put a transport-level event into the ordered event log. Unlike turn
|
|
803
827
|
# events it reaches only persistent observers (no turn sink, no memory
|
|
@@ -1186,6 +1210,11 @@ module Samagotchi
|
|
|
1186
1210
|
price_host: model.host_name)
|
|
1187
1211
|
kit_prompt = kit_prompt_stats
|
|
1188
1212
|
snapshot = snapshot.merge(kit_prompt: kit_prompt) if kit_prompt
|
|
1213
|
+
# Each participant's spend under their name (participants.json's).
|
|
1214
|
+
if (spend = snapshot[:spend_by_author])
|
|
1215
|
+
named = spend.to_h { |author, row| [author, row.merge(name: @participants.name_for(author, session: @session))] }
|
|
1216
|
+
snapshot = snapshot.merge(spend_by_author: named)
|
|
1217
|
+
end
|
|
1189
1218
|
# The session's own settings (chi --set), as --set names them.
|
|
1190
1219
|
settings = @session&.settings&.flat || {}
|
|
1191
1220
|
snapshot = snapshot.merge(settings: settings) unless settings.empty?
|
|
@@ -1302,6 +1331,8 @@ module Samagotchi
|
|
|
1302
1331
|
# that served model under served: (#served_expected_by)
|
|
1303
1332
|
# :recap_enabled [Boolean] whether an idle recap is configured, with
|
|
1304
1333
|
# :recap_min_user_turns and :recap_inactivity_seconds (nil when not)
|
|
1334
|
+
# :participants [Array<Hash>] who is in the review now ({id:, name:,
|
|
1335
|
+
# kind:}; ParticipantsWatch#active), [] for nobody
|
|
1305
1336
|
def session_state_snapshot
|
|
1306
1337
|
served_pair = served_model(probe: false)
|
|
1307
1338
|
metrics = @metrics.snapshot
|
|
@@ -1330,7 +1361,8 @@ module Samagotchi
|
|
|
1330
1361
|
**setting_chips(SessionSetting::ALL, model),
|
|
1331
1362
|
recap_enabled: !@recap.nil?,
|
|
1332
1363
|
recap_min_user_turns: @recap&.min_user_turns,
|
|
1333
|
-
recap_inactivity_seconds: @recap&.inactivity&.to_i
|
|
1364
|
+
recap_inactivity_seconds: @recap&.inactivity&.to_i,
|
|
1365
|
+
participants: @participants_watch.active
|
|
1334
1366
|
}
|
|
1335
1367
|
end
|
|
1336
1368
|
|
|
@@ -1473,6 +1505,17 @@ module Samagotchi
|
|
|
1473
1505
|
**stop_facts(event))
|
|
1474
1506
|
end
|
|
1475
1507
|
|
|
1508
|
+
# A participant joined (invited) or left (revoked, expired) since the
|
|
1509
|
+
# last look: :participants_changed to the observers (ParticipantsWatch;
|
|
1510
|
+
# the worker looks on its idle tick). The first look announces nobody.
|
|
1511
|
+
# @return [ParticipantsWatch::Change, nil]
|
|
1512
|
+
def announce_participants
|
|
1513
|
+
change = @participants_watch.changes or return nil
|
|
1514
|
+
|
|
1515
|
+
announce(change.to_event)
|
|
1516
|
+
change
|
|
1517
|
+
end
|
|
1518
|
+
|
|
1476
1519
|
# Reads the session's participants at a turn's start; the first to join
|
|
1477
1520
|
# and the last to leave rebuild the system prompt (its participants
|
|
1478
1521
|
# line), once each: a turn in between sends the same prefix.
|
|
@@ -1640,7 +1683,8 @@ module Samagotchi
|
|
|
1640
1683
|
event_routes: @event_routes,
|
|
1641
1684
|
question_desk: -> { @question_desk },
|
|
1642
1685
|
cancel_controller: -> { active_cancel_controller },
|
|
1643
|
-
steer: ->(text, source) { steer(text, source: source) }
|
|
1686
|
+
steer: ->(text, source) { steer(text, source: source) },
|
|
1687
|
+
turn_author: -> { turn_author }
|
|
1644
1688
|
)
|
|
1645
1689
|
end
|
|
1646
1690
|
private :build_hook_routes
|
|
@@ -2105,6 +2149,9 @@ module Samagotchi
|
|
|
2105
2149
|
turn_started[:resumed] = true if turn.resumed?
|
|
2106
2150
|
turn_started[:images] = image_refs unless image_refs.empty?
|
|
2107
2151
|
turn_started[:cut_after] = steer_cut_after
|
|
2152
|
+
# A participant's turn names them (author, author_name) for the UIs'
|
|
2153
|
+
# badge; the owner's turn_started stays as it was.
|
|
2154
|
+
turn_started.merge!(author_fields(turn.origin, session: session))
|
|
2108
2155
|
emit_event(turn.on_event, turn.tag(turn_started))
|
|
2109
2156
|
raise image_error if image_error
|
|
2110
2157
|
|
|
@@ -2256,7 +2303,9 @@ module Samagotchi
|
|
|
2256
2303
|
# Inject due reminders as a tail system message (after history, before
|
|
2257
2304
|
# the new user prompt) to preserve prefix KV cache. Mutating the head
|
|
2258
2305
|
# system prompt invalidates cache for the entire prefix.
|
|
2259
|
-
|
|
2306
|
+
# Not into a participant's turn: the owner's reminders are the owner's,
|
|
2307
|
+
# and would be used up there under the review boundary.
|
|
2308
|
+
due_reminders = Steer.participant_author?(turn.origin&.dig(:author)) ? [] : @reminder_queue.inject!(turn.messages)
|
|
2260
2309
|
if due_reminders.any?
|
|
2261
2310
|
emit_event(turn.on_event, {
|
|
2262
2311
|
type: :reminder_injected,
|
|
@@ -2617,12 +2666,18 @@ module Samagotchi
|
|
|
2617
2666
|
end
|
|
2618
2667
|
|
|
2619
2668
|
# Restore the current session's conversation to a #messages_checkpoint.
|
|
2669
|
+
# Between turns the working set is announced when that changed it (a
|
|
2670
|
+
# !rollback or a canceled continue dropping the turn's tool calls, the
|
|
2671
|
+
# set emptied too), so the page doesn't keep the dropped items until
|
|
2672
|
+
# the next request.
|
|
2620
2673
|
# @param checkpoint [Array<Hash>]
|
|
2621
2674
|
# @return [Array<Hash>] the session's messages
|
|
2622
2675
|
def rollback_to(checkpoint)
|
|
2623
2676
|
raise ArgumentError, "no current session" unless @session
|
|
2624
2677
|
|
|
2625
|
-
replace_session_messages(@session, clone_messages(checkpoint))
|
|
2678
|
+
messages = replace_session_messages(@session, clone_messages(checkpoint))
|
|
2679
|
+
announce_working_set unless turn_running?
|
|
2680
|
+
messages
|
|
2626
2681
|
end
|
|
2627
2682
|
|
|
2628
2683
|
# Clone a messages array (shallow dup of each element).
|
|
@@ -2826,7 +2881,8 @@ module Samagotchi
|
|
|
2826
2881
|
prompt_files: -> { @prompt_builder&.prompt_files || [] },
|
|
2827
2882
|
frontend: -> { interface },
|
|
2828
2883
|
cost: -> { @metrics.cost },
|
|
2829
|
-
usage: -> { @metrics.usage }
|
|
2884
|
+
usage: -> { @metrics.usage },
|
|
2885
|
+
turn_author: -> { turn_author }
|
|
2830
2886
|
)
|
|
2831
2887
|
end
|
|
2832
2888
|
|
|
@@ -113,11 +113,21 @@ module Samagotchi
|
|
|
113
113
|
|
|
114
114
|
# The gate's core checks, in order. A delegate child's boundary comes
|
|
115
115
|
# before the rules, so its ask keeps the child-boundary rule id (and
|
|
116
|
-
# its once/session scopes) when a strict rule asks too.
|
|
116
|
+
# its once/session scopes) when a strict rule asks too. The review
|
|
117
|
+
# boundary comes before both: a participant's turn is denied what it
|
|
118
|
+
# may not do, never asked about it (deny is sticky, so no later vote
|
|
119
|
+
# can turn it into a question).
|
|
117
120
|
def checks
|
|
118
121
|
rules = self.rules
|
|
119
|
-
[@failures, rules.hook_asks, protected_paths,
|
|
120
|
-
child_boundary, rules].compact
|
|
122
|
+
[@failures, review_boundary, rules.hook_asks, protected_paths,
|
|
123
|
+
(@scratch_writes ||= Guardrails::ScratchWrites.new if @scratch), child_boundary, rules].compact
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# A participant's turn's boundary (Guardrails::ReviewBoundary): in
|
|
127
|
+
# every session, a no-op unless the running turn's author is a
|
|
128
|
+
# participant's.
|
|
129
|
+
def review_boundary
|
|
130
|
+
@review_boundary ||= Guardrails::ReviewBoundary.new(author: -> { turn_author }, root: -> { boundary_root })
|
|
121
131
|
end
|
|
122
132
|
|
|
123
133
|
# The delegate child's boundary (Guardrails::ChildBoundary); nil for
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "protected_paths"
|
|
4
|
+
require_relative "shell_paths"
|
|
5
|
+
require_relative "../paths"
|
|
6
|
+
require_relative "../toolkit"
|
|
7
|
+
require_relative "../steer"
|
|
8
|
+
|
|
9
|
+
module Samagotchi
|
|
10
|
+
module Guardrails
|
|
11
|
+
# A participant's turn may only read (D3, D4: it proposes, the owner
|
|
12
|
+
# applies). A core check, on in every guardrails mode and with
|
|
13
|
+
# guardrails.enabled: false, active only while the running turn's author
|
|
14
|
+
# is a participant (GuardrailWiring#checks); the owner's turns never get
|
|
15
|
+
# it. It denies, never asks (nobody on the participant's side may
|
|
16
|
+
# approve, and the owner's approval would be the participant's text
|
|
17
|
+
# acting with the owner's power), every call outside the review kit
|
|
18
|
+
# (Toolkit::REVIEW_TOOLS). Not forget_outputs: it would replace the
|
|
19
|
+
# owner's earlier tool outputs with a note of the participant's, which
|
|
20
|
+
# later owner turns read as their own output, with no review header.
|
|
21
|
+
# read: only under the session's project root, and never a file of
|
|
22
|
+
# the read denylist (chi's state and config dirs, home secret dirs,
|
|
23
|
+
# project secret files), checked on the path as given and as it
|
|
24
|
+
# resolves (a symlink in the project to ~/.ssh is ~/.ssh);
|
|
25
|
+
# memory_read: the project scope only, named (no fallback to system);
|
|
26
|
+
# anything else, call_tool's real target included: a call_tool of a
|
|
27
|
+
# built-in reaches the gate as that built-in (ToolRunner#unwrapped),
|
|
28
|
+
# so what is left of call_tool is a plugin or MCP tool, denied in v1.
|
|
29
|
+
# The declared tools stay the session's (no prompt rebuild per author),
|
|
30
|
+
# so a participant turn's model may still try a write: it is denied
|
|
31
|
+
# here, with the header's "This turn may only read" to keep it rare.
|
|
32
|
+
class ReviewBoundary
|
|
33
|
+
RULE = "review-boundary"
|
|
34
|
+
SOURCE = "core"
|
|
35
|
+
ALLOWED = Toolkit::REVIEW_TOOLS
|
|
36
|
+
PROJECT_SCOPE = "project"
|
|
37
|
+
|
|
38
|
+
# Home folders that hold keys and tokens, never read in a review turn.
|
|
39
|
+
HOME_SECRET_DIRS = %w[.ssh .gnupg .aws].freeze
|
|
40
|
+
# Project files that hold secrets, by a path part's name: .env*,
|
|
41
|
+
# netrc, keys and certificates, anything named credentials, package
|
|
42
|
+
# registry tokens (.npmrc, .pypirc), ssh keys, terraform state, a
|
|
43
|
+
# secrets file; and a git config (.git/config, a submodule's
|
|
44
|
+
# .git/modules/x/config), checked apart.
|
|
45
|
+
SECRET_FILE = /\A(?:\.env.*|[._]netrc|.*\.(?:pem|key|p12|pfx|tfstate(?:\.backup)?)|.*credentials.*|
|
|
46
|
+
\.npmrc|\.pypirc|\.git-credentials|id_(?:rsa|dsa|ecdsa|ed25519)(?:\.pub)?|secrets?\.(?:ya?ml|json))\z/xi
|
|
47
|
+
# The .docker/config.json kind: a dot dir's config holding tokens.
|
|
48
|
+
SECRET_DIR_FILES = { ".docker" => "config.json", ".kube" => "config" }.freeze
|
|
49
|
+
GIT_DIR = ".git"
|
|
50
|
+
|
|
51
|
+
ADVICE = "This is a participant's review turn: answer from what you can read and propose any change as " \
|
|
52
|
+
"text or a diff for the owner to apply."
|
|
53
|
+
NOT_IN_KIT = "a participant's review turn may only read (read, memory_read): %s is not allowed"
|
|
54
|
+
OUTSIDE = "a participant's review turn reads only inside the session's project"
|
|
55
|
+
SECRET = "a participant's review turn can't read secrets or chi's own files"
|
|
56
|
+
HOME_ROOT = "a participant's review turn reads only inside a project, and this session's folder is the " \
|
|
57
|
+
"home folder or above it"
|
|
58
|
+
MEMORY_SCOPE = "a participant's review turn reads project memories only: call memory_read with scope: project"
|
|
59
|
+
|
|
60
|
+
# @param author [#call] → String, nil: the running turn's author
|
|
61
|
+
# (GuardrailWiring#turn_author); the check is off unless it is a
|
|
62
|
+
# participant's (Steer.participant_author?)
|
|
63
|
+
# @param root [#call] → String, nil: the session's project root (the
|
|
64
|
+
# work tree top of its folder); nil denies every read
|
|
65
|
+
# @param denied_dirs [#call] → Array<String>: chi's own dirs (state,
|
|
66
|
+
# config: sessions, grants, the web token, memories and .bundles,
|
|
67
|
+
# config.yml, hooks)
|
|
68
|
+
# @param home [#call] → String, nil
|
|
69
|
+
def initialize(author:, root:, denied_dirs: -> { ReviewBoundary.chi_dirs }, home: -> { Dir.home })
|
|
70
|
+
@author = author
|
|
71
|
+
@root = root
|
|
72
|
+
@denied_dirs = denied_dirs
|
|
73
|
+
@home = home
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# chi's state dir and config dir, as Paths has them now.
|
|
77
|
+
def self.chi_dirs = [Paths.state_dir, File.join(Paths.config_home, Paths::APP_DIR)]
|
|
78
|
+
|
|
79
|
+
# Vote on +verdict+ (its call and targets).
|
|
80
|
+
def check(verdict)
|
|
81
|
+
return verdict unless Steer.participant_author?(@author.call)
|
|
82
|
+
|
|
83
|
+
reason = refusal(verdict)
|
|
84
|
+
return verdict unless reason
|
|
85
|
+
|
|
86
|
+
verdict.deny!(reason, rule: RULE, source: SOURCE, decided_by: "core", advice: ADVICE)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
private
|
|
90
|
+
|
|
91
|
+
def refusal(verdict)
|
|
92
|
+
call = verdict.call || {}
|
|
93
|
+
tool = call[:name].to_s
|
|
94
|
+
return format(NOT_IN_KIT, tool_label(tool, verdict.targets)) unless ALLOWED.include?(tool)
|
|
95
|
+
|
|
96
|
+
case tool
|
|
97
|
+
when "read" then read_refusal(verdict.targets&.paths || [])
|
|
98
|
+
when "memory_read" then MEMORY_SCOPE unless call[:scope].to_s.strip.downcase == PROJECT_SCOPE
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# A plugin or MCP tool behind call_tool names what it acts as.
|
|
103
|
+
def tool_label(tool, targets)
|
|
104
|
+
acts_as = targets&.acts_as
|
|
105
|
+
acts_as && acts_as != tool ? "#{tool} (#{acts_as})" : tool
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def read_refusal(paths)
|
|
109
|
+
return OUTSIDE if paths.empty?
|
|
110
|
+
|
|
111
|
+
root = @root.call.to_s
|
|
112
|
+
return OUTSIDE if root.empty?
|
|
113
|
+
|
|
114
|
+
real_root = ProtectedPaths.real(root)
|
|
115
|
+
home = @home.call.to_s
|
|
116
|
+
return HOME_ROOT if !home.empty? && ShellPaths.within?(ProtectedPaths.real(home), real_root)
|
|
117
|
+
|
|
118
|
+
paths.each do |path|
|
|
119
|
+
given = File.expand_path(path)
|
|
120
|
+
real = ProtectedPaths.real(given)
|
|
121
|
+
return OUTSIDE unless ShellPaths.within?(real, real_root)
|
|
122
|
+
return SECRET if secret?(given, home) || secret?(real, home)
|
|
123
|
+
end
|
|
124
|
+
nil
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def secret?(path, home)
|
|
128
|
+
dirs = @denied_dirs.call.compact.map { |dir| File.expand_path(dir) }
|
|
129
|
+
dirs += dirs.map { |dir| ProtectedPaths.real(dir) }
|
|
130
|
+
dirs += HOME_SECRET_DIRS.map { |dir| File.join(home, dir) } unless home.empty?
|
|
131
|
+
return true if dirs.any? { |dir| ShellPaths.within?(path, dir) }
|
|
132
|
+
|
|
133
|
+
parts = path.split(File::SEPARATOR)
|
|
134
|
+
return true if parts.any? { |part| SECRET_FILE.match?(part) }
|
|
135
|
+
return true if parts.each_cons(2).any? { |dir, file| SECRET_DIR_FILES[dir.downcase] == file.downcase }
|
|
136
|
+
|
|
137
|
+
git_config?(parts)
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A config file anywhere under a .git dir (.git/config,
|
|
141
|
+
# .git/modules/<sub>/config, a worktree's config.worktree).
|
|
142
|
+
def git_config?(parts)
|
|
143
|
+
git = parts.index { |part| part.casecmp?(GIT_DIR) } or return false
|
|
144
|
+
parts[(git + 1)..].any? { |part| part.downcase.start_with?("config") }
|
|
145
|
+
end
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
@@ -13,6 +13,7 @@ require_relative "guardrails/approvals"
|
|
|
13
13
|
require_relative "guardrails/protected_paths"
|
|
14
14
|
require_relative "guardrails/scratch_writes"
|
|
15
15
|
require_relative "guardrails/child_boundary"
|
|
16
|
+
require_relative "guardrails/review_boundary"
|
|
16
17
|
require_relative "guardrails/load_failures"
|
|
17
18
|
require_relative "guardrails/rules"
|
|
18
19
|
require_relative "guardrails/gate"
|
|
@@ -22,11 +22,13 @@ module Samagotchi
|
|
|
22
22
|
# @param question_desk [#call] → QuestionDesk
|
|
23
23
|
# @param cancel_controller [#call] → the running turn's CancellationController, nil
|
|
24
24
|
# @param steer [#call] (text, source) → steers the running turn
|
|
25
|
-
|
|
25
|
+
# @param turn_author [#call] → the running turn's participant author, nil
|
|
26
|
+
def initialize(event_routes:, question_desk:, cancel_controller:, steer:, turn_author: -> {})
|
|
26
27
|
@event_routes = event_routes
|
|
27
28
|
@question_desk = question_desk
|
|
28
29
|
@cancel_controller = cancel_controller
|
|
29
30
|
@steer = steer
|
|
31
|
+
@turn_author = turn_author
|
|
30
32
|
@unknown_fallbacks = Set.new
|
|
31
33
|
end
|
|
32
34
|
|
|
@@ -40,7 +42,8 @@ module Samagotchi
|
|
|
40
42
|
ask_user: ->(question) { ask_user(question) },
|
|
41
43
|
stop_turn: ->(reason:, hook:) { stop_turn(reason, hook) },
|
|
42
44
|
steer: ->(text:, hook:) { @steer.call(text, source(hook)) },
|
|
43
|
-
stop_generation: ->(reason:, hook:) { stop_generation(reason, hook) }
|
|
45
|
+
stop_generation: ->(reason:, hook:) { stop_generation(reason, hook) },
|
|
46
|
+
turn_author: @turn_author
|
|
44
47
|
)
|
|
45
48
|
end
|
|
46
49
|
|
|
@@ -119,9 +119,12 @@ module Samagotchi
|
|
|
119
119
|
# cancels the running turn (true when it did); +steer+ takes (text:,
|
|
120
120
|
# hook:) and puts the text into the running turn (Engine#steer, true
|
|
121
121
|
# when queued); +stop_generation+ takes (reason:, hook:) and cuts the
|
|
122
|
-
# streaming generation while the turn goes on (true when it did)
|
|
123
|
-
#
|
|
124
|
-
|
|
122
|
+
# streaming generation while the turn goes on (true when it did);
|
|
123
|
+
# +turn_author+ returns the running turn's author when a participant
|
|
124
|
+
# wrote it ("p:<id>"), nil for the owner's (every event of such a turn
|
|
125
|
+
# carries it as author:). A registry without one gives hooks no-op
|
|
126
|
+
# helpers.
|
|
127
|
+
Runtime = Struct.new(:notify, :ask_user, :stop_turn, :steer, :stop_generation, :turn_author, keyword_init: true)
|
|
125
128
|
|
|
126
129
|
# A thread-safe registry for named hook callbacks.
|
|
127
130
|
#
|
|
@@ -386,6 +389,12 @@ module Samagotchi
|
|
|
386
389
|
def with_runtime(event)
|
|
387
390
|
return unless event.is_a?(Hash)
|
|
388
391
|
|
|
392
|
+
# A participant's turn: its events say whose it is (the owner's
|
|
393
|
+
# carry no author:, as before).
|
|
394
|
+
if !event.key?(:author) && (author = @runtime&.turn_author&.call)
|
|
395
|
+
event[:author] = author
|
|
396
|
+
end
|
|
397
|
+
|
|
389
398
|
event[:notify] ||= lambda { |text, level: :info, fallback_for: nil|
|
|
390
399
|
notice = { text: text.to_s, level: level, hook: event[:hook] }
|
|
391
400
|
notice[:fallback_for] = fallback_for unless fallback_for.nil?
|
|
@@ -55,6 +55,8 @@ module Samagotchi
|
|
|
55
55
|
TOOL_OUTPUT_SEPARATOR = "\n\n---\n\n"
|
|
56
56
|
TOOL_OUTPUT_HEADER_RE = /\A\[([\w.:-]+)\]/
|
|
57
57
|
DELEGATE_REPORT_LINE = "(a delegate session's report, delivered by chi; not the user's words)"
|
|
58
|
+
# A participant's message (chi share): a reviewer's, not the user's.
|
|
59
|
+
PARTICIPANT_LINE = "(a review contribution from %s, a participant; not the user's words)"
|
|
58
60
|
|
|
59
61
|
module_function
|
|
60
62
|
|
|
@@ -75,6 +77,8 @@ module Samagotchi
|
|
|
75
77
|
# A delegate child's report (ChildReports) is chi's news, not
|
|
76
78
|
# what the user said.
|
|
77
79
|
content = "#{DELEGATE_REPORT_LINE}\n#{content}" if message["source"].to_s == Steer::DELEGATE_REPORT
|
|
80
|
+
# A participant's is theirs, by the name it was saved with.
|
|
81
|
+
content = "#{participant_line(message)}\n#{content}" if Steer.participant_message?(message)
|
|
78
82
|
images = ImageRef.placeholder_only?(content) ? [] : image_lines(message["images"])
|
|
79
83
|
[content, *images].reject(&:empty?).join("\n")
|
|
80
84
|
when "model", "assistant"
|
|
@@ -97,6 +101,11 @@ module Samagotchi
|
|
|
97
101
|
end
|
|
98
102
|
end
|
|
99
103
|
|
|
104
|
+
def participant_line(message)
|
|
105
|
+
name = ParticipantName.clean(message["author_name"])
|
|
106
|
+
format(PARTICIPANT_LINE, name.empty? ? "a participant" : name)
|
|
107
|
+
end
|
|
108
|
+
|
|
100
109
|
def image_lines(images)
|
|
101
110
|
Array(images).filter_map { |ref| "[image #{ref["name"] || File.basename(ref["file"].to_s)}]" if ref.is_a?(Hash) }
|
|
102
111
|
end
|
|
@@ -33,14 +33,20 @@ module Samagotchi
|
|
|
33
33
|
end
|
|
34
34
|
end
|
|
35
35
|
|
|
36
|
+
# A participant's turn waiting on the session's owner (a question or
|
|
37
|
+
# approval it raised, the owner's step-limit Continue offer it is held
|
|
38
|
+
# for): what chi web's participant wait says, and how it reads here.
|
|
39
|
+
WAITING_FOR_OWNER = "waiting for the session's owner"
|
|
40
|
+
|
|
36
41
|
module_function
|
|
37
42
|
|
|
38
43
|
# @param result [ReplyWait::Result]
|
|
39
44
|
# @return [String] answered, question, failed, canceled, limit (the
|
|
40
45
|
# turn ran out of iterations, nobody asked), not_continued (a Stop
|
|
41
46
|
# answered the step-limit question), removed (the queued message was
|
|
42
|
-
# taken back before it ran), no_answer, error, worker_gone, stopped
|
|
43
|
-
#
|
|
47
|
+
# taken back before it ran), no_answer, error, worker_gone, stopped,
|
|
48
|
+
# waiting_for_owner (a participant's turn waits on the owner: chi
|
|
49
|
+
# web's participant wait, through --remote) or running
|
|
44
50
|
def status(result)
|
|
45
51
|
case result.status
|
|
46
52
|
when :done then "answered"
|
|
@@ -57,6 +63,7 @@ module Samagotchi
|
|
|
57
63
|
when :error then "error"
|
|
58
64
|
when :worker_gone then "worker_gone"
|
|
59
65
|
when :stopped then "stopped"
|
|
66
|
+
when :waiting_for_owner then "waiting_for_owner"
|
|
60
67
|
# ReplyWait's own :canceled is the caller giving up (its cancelled:),
|
|
61
68
|
# and :timeout the deadline: the turn goes on either way.
|
|
62
69
|
else "running"
|
|
@@ -64,12 +71,13 @@ module Samagotchi
|
|
|
64
71
|
end
|
|
65
72
|
|
|
66
73
|
# @return [Integer] 0 answered (or not continued: the Stop asked for
|
|
67
|
-
# was carried out), 3 a question waits,
|
|
74
|
+
# was carried out), 3 a question waits (or, for a participant, the
|
|
75
|
+
# owner: waiting_for_owner), 4 still running, 1 anything
|
|
68
76
|
# else
|
|
69
77
|
def exit_status(result)
|
|
70
78
|
case status(result)
|
|
71
79
|
when "answered", "not_continued" then CLI::Exit::OK
|
|
72
|
-
when "question" then CLI::Exit::QUESTION
|
|
80
|
+
when "question", "waiting_for_owner" then CLI::Exit::QUESTION
|
|
73
81
|
when "running" then CLI::Exit::RUNNING
|
|
74
82
|
else CLI::Exit::FAILED
|
|
75
83
|
end
|
|
@@ -304,8 +312,12 @@ module Samagotchi
|
|
|
304
312
|
when :error then "the worker failed: #{result.text}; #{shows} shows what happened"
|
|
305
313
|
when :worker_gone then "the worker is gone; #{shows} shows what happened"
|
|
306
314
|
when :stopped then "the session was stopped (chi sessions stop)"
|
|
315
|
+
when :waiting_for_owner
|
|
316
|
+
turn = turn_id || result.turn_id
|
|
317
|
+
turn.to_s.empty? ? WAITING_FOR_OWNER : "#{WAITING_FOR_OWNER}: #{via.command("send --wait --turn #{turn} #{session_id}")}"
|
|
307
318
|
else
|
|
308
|
-
|
|
319
|
+
turn = turn_id || result.turn_id
|
|
320
|
+
wait_again = via.command("send --wait --turn #{turn} #{session_id}") unless turn.to_s.empty?
|
|
309
321
|
hint = wait_again || shows
|
|
310
322
|
timeout ? "still running after #{format("%g", timeout)} s: #{hint}" : "still running: #{hint}"
|
|
311
323
|
end
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Samagotchi
|
|
4
|
+
# The words a participant is refused with, one per condition, wherever
|
|
5
|
+
# it is met: chi web's routes, the Bridge, the worker, and the
|
|
6
|
+
# SessionAccess errors a chi --remote client raises for the same codes.
|
|
7
|
+
# No requires, so any file can use it.
|
|
8
|
+
module ParticipantMessages
|
|
9
|
+
# 403 not_permitted: a route, a command or a shell line only the owner may use.
|
|
10
|
+
NOT_PERMITTED = "a participant can't do that: only the session's owner can"
|
|
11
|
+
# 429 too_many_turns: their running turn and its queue are full.
|
|
12
|
+
TOO_MANY_TURNS = "this participant has too many turns waiting: wait for one to finish"
|
|
13
|
+
# 429 cap_reached: their grant spent its cap.
|
|
14
|
+
CAP_REACHED = "this participant's spending cap is reached: ask the session's owner to raise it"
|
|
15
|
+
end
|
|
16
|
+
end
|