samagotchi 0.181.0 → 0.183.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 +93 -1
- data/bin/chi +4 -1
- data/docs/architecture.md +17 -7
- data/docs/bundle-authoring.md +1 -1
- data/docs/bundles.md +103 -4
- data/docs/cli.md +58 -10
- data/docs/configuration.md +3 -1
- data/docs/guardrails.md +16 -1
- data/docs/internals/block-renderers.md +172 -3
- data/docs/internals/bridge.md +23 -5
- data/docs/internals/bundles.md +21 -4
- data/docs/internals/engine.md +29 -8
- data/docs/internals/inline-widgets.md +3 -1
- data/docs/internals/prompt-caching.md +9 -1
- data/docs/internals/remote-sessions.md +38 -7
- data/docs/internals/web-frontend.md +116 -26
- data/docs/internals/working-set.md +1 -1
- data/docs/mcp-server.md +80 -0
- data/docs/plugins.md +308 -21
- data/docs/sessions.md +7 -4
- data/docs/sub-agent.md +9 -0
- data/docs/testing.md +3 -1
- data/docs/thin-clients.md +146 -0
- data/lib/samagotchi/answer_display.rb +3 -2
- data/lib/samagotchi/block_fix/stamp.rb +49 -0
- data/lib/samagotchi/block_fix.rb +220 -0
- data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
- data/lib/samagotchi/bridge/pending_card.rb +4 -3
- data/lib/samagotchi/bridge/sse_writer.rb +16 -15
- data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
- data/lib/samagotchi/bridge.rb +69 -69
- data/lib/samagotchi/bridge_client.rb +9 -0
- data/lib/samagotchi/bundle_command/api_calls.rb +1 -1
- data/lib/samagotchi/bundle_command/check.rb +36 -19
- data/lib/samagotchi/bundle_command.rb +50 -21
- data/lib/samagotchi/bundle_manifest.rb +101 -4
- data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
- data/lib/samagotchi/bundles/mcp/client.rb +260 -0
- data/lib/samagotchi/bundles/mcp/content.rb +107 -0
- data/lib/samagotchi/bundles/mcp/http_client.rb +407 -0
- data/lib/samagotchi/bundles/mcp/manifest.yml +13 -5
- data/lib/samagotchi/bundles/mcp/oauth.rb +814 -0
- data/lib/samagotchi/bundles/mcp/plugin.rb +556 -574
- data/lib/samagotchi/bundles/mcp/tools.rb +232 -0
- data/lib/samagotchi/bundles/system/config_modification_protocol.md +10 -2
- data/lib/samagotchi/bundles/system/manifest.yml +2 -2
- data/lib/samagotchi/cli/parent_wait.rb +5 -3
- data/lib/samagotchi/command_intake.rb +7 -0
- data/lib/samagotchi/commands/registry.rb +8 -3
- data/lib/samagotchi/config.rb +3 -0
- data/lib/samagotchi/continue_intent.rb +8 -0
- data/lib/samagotchi/continue_offer.rb +24 -15
- data/lib/samagotchi/engine/block_fix.rb +332 -0
- data/lib/samagotchi/engine/cards.rb +14 -5
- data/lib/samagotchi/engine/events.rb +10 -15
- data/lib/samagotchi/engine/models.rb +1 -1
- data/lib/samagotchi/engine/plugins.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -1
- data/lib/samagotchi/engine/side_requests.rb +8 -6
- data/lib/samagotchi/engine/sign_in.rb +142 -0
- data/lib/samagotchi/engine/turn_end.rb +3 -3
- data/lib/samagotchi/engine.rb +47 -27
- data/lib/samagotchi/engine_snapshots.rb +1 -1
- data/lib/samagotchi/events.rb +1 -0
- data/lib/samagotchi/guardrail_wiring.rb +3 -1
- data/lib/samagotchi/guardrails/protected_paths.rb +51 -2
- data/lib/samagotchi/guardrails/verdict.rb +3 -2
- data/lib/samagotchi/hold_stop.rb +4 -4
- data/lib/samagotchi/idle_client.rb +10 -6
- data/lib/samagotchi/idle_recap.rb +19 -11
- data/lib/samagotchi/idle_reminders.rb +7 -5
- data/lib/samagotchi/idle_scheduler.rb +7 -10
- data/lib/samagotchi/inline_widgets.rb +12 -0
- data/lib/samagotchi/installed_bundles/dir.rb +37 -0
- data/lib/samagotchi/installed_bundles/manifest.rb +7 -1
- data/lib/samagotchi/installed_bundles/plugin_ref.rb +9 -5
- data/lib/samagotchi/installed_bundles/record.rb +3 -2
- data/lib/samagotchi/installed_bundles/standing.rb +3 -1
- data/lib/samagotchi/kernel_loop.rb +7 -5
- data/lib/samagotchi/llm/chat_loop.rb +2 -0
- data/lib/samagotchi/llm/http.rb +96 -5
- data/lib/samagotchi/markdown_parser.rb +28 -0
- data/lib/samagotchi/mcp_command.rb +110 -0
- data/lib/samagotchi/mcp_server/http.rb +112 -0
- data/lib/samagotchi/mcp_server/protocol.rb +244 -0
- data/lib/samagotchi/mcp_server/stdio.rb +92 -0
- data/lib/samagotchi/mcp_server/text.rb +155 -0
- data/lib/samagotchi/mcp_server/tools.rb +245 -0
- data/lib/samagotchi/memory_bundle/asset_installer.rb +40 -12
- data/lib/samagotchi/memory_bundle/builder.rb +20 -2
- data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
- data/lib/samagotchi/memory_bundle/installer.rb +16 -3
- data/lib/samagotchi/memory_bundle/provenance.rb +18 -1
- data/lib/samagotchi/memory_bundle/shas.rb +6 -4
- data/lib/samagotchi/memory_bundle/shipped_update.rb +7 -3
- data/lib/samagotchi/memory_bundle/status.rb +15 -6
- data/lib/samagotchi/memory_bundle/uninstaller.rb +9 -4
- data/lib/samagotchi/plugin/api.rb +6 -1
- data/lib/samagotchi/plugin/card.rb +36 -11
- data/lib/samagotchi/plugin/context.rb +63 -6
- data/lib/samagotchi/plugin/http.rb +40 -15
- data/lib/samagotchi/plugin/http_errors.rb +15 -0
- data/lib/samagotchi/plugin/http_stream.rb +86 -0
- data/lib/samagotchi/plugin/http_transport.rb +58 -16
- data/lib/samagotchi/plugin/loader.rb +18 -11
- data/lib/samagotchi/plugin/secrets.rb +266 -0
- data/lib/samagotchi/plugin/sign_in.rb +392 -0
- data/lib/samagotchi/plugin/sse_reader.rb +103 -0
- data/lib/samagotchi/queued_command.rb +28 -4
- data/lib/samagotchi/recap_setup.rb +13 -4
- data/lib/samagotchi/ref.rb +3 -2
- data/lib/samagotchi/relay_verifier.rb +8 -7
- data/lib/samagotchi/relay_watcher.rb +4 -4
- data/lib/samagotchi/run_options.rb +2 -1
- data/lib/samagotchi/send_command.rb +54 -41
- data/lib/samagotchi/session_access/ask.rb +244 -0
- data/lib/samagotchi/session_access/contract.rb +8 -0
- data/lib/samagotchi/session_access/delete_plan.rb +77 -0
- data/lib/samagotchi/session_access/exchange/token.rb +101 -0
- data/lib/samagotchi/session_access/exchange.rb +173 -0
- data/lib/samagotchi/session_access/local.rb +38 -6
- data/lib/samagotchi/session_access/remote.rb +23 -2
- data/lib/samagotchi/session_access.rb +1 -0
- data/lib/samagotchi/session_chain/delete.rb +39 -81
- data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
- data/lib/samagotchi/session_command_catalog.rb +10 -0
- data/lib/samagotchi/session_commands.rb +17 -0
- data/lib/samagotchi/session_config.rb +4 -4
- data/lib/samagotchi/session_delete_command.rb +129 -3
- data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
- data/lib/samagotchi/session_manager/listing.rb +4 -4
- data/lib/samagotchi/session_manager/removal.rb +38 -12
- data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
- data/lib/samagotchi/session_manager.rb +1 -0
- data/lib/samagotchi/session_observer.rb +27 -0
- data/lib/samagotchi/steer_cut.rb +4 -3
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +4 -1
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -0
- data/lib/samagotchi/testing/boundary.rb +4 -0
- data/lib/samagotchi/testing/bundle.rb +48 -2
- data/lib/samagotchi/testing/fake_http.rb +60 -15
- data/lib/samagotchi/testing/host.rb +4 -1
- data/lib/samagotchi/testing/recorder.rb +45 -5
- data/lib/samagotchi/tools/tool_index.rb +14 -3
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +20 -6
- data/lib/samagotchi/web/ask_routes.rb +292 -0
- data/lib/samagotchi/web/block_fix_routes.rb +39 -0
- data/lib/samagotchi/web/markdown_renderer.rb +24 -9
- data/lib/samagotchi/web/mcp_route.rb +88 -0
- data/lib/samagotchi/web/message_display.rb +6 -1
- data/lib/samagotchi/web/participant_view.rb +1 -1
- data/lib/samagotchi/web/public/app.js +7 -8
- data/lib/samagotchi/web/public/block_fix.js +270 -0
- data/lib/samagotchi/web/public/block_fix_note.js +104 -0
- data/lib/samagotchi/web/public/block_frame.js +77 -13
- data/lib/samagotchi/web/public/block_host.js +45 -9
- data/lib/samagotchi/web/public/block_regions.js +126 -0
- data/lib/samagotchi/web/public/block_renderers.css +12 -0
- data/lib/samagotchi/web/public/block_watch.js +16 -2
- data/lib/samagotchi/web/public/card.js +22 -9
- data/lib/samagotchi/web/public/card_actions.js +21 -0
- data/lib/samagotchi/web/public/card_kinds.js +1 -1
- data/lib/samagotchi/web/public/card_registry.js +3 -2
- data/lib/samagotchi/web/public/data.js +15 -12
- data/lib/samagotchi/web/public/delete_sheet.js +222 -162
- data/lib/samagotchi/web/public/diagram_wiring.js +28 -0
- data/lib/samagotchi/web/public/format.js +3 -0
- data/lib/samagotchi/web/public/index.html +52 -7
- data/lib/samagotchi/web/public/inline_widgets.js +1 -1
- data/lib/samagotchi/web/public/more_sheet.js +6 -4
- data/lib/samagotchi/web/public/picture_regions.js +243 -0
- data/lib/samagotchi/web/public/picture_viewer.js +113 -12
- data/lib/samagotchi/web/public/refs.js +5 -3
- data/lib/samagotchi/web/public/region_hit.js +144 -0
- data/lib/samagotchi/web/public/removal_sheet_model.js +275 -0
- data/lib/samagotchi/web/public/running_tasks.js +6 -4
- data/lib/samagotchi/web/public/session_actions.js +105 -100
- data/lib/samagotchi/web/public/stream_handlers.js +5 -1
- data/lib/samagotchi/web/public/turn_block.js +5 -2
- data/lib/samagotchi/web/server.rb +6 -4
- data/lib/samagotchi/web/session_access_routes.rb +14 -11
- data/lib/samagotchi/web/session_control.rb +84 -30
- data/lib/samagotchi/web/session_create.rb +2 -2
- data/lib/samagotchi/worker/block_fixes.rb +22 -0
- data/lib/samagotchi/worker/child_side.rb +3 -2
- data/lib/samagotchi/worker/command_queue.rb +13 -13
- data/lib/samagotchi/worker/leave.rb +3 -3
- data/lib/samagotchi/worker/scratch.rb +6 -6
- data/lib/samagotchi/worker/signal_grace.rb +6 -5
- data/lib/samagotchi/worker/startup.rb +3 -3
- data/lib/samagotchi/worker/wake_turns.rb +4 -4
- data/lib/samagotchi/worker.rb +42 -24
- data/lib/samagotchi/worker_exit_claim.rb +1 -1
- data/lib/samagotchi/worker_idle_exit.rb +18 -9
- data/lib/samagotchi/worker_inbound.rb +12 -7
- data/lib/samagotchi/worker_participant_gate.rb +9 -7
- data/lib/samagotchi/worker_task_wakes.rb +7 -5
- metadata +42 -1
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../answer_display"
|
|
4
|
+
require_relative "../answer_tail"
|
|
5
|
+
require_relative "../block_fix"
|
|
6
|
+
require_relative "../bridge/turn_drawings"
|
|
7
|
+
require_relative "../log"
|
|
8
|
+
|
|
9
|
+
module Samagotchi
|
|
10
|
+
class Engine
|
|
11
|
+
# A failed block asked for again (docs/internals/block-renderers.md, A
|
|
12
|
+
# failed block is asked for again). #fix_block answers the Bridge's
|
|
13
|
+
# route and starts the side request; its result waits in
|
|
14
|
+
# @block_fix_ready until the worker's loop, between turns, calls
|
|
15
|
+
# #apply_block_fixes, which writes the conversation: the answer's text
|
|
16
|
+
# when no turn started since the ask and it is still the newest answer,
|
|
17
|
+
# else its display only, or nothing when the answer is gone.
|
|
18
|
+
# Writes @block_fix_running, @block_fix_ready, @block_fix_turns (under
|
|
19
|
+
# @block_fix_mutex; the counter on the turn thread), reads @session,
|
|
20
|
+
# @side_judgments, @event_log.
|
|
21
|
+
module BlockFixPart
|
|
22
|
+
# What a pending result knows of its answer: its place in the
|
|
23
|
+
# conversation, its digest then (Bridge::TurnDrawings.digest), and the
|
|
24
|
+
# turn counter then (nil: a turn was running as it was asked).
|
|
25
|
+
Anchor = Data.define(:index, :digest, :turns)
|
|
26
|
+
MOVED_DIGEST_LIMIT = 64
|
|
27
|
+
# A finished ask: +fixed+ (the new source) or +reason+ (why not).
|
|
28
|
+
Result = Data.define(:turn_id, :lang, :key, :source, :fixed, :reason, :anchor, :started) do
|
|
29
|
+
def fixed? = !fixed.nil?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The worker's wake (Waker#wake), called once a result waits: an idle
|
|
33
|
+
# worker applies it at once. nil (the default): nothing is woken.
|
|
34
|
+
def block_fix_ready=(callback)
|
|
35
|
+
@block_fix_wake = callback
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# The Bridge's POST blocks/fix: whether +source+, a +lang+ block of
|
|
39
|
+
# +turn_id+'s answer, is asked for again.
|
|
40
|
+
# @return [Array(Integer, Hash)] the status and the body: 200
|
|
41
|
+
# {state: "fixed" | "failed", reason:} (asked before), 202
|
|
42
|
+
# {state: "running"}, or an error ({error:, detail:}: 409
|
|
43
|
+
# fix_unavailable / block_not_found, 413 block_too_large, 429
|
|
44
|
+
# fix_limit (BlockFix::PER_ANSWER) / fix_busy)
|
|
45
|
+
def fix_block(turn_id:, lang:, source:, reason:)
|
|
46
|
+
session = @session
|
|
47
|
+
return block_fix_refused(409, :fix_unavailable) if session.nil? || session.scratch || !BlockFix.available?
|
|
48
|
+
return block_fix_refused(413, :block_too_large) if source.to_s.bytesize > BlockFix::MAX_SOURCE_BYTES
|
|
49
|
+
|
|
50
|
+
# The counter before the running flag: a turn sets the flag, then
|
|
51
|
+
# counts (#note_block_fix_turn), so one starting between the reads
|
|
52
|
+
# still shows as a change. The running turn ending with this answer
|
|
53
|
+
# (the page asks on its end event, before its hooks are done) is no
|
|
54
|
+
# turn since; any other running turn is.
|
|
55
|
+
turns = @block_fix_turns
|
|
56
|
+
turns = nil if turn_running? && turn_context&.id.to_s != turn_id.to_s
|
|
57
|
+
messages = session.messages
|
|
58
|
+
answer = AnswerTail.find(messages, turn_id: turn_id)
|
|
59
|
+
index = answer && messages.index { |message| message.equal?(answer) }
|
|
60
|
+
return block_fix_refused(409, :block_not_found) unless index
|
|
61
|
+
|
|
62
|
+
key = BlockFix.key(lang, source)
|
|
63
|
+
shown = !BlockFix.locate(block_fix_shown(answer), lang: lang, source: source).nil?
|
|
64
|
+
# The stamp before the text: a page that drew the answer before its
|
|
65
|
+
# fix (a second tab) asks for a block no longer in it, and is told
|
|
66
|
+
# fixed (it reads the answer again).
|
|
67
|
+
known = BlockFix.stamp_for(answer, key)
|
|
68
|
+
return [200, block_fix_known(known, key, shown: shown)] if known
|
|
69
|
+
return block_fix_refused(409, :block_not_found) unless shown
|
|
70
|
+
|
|
71
|
+
# In flight per answer and block: the same block in a newer answer
|
|
72
|
+
# is that answer's own ask (its result lands in it, its event names it).
|
|
73
|
+
asking = [index, key]
|
|
74
|
+
@block_fix_mutex.synchronize do
|
|
75
|
+
return [202, { state: "running" }] if block_fix_pending?(asking)
|
|
76
|
+
return block_fix_refused(429, :fix_limit) if block_fix_asked(answer, index) >= BlockFix::PER_ANSWER
|
|
77
|
+
return block_fix_refused(429, :fix_busy) if @block_fix_running.size >= BlockFix::MAX_RUNNING
|
|
78
|
+
|
|
79
|
+
@block_fix_running << asking
|
|
80
|
+
end
|
|
81
|
+
anchor = Anchor.new(index: index, digest: Bridge::TurnDrawings.digest(answer), turns: turns)
|
|
82
|
+
conversation = AnswerDisplay.strip_all(messages[0..index])
|
|
83
|
+
Log.info(:model, "block_fix_asked", lang: lang, key: key, bytes: source.to_s.bytesize)
|
|
84
|
+
start_block_fix(conversation, turn_id: turn_id.to_s, lang: lang.to_s, key: key, source: source.to_s,
|
|
85
|
+
reason: reason.to_s, anchor: anchor)
|
|
86
|
+
[202, { state: "running" }]
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
# The results waiting, applied: on the worker's loop, between turns
|
|
90
|
+
# (a running turn holds its own copy of the conversation and writes it
|
|
91
|
+
# back at its end). The caller saves the session when this is > 0.
|
|
92
|
+
# @return [Integer] how many changed the conversation
|
|
93
|
+
def apply_block_fixes
|
|
94
|
+
return 0 if turn_running? || @session.nil?
|
|
95
|
+
|
|
96
|
+
ready = @block_fix_mutex.synchronize { @block_fix_ready.slice!(0..) }
|
|
97
|
+
return 0 if ready.empty?
|
|
98
|
+
|
|
99
|
+
synchronize_events { ready.count { |result| apply_block_fix(@session, result) } }
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# The block fix's [answer index, key] pairs still asked for (specs).
|
|
103
|
+
def block_fixes_running = @block_fix_mutex.synchronize { @block_fix_running.dup }
|
|
104
|
+
|
|
105
|
+
private
|
|
106
|
+
|
|
107
|
+
def init_block_fix
|
|
108
|
+
@block_fix_mutex = Mutex.new
|
|
109
|
+
@block_fix_running = []
|
|
110
|
+
@block_fix_ready = []
|
|
111
|
+
@block_fix_turns = 0
|
|
112
|
+
@block_fix_wake = nil
|
|
113
|
+
# An answer's digest before a fix in its text => after (on the loop
|
|
114
|
+
# thread): the other block of the same answer, asked before that fix
|
|
115
|
+
# landed, still knows its answer.
|
|
116
|
+
@block_fix_moved = {}
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# A turn takes its messages (#turn_messages): a fix asked before it
|
|
120
|
+
# lands in the answer's display only.
|
|
121
|
+
def note_block_fix_turn
|
|
122
|
+
@block_fix_turns += 1
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Whether [answer index, key] is asked for or its result waits (under
|
|
126
|
+
# @block_fix_mutex).
|
|
127
|
+
def block_fix_pending?(asking)
|
|
128
|
+
@block_fix_running.include?(asking) || @block_fix_ready.any? { |r| [r.anchor.index, r.key] == asking }
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
# How many blocks of the answer at +index+ were asked for: its stamp's
|
|
132
|
+
# and those in flight or waiting (under @block_fix_mutex). The stamp
|
|
133
|
+
# is on the message, so the count holds across a page's reload.
|
|
134
|
+
def block_fix_asked(answer, index)
|
|
135
|
+
keys = BlockFix.stamps(answer).map { |entry| entry[:key] }
|
|
136
|
+
keys += @block_fix_running.filter_map { |at, key| key if at == index }
|
|
137
|
+
keys += @block_fix_ready.filter_map { |r| r.key if r.anchor.index == index }
|
|
138
|
+
keys.uniq.size
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def block_fix_refused(status, code)
|
|
142
|
+
[status, { error: code.to_s, detail: BlockFix::REFUSALS.fetch(code) }]
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
# A block the answer's stamp knows: asked before. A block that is the
|
|
146
|
+
# fix of an earlier one fails again: failed, whatever that fix was. A
|
|
147
|
+
# fixed block still +shown+ (a copy the fix couldn't reach) is failed
|
|
148
|
+
# too: "fixed" would have the page read the answer again, draw the
|
|
149
|
+
# copy, fail it and ask again, with no end.
|
|
150
|
+
def block_fix_known(entry, key, shown:)
|
|
151
|
+
return { state: "failed", reason: "it failed again after a fix" } if entry[:fixed_key] == key && entry[:key] != key
|
|
152
|
+
return { state: "failed", reason: "a copy of it is still in the answer" } if entry[:state].to_s == "fixed" && shown
|
|
153
|
+
|
|
154
|
+
{ state: entry[:state].to_s, reason: entry[:reason] }.compact
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
# The text the page draws an answer from.
|
|
158
|
+
def block_fix_shown(message)
|
|
159
|
+
(message[AnswerDisplay::KEY] || message[AnswerDisplay::KEY.to_s] || message[:content] || message["content"]).to_s
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
def start_block_fix(conversation, **job)
|
|
163
|
+
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
164
|
+
warn = ->(error) { Log.warn(:model, "block_fix_thread_failed", error: error.class.name, msg: error.message.to_s[0, 200]) }
|
|
165
|
+
@side_judgments.aside(warn: warn) do
|
|
166
|
+
result = begin
|
|
167
|
+
ask_block_fix(conversation, **job, started: started)
|
|
168
|
+
rescue StandardError => e
|
|
169
|
+
Result.new(**job.except(:reason), started: started, fixed: nil,
|
|
170
|
+
reason: e.message.to_s.lines.first.to_s.strip[0, 200])
|
|
171
|
+
end
|
|
172
|
+
@block_fix_mutex.synchronize do
|
|
173
|
+
@block_fix_running.delete([job[:anchor].index, job[:key]])
|
|
174
|
+
@block_fix_ready << result
|
|
175
|
+
end
|
|
176
|
+
@block_fix_wake&.call
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def ask_block_fix(conversation, turn_id:, lang:, key:, source:, reason:, anchor:, started:)
|
|
181
|
+
request = BlockFix.request(messages: conversation, lang: lang, source: source, reason: reason)
|
|
182
|
+
answer = ask_side_model(request, timeout: BlockFix::TIMEOUT_SECONDS, max_tokens: BlockFix::MAX_TOKENS,
|
|
183
|
+
cancel_controller: nil, source: BlockFix::SOURCE, whole: true)
|
|
184
|
+
extracted = BlockFix.extract(answer.text, cut_off: answer.cut_off, failed: source)
|
|
185
|
+
refusal = extracted.refusal || BlockFix.check(lang: lang, source: extracted.source)
|
|
186
|
+
Result.new(turn_id: turn_id, lang: lang, key: key, source: source, anchor: anchor, started: started,
|
|
187
|
+
fixed: refusal ? nil : extracted.source, reason: refusal)
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
# One result, under the event lock.
|
|
191
|
+
# @return [Boolean] whether the conversation changed
|
|
192
|
+
# A fix that can't land in an answer still there (its shown text lost
|
|
193
|
+
# the block, or the fix would pass the display's size) stamps it
|
|
194
|
+
# failed with the reason, so a reload doesn't ask again; only a gone
|
|
195
|
+
# or changed answer is dropped, with no stamp.
|
|
196
|
+
def apply_block_fix(session, result)
|
|
197
|
+
messages = session.messages
|
|
198
|
+
answer = messages[result.anchor.index]
|
|
199
|
+
return block_fix_dropped(result, "the answer changed") unless answer && block_fix_anchored?(messages, answer, result)
|
|
200
|
+
|
|
201
|
+
unless BlockFix.locate(block_fix_shown(answer), lang: result.lang, source: result.source)
|
|
202
|
+
result = result.with(fixed: nil, reason: "the answer changed")
|
|
203
|
+
end
|
|
204
|
+
copy, landed = block_fix_patched(messages, answer, result)
|
|
205
|
+
unless copy
|
|
206
|
+
result = result.with(fixed: nil, reason: "the fix is too long")
|
|
207
|
+
copy, landed = block_fix_patched(messages, answer, result)
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
list = messages.dup
|
|
211
|
+
list[result.anchor.index] = copy
|
|
212
|
+
replace_session_messages(session, list)
|
|
213
|
+
was = Bridge::TurnDrawings.digest(answer)
|
|
214
|
+
now = Bridge::TurnDrawings.digest(copy)
|
|
215
|
+
if was != now
|
|
216
|
+
@block_fix_moved.delete(was)
|
|
217
|
+
@block_fix_moved[was] = now
|
|
218
|
+
@block_fix_moved.shift while @block_fix_moved.size > MOVED_DIGEST_LIMIT
|
|
219
|
+
end
|
|
220
|
+
block_fix_announce(result, state: result.fixed? ? "fixed" : "failed", landed: landed, was_digest: was, digest: now)
|
|
221
|
+
true
|
|
222
|
+
end
|
|
223
|
+
|
|
224
|
+
# Still the message asked about: the same digest, and still the one
|
|
225
|
+
# the turn id names (a reminder turn's id names no prompt, so it falls
|
|
226
|
+
# back to the newest answer, which a later turn's answer becomes).
|
|
227
|
+
def block_fix_anchored?(messages, answer, result)
|
|
228
|
+
Bridge::TurnDrawings.digest(answer) == block_fix_digest_now(result.anchor.digest) &&
|
|
229
|
+
AnswerTail.find(messages, turn_id: result.turn_id).equal?(answer)
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
# +digest+ after the fixes that landed in its answer's text since.
|
|
233
|
+
def block_fix_digest_now(digest)
|
|
234
|
+
seen = []
|
|
235
|
+
while (moved = @block_fix_moved[digest]) && !seen.include?(digest)
|
|
236
|
+
seen << digest
|
|
237
|
+
digest = moved
|
|
238
|
+
end
|
|
239
|
+
digest
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
# The answer's copy with the fix (or, for a failed ask, only the stamp)
|
|
243
|
+
# and where it landed ("answer", "display", nil); nil copy: refused.
|
|
244
|
+
def block_fix_patched(messages, answer, result)
|
|
245
|
+
unless result.fixed?
|
|
246
|
+
entry = BlockFix.entry(key: result.key, state: "failed", reason: result.reason)
|
|
247
|
+
return [BlockFix.with_stamp(answer, entry), nil]
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
copy, landed = if result.anchor.turns == @block_fix_turns && AnswerTail.find(messages).equal?(answer)
|
|
251
|
+
block_fix_in_answer(answer, result)
|
|
252
|
+
else
|
|
253
|
+
block_fix_in_display(answer, result)
|
|
254
|
+
end
|
|
255
|
+
return [nil, nil] unless copy
|
|
256
|
+
|
|
257
|
+
entry = BlockFix.entry(key: result.key, fixed_key: BlockFix.key(result.lang, result.fixed), state: "fixed")
|
|
258
|
+
[BlockFix.with_stamp(copy, entry), landed]
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# No turn since, the newest answer: its text (and its display, and
|
|
262
|
+
# on api: anthropic its saved blocks) holds the fix, in every copy of
|
|
263
|
+
# the block; nil copy: the display would pass its size.
|
|
264
|
+
def block_fix_in_answer(answer, result)
|
|
265
|
+
content = answer[:content].to_s
|
|
266
|
+
places = BlockFix.locate_all(content, lang: result.lang, source: result.source)
|
|
267
|
+
return block_fix_in_display(answer, result) if places.empty?
|
|
268
|
+
|
|
269
|
+
copy = answer.merge(content: BlockFix.replace_all(content, places, result.fixed))
|
|
270
|
+
if (display = answer[AnswerDisplay::KEY])
|
|
271
|
+
shown = BlockFix.locate_all(display, lang: result.lang, source: result.source)
|
|
272
|
+
text = shown.empty? ? display : BlockFix.replace_all(display, shown, result.fixed)
|
|
273
|
+
# The display's own cap (AnswerDisplay::MAX_CHARS), as #block_fix_in_display keeps it.
|
|
274
|
+
return [nil, nil] if text.length > AnswerDisplay::MAX_CHARS
|
|
275
|
+
|
|
276
|
+
copy[AnswerDisplay::KEY] = text
|
|
277
|
+
end
|
|
278
|
+
[block_fix_blocks(copy, result, places.size), "answer"]
|
|
279
|
+
end
|
|
280
|
+
|
|
281
|
+
# A turn since: only what the page renders (`display`), never what a
|
|
282
|
+
# model was sent.
|
|
283
|
+
def block_fix_in_display(answer, result)
|
|
284
|
+
shown = block_fix_shown(answer)
|
|
285
|
+
places = BlockFix.locate_all(shown, lang: result.lang, source: result.source)
|
|
286
|
+
return [nil, nil] if places.empty?
|
|
287
|
+
|
|
288
|
+
text = BlockFix.replace_all(shown, places, result.fixed)
|
|
289
|
+
return [nil, nil] if text.length > AnswerDisplay::MAX_CHARS
|
|
290
|
+
|
|
291
|
+
[answer.merge(AnswerDisplay::KEY => text), "display"]
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
# The saved Messages-API blocks (anthropic_content), which
|
|
295
|
+
# AnthropicWire sends as they are: the text blocks holding the
|
|
296
|
+
# +copies+ the text had patched, or the blocks dropped (the answer goes
|
|
297
|
+
# as its text) when they don't hold every copy whole. A thinking block
|
|
298
|
+
# is never touched.
|
|
299
|
+
def block_fix_blocks(copy, result, copies)
|
|
300
|
+
blocks = copy[:anthropic_content]
|
|
301
|
+
return copy unless blocks.is_a?(Array) && !blocks.empty?
|
|
302
|
+
|
|
303
|
+
found = 0
|
|
304
|
+
patched = blocks.map do |block|
|
|
305
|
+
next block if !block.is_a?(Hash) || (block["type"] || block[:type]).to_s != "text"
|
|
306
|
+
|
|
307
|
+
text_key = block.key?("text") ? "text" : :text
|
|
308
|
+
places = BlockFix.locate_all(block[text_key].to_s, lang: result.lang, source: result.source)
|
|
309
|
+
next block if places.empty?
|
|
310
|
+
|
|
311
|
+
found += places.size
|
|
312
|
+
block.merge(text_key => BlockFix.replace_all(block[text_key].to_s, places, result.fixed))
|
|
313
|
+
end
|
|
314
|
+
found == copies ? copy.merge(anthropic_content: patched) : copy.except(:anthropic_content)
|
|
315
|
+
end
|
|
316
|
+
|
|
317
|
+
def block_fix_dropped(result, why)
|
|
318
|
+
block_fix_announce(result, state: "dropped", landed: nil, reason: why)
|
|
319
|
+
false
|
|
320
|
+
end
|
|
321
|
+
|
|
322
|
+
def block_fix_announce(result, state:, landed:, reason: result.reason, was_digest: nil, digest: nil)
|
|
323
|
+
ms = ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - result.started) * 1000).round
|
|
324
|
+
Log.info(:model, "block_fix_#{state}", lang: result.lang, key: result.key, bytes: result.fixed.to_s.bytesize,
|
|
325
|
+
ms: ms, landed: landed, reason: reason)
|
|
326
|
+
@event_log.notify({ type: :block_fix, turn_id: result.turn_id, key: result.key, lang: result.lang,
|
|
327
|
+
state: state, landed: landed, reason: reason, was_digest: was_digest,
|
|
328
|
+
digest: digest }.compact)
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
end
|
|
332
|
+
end
|
|
@@ -12,17 +12,26 @@ module Samagotchi
|
|
|
12
12
|
# ── Cards ───────────────────────────────────────────────────────────────
|
|
13
13
|
|
|
14
14
|
# Show a card in every UI (docs/plugins.md, Cards): {type: :card, id:,
|
|
15
|
-
# source:, title:, body:, level:, actions: [{label:, command:}
|
|
15
|
+
# source:, title:, body:, level:, actions: [{label:, command:} |
|
|
16
|
+
# {label:, url:}], in_turn:}.
|
|
16
17
|
# Where it goes (held while the plugins load, an anytime command's, an
|
|
17
18
|
# init task's, a turn event, else announced) is EventRoutes#route's,
|
|
18
19
|
# marked in_turn: (CARD_MARKS). A card with the id of an earlier one
|
|
19
|
-
# replaces it (btw's "thinking…" → the answer).
|
|
20
|
+
# replaces it (btw's "thinking…" → the answer). +between_turns+: never a
|
|
21
|
+
# running turn's: while one runs it is shown as an anytime command's
|
|
22
|
+
# card is (a sign-in flow's, a plugin thread's that must not land in a
|
|
23
|
+
# turn's block, which the web collapses).
|
|
20
24
|
# @param source [String] who shows it (a bundle's name)
|
|
21
|
-
# @param actions [Array<Hash>] {label:, command:}
|
|
22
|
-
#
|
|
25
|
+
# @param actions [Array<Hash>] {label:, command:}, a line the session
|
|
26
|
+
# runs as typed (D3), or {label:, url:}, an http(s) address a UI opens
|
|
27
|
+
# (it sends nothing; Plugin::Card.build checks it)
|
|
23
28
|
# @return [String] the card's id
|
|
24
29
|
# @raise [ArgumentError] a card without a title, or a bad action
|
|
25
|
-
def show_card(source:, title:, body: "", actions: [], level: :info, id: nil)
|
|
30
|
+
def show_card(source:, title:, body: "", actions: [], level: :info, id: nil, between_turns: false)
|
|
31
|
+
if between_turns && turn_running?
|
|
32
|
+
return running_anytime { show_card(source: source, title: title, body: body, actions: actions, level: level, id: id) }
|
|
33
|
+
end
|
|
34
|
+
|
|
26
35
|
card = Plugin::Card.build(source: source, title: title, body: body, actions: actions, level: level, id: id)
|
|
27
36
|
command = Thread.current[running_command_key]
|
|
28
37
|
card[:command] = command if command
|
|
@@ -8,10 +8,10 @@ require_relative "../working_set"
|
|
|
8
8
|
|
|
9
9
|
module Samagotchi
|
|
10
10
|
class Engine
|
|
11
|
-
# The event log: persistent observers, #announce (ANNOUNCEABLE_EVENTS),
|
|
11
|
+
# The event log: persistent observers, #announce (the log's ANNOUNCEABLE_EVENTS),
|
|
12
12
|
# the event lock, held announcements, the load events, and the stream
|
|
13
13
|
# and emit helpers (EventFanout's, forwarded). Writes
|
|
14
|
-
# @guardrail_failures_announced; reads @
|
|
14
|
+
# @guardrail_failures_announced; reads @event_log, @event_fanout,
|
|
15
15
|
# @extension_load, @guardrail_failures, @plugin_failures.
|
|
16
16
|
module EventsPart
|
|
17
17
|
# Subscribe a persistent observer to engine events.
|
|
@@ -23,38 +23,33 @@ module Samagotchi
|
|
|
23
23
|
# @param observer [#call] receives event hashes (with `event_seq:` merged in)
|
|
24
24
|
# @return [Samagotchi::SessionObserver::SubscribedObserver] handle to unsubscribe
|
|
25
25
|
def subscribe(observer:)
|
|
26
|
-
@
|
|
26
|
+
@event_log.subscribe(observer: observer)
|
|
27
27
|
end
|
|
28
28
|
|
|
29
29
|
# Unsubscribe a previously-registered observer.
|
|
30
30
|
# @param handle [Samagotchi::SessionObserver::SubscribedObserver]
|
|
31
31
|
# @return [Boolean] whether the observer was removed (nil/unknown never raises)
|
|
32
32
|
def unsubscribe(handle:)
|
|
33
|
-
@
|
|
33
|
+
@event_log.unsubscribe(handle: handle)
|
|
34
34
|
end
|
|
35
35
|
|
|
36
36
|
# @return [Integer] total engine events emitted so far (locally monotonic)
|
|
37
37
|
def event_count
|
|
38
|
-
@
|
|
38
|
+
@event_log.event_count
|
|
39
39
|
end
|
|
40
40
|
|
|
41
|
-
# Put a transport-level event into the ordered event log
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
# @param event [Hash] with :type in ANNOUNCEABLE_EVENTS
|
|
41
|
+
# Put a transport-level event into the ordered event log
|
|
42
|
+
# (SessionObserver#announce, which checks the type).
|
|
43
|
+
# @param event [Hash] with :type in SessionObserver::ANNOUNCEABLE_EVENTS
|
|
45
44
|
# @raise [ArgumentError] for any other type
|
|
46
45
|
def announce(event)
|
|
47
|
-
|
|
48
|
-
raise ArgumentError, "cannot announce #{event[:type].inspect} (allowed: #{ANNOUNCEABLE_EVENTS.join(", ")})"
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
@session_observer.notify(event)
|
|
46
|
+
@event_log.announce(event)
|
|
52
47
|
end
|
|
53
48
|
|
|
54
49
|
# Run the block with the event log held: no event is numbered or
|
|
55
50
|
# delivered meanwhile, and events the block emits keep their order.
|
|
56
51
|
def synchronize_events(&)
|
|
57
|
-
@
|
|
52
|
+
@event_log.synchronize(&)
|
|
58
53
|
end
|
|
59
54
|
|
|
60
55
|
def reserve_enqueued_id(id) = @turn_state.reserve_enqueued_id(id)
|
|
@@ -228,7 +228,7 @@ module Samagotchi
|
|
|
228
228
|
window_source: window.source.to_s, source: "switch" }
|
|
229
229
|
event = { type: :context_status, usage: usage, bucket: display&.dig(:bucket),
|
|
230
230
|
context_window_tokens: window&.tokens, context_window_source: window&.source&.to_s }
|
|
231
|
-
@
|
|
231
|
+
@event_log.notify(event)
|
|
232
232
|
event
|
|
233
233
|
rescue StandardError => e
|
|
234
234
|
Log.warn(:model, "switched_context_failed", error: e.class.name, msg: e.message.to_s[0, 200])
|
|
@@ -151,6 +151,7 @@ module Samagotchi
|
|
|
151
151
|
cancelled: lambda {
|
|
152
152
|
(task = current_init_task) ? task.cancelled? : active_cancel_controller&.cancelled?
|
|
153
153
|
},
|
|
154
|
+
current_cancel: -> { (task = current_init_task) ? task.cancel : active_cancel_controller },
|
|
154
155
|
card: ->(**card) { show_card(**card) },
|
|
155
156
|
steer: ->(text, label) { steer(text, source: hook_source(label)) },
|
|
156
157
|
stop_turn: ->(reason, label) { hook_stop_turn(reason, label) },
|
|
@@ -173,6 +174,8 @@ module Samagotchi
|
|
|
173
174
|
[]
|
|
174
175
|
end,
|
|
175
176
|
state_dir: -> { session_state_dir },
|
|
177
|
+
secrets_store: ->(bundle) { Plugin::Secrets::FileStore.for_bundle(bundle) },
|
|
178
|
+
sign_in: ->(bundle, **options, &build) { start_sign_in(bundle: bundle, build: build, **options) },
|
|
176
179
|
scratch: -> { @scratch },
|
|
177
180
|
setup: -> { SessionSetup.of(@session, forked: true) },
|
|
178
181
|
# The files whose text the built system prompt holds; [] until a
|
|
@@ -29,7 +29,7 @@ module Samagotchi
|
|
|
29
29
|
def emit_recap(recap:, generation:, covered: nil, created_at: nil)
|
|
30
30
|
event = { type: :recap_ready, recap: recap, generation: generation, covered: covered }
|
|
31
31
|
event[:created_at] = created_at if created_at
|
|
32
|
-
@
|
|
32
|
+
@event_log.notify(event)
|
|
33
33
|
end
|
|
34
34
|
|
|
35
35
|
# @return [IdleRecap, nil] the idle recap detector, or nil when disabled
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative "../block_fix"
|
|
3
4
|
require_relative "../idle_client"
|
|
4
5
|
require_relative "../idle_target"
|
|
5
6
|
require_relative "../log"
|
|
@@ -35,7 +36,7 @@ module Samagotchi
|
|
|
35
36
|
usage = usage.with(author: side_author(usage.kind)) if usage.author.nil?
|
|
36
37
|
synchronize_events do
|
|
37
38
|
block = @metrics.add_side(usage)
|
|
38
|
-
@
|
|
39
|
+
@event_log.notify(type: :side_usage, kind: usage.kind, side: block)
|
|
39
40
|
end
|
|
40
41
|
sid = @metrics.session_id
|
|
41
42
|
return if sid.nil? || turn_running?
|
|
@@ -50,9 +51,9 @@ module Samagotchi
|
|
|
50
51
|
# command's ask_model between turns).
|
|
51
52
|
# @return [String, nil]
|
|
52
53
|
def side_author(kind)
|
|
53
|
-
# Check this
|
|
54
|
+
# Check this, the tracker and a block fix are only ever the owner's, whichever turn
|
|
54
55
|
# runs as they end.
|
|
55
|
-
return nil if [ObjectivesCheck::SOURCE, ObjectivesTrack::SOURCE].include?(kind.to_s)
|
|
56
|
+
return nil if [ObjectivesCheck::SOURCE, ObjectivesTrack::SOURCE, BlockFix::SOURCE].include?(kind.to_s)
|
|
56
57
|
|
|
57
58
|
# One read each of the published contexts (TurnState): any thread.
|
|
58
59
|
running = turn_context
|
|
@@ -102,8 +103,9 @@ module Samagotchi
|
|
|
102
103
|
# refused thinking-off, holds for the next request); the session's id
|
|
103
104
|
# goes with the request. Its usage counts in the session's side line
|
|
104
105
|
# (#note_side_usage) under +source+, the asking bundle's name.
|
|
105
|
-
# @
|
|
106
|
-
|
|
106
|
+
# @param whole [Boolean] return the IdleClient::Summary (its cut_off)
|
|
107
|
+
# @return [String, IdleClient::Summary] the answer
|
|
108
|
+
def ask_side_model(request, timeout:, max_tokens:, cancel_controller:, model: nil, source: nil, whole: false)
|
|
107
109
|
target = model.nil? ? session_model_recap_target : configured_side_target(model)
|
|
108
110
|
client = side_model_client(target, timeout)
|
|
109
111
|
started = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
@@ -111,7 +113,7 @@ module Samagotchi
|
|
|
111
113
|
kind: source.to_s.empty? ? "ask" : source.to_s, on_usage: method(:note_side_usage))
|
|
112
114
|
Log.info(:plugins, "ask_model", model: target.label, answer_model: answer.model, chars: answer.text.length,
|
|
113
115
|
ms: ((Process.clock_gettime(Process::CLOCK_MONOTONIC) - started) * 1000).round)
|
|
114
|
-
answer.text
|
|
116
|
+
whole ? answer : answer.text
|
|
115
117
|
end
|
|
116
118
|
private :ask_side_model
|
|
117
119
|
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "uri"
|
|
4
|
+
require_relative "../log"
|
|
5
|
+
require_relative "../plugin/card"
|
|
6
|
+
require_relative "../plugin/sign_in"
|
|
7
|
+
|
|
8
|
+
module Samagotchi
|
|
9
|
+
class Engine
|
|
10
|
+
# Browser sign-ins (ctx.sign_in, docs/plugins.md, Sign-in): the flows
|
|
11
|
+
# waiting now, their cards, the /callback paste, and their end when the
|
|
12
|
+
# worker leaves (#shutdown closes every one first, so a card a later
|
|
13
|
+
# worker loads never offers a dead sign-in). A flow never outlives its
|
|
14
|
+
# worker and is tied to no turn (unless the bundle passes cancel:, a
|
|
15
|
+
# turn's controller). Reads and writes @sign_in_flows under
|
|
16
|
+
# @lifecycle_mutex.
|
|
17
|
+
module SignInPart
|
|
18
|
+
# Start a flow for +bundle+: a loopback listener, the address +build+
|
|
19
|
+
# makes for its redirect_uri, the card with its button. Returns at
|
|
20
|
+
# once; the bundle waits on Plugin::SignIn#wait. Once #shutdown has
|
|
21
|
+
# begun it starts nothing: an already-stopped flow, no card, no port
|
|
22
|
+
# (a command or a thread of the plugin's may still call it then). The
|
|
23
|
+
# flow is listed before its card shows, so a shutdown in between
|
|
24
|
+
# stops it and its card says cancelled.
|
|
25
|
+
# @param build [#call] (redirect_uri) → the address to send the user to
|
|
26
|
+
# @param cancel [CancellationController, nil] ends it :cancelled (a
|
|
27
|
+
# tool's turn's Stop, when the bundle passes ctx.cancel_controller)
|
|
28
|
+
# @return [Plugin::SignIn]
|
|
29
|
+
# @raise [ArgumentError] a bad option, a taken port, an address the
|
|
30
|
+
# card refuses (Plugin::Card's url check); nothing stays open then
|
|
31
|
+
def start_sign_in(bundle:, title:, button:, timeout:, build:, port: nil, path: Plugin::SignIn::DEFAULT_PATH,
|
|
32
|
+
cancel: nil, listener_options: {})
|
|
33
|
+
Plugin::SignIn.check_options!(timeout: timeout, path: path, cancel: cancel)
|
|
34
|
+
return Plugin::SignIn.stopped(bundle: bundle, title: title) if sign_ins_closed?
|
|
35
|
+
|
|
36
|
+
listener = Plugin::SignIn::Listener.new(port: port, path: path, **listener_options)
|
|
37
|
+
flow = Plugin::SignIn.new(bundle: bundle, title: title, button: button, timeout: timeout, listener: listener,
|
|
38
|
+
cancel: cancel, on_settle: ->(settled, outcome) { sign_in_settled(settled, outcome) })
|
|
39
|
+
begin
|
|
40
|
+
address = build.call(flow.redirect_uri).to_s.strip
|
|
41
|
+
card = { title: title, body: Plugin::SignIn::PASTE_LINE, actions: [{ label: button, url: address }] }
|
|
42
|
+
# The card's checks (the address), before anything is listed.
|
|
43
|
+
card_id = Plugin::Card.build(source: bundle, level: :info, id: nil, **card)[:id]
|
|
44
|
+
flow.started(address, card_id)
|
|
45
|
+
rescue StandardError
|
|
46
|
+
listener.close
|
|
47
|
+
raise
|
|
48
|
+
end
|
|
49
|
+
unless list_sign_in(flow)
|
|
50
|
+
listener.close
|
|
51
|
+
return Plugin::SignIn.stopped(bundle: bundle, title: title)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
show_sign_in_card(flow, card)
|
|
55
|
+
Log.info(:plugins, "sign_in_started", bundle: bundle, card: card_id, port: listener.port, to: address_shown(address))
|
|
56
|
+
flow.listen
|
|
57
|
+
flow
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Whether a flow waits for its callback (WorkerIdleExit holds then).
|
|
61
|
+
def sign_in_waiting? = waiting_sign_ins.any?
|
|
62
|
+
|
|
63
|
+
# /callback <address>: the waiting flow whose redirect_uri +address+
|
|
64
|
+
# starts with takes its query, without a #fragment (a provider's
|
|
65
|
+
# "#_=_" isn't the query's).
|
|
66
|
+
# @return [Array(Boolean, String)] settled, and the line to answer
|
|
67
|
+
def sign_in_callback(address)
|
|
68
|
+
address = address.to_s.strip.split("#", 2).first.to_s
|
|
69
|
+
flows = waiting_sign_ins
|
|
70
|
+
return [false, "no sign-in is waiting"] if flows.empty?
|
|
71
|
+
|
|
72
|
+
flow = flows.find { |waiting| waiting.redirect_of?(address) }
|
|
73
|
+
return [false, "that isn't this sign-in's address (it starts #{flows.first.redirect_uri})"] unless flow
|
|
74
|
+
return [false, "that isn't this sign-in's address"] unless flow.offer(address.split("?", 2)[1])
|
|
75
|
+
|
|
76
|
+
[true, "Got it."]
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
private
|
|
80
|
+
|
|
81
|
+
def sign_ins_closed? = @lifecycle_mutex.synchronize { @shut_down }
|
|
82
|
+
|
|
83
|
+
# @return [Boolean] false once #shutdown has begun (nothing listed)
|
|
84
|
+
def list_sign_in(flow)
|
|
85
|
+
@lifecycle_mutex.synchronize do
|
|
86
|
+
next false if @shut_down
|
|
87
|
+
|
|
88
|
+
(@sign_in_flows ||= []) << flow
|
|
89
|
+
true
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Its card, unless a shutdown stopped it already; one that stopped it
|
|
94
|
+
# while the card showed gets its ended card again, after this one.
|
|
95
|
+
def show_sign_in_card(flow, card)
|
|
96
|
+
return unless flow.waiting?
|
|
97
|
+
|
|
98
|
+
sign_in_card(flow.bundle, id: flow.card_id, **card)
|
|
99
|
+
ended = Plugin::SignIn.ended_card(flow, flow.outcome) unless flow.waiting?
|
|
100
|
+
sign_in_card(flow.bundle, **ended) if ended
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# Read off the lock: a flow past its deadline ends in #waiting?, and
|
|
104
|
+
# its #sign_in_settled takes the lock.
|
|
105
|
+
def waiting_sign_ins
|
|
106
|
+
@lifecycle_mutex.synchronize { (@sign_in_flows || []).dup }.select(&:waiting?)
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# The worker leaves (#shutdown): each waiting flow stops, its card
|
|
110
|
+
# replaced by the cancelled one, before the services go.
|
|
111
|
+
def stop_sign_ins
|
|
112
|
+
waiting_sign_ins.each(&:stop)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# A flow ended: it leaves the list; one that ended without a callback
|
|
116
|
+
# gets its ended card in place of its button. :done leaves the card
|
|
117
|
+
# for the bundle's result.
|
|
118
|
+
def sign_in_settled(flow, outcome)
|
|
119
|
+
@lifecycle_mutex.synchronize { @sign_in_flows&.delete(flow) }
|
|
120
|
+
Log.info(:plugins, "sign_in_ended", bundle: flow.bundle, card: flow.card_id, outcome: outcome)
|
|
121
|
+
card = Plugin::SignIn.ended_card(flow, outcome)
|
|
122
|
+
sign_in_card(flow.bundle, **card) if card
|
|
123
|
+
rescue StandardError => e
|
|
124
|
+
Log.warn(:plugins, "sign_in_card_failed", bundle: flow.bundle, error: e.class.name)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# A flow's card is never a running turn's (a timeout fires whenever):
|
|
128
|
+
# Engine#show_card's between_turns.
|
|
129
|
+
def sign_in_card(bundle, **card)
|
|
130
|
+
show_card(source: bundle, **card, between_turns: true)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# Its host and path: an authorize address can carry a login hint.
|
|
134
|
+
def address_shown(address)
|
|
135
|
+
uri = URI.parse(address)
|
|
136
|
+
"#{uri.host}#{uri.path}"
|
|
137
|
+
rescue URI::InvalidURIError
|
|
138
|
+
"?"
|
|
139
|
+
end
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
end
|