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
|
@@ -4,6 +4,7 @@ require "fileutils"
|
|
|
4
4
|
require "json"
|
|
5
5
|
require "time"
|
|
6
6
|
require_relative "atomic_file"
|
|
7
|
+
require_relative "participant_messages"
|
|
7
8
|
require_relative "archive_store"
|
|
8
9
|
require_relative "session"
|
|
9
10
|
require_relative "web/grants"
|
|
@@ -26,11 +27,16 @@ module Samagotchi
|
|
|
26
27
|
module Participants
|
|
27
28
|
FILE = "participants.json"
|
|
28
29
|
LOG = "participants.jsonl"
|
|
30
|
+
# What a participant is told when they try what only the owner may do:
|
|
31
|
+
# a route (chi web's 403 not_permitted), a command or a shell line.
|
|
32
|
+
NOT_PERMITTED = ParticipantMessages::NOT_PERMITTED
|
|
29
33
|
|
|
30
34
|
# One participant as the sidecar has them. +removed_at+ when the grant
|
|
31
35
|
# was revoked (nil while it works, or once it simply expired:
|
|
32
|
-
# +expires_at+ says when).
|
|
33
|
-
|
|
36
|
+
# +expires_at+ says when). +cap+: their spending cap in dollars (the
|
|
37
|
+
# worker's check before their queued turn starts), nil in a sidecar
|
|
38
|
+
# from before caps.
|
|
39
|
+
Entry = Data.define(:id, :kind, :name, :role, :added_at, :expires_at, :removed_at, :cap) do
|
|
34
40
|
def self.from_h(hash) = new(**members.to_h { |key| [key, hash[key.to_s]] })
|
|
35
41
|
|
|
36
42
|
# Still in the review at +now+.
|
|
@@ -65,7 +71,8 @@ module Samagotchi
|
|
|
65
71
|
|
|
66
72
|
entries = grants.map do |grant|
|
|
67
73
|
{ "id" => grant.id, "kind" => grant.kind, "name" => grant.name, "role" => grant.role,
|
|
68
|
-
"added_at" => grant.created_at, "expires_at" => grant.expires_at, "removed_at" => grant.revoked_at
|
|
74
|
+
"added_at" => grant.created_at, "expires_at" => grant.expires_at, "removed_at" => grant.revoked_at,
|
|
75
|
+
"cap" => grant.spend_cap }
|
|
69
76
|
end
|
|
70
77
|
AtomicFile.write(File.join(dir, FILE), "#{JSON.pretty_generate({ "participants" => entries })}\n", perm: 0o600)
|
|
71
78
|
end
|
|
@@ -85,20 +92,36 @@ module Samagotchi
|
|
|
85
92
|
# @return [Web::Grants::Issued]
|
|
86
93
|
# @raise [Web::Grants::Invalid] an archived session, or bad values
|
|
87
94
|
# @raise [ArgumentError] no such session
|
|
88
|
-
def invite(grants, session_id:, name:, kind: "human", ttl: nil, state_dir: Session.default_state_dir)
|
|
95
|
+
def invite(grants, session_id:, name:, kind: "human", ttl: nil, cap: nil, state_dir: Session.default_state_dir)
|
|
89
96
|
raise ArgumentError, "Session not found: #{session_id}" unless File.exist?(Session.session_file(session_id, state_dir: state_dir))
|
|
90
97
|
if ArchiveStore.archived?(Session.session_dir(session_id, state_dir: state_dir))
|
|
91
98
|
raise Web::Grants::Invalid, "session #{session_id[0, 8]} is archived: unarchive it to invite anyone"
|
|
92
99
|
end
|
|
93
100
|
|
|
94
|
-
issued = grants.issue(session_id: session_id, name: name, kind: kind,
|
|
101
|
+
issued = grants.issue(session_id: session_id, name: name, kind: kind, ttl: ttl, cap: cap)
|
|
95
102
|
write(session_id, grants.list(session_id: session_id), state_dir: state_dir)
|
|
96
103
|
grant = issued.grant
|
|
97
104
|
log(session_id, :issued, state_dir: state_dir, participant: grant.id, name: grant.name, kind: grant.kind,
|
|
98
|
-
role: grant.role, expires_at: grant.expires_at)
|
|
105
|
+
role: grant.role, expires_at: grant.expires_at, cap: grant.spend_cap)
|
|
99
106
|
issued
|
|
100
107
|
end
|
|
101
108
|
|
|
109
|
+
# Sets participant +id+'s cap on +session_id+ (chi share cap): the
|
|
110
|
+
# grant, the sidecar (the worker's check) and the audit line.
|
|
111
|
+
# @return [Web::Grants::Grant, nil] nil when +id+ isn't an active grant there
|
|
112
|
+
# @raise [Web::Grants::Invalid] a cap out of range
|
|
113
|
+
def set_cap(grants, session_id:, id:, cap:, state_dir: Session.default_state_dir)
|
|
114
|
+
grant = grants.set_cap(id, cap, session_id: session_id) or return nil
|
|
115
|
+
|
|
116
|
+
write(session_id, grants.list(session_id: session_id), state_dir: state_dir)
|
|
117
|
+
log(session_id, :cap, state_dir: state_dir, participant: grant.id, cap: grant.spend_cap)
|
|
118
|
+
grant
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
# The refusal for a cap change of +id+, no active participant of
|
|
122
|
+
# +session_id+.
|
|
123
|
+
def no_participant(id, session_id) = "no active participant #{id} in #{session_id.to_s[0, 8]} (chi share ls lists them)"
|
|
124
|
+
|
|
102
125
|
# Revokes grants (Web::Grants#revoke's arguments) and brings each
|
|
103
126
|
# affected session's sidecar and audit trail along.
|
|
104
127
|
# @return [Array<Web::Grants::Grant>] the grants it revoked
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Samagotchi
|
|
4
|
+
# Loaded only when a sidecar is read: it brings Web::Grants (and Rack).
|
|
5
|
+
autoload :Participants, File.expand_path("participants", __dir__)
|
|
6
|
+
|
|
7
|
+
# Who is in a session's review now, and who came or went since the last
|
|
8
|
+
# look: participants.json (Participants.read) read again when its mtime
|
|
9
|
+
# moves, each entry's expiry measured at every look. The worker asks
|
|
10
|
+
# #changes on its idle tick and announces :participants_changed; the
|
|
11
|
+
# Bridge's join snapshot asks #active (any thread). A session nobody was
|
|
12
|
+
# invited to has no sidecar: one stat per look. Joined means invited (an
|
|
13
|
+
# active grant the last look didn't have), left means revoked or expired:
|
|
14
|
+
# participants have no presence of their own yet. See
|
|
15
|
+
# docs/internals/participants.md (The owner's terminal).
|
|
16
|
+
class ParticipantsWatch
|
|
17
|
+
# Participants::FILE, without loading Participants for a stat.
|
|
18
|
+
FILE = "participants.json"
|
|
19
|
+
|
|
20
|
+
# What changed: the active participants now, the ones that joined, and
|
|
21
|
+
# the ones that left (each with +reason+ "revoked" or "expired").
|
|
22
|
+
Change = Data.define(:participants, :joined, :left) do
|
|
23
|
+
def to_event = { type: :participants_changed, participants: participants, joined: joined, left: left }
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @param session_id [#call] → the session's id (nil before it has one)
|
|
27
|
+
# @param state_dir [#call] → the sessions' state dir
|
|
28
|
+
# @param clock [#call] → Time
|
|
29
|
+
def initialize(session_id:, state_dir:, clock: -> { Time.now })
|
|
30
|
+
@session_id = session_id
|
|
31
|
+
@state_dir = state_dir
|
|
32
|
+
@clock = clock
|
|
33
|
+
@mutex = Mutex.new
|
|
34
|
+
@stamp = nil
|
|
35
|
+
@entries = []
|
|
36
|
+
@known = nil
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The participants in the review now: [{id:, name:, kind:}], [] for none.
|
|
40
|
+
# @return [Array<Hash>]
|
|
41
|
+
def active
|
|
42
|
+
@mutex.synchronize { active_rows(read_entries) }
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
# The change since the last call, nil when none. The first call only
|
|
46
|
+
# takes the state as it is (a worker's start announces nobody).
|
|
47
|
+
# @return [Change, nil]
|
|
48
|
+
def changes
|
|
49
|
+
@mutex.synchronize do
|
|
50
|
+
entries = read_entries
|
|
51
|
+
rows = active_rows(entries)
|
|
52
|
+
known = @known
|
|
53
|
+
@known = rows.to_h { |row| [row[:id], row] }
|
|
54
|
+
next nil if known.nil?
|
|
55
|
+
|
|
56
|
+
joined = rows.reject { |row| known.key?(row[:id]) }
|
|
57
|
+
left = known.values.reject { |row| @known.key?(row[:id]) }.map do |row|
|
|
58
|
+
entry = entries.find { |e| e.id == row[:id] }
|
|
59
|
+
row.merge(reason: entry&.removed_at ? "revoked" : "expired")
|
|
60
|
+
end
|
|
61
|
+
next nil if joined.empty? && left.empty?
|
|
62
|
+
|
|
63
|
+
Change.new(participants: rows, joined: joined, left: left)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
private
|
|
68
|
+
|
|
69
|
+
def active_rows(entries)
|
|
70
|
+
now = @clock.call
|
|
71
|
+
entries.select { |entry| entry.active?(now) }.map { |entry| { id: entry.id, name: entry.name, kind: entry.kind } }
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# The sidecar's entries, read again only when its mtime moved.
|
|
75
|
+
def read_entries
|
|
76
|
+
id = @session_id.call
|
|
77
|
+
return @entries = [] if id.to_s.empty?
|
|
78
|
+
|
|
79
|
+
file = File.join(Session.session_dir(id, state_dir: @state_dir.call), FILE)
|
|
80
|
+
stamp = [id, File.mtime(file)]
|
|
81
|
+
return @entries if stamp == @stamp
|
|
82
|
+
|
|
83
|
+
@stamp = stamp
|
|
84
|
+
@entries = Participants.read(id, state_dir: @state_dir.call)
|
|
85
|
+
rescue SystemCallError
|
|
86
|
+
@stamp = nil
|
|
87
|
+
@entries = []
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
@@ -61,7 +61,7 @@ module Samagotchi
|
|
|
61
61
|
Host = Struct.new(:session_id, :cwd, :messages, :messages_partial, :notify, :ask_user, :cancelled, :card,
|
|
62
62
|
:ask_model, :model_name, :model_key, :state_dir, :scratch, :setup, :prompt_files,
|
|
63
63
|
:steer, :stop_turn, :stop_generation, :frontend, :toolkit, :declared_tools, :use_toolkit, :load_tools,
|
|
64
|
-
:cancel_controller, :cost, :usage, keyword_init: true)
|
|
64
|
+
:cancel_controller, :cost, :usage, :turn_author, keyword_init: true)
|
|
65
65
|
|
|
66
66
|
# ctx.ask_model failed: the model couldn't be reached, timed out, or
|
|
67
67
|
# sent nothing usable. The message says why, for the user.
|
|
@@ -175,6 +175,12 @@ module Samagotchi
|
|
|
175
175
|
# with no session
|
|
176
176
|
def usage = @host.usage&.call || SessionMetrics::Usage.zero
|
|
177
177
|
|
|
178
|
+
# @return [String, nil] who wrote the running turn when it isn't the
|
|
179
|
+
# session's owner: a participant's "p:<id>" (chi share); nil for the
|
|
180
|
+
# owner's turns and between turns. A handler runs with the owner's
|
|
181
|
+
# power whatever the author (docs/hooks.md).
|
|
182
|
+
def turn_author = @host.turn_author&.call
|
|
183
|
+
|
|
178
184
|
# @return [String, nil] the name of the session's toolkit ("full" for
|
|
179
185
|
# one from before toolkits; nil with no session)
|
|
180
186
|
def toolkit = @host.toolkit&.call
|
|
@@ -42,13 +42,21 @@ module Samagotchi
|
|
|
42
42
|
line = IdleRecap::TranscriptFilter.build([message])
|
|
43
43
|
next if line.strip.empty?
|
|
44
44
|
|
|
45
|
-
"#{message
|
|
45
|
+
"#{speaker(message)}: #{line}"
|
|
46
46
|
end.join("\n\n")
|
|
47
47
|
return text if text.length <= MAX_TRANSCRIPT_CHARS
|
|
48
48
|
|
|
49
49
|
"[earlier conversation left out]\n\n#{text[-MAX_TRANSCRIPT_CHARS..]}"
|
|
50
50
|
end
|
|
51
51
|
|
|
52
|
+
# Who a paragraph is: a participant's message is a Participant's (the
|
|
53
|
+
# filter's line names them), any other user message the User's.
|
|
54
|
+
def speaker(message)
|
|
55
|
+
return "Assistant" unless message["role"] == "user"
|
|
56
|
+
|
|
57
|
+
Steer.participant_message?(message) ? "Participant" : "User"
|
|
58
|
+
end
|
|
59
|
+
|
|
52
60
|
# String keys, as TranscriptFilter reads a saved session.
|
|
53
61
|
def stringified(messages)
|
|
54
62
|
JSON.parse(JSON.generate(Array(messages)))
|
|
@@ -3,6 +3,8 @@
|
|
|
3
3
|
require_relative "../session"
|
|
4
4
|
require_relative "../session_inbox"
|
|
5
5
|
require_relative "../turn_checkpoint"
|
|
6
|
+
require_relative "../continue_offer"
|
|
7
|
+
require_relative "../steer"
|
|
6
8
|
|
|
7
9
|
module Samagotchi
|
|
8
10
|
module ReplyWait
|
|
@@ -70,7 +72,7 @@ module Samagotchi
|
|
|
70
72
|
|
|
71
73
|
note_own_running(session)
|
|
72
74
|
follow_merge(session)
|
|
73
|
-
reply(session) || settled_turn(session) || status_end(session) || question(session) || canceled ||
|
|
75
|
+
reply(session) || settled_turn(session) || status_end(session) || question(session) || held(session) || canceled ||
|
|
74
76
|
ended_before_reply(session) || ended_before_turn(session) || turn_end(session) || owner_gone(session) ||
|
|
75
77
|
timed_out
|
|
76
78
|
end
|
|
@@ -78,8 +80,9 @@ module Samagotchi
|
|
|
78
80
|
def look_at_input
|
|
79
81
|
return unless @turn && !@claimed
|
|
80
82
|
|
|
81
|
-
waiting = SessionInbox.
|
|
82
|
-
@own_turn_input ||= waiting
|
|
83
|
+
waiting = SessionInbox.waiting_input_data(@session_dir, @turn)
|
|
84
|
+
@own_turn_input ||= !waiting.nil? && SessionInbox.turn_end_data?(waiting)
|
|
85
|
+
@participant_input ||= !waiting.nil? && Steer.participant_author?(waiting["author"])
|
|
83
86
|
@claimed = waiting.nil?
|
|
84
87
|
end
|
|
85
88
|
|
|
@@ -161,6 +164,18 @@ module Samagotchi
|
|
|
161
164
|
Result.new(status: :waiting_for_answer, question: pending, session: session)
|
|
162
165
|
end
|
|
163
166
|
|
|
167
|
+
# A participant's turn its worker holds while the owner's Continue
|
|
168
|
+
# offer stands (Worker#run_input_files): it waits for the owner's
|
|
169
|
+
# answer to that question, whenever it was asked (the baseline's
|
|
170
|
+
# question too). An owner's turn is never held: it drops the offer.
|
|
171
|
+
def held(session)
|
|
172
|
+
pending = session.pending_question
|
|
173
|
+
return nil unless @participant_input && !@claimed && pending&.kind.to_s == ContinueOffer::KIND &&
|
|
174
|
+
session.waiting_question(live: SessionManager.worker_live?(@id, state_dir: @state_dir))
|
|
175
|
+
|
|
176
|
+
Result.new(status: :waiting_for_answer, question: pending, session: session)
|
|
177
|
+
end
|
|
178
|
+
|
|
164
179
|
def canceled = @cancelled.call ? Result.new(status: :canceled) : nil
|
|
165
180
|
|
|
166
181
|
# The turn that ended before this wait (ended_before: chi send --wait
|
|
@@ -146,7 +146,7 @@ module Samagotchi
|
|
|
146
146
|
chi context <add|push|ls|show|refresh|rm|mute|unmute> live external text attached to sessions
|
|
147
147
|
chi send [-m TEXT] ID...|--new a message into sessions, as if typed there
|
|
148
148
|
chi answer ID --question QID --option N answer a question a session waits on
|
|
149
|
-
chi share ID --name NAME | ls [ID] | revoke ID [P]|--all participants who review a session
|
|
149
|
+
chi share ID --name NAME | ls [ID] | cap ID P D | revoke ID [P]|--all participants who review a session
|
|
150
150
|
chi memory <ls|rm> [--scope project|system] the memories; rm moves one to the bundle trash
|
|
151
151
|
chi bundle <install|upgrade|uninstall|status|diff|list|build|trash|new|test|check|try>
|
|
152
152
|
chi desktop <install|upgrade|uninstall|status> macOS "Send to chi" helper
|
|
@@ -143,11 +143,12 @@ module Samagotchi
|
|
|
143
143
|
status answered (text), question (question,
|
|
144
144
|
answer_with), turn_id when known, running, or failed, canceled,
|
|
145
145
|
limit, no_answer, error, worker_gone, stopped,
|
|
146
|
-
command (detail); with --cut or --queue also
|
|
146
|
+
command, waiting_for_owner (detail); with --cut or --queue also
|
|
147
147
|
delivery (what the worker applied: next_step, cut,
|
|
148
148
|
queue), with --cut also cut (now, waits, off)
|
|
149
149
|
Exit with --wait: 0 answered, 1 failed or gone, 2 usage, 3 a
|
|
150
|
-
question waits,
|
|
150
|
+
question waits (or the owner, for a participant's token), 4
|
|
151
|
+
still running (--timeout), 130 Ctrl-C.
|
|
151
152
|
Sessions on this machine, or another's: chi --remote NAME send …
|
|
152
153
|
(chi remote). Answers show in the attached TUI or web page, not
|
|
153
154
|
here.
|
|
@@ -259,6 +260,7 @@ module Samagotchi
|
|
|
259
260
|
def run_parsed(options)
|
|
260
261
|
# With --wait stdout is the answer alone.
|
|
261
262
|
@info = options[:wait] ? @stderr : @stdout
|
|
263
|
+
@waiting = options[:wait] == true
|
|
262
264
|
@cut = options[:cut] == true
|
|
263
265
|
@queue = options[:queue] == true
|
|
264
266
|
prompt = compose(utf8(read_stdin), utf8(options[:message]))
|
|
@@ -581,6 +583,8 @@ module Samagotchi
|
|
|
581
583
|
# @return [Integer] the exit status
|
|
582
584
|
def run_wait(prompt, options)
|
|
583
585
|
id = resolve(options[:ids].first) or return 1
|
|
586
|
+
return run_participant_wait(id, prompt, options) if @access.participant?
|
|
587
|
+
|
|
584
588
|
# Local: one read for the check and the baseline; remotely the
|
|
585
589
|
# server's wait prepare route reads both (Busy's words).
|
|
586
590
|
session = @access.show(id) unless @access.remote?
|
|
@@ -598,6 +602,27 @@ module Samagotchi
|
|
|
598
602
|
wait_for_reply(id, cursor: start.cursor, baseline: start.baseline, timeout: options[:timeout], **target.to_h)
|
|
599
603
|
end
|
|
600
604
|
|
|
605
|
+
# A participant's --wait -m (a review turn through chi --remote with
|
|
606
|
+
# their token): their message always runs as a queued turn of its own
|
|
607
|
+
# (chi web's review turns), so a running turn doesn't refuse it and
|
|
608
|
+
# there is no busy check (the owner's route, refused to them). Their
|
|
609
|
+
# wait routes take only a turn of theirs: the wait is on the one just
|
|
610
|
+
# queued, as --wait --turn T.
|
|
611
|
+
# @return [Integer] the exit status
|
|
612
|
+
def run_participant_wait(id, prompt, options)
|
|
613
|
+
return 1 unless deliver(id, prompt)
|
|
614
|
+
|
|
615
|
+
turn = @delivered.enqueued_id.to_s
|
|
616
|
+
if turn.empty?
|
|
617
|
+
error_line("chi send: #{id[0, 8]}: the review turn's id didn't come back; chi --remote … send --wait --turn T waits once you have it")
|
|
618
|
+
return 1
|
|
619
|
+
end
|
|
620
|
+
start = @access.prepare_wait(id, turn: turn)
|
|
621
|
+
wait_for_reply(id, cursor: start.cursor, baseline: start.baseline, timeout: options[:timeout],
|
|
622
|
+
owner_grace: start.live ? self.class::WORKER_GONE_AFTER : nil,
|
|
623
|
+
**WaitTarget.after(start, requested: turn).to_h)
|
|
624
|
+
end
|
|
625
|
+
|
|
601
626
|
# --wait with no message: the session's next reply, sending nothing,
|
|
602
627
|
# so an agent can go back to waiting after exit 3 or 130. A running
|
|
603
628
|
# turn is fine here (nothing joins its queue); a question pending now
|
|
@@ -765,7 +790,10 @@ module Samagotchi
|
|
|
765
790
|
# (next_step for an idle session) and a cut's outcome.
|
|
766
791
|
@sent_delivery = result.delivery || delivery
|
|
767
792
|
@sent_cut = result.cut
|
|
768
|
-
@
|
|
793
|
+
note = if @access.participant? then participant_note(id, result)
|
|
794
|
+
else delivery_note(result, delivery: delivery, running: running, owner: owner)
|
|
795
|
+
end
|
|
796
|
+
@info.puts("#{short} sent#{with_images}#{note}")
|
|
769
797
|
true
|
|
770
798
|
rescue SessionManager::OwnedByTUI
|
|
771
799
|
@info.puts("#{short} refused: it is open in a chi REPL; messages need attached mode")
|
|
@@ -801,6 +829,16 @@ module Samagotchi
|
|
|
801
829
|
" #{TerminalUI::Formatting.steer_cut_note(outcome.to_sym, result.cut_after)}"
|
|
802
830
|
end
|
|
803
831
|
|
|
832
|
+
# A participant's message: queued as a review turn of its own (chi
|
|
833
|
+
# web's), its id the one their wait takes.
|
|
834
|
+
def participant_note(id, result)
|
|
835
|
+
turn = result.enqueued_id.to_s
|
|
836
|
+
return " (queued as a review turn)" if turn.empty?
|
|
837
|
+
return " (queued as review turn #{turn})" if @waiting
|
|
838
|
+
|
|
839
|
+
" (queued as review turn #{turn}; #{@access.invocation.command("send --wait --turn #{turn} #{id}")} waits for its reply)"
|
|
840
|
+
end
|
|
841
|
+
|
|
804
842
|
# A --queue, worded from the Bridge's ack: queue when a turn ran as it
|
|
805
843
|
# went in (one another client started too), next_step when none did
|
|
806
844
|
# (it runs now). The input file (no Bridge) carries the queue to
|
|
@@ -122,6 +122,8 @@ module Samagotchi
|
|
|
122
122
|
"share --name" => SUPPORTED,
|
|
123
123
|
"share --agent" => SUPPORTED,
|
|
124
124
|
"share --expires" => SUPPORTED,
|
|
125
|
+
"share --cap" => SUPPORTED,
|
|
126
|
+
"share cap" => SUPPORTED,
|
|
125
127
|
"share ls" => SUPPORTED,
|
|
126
128
|
"share ls --format" => SUPPORTED,
|
|
127
129
|
"share revoke" => SUPPORTED,
|
|
@@ -17,6 +17,7 @@ require_relative "../participants"
|
|
|
17
17
|
require_relative "../config"
|
|
18
18
|
require_relative "../web/grants"
|
|
19
19
|
require_relative "../web/info_probe"
|
|
20
|
+
require_relative "../web/running"
|
|
20
21
|
|
|
21
22
|
module Samagotchi
|
|
22
23
|
# Loaded on first use: SessionManager and its ~80 files only when a
|
|
@@ -38,6 +39,9 @@ module Samagotchi
|
|
|
38
39
|
|
|
39
40
|
def remote? = false
|
|
40
41
|
|
|
42
|
+
# This machine's sessions are the owner's.
|
|
43
|
+
def participant? = false
|
|
44
|
+
|
|
41
45
|
# The printed commands' spelling: this machine's chi.
|
|
42
46
|
# @return [ChiInvocation]
|
|
43
47
|
def invocation = ChiInvocation::HERE
|
|
@@ -339,13 +343,14 @@ module Samagotchi
|
|
|
339
343
|
grants.list(session_id: id).map { |grant| ParticipantView.from_h(grant.public_h(grants.now)) }
|
|
340
344
|
end
|
|
341
345
|
|
|
342
|
-
# The link names this machine's chi web: its LAN address
|
|
343
|
-
# runs on the LAN (
|
|
346
|
+
# The link names this machine's chi web: its LAN address and port
|
|
347
|
+
# when one runs on the LAN (the port it recorded, else web.port),
|
|
348
|
+
# else 127.0.0.1 (+lan+ false).
|
|
344
349
|
# @return [Invitation]
|
|
345
350
|
# @raise [ArgumentError] no such session
|
|
346
351
|
# @raise [BadRequest] a bad name, kind or ttl, an archived session
|
|
347
|
-
def invite(id, name:, kind:, ttl: nil)
|
|
348
|
-
issued = Participants.invite(grants, session_id: id, name: name, kind: kind, ttl: ttl, state_dir: state_dir!)
|
|
352
|
+
def invite(id, name:, kind:, ttl: nil, cap: nil)
|
|
353
|
+
issued = Participants.invite(grants, session_id: id, name: name, kind: kind, ttl: ttl, cap: cap, state_dir: state_dir!)
|
|
349
354
|
ip, port = local_web
|
|
350
355
|
Invitation.new(participant: ParticipantView.from_h(issued.grant.public_h(grants.now)), token: issued.token,
|
|
351
356
|
link: "http://#{ip || "127.0.0.1"}:#{port}/?token=#{issued.token}#s=#{id}", lan: !ip.nil?)
|
|
@@ -359,6 +364,17 @@ module Samagotchi
|
|
|
359
364
|
.map { |grant| ParticipantView.from_h(grant.public_h(grants.now)) }
|
|
360
365
|
end
|
|
361
366
|
|
|
367
|
+
# @return [ParticipantView] the participant with their new cap
|
|
368
|
+
# @raise [BadRequest] a cap out of range, or no such active participant
|
|
369
|
+
def set_participant_cap(id, participant:, cap:)
|
|
370
|
+
grant = Participants.set_cap(grants, session_id: id, id: participant, cap: cap, state_dir: state_dir!)
|
|
371
|
+
raise BadRequest.new(Participants.no_participant(participant, id), code: "invalid_participant") unless grant
|
|
372
|
+
|
|
373
|
+
ParticipantView.from_h(grant.public_h(grants.now))
|
|
374
|
+
rescue Web::Grants::Invalid => e
|
|
375
|
+
raise BadRequest.new(e.message, code: "invalid_participant")
|
|
376
|
+
end
|
|
377
|
+
|
|
362
378
|
private
|
|
363
379
|
|
|
364
380
|
def grants = @grants ||= Web::Grants.new(Web::Grants.path(state_dir: state_dir!))
|
|
@@ -367,12 +383,18 @@ module Samagotchi
|
|
|
367
383
|
WEB_PORT = 4567
|
|
368
384
|
LOCAL_PROBE_TIMEOUT = 1.0
|
|
369
385
|
|
|
370
|
-
# [LAN ip or nil, port] of this machine's chi web (GET /api/info)
|
|
386
|
+
# [LAN ip or nil, port] of this machine's chi web (GET /api/info):
|
|
387
|
+
# the port the running one recorded (Web::Running) when a chi web
|
|
388
|
+
# answers there, else config's web.port.
|
|
371
389
|
def local_web
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
[
|
|
390
|
+
configured = Config.get("web.port").to_s.to_i
|
|
391
|
+
configured = WEB_PORT unless configured.positive?
|
|
392
|
+
recorded = Web::Running.port(path: Web::Running.path(state_dir: state_dir!))
|
|
393
|
+
[recorded, configured].compact.uniq.each do |port|
|
|
394
|
+
info = Web::InfoProbe.call("127.0.0.1", port, timeout: LOCAL_PROBE_TIMEOUT)
|
|
395
|
+
return [info["lan"], port] if info.is_a?(Hash)
|
|
396
|
+
end
|
|
397
|
+
[nil, configured]
|
|
376
398
|
end
|
|
377
399
|
|
|
378
400
|
def dir = @state_dir ? { state_dir: @state_dir } : {}
|
|
@@ -58,6 +58,14 @@ module Samagotchi
|
|
|
58
58
|
def remote? = true
|
|
59
59
|
def name = @entry.name
|
|
60
60
|
|
|
61
|
+
# Web::Grants::PREFIX, without loading chi web.
|
|
62
|
+
PARTICIPANT_PREFIX = "chp_"
|
|
63
|
+
|
|
64
|
+
# Whether the token is a participant's (chi share's link), not the
|
|
65
|
+
# owner's: their turns are queued review turns, their waits take only
|
|
66
|
+
# a turn of theirs.
|
|
67
|
+
def participant? = @entry.token.to_s.start_with?(PARTICIPANT_PREFIX)
|
|
68
|
+
|
|
61
69
|
# The printed commands' spelling: chi --remote NAME.
|
|
62
70
|
# @return [ChiInvocation]
|
|
63
71
|
def invocation = ChiInvocation.new(remote: name)
|
|
@@ -245,8 +253,8 @@ module Samagotchi
|
|
|
245
253
|
# @param ttl [Integer, nil] seconds; nil: the grant's default
|
|
246
254
|
# @return [Invitation]
|
|
247
255
|
# @raise [BadRequest] a bad name, kind or ttl, an archived session
|
|
248
|
-
def invite(id, name:, kind:, ttl: nil)
|
|
249
|
-
reply = request(:post, "#{session_path(id)}/participants", body: { name: name, kind: kind, ttl: ttl }.compact)
|
|
256
|
+
def invite(id, name:, kind:, ttl: nil, cap: nil)
|
|
257
|
+
reply = request(:post, "#{session_path(id)}/participants", body: { name: name, kind: kind, ttl: ttl, cap: cap }.compact)
|
|
250
258
|
raise_refusal(reply) unless reply.status == 201
|
|
251
259
|
|
|
252
260
|
json = reply.json
|
|
@@ -265,6 +273,15 @@ module Samagotchi
|
|
|
265
273
|
Array(reply.json["revoked"]).map { |row| ParticipantView.from_h(row) }
|
|
266
274
|
end
|
|
267
275
|
|
|
276
|
+
# @return [ParticipantView] the participant with their new cap
|
|
277
|
+
# @raise [BadRequest] a cap out of range, or no such active participant
|
|
278
|
+
def set_participant_cap(id, participant:, cap:)
|
|
279
|
+
reply = request(:post, "#{session_path(id)}/participants/#{participant}/cap", body: { cap: cap })
|
|
280
|
+
raise_refusal(reply) unless reply.status == 200
|
|
281
|
+
|
|
282
|
+
ParticipantView.from_h(reply.json.fetch("participant"))
|
|
283
|
+
end
|
|
284
|
+
|
|
268
285
|
def note(id, text:, source:)
|
|
269
286
|
reply = request(:post, "#{session_path(id)}/notes", body: { text: text, source: source })
|
|
270
287
|
json = reply.json.is_a?(Hash) ? reply.json : {}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "participant_messages"
|
|
4
|
+
|
|
3
5
|
module Samagotchi
|
|
4
6
|
# The seam between chi's session commands (chi send, answer, note,
|
|
5
7
|
# session-scope context, sessions list/stop/restart/archive/unarchive/
|
|
@@ -60,13 +62,13 @@ module Samagotchi
|
|
|
60
62
|
# queue up to their limit.
|
|
61
63
|
class TooManyTurns < RemoteFailed
|
|
62
64
|
CODE = "too_many_turns"
|
|
63
|
-
def initialize(label) = super("#{label}:
|
|
65
|
+
def initialize(label) = super("#{label}: #{ParticipantMessages::TOO_MANY_TURNS}")
|
|
64
66
|
end
|
|
65
67
|
|
|
66
68
|
# 429 cap_reached (P3b): the participant's grant spent its cap.
|
|
67
69
|
class CapReached < RemoteFailed
|
|
68
70
|
CODE = "cap_reached"
|
|
69
|
-
def initialize(label) = super("#{label}:
|
|
71
|
+
def initialize(label) = super("#{label}: #{ParticipantMessages::CAP_REACHED}")
|
|
70
72
|
end
|
|
71
73
|
|
|
72
74
|
# A remote's chi web refused a request for a reason this machine checks
|
|
@@ -91,8 +93,9 @@ module Samagotchi
|
|
|
91
93
|
# One participant's grant as chi share ls shows it
|
|
92
94
|
# (Web::Grants::Grant#public_h): never its token or digest. +state+
|
|
93
95
|
# "active", "revoked" or "expired".
|
|
96
|
+
# +cap+ dollars and +queue+ turns (nil from a chi web before them).
|
|
94
97
|
ParticipantView = Data.define(:id, :session_id, :kind, :name, :role, :created_at, :expires_at, :revoked_at,
|
|
95
|
-
:revoked_by, :last_used_at, :state) do
|
|
98
|
+
:revoked_by, :last_used_at, :state, :cap, :queue) do
|
|
96
99
|
def self.from_h(hash) = new(**members.to_h { |key| [key, hash[key] || hash[key.to_s]] })
|
|
97
100
|
end
|
|
98
101
|
|
|
@@ -158,6 +158,12 @@ module Samagotchi
|
|
|
158
158
|
# runs (the worker's POST turn/dequeue); the web has × on the bubble.
|
|
159
159
|
registry.register("/unqueue", "remove your newest queued message before it runs", local: true, uis: [:attached],
|
|
160
160
|
match: ->(text) { text.casecmp?("/unqueue") })
|
|
161
|
+
# chi share for the attached session (TerminalUI::ParticipantCommands):
|
|
162
|
+
# a participant's turns run on its worker, so the REPL points there.
|
|
163
|
+
registry.register("/share", "invite someone to review this session (/share NAME [--agent] [--expires 3d] [--cap 2])",
|
|
164
|
+
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/share(?:\s.*)?\z}i) })
|
|
165
|
+
registry.register("/participants", "list this session's participants (revoke [P], cap P DOLLARS)",
|
|
166
|
+
local: true, uis: [:attached], match: ->(text) { text.match?(%r{\A/participants(?:\s.*)?\z}i) })
|
|
161
167
|
registry
|
|
162
168
|
end
|
|
163
169
|
|
|
@@ -8,6 +8,7 @@ require_relative "atomic_file"
|
|
|
8
8
|
require_relative "delivery"
|
|
9
9
|
require_relative "log"
|
|
10
10
|
require_relative "session"
|
|
11
|
+
require_relative "steer"
|
|
11
12
|
|
|
12
13
|
module Samagotchi
|
|
13
14
|
# A session directory's file inbox and outbox (see SessionManager for the
|
|
@@ -248,31 +249,56 @@ module Samagotchi
|
|
|
248
249
|
|
|
249
250
|
# Whether a mid-turn drain leaves this unclaimed input file for a turn
|
|
250
251
|
# of its own instead of merging it at a step boundary: a queued message
|
|
251
|
-
# (`delivery: queue`)
|
|
252
|
+
# (`delivery: queue`), an image message (steering merges text only), or
|
|
253
|
+
# a participant's, whatever its delivery says: theirs always runs as its
|
|
254
|
+
# own turn, never inside another author's (D6; chi web queues it, and
|
|
255
|
+
# this is the worker's own layer).
|
|
252
256
|
# The drain skips such a file without stopping at it, so a later text
|
|
253
257
|
# line still merges; the drain's file filter reads this one predicate,
|
|
254
258
|
# so what a boundary leaves changes in one place.
|
|
255
259
|
def self.waits_for_turn_end?(input_file)
|
|
256
260
|
data = JSON.parse(File.read(input_file))
|
|
257
|
-
data.is_a?(Hash) &&
|
|
261
|
+
data.is_a?(Hash) && turn_end_data?(data)
|
|
258
262
|
rescue JSON::ParserError, SystemCallError
|
|
259
263
|
false
|
|
260
264
|
end
|
|
261
265
|
|
|
266
|
+
# Whether an unclaimed input file may be a participant's (it names a
|
|
267
|
+
# participant author): what the worker holds while the owner's Continue
|
|
268
|
+
# offer stands (Worker#run_input_files). An unreadable file counts:
|
|
269
|
+
# holding it costs nothing (it runs no turn), running it unread could.
|
|
270
|
+
def self.participant_input?(input_file)
|
|
271
|
+
data = JSON.parse(File.read(input_file))
|
|
272
|
+
!data.is_a?(Hash) || Steer.participant_author?(data["author"])
|
|
273
|
+
rescue JSON::ParserError, SystemCallError
|
|
274
|
+
true
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# A parsed input file's #waits_for_turn_end?.
|
|
278
|
+
def self.turn_end_data?(data)
|
|
279
|
+
Array(data["images"]).any? || Delivery.queue?(data["delivery"]) || Steer.participant_author?(data["author"])
|
|
280
|
+
end
|
|
281
|
+
|
|
262
282
|
# How the unclaimed input file carrying +enqueued_id+ waits: :turn_end
|
|
263
283
|
# (a queue or an image message: it runs as a turn of its own, never
|
|
264
284
|
# merged, #waits_for_turn_end?), :merges (a running turn's drain may
|
|
265
285
|
# take it as a steer), or nil when none waits.
|
|
266
286
|
# @return [Symbol, nil]
|
|
267
287
|
def self.waiting_input(session_dir, enqueued_id)
|
|
288
|
+
data = waiting_input_data(session_dir, enqueued_id) or return nil
|
|
289
|
+
turn_end_data?(data) ? :turn_end : :merges
|
|
290
|
+
end
|
|
291
|
+
|
|
292
|
+
# The unclaimed input file carrying +enqueued_id+, as parsed; nil when
|
|
293
|
+
# none waits.
|
|
294
|
+
# @return [Hash, nil]
|
|
295
|
+
def self.waiting_input_data(session_dir, enqueued_id)
|
|
268
296
|
id = enqueued_id.to_s
|
|
269
297
|
return nil if id.empty?
|
|
270
298
|
|
|
271
299
|
find_new_input_files(session_dir).each do |file|
|
|
272
300
|
data = JSON.parse(File.read(file))
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
return Array(data["images"]).any? || Delivery.queue?(data["delivery"]) ? :turn_end : :merges
|
|
301
|
+
return data if data.is_a?(Hash) && data["enqueued_id"] == id
|
|
276
302
|
rescue JSON::ParserError, SystemCallError
|
|
277
303
|
next
|
|
278
304
|
end
|