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,30 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../block_fix"
|
|
4
|
+
require_relative "../block_renderers"
|
|
5
|
+
|
|
6
|
+
module Samagotchi
|
|
7
|
+
class Bridge
|
|
8
|
+
# POST /session/:id/blocks/fix {turn_id, lang, source, reason}: a block
|
|
9
|
+
# the page couldn't draw in the newest answer, asked for again once
|
|
10
|
+
# (Engine#fix_block; docs/internals/block-renderers.md, A failed block is
|
|
11
|
+
# asked for again). Mixed into Bridge (#parse_json, #fetched, @engine).
|
|
12
|
+
module BlockFixRoute
|
|
13
|
+
# 400 missing_fields without a turn id, a language a fence can name or
|
|
14
|
+
# a source; else Engine#fix_block's status and body.
|
|
15
|
+
# @return [Array(Hash, Integer, Hash)]
|
|
16
|
+
def handle_block_fix(_session_id, body)
|
|
17
|
+
parsed = parse_json(body)
|
|
18
|
+
return [{ "Allow" => "POST" }, 400, { error: "invalid_json" }] unless parsed.is_a?(Hash)
|
|
19
|
+
|
|
20
|
+
turn_id, lang, source, reason = %w[turn_id lang source reason].map { |key| fetched(parsed, key) }
|
|
21
|
+
unless [turn_id, lang, source].all? { |v| v.is_a?(String) && !v.empty? } && lang.match?(BlockRenderers::LANG)
|
|
22
|
+
return [{ "Allow" => "POST" }, 400, { error: "missing_fields", detail: "turn_id, lang and source required" }]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
status, answer = @engine.fix_block(turn_id: turn_id, lang: lang, source: source, reason: reason.to_s)
|
|
26
|
+
[{}, status, answer]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
@@ -4,11 +4,12 @@ require "json"
|
|
|
4
4
|
require "fileutils"
|
|
5
5
|
require_relative "../atomic_file"
|
|
6
6
|
require_relative "../events"
|
|
7
|
+
require_relative "card_wait"
|
|
7
8
|
|
|
8
9
|
module Samagotchi
|
|
9
10
|
class Bridge
|
|
10
|
-
# The running turn's open card with actions (check-in's Nudge /
|
|
11
|
-
# going / Stop) as a file in the session's folder, pending_card.json
|
|
11
|
+
# The running turn's open card with command actions (check-in's Nudge /
|
|
12
|
+
# Keep going / Stop; a url action alone doesn't ask, CardWait) as a file in the session's folder, pending_card.json
|
|
12
13
|
# {id:, bundle:}, for `chi web`'s session hub: it watches files, not
|
|
13
14
|
# workers, and a tab behind notifies from its summary (notify.js), as
|
|
14
15
|
# for a pending question. A persistent observer, like CardStore.
|
|
@@ -63,7 +64,7 @@ module Samagotchi
|
|
|
63
64
|
end
|
|
64
65
|
|
|
65
66
|
def card(event)
|
|
66
|
-
asks = Array(event[:actions]).any?
|
|
67
|
+
asks = Array(event[:actions]).any? { |action| CardWait.command?(action) }
|
|
67
68
|
if event[:in_turn] && asks
|
|
68
69
|
write(event[:id].to_s, event[:source].to_s)
|
|
69
70
|
elsif @id && event[:id].to_s == @id
|
|
@@ -12,7 +12,7 @@ module Samagotchi
|
|
|
12
12
|
# Per-connection SSE writer.
|
|
13
13
|
#
|
|
14
14
|
# This object doubles as the per-connection live observer wired into the
|
|
15
|
-
#
|
|
15
|
+
# event log via `SessionObserver#subscribe`: its `#call` is invoked synchronously from
|
|
16
16
|
# `Engine#emit_event` inside the turn thread, so it must be strictly
|
|
17
17
|
# non-blocking — it only enqueues onto a bounded queue and returns. A
|
|
18
18
|
# dedicated writer thread (the connection's own thread) drains the queue
|
|
@@ -45,7 +45,7 @@ module Samagotchi
|
|
|
45
45
|
# Queued by the hang-up watcher (#watch_hangup) to wake #drain!.
|
|
46
46
|
HANGUP = Object.new.freeze
|
|
47
47
|
|
|
48
|
-
# @param
|
|
48
|
+
# @param event_log [Samagotchi::SessionObserver] the Engine's event log (live fan-out)
|
|
49
49
|
# @param ring [Samagotchi::Bridge::RingBuffer] shared capture buffer
|
|
50
50
|
# @param session_id [String]
|
|
51
51
|
# @param last_event_id [String, nil] reconnect cursor (or ?from_seq=):
|
|
@@ -58,23 +58,24 @@ module Samagotchi
|
|
|
58
58
|
# to just the event_seq.
|
|
59
59
|
# @param join_with_snapshot [Boolean] start with a snapshot frame rather
|
|
60
60
|
# than a replay
|
|
61
|
-
# @param
|
|
61
|
+
# @param stopped [#call, nil] whether the stream should end (the
|
|
62
|
+
# Bridge's #stopped?): asked when an idle wait ends
|
|
62
63
|
# @param max_queue [Integer] bounded-queue capacity
|
|
63
64
|
# @param heartbeat_interval [Float] idle-seconds between `: ping` frames
|
|
64
65
|
# @param raw [Boolean] write events as the engine emitted them, not as
|
|
65
66
|
# clients get them (ClientEvent): `?raw=1`, for debugging
|
|
66
|
-
def initialize(
|
|
67
|
-
frame_provider: nil, join_with_snapshot: false,
|
|
67
|
+
def initialize(event_log:, ring:, session_id:, last_event_id: nil, epoch: nil,
|
|
68
|
+
frame_provider: nil, join_with_snapshot: false, stopped: nil,
|
|
68
69
|
max_queue: DEFAULT_MAX_QUEUE,
|
|
69
70
|
heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL, raw: false)
|
|
70
|
-
@
|
|
71
|
+
@event_log = event_log
|
|
71
72
|
@ring = ring
|
|
72
73
|
@session_id = session_id
|
|
73
74
|
@last_event_id = last_event_id
|
|
74
75
|
@epoch = epoch
|
|
75
|
-
@frame_provider = frame_provider || -> { { snapshot: { event_seq: @
|
|
76
|
+
@frame_provider = frame_provider || -> { { snapshot: { event_seq: @event_log.event_count }, session_state_snapshot: {} } }
|
|
76
77
|
@join_with_snapshot = join_with_snapshot
|
|
77
|
-
@
|
|
78
|
+
@stopped = stopped
|
|
78
79
|
@max_queue = max_queue
|
|
79
80
|
@heartbeat_interval = heartbeat_interval
|
|
80
81
|
@raw = raw
|
|
@@ -110,16 +111,16 @@ module Samagotchi
|
|
|
110
111
|
begin
|
|
111
112
|
if @join_with_snapshot
|
|
112
113
|
# Subscribe and snapshot as one step of the event log, then go live.
|
|
113
|
-
frame = @
|
|
114
|
-
@handle = @
|
|
114
|
+
frame = @event_log.synchronize do
|
|
115
|
+
@handle = @event_log.subscribe(observer: self)
|
|
115
116
|
take_snapshot
|
|
116
117
|
end
|
|
117
118
|
write_snapshot_frame(io, :snapshot, frame)
|
|
118
119
|
else
|
|
119
120
|
# (1) subscribe the live queue FIRST.
|
|
120
|
-
@handle = @
|
|
121
|
+
@handle = @event_log.subscribe(observer: self)
|
|
121
122
|
# (2) capture the snapshot sequence.
|
|
122
|
-
snapshot_seq = @
|
|
123
|
+
snapshot_seq = @event_log.event_count
|
|
123
124
|
# (3) replay buffered (from_seq, snapshot].
|
|
124
125
|
replay!(io, snapshot_seq)
|
|
125
126
|
end
|
|
@@ -128,7 +129,7 @@ module Samagotchi
|
|
|
128
129
|
rescue Errno::EPIPE, Errno::ECONNRESET, IOError
|
|
129
130
|
# Client hung up; nothing to do.
|
|
130
131
|
ensure
|
|
131
|
-
@
|
|
132
|
+
@event_log.unsubscribe(handle: @handle) if @handle
|
|
132
133
|
watcher&.kill
|
|
133
134
|
end
|
|
134
135
|
end
|
|
@@ -198,7 +199,7 @@ module Samagotchi
|
|
|
198
199
|
# behind; nudge a reconnect rather than flooding more events.
|
|
199
200
|
emit_reset(io) if @queue.overflow_dropped?
|
|
200
201
|
@queue.clear_overflow!
|
|
201
|
-
elsif @
|
|
202
|
+
elsif @stopped&.call
|
|
202
203
|
break
|
|
203
204
|
else
|
|
204
205
|
write_heartbeat(io)
|
|
@@ -258,7 +259,7 @@ module Samagotchi
|
|
|
258
259
|
# Emit a control frame carrying the current snapshot so a client that
|
|
259
260
|
# cannot replay can re-derive state, then continue live.
|
|
260
261
|
def emit_reset(io)
|
|
261
|
-
write_snapshot_frame(io, :reset, @
|
|
262
|
+
write_snapshot_frame(io, :reset, @event_log.synchronize { take_snapshot })
|
|
262
263
|
end
|
|
263
264
|
|
|
264
265
|
# With the event log held: the snapshot frame's content (the snapshot
|
|
@@ -104,6 +104,25 @@ module Samagotchi
|
|
|
104
104
|
Log.warn(:bridge, "turn_drawing_failed", error: "#{e.class}: #{e.message}")
|
|
105
105
|
end
|
|
106
106
|
|
|
107
|
+
# An observer of the Engine's events: a block fix that patched an
|
|
108
|
+
# answer's text (:block_fix, landed "answer") changes the digest its
|
|
109
|
+
# turn's line ends on, so that line is written again with the new end
|
|
110
|
+
# digest, only when it ended on the answer as it was (+was_digest+).
|
|
111
|
+
# A fix in the display only keeps the digest. Never raises.
|
|
112
|
+
def call(event)
|
|
113
|
+
return unless @path && event[:type] == :block_fix && event[:landed] == "answer"
|
|
114
|
+
|
|
115
|
+
id = event[:turn_id].to_s
|
|
116
|
+
line = [@last].compact.find { |l| l.turn_id == id } || self.class.read(File.dirname(@path)).reverse_each.find { |l| l.turn_id == id }
|
|
117
|
+
return unless line && line.to_digest == event[:was_digest]
|
|
118
|
+
|
|
119
|
+
restamped = line.with(to_digest: event[:digest], from_digest: line.from == line.to - 1 ? event[:digest] : line.from_digest)
|
|
120
|
+
write(restamped)
|
|
121
|
+
@last = restamped
|
|
122
|
+
rescue StandardError => e
|
|
123
|
+
Log.warn(:bridge, "turn_drawing_restamp_failed", error: "#{e.class}: #{e.message}")
|
|
124
|
+
end
|
|
125
|
+
|
|
107
126
|
def line_for(turn, end_event)
|
|
108
127
|
from = end_event[:kept_from]
|
|
109
128
|
to = end_event[:kept_to]
|
data/lib/samagotchi/bridge.rb
CHANGED
|
@@ -15,6 +15,7 @@ require_relative "delivery"
|
|
|
15
15
|
require_relative "session_inbox"
|
|
16
16
|
require_relative "turn_anchors"
|
|
17
17
|
|
|
18
|
+
require_relative "bridge/block_fix_route"
|
|
18
19
|
require_relative "bridge/bounded_queue"
|
|
19
20
|
require_relative "bridge/card_store"
|
|
20
21
|
require_relative "bridge/event_id"
|
|
@@ -53,7 +54,7 @@ module Samagotchi
|
|
|
53
54
|
# turn via HTTP POST.
|
|
54
55
|
#
|
|
55
56
|
# Design invariants (see multi_ui_architecture):
|
|
56
|
-
# * Reuses
|
|
57
|
+
# * Reuses the Engine's event log (`event_log:`) for SSE fan-out — never a second Engine, never
|
|
57
58
|
# `run_turn` across the thread/process boundary. POST reuses the
|
|
58
59
|
# `SessionInbox` file-IPC input path.
|
|
59
60
|
# * One capture observer fills a shared ring buffer; each connection gets a
|
|
@@ -65,33 +66,21 @@ module Samagotchi
|
|
|
65
66
|
# * Optional at runtime: this module is never loaded unless explicitly
|
|
66
67
|
# engaged as a transport.
|
|
67
68
|
class Bridge
|
|
69
|
+
include BlockFixRoute
|
|
70
|
+
|
|
68
71
|
ENGINE_CALLS = {
|
|
69
|
-
active_cancel_controller: "turn-state monitor",
|
|
70
|
-
annotate_question: "question-desk control",
|
|
71
|
-
announce: "event-log monitor",
|
|
72
|
-
answer_question: "question-desk control",
|
|
73
|
-
cancel_current_turn!: "turn cancellation",
|
|
74
|
-
cancel_question: "question-desk control",
|
|
75
72
|
command_registry: "startup-built registry",
|
|
76
|
-
|
|
77
|
-
drop_enqueued_id: "turn-state monitor",
|
|
78
|
-
enqueued_id?: "turn-state monitor",
|
|
79
|
-
event_count: "observer-registry monitor",
|
|
80
|
-
forget_enqueued_id: "turn-state monitor",
|
|
81
|
-
generation_snapshot: "generation-phase snapshot",
|
|
73
|
+
fix_block: "block-fix mutex; replace-only session messages",
|
|
82
74
|
guardrail_warning: "startup-set warning reader",
|
|
83
75
|
init_tasks: "plugin-task monitor",
|
|
84
|
-
input_epoch: "waiting-steer monitor",
|
|
85
76
|
last_answer_message: "message snapshot under event lock",
|
|
86
77
|
messages_checkpoint: "replace-only message copy",
|
|
87
78
|
objectives_view: "objectives snapshot under event lock",
|
|
88
|
-
pending_question: "question-desk monitor",
|
|
89
79
|
plugin_warning: "warning-state reader",
|
|
90
80
|
recap: "recap-job reference",
|
|
91
81
|
recap_override: "session-setting reader",
|
|
92
82
|
relay_desk: "relay-store reference; status is monitor-protected",
|
|
93
83
|
request_recap: "idle-recap monitor",
|
|
94
|
-
reserve_enqueued_id: "turn-state monitor",
|
|
95
84
|
working_set_snapshot: "idle working-set snapshot under event lock",
|
|
96
85
|
working_set_item: "turn view under its mutex, else session messages under event lock",
|
|
97
86
|
"running_turn_messages=": "callback writer; Bridge lifecycle",
|
|
@@ -99,11 +88,7 @@ module Samagotchi
|
|
|
99
88
|
session: "session reference; messages are replace-only",
|
|
100
89
|
session_state_snapshot: "snapshot; event lock for consistency",
|
|
101
90
|
stats_snapshot: "stats snapshot; may probe model",
|
|
102
|
-
steer_cut_after: "per-read model setting"
|
|
103
|
-
subscribe: "observer-registry monitor",
|
|
104
|
-
synchronize_events: "event-log monitor",
|
|
105
|
-
turn_running?: "turn-state monitor",
|
|
106
|
-
unsubscribe: "observer-registry monitor"
|
|
91
|
+
steer_cut_after: "per-read model setting"
|
|
107
92
|
}.freeze
|
|
108
93
|
|
|
109
94
|
DEFAULT_BIND = "127.0.0.1"
|
|
@@ -132,6 +117,10 @@ module Samagotchi
|
|
|
132
117
|
SECRET_BYTES = 32
|
|
133
118
|
# @param engine [Samagotchi::Engine] the owning engine (must already live
|
|
134
119
|
# in this process)
|
|
120
|
+
# @param event_log [SessionObserver] that engine's event log (Engine#event_log)
|
|
121
|
+
# @param turn_state [TurnState] that engine's turn state (Engine#turn_state)
|
|
122
|
+
# @param question_desk [QuestionDesk] that engine's question desk (Engine#question_desk)
|
|
123
|
+
# @param steer_cut [SteerCut] that engine's steer cut (Engine#steer_cut)
|
|
135
124
|
# @param state_dir [String] the session state directory root
|
|
136
125
|
# @param session_id [String] this bridge's session id
|
|
137
126
|
# @param bind [String] bind address (127.0.0.1 only)
|
|
@@ -172,12 +161,16 @@ module Samagotchi
|
|
|
172
161
|
# @param on_activity [#call, nil] what the worker is busy with right now
|
|
173
162
|
# (Worker#activity_snapshot: the same signals WorkerIdleExit#hold reads)
|
|
174
163
|
# for GET /activity; without one that route answers 501
|
|
175
|
-
def initialize(engine:, state_dir:, session_id:, bind: DEFAULT_BIND,
|
|
164
|
+
def initialize(engine:, event_log:, turn_state:, question_desk:, steer_cut:, state_dir:, session_id:, bind: DEFAULT_BIND,
|
|
176
165
|
port: 0, ring_capacity: DEFAULT_RING_CAPACITY,
|
|
177
166
|
heartbeat_interval: DEFAULT_HEARTBEAT_INTERVAL, input_format: nil, on_input: nil,
|
|
178
167
|
on_command: nil, on_exit_request: nil, exit_discards: nil, exit_stopped: nil,
|
|
179
168
|
on_holds: nil, on_holds_stop: nil, on_activity: nil)
|
|
180
169
|
@engine = engine
|
|
170
|
+
@event_log = event_log
|
|
171
|
+
@turn_state = turn_state
|
|
172
|
+
@question_desk = question_desk
|
|
173
|
+
@steer_cut = steer_cut
|
|
181
174
|
@on_input = on_input
|
|
182
175
|
@on_command = on_command
|
|
183
176
|
@on_exit_request = on_exit_request
|
|
@@ -284,14 +277,15 @@ module Samagotchi
|
|
|
284
277
|
def start
|
|
285
278
|
@server = TCPServer.new(@bind, @port)
|
|
286
279
|
@port = @server.local_address.ip_port
|
|
287
|
-
@capture_handle = @
|
|
288
|
-
@accumulator_handle = @
|
|
280
|
+
@capture_handle = @event_log.subscribe(observer: capture_observer)
|
|
281
|
+
@accumulator_handle = @event_log.subscribe(observer: @accumulator)
|
|
282
|
+
@drawings_handle = @event_log.subscribe(observer: @drawings)
|
|
289
283
|
# A plugin's ctx.messages mid-turn holds the turn so far (plan O1).
|
|
290
284
|
@engine.running_turn_messages = -> { @accumulator.current_messages }
|
|
291
|
-
@cards_handle = @
|
|
285
|
+
@cards_handle = @event_log.subscribe(observer: @cards)
|
|
292
286
|
# One a worker that died left is not open.
|
|
293
287
|
@pending_card.clear
|
|
294
|
-
@pending_card_handle = @
|
|
288
|
+
@pending_card_handle = @event_log.subscribe(observer: @pending_card)
|
|
295
289
|
# #stop and #close_intake wake the acceptor through it: closing the
|
|
296
290
|
# listen socket doesn't cut a select on it short on Linux.
|
|
297
291
|
@wake_reader, @wake_writer = IO.pipe
|
|
@@ -342,6 +336,7 @@ module Samagotchi
|
|
|
342
336
|
wake_acceptor
|
|
343
337
|
@capture_handle&.unsubscribe
|
|
344
338
|
@accumulator_handle&.unsubscribe
|
|
339
|
+
@drawings_handle&.unsubscribe
|
|
345
340
|
@engine.running_turn_messages = nil
|
|
346
341
|
@cards_handle&.unsubscribe
|
|
347
342
|
@pending_card_handle&.unsubscribe
|
|
@@ -394,12 +389,12 @@ module Samagotchi
|
|
|
394
389
|
# guardrail_warning:, plugin_warning:, init_tasks:, cards:, commands:, chi_version:, event_seq:, event_id:, last_turn_drawing:,
|
|
395
390
|
# objectives:, idle_working_set:}
|
|
396
391
|
def snapshot
|
|
397
|
-
@
|
|
398
|
-
seq = @
|
|
392
|
+
@event_log.synchronize do
|
|
393
|
+
seq = @event_log.event_count
|
|
399
394
|
messages = @engine.messages_checkpoint
|
|
400
395
|
drawing = last_turn_drawing(messages)
|
|
401
396
|
idle_working_set_unavailable = Object.new
|
|
402
|
-
idle_working_set = if @
|
|
397
|
+
idle_working_set = if @turn_state.running?
|
|
403
398
|
idle_working_set_unavailable
|
|
404
399
|
else
|
|
405
400
|
Degrade.call(:bridge, "working_set_snapshot_failed", fallback: idle_working_set_unavailable) do
|
|
@@ -419,7 +414,7 @@ module Samagotchi
|
|
|
419
414
|
recap: @accumulator.recap,
|
|
420
415
|
saved_recap: @engine.saved_recap,
|
|
421
416
|
continue_offer: @accumulator.continue_offer,
|
|
422
|
-
pending_question: @
|
|
417
|
+
pending_question: @question_desk.pending&.to_h,
|
|
423
418
|
guardrail_warning: @engine.guardrail_warning,
|
|
424
419
|
plugin_warning: @engine.plugin_warning,
|
|
425
420
|
init_tasks: @engine.init_tasks,
|
|
@@ -475,7 +470,7 @@ module Samagotchi
|
|
|
475
470
|
# snapshot or reset frame (SSEWriter) and GET snapshot carry.
|
|
476
471
|
# @return [Hash] {snapshot:, session_state_snapshot:}
|
|
477
472
|
def snapshot_frame
|
|
478
|
-
@
|
|
473
|
+
@event_log.synchronize do
|
|
479
474
|
snap = snapshot
|
|
480
475
|
state = @engine.session_state_snapshot.merge(event_seq: snap[:event_seq], event_id: snap[:event_id])
|
|
481
476
|
{ snapshot: snap, session_state_snapshot: state }
|
|
@@ -492,8 +487,8 @@ module Samagotchi
|
|
|
492
487
|
# @return [Hash] {session_id:, working_directory:, session_state_snapshot:, answer:, cards:, event_seq:, event_id:}
|
|
493
488
|
# +turn_id+: that turn's answer, not the newest (Engine#last_answer_message).
|
|
494
489
|
def tail_frame(turn_id: nil)
|
|
495
|
-
@
|
|
496
|
-
seq = @
|
|
490
|
+
@event_log.synchronize do
|
|
491
|
+
seq = @event_log.event_count
|
|
497
492
|
state = @engine.session_state_snapshot.merge(event_seq: seq, event_id: event_id(seq))
|
|
498
493
|
{ session_id: @session_id, working_directory: @engine.session&.working_directory,
|
|
499
494
|
session_state_snapshot: state, answer: @engine.last_answer_message(turn_id: turn_id),
|
|
@@ -511,9 +506,10 @@ module Samagotchi
|
|
|
511
506
|
# A card's action (the step-limit question's answer): the UIs leave
|
|
512
507
|
# its line out. +enqueued_id+: a file-delivered command's input, whose
|
|
513
508
|
# sender's wait its command_ran settles. +author+: its input's.
|
|
514
|
-
|
|
509
|
+
line = line.to_s.strip
|
|
510
|
+
command = QueuedCommand.mint(line, client_id: client_id, card: card, author: author, private_args: command_registry.private_args?(line))
|
|
515
511
|
.with(enqueued_id: enqueued_id)
|
|
516
|
-
@
|
|
512
|
+
@event_log.synchronize { queue_command_locked(command, after_file: after_file) }
|
|
517
513
|
command.command_id
|
|
518
514
|
end
|
|
519
515
|
|
|
@@ -528,11 +524,11 @@ module Samagotchi
|
|
|
528
524
|
|
|
529
525
|
# The turn in progress (TurnAccumulator#current_turn) with the running
|
|
530
526
|
# generation's phase as generation: {thinking_only:, age:}
|
|
531
|
-
# (
|
|
527
|
+
# (SteerCut#generation_snapshot), so a page joining mid-generation can
|
|
532
528
|
# clock its delivery line and say whether a cut can land.
|
|
533
529
|
def current_turn_with_generation
|
|
534
530
|
turn = @accumulator.current_turn
|
|
535
|
-
gen = turn && @
|
|
531
|
+
gen = turn && @steer_cut.generation_snapshot
|
|
536
532
|
gen ? turn.merge(generation: gen.to_h) : turn
|
|
537
533
|
end
|
|
538
534
|
|
|
@@ -688,7 +684,7 @@ module Samagotchi
|
|
|
688
684
|
end
|
|
689
685
|
|
|
690
686
|
writer = SSEWriter.new(
|
|
691
|
-
|
|
687
|
+
event_log: @event_log,
|
|
692
688
|
ring: @ring,
|
|
693
689
|
session_id: @session_id,
|
|
694
690
|
last_event_id: last_event_id,
|
|
@@ -696,7 +692,7 @@ module Samagotchi
|
|
|
696
692
|
frame_provider: -> { snapshot_frame },
|
|
697
693
|
# A reconnect (with a cursor) replays; only a fresh join snapshots.
|
|
698
694
|
join_with_snapshot: snapshot && last_event_id.nil?,
|
|
699
|
-
|
|
695
|
+
stopped: method(:stopped?),
|
|
700
696
|
heartbeat_interval: @heartbeat_interval,
|
|
701
697
|
raw: raw
|
|
702
698
|
)
|
|
@@ -813,7 +809,7 @@ module Samagotchi
|
|
|
813
809
|
SessionInbox::UNQUEUE_FEATURE, Delivery::REVIEW_FEATURE, Delivery::PAIR_FEATURE, TurnDrawings::FEATURE,
|
|
814
810
|
WorkerLink::ACTIVITY_FEATURE, ObjectivesTrack::FEATURE, Delivery::OWN_TURN_STEER_FEATURE,
|
|
815
811
|
Delivery::CANCEL_TURN_ID_FEATURE, ReviseAnchor::PARTICIPANT_FEATURE, Delivery::PARTICIPANT_ID_FEATURE,
|
|
816
|
-
WorkingSet::ITEM_FEATURE].freeze
|
|
812
|
+
WorkingSet::ITEM_FEATURE, BlockFix::FEATURE].freeze
|
|
817
813
|
|
|
818
814
|
# The routes, by method and what follows /session/:id/: a handler takes
|
|
819
815
|
# the session id and the request body and returns [headers, status, body].
|
|
@@ -839,7 +835,8 @@ module Samagotchi
|
|
|
839
835
|
%w[GET stats] => :handle_stats,
|
|
840
836
|
%w[GET snapshot] => :handle_snapshot,
|
|
841
837
|
%w[GET tail] => :handle_tail,
|
|
842
|
-
%w[GET working_set/item] => :handle_working_set_item
|
|
838
|
+
%w[GET working_set/item] => :handle_working_set_item,
|
|
839
|
+
%w[POST blocks/fix] => :handle_block_fix
|
|
843
840
|
}.freeze
|
|
844
841
|
ROUTE_PATH = %r{\A/session/([^/]+)/(.+)\z}u
|
|
845
842
|
# The handlers that take the request's query string as a third
|
|
@@ -896,10 +893,10 @@ module Samagotchi
|
|
|
896
893
|
turn_id = nil if turn_id && turn_id.empty?
|
|
897
894
|
end
|
|
898
895
|
|
|
899
|
-
if @
|
|
900
|
-
# With a turn_id the
|
|
901
|
-
# its lock; clients that send none (older ones) cancel whichever runs.
|
|
902
|
-
ok =
|
|
896
|
+
if @turn_state.running? && @turn_state.controller
|
|
897
|
+
# With a turn_id the turn state compares it to the running turn's under
|
|
898
|
+
# its lock; clients that send none (older ones, nil) cancel whichever runs.
|
|
899
|
+
ok = @turn_state.cancel!(reason, turn_id: turn_id)
|
|
903
900
|
if ok == :turn_mismatch
|
|
904
901
|
return [{}, 409, { error: "turn_mismatch", detail: "the running turn is not the one named", session_id: @session_id }]
|
|
905
902
|
end
|
|
@@ -973,9 +970,9 @@ module Samagotchi
|
|
|
973
970
|
return deadline_passed("answer") if expired?("answer_expired", deadline, sid: session_id, id: qid)
|
|
974
971
|
|
|
975
972
|
begin
|
|
976
|
-
result = @
|
|
973
|
+
result = @question_desk.answer(id: qid, selected: selected, freeform: freeform, client_id: client_id)
|
|
977
974
|
[{}, 200, { status: "answered", session_id: session_id, answer: result.to_h }]
|
|
978
|
-
rescue
|
|
975
|
+
rescue QuestionDesk::NotPending => e
|
|
979
976
|
# Another client answered first, or the question was cancelled.
|
|
980
977
|
[{}, 409, { error: "question_not_pending", detail: e.message }]
|
|
981
978
|
rescue QuestionDesk::Refused => e
|
|
@@ -1038,7 +1035,7 @@ module Samagotchi
|
|
|
1038
1035
|
reason = fetched(parsed, "reason").to_s
|
|
1039
1036
|
relay_closed(qid, relay_id, RELAY_CLOSE_REASONS.include?(reason) ? reason : "closed")
|
|
1040
1037
|
else
|
|
1041
|
-
RelayVerifier.new(
|
|
1038
|
+
RelayVerifier.new(question_desk: @question_desk, state_dir: @state_dir, session_id: session_id)
|
|
1042
1039
|
.answer(relay_id: relay_id, question_id: qid)
|
|
1043
1040
|
end
|
|
1044
1041
|
end
|
|
@@ -1046,7 +1043,7 @@ module Samagotchi
|
|
|
1046
1043
|
# Dismiss the pending question (an empty answer at a terminal): the
|
|
1047
1044
|
# tool returns without an answer and every UI gets :question_cancelled.
|
|
1048
1045
|
# Only the question the client saw, and only while nobody has answered
|
|
1049
|
-
# it:
|
|
1046
|
+
# it: QuestionDesk#cancel checks both under the question lock. A past
|
|
1050
1047
|
# +deadline+ (dismissing an approval denies it): 408, as for an answer.
|
|
1051
1048
|
# Returns [headers, status, body].
|
|
1052
1049
|
def handle_dismiss_question(session_id, body)
|
|
@@ -1061,7 +1058,7 @@ module Samagotchi
|
|
|
1061
1058
|
return deadline_passed("dismissal") if expired?("dismiss_expired", deadline, sid: session_id, id: qid)
|
|
1062
1059
|
|
|
1063
1060
|
begin
|
|
1064
|
-
dismissed = @
|
|
1061
|
+
dismissed = @question_desk.cancel("dismissed", id: qid)
|
|
1065
1062
|
rescue QuestionDesk::NotDismissable => e
|
|
1066
1063
|
# A step-limit question: its answer is Continue or Stop.
|
|
1067
1064
|
return [{}, 409, { error: "not_dismissable", detail: e.message }]
|
|
@@ -1117,8 +1114,8 @@ module Samagotchi
|
|
|
1117
1114
|
# /turn's (a /word sent as a message).
|
|
1118
1115
|
def queue_command_request(session_id, line, client_id:, deadline: nil, card: false, author: nil)
|
|
1119
1116
|
# A card's action: the UIs leave its line out (the card is the echo).
|
|
1120
|
-
command = QueuedCommand.mint(line, client_id: client_id, card: card, author: author)
|
|
1121
|
-
queued = @
|
|
1117
|
+
command = QueuedCommand.mint(line, client_id: client_id, card: card, author: author, private_args: command_registry.private_args?(line))
|
|
1118
|
+
queued = @event_log.synchronize do
|
|
1122
1119
|
raise IntakeClosed if stopped?
|
|
1123
1120
|
next false if expired?("command_expired", deadline, sid: session_id, client_id: command.client_id)
|
|
1124
1121
|
|
|
@@ -1142,7 +1139,7 @@ module Samagotchi
|
|
|
1142
1139
|
when :queue then { waits: "turn_end" }
|
|
1143
1140
|
else {}
|
|
1144
1141
|
end
|
|
1145
|
-
@
|
|
1142
|
+
@event_log.announce(type: :command_queued, **command.event_fields, **marks)
|
|
1146
1143
|
end
|
|
1147
1144
|
|
|
1148
1145
|
# A client asks the worker to exit now (`/exit` in the attached TUI), or
|
|
@@ -1183,7 +1180,7 @@ module Samagotchi
|
|
|
1183
1180
|
return BAD_DEADLINE unless deadline_valid?(deadline)
|
|
1184
1181
|
|
|
1185
1182
|
stopped = nil
|
|
1186
|
-
hold = @
|
|
1183
|
+
hold = @event_log.synchronize do
|
|
1187
1184
|
raise IntakeClosed if stopped?
|
|
1188
1185
|
next :expired if expired?("exit_expired", deadline, sid: session_id, client_id: client_id)
|
|
1189
1186
|
|
|
@@ -1352,6 +1349,7 @@ module Samagotchi
|
|
|
1352
1349
|
return [{}, 403, { error: CommandIntake::REFUSED_ERROR, detail: CommandIntake::REFUSED }]
|
|
1353
1350
|
end
|
|
1354
1351
|
end
|
|
1352
|
+
return CommandIntake::PRIVATE_REFUSED if command_registry.private_args?(prompt.to_s) # never a message
|
|
1355
1353
|
|
|
1356
1354
|
enqueued_id = fetched(parsed, "enqueued_id")
|
|
1357
1355
|
enqueued_id = SessionInbox.new_enqueued_id unless SessionInbox.valid_enqueued_id?(enqueued_id)
|
|
@@ -1364,7 +1362,7 @@ module Samagotchi
|
|
|
1364
1362
|
pending_limit:, enqueued_id:)
|
|
1365
1363
|
# Before it is queued: a drain after this may have taken it (#cut_for_steer).
|
|
1366
1364
|
@post_turn_mutex.synchronize do
|
|
1367
|
-
epoch = @
|
|
1365
|
+
epoch = @steer_cut.input_epoch
|
|
1368
1366
|
# Whether a queue waits for a running turn: read with the write under
|
|
1369
1367
|
# the event log, so no :turn_started or :turn_completed falls between.
|
|
1370
1368
|
# To an idle worker the queued file simply runs as the next turn.
|
|
@@ -1372,17 +1370,17 @@ module Samagotchi
|
|
|
1372
1370
|
# Write and announce with the event log held: the worker can't emit
|
|
1373
1371
|
# this turn's :turn_started (or merge it mid-turn) before
|
|
1374
1372
|
# :turn_enqueued, and a failed write announces nothing.
|
|
1375
|
-
enqueued = @
|
|
1373
|
+
enqueued = @event_log.synchronize do
|
|
1376
1374
|
raise IntakeClosed if stopped?
|
|
1377
1375
|
|
|
1378
1376
|
existing = @enqueued_requests[enqueued_id]
|
|
1379
1377
|
if existing
|
|
1380
1378
|
next(existing[:prompt] == prompt.to_s ? :duplicate : :conflict)
|
|
1381
1379
|
end
|
|
1382
|
-
next :duplicate if @
|
|
1380
|
+
next :duplicate if @turn_state.enqueued_id?(enqueued_id)
|
|
1383
1381
|
next :expired if expired?("turn_expired", deadline, sid: sid, client_id: client_id)
|
|
1384
1382
|
next :too_many if pending_limit && pending_turns(author) >= pending_limit
|
|
1385
|
-
next :duplicate unless @
|
|
1383
|
+
next :duplicate unless @turn_state.reserve_enqueued_id(enqueued_id)
|
|
1386
1384
|
|
|
1387
1385
|
# Input goes into a running turn only of its own author: the
|
|
1388
1386
|
# owner's never into a participant's turn (a steer, a cut), a
|
|
@@ -1392,7 +1390,7 @@ module Samagotchi
|
|
|
1392
1390
|
queued_behind_turn = Delivery.queue?(delivery) && !@accumulator.current_turn.nil?
|
|
1393
1391
|
unless enqueue_turn(prompt: prompt, client_id: client_id, enqueued_id: enqueued_id,
|
|
1394
1392
|
no_interrupt: no_interrupt, images: images, delivery: delivery, author: author, anchors: anchors)
|
|
1395
|
-
@
|
|
1393
|
+
@turn_state.forget_enqueued_id(enqueued_id)
|
|
1396
1394
|
next false
|
|
1397
1395
|
end
|
|
1398
1396
|
|
|
@@ -1400,7 +1398,7 @@ module Samagotchi
|
|
|
1400
1398
|
enqueued_event[:images] = images unless images.empty?
|
|
1401
1399
|
enqueued_event[:delivery] = Delivery::QUEUE if queued_behind_turn
|
|
1402
1400
|
enqueued_event[:author] = author if author
|
|
1403
|
-
@
|
|
1401
|
+
@event_log.announce(enqueued_event)
|
|
1404
1402
|
@on_input&.call
|
|
1405
1403
|
true
|
|
1406
1404
|
end
|
|
@@ -1424,7 +1422,7 @@ module Samagotchi
|
|
|
1424
1422
|
# that says nothing (an older client, the web composer) goes in at
|
|
1425
1423
|
# the running turn's next step.
|
|
1426
1424
|
cut = nil
|
|
1427
|
-
cut = @
|
|
1425
|
+
cut = @steer_cut.cut_for_steer(Steer.source_for_client(client_id), epoch: epoch) if images.empty? && Delivery.cut?(delivery)
|
|
1428
1426
|
# A queue to an idle worker is a turn now: the ack says next_step.
|
|
1429
1427
|
ack_delivery = Delivery.queue?(delivery) && !queued_behind_turn ? Delivery::NEXT_STEP : delivery
|
|
1430
1428
|
ack = { status: "accepted", enqueued_id: enqueued_id, session_id: @session_id, delivery: ack_delivery }
|
|
@@ -1465,14 +1463,14 @@ module Samagotchi
|
|
|
1465
1463
|
return BAD_DEADLINE unless deadline_valid?(deadline)
|
|
1466
1464
|
|
|
1467
1465
|
client_id = fetched(parsed, "client_id")
|
|
1468
|
-
taken, input = @
|
|
1466
|
+
taken, input = @event_log.synchronize do
|
|
1469
1467
|
raise IntakeClosed if stopped?
|
|
1470
1468
|
next [:expired, nil] if expired?("dequeue_expired", deadline, sid: session_id, enqueued_id: id)
|
|
1471
1469
|
|
|
1472
1470
|
SessionInbox.take_queued_input(session_dir, id).tap do |status, _|
|
|
1473
1471
|
if status == :removed
|
|
1474
|
-
@
|
|
1475
|
-
@
|
|
1472
|
+
@turn_state.drop_enqueued_id(id)
|
|
1473
|
+
@event_log.announce(type: :turn_dequeued, enqueued_id: id, client_id: client_id)
|
|
1476
1474
|
end
|
|
1477
1475
|
end
|
|
1478
1476
|
end
|
|
@@ -1525,7 +1523,7 @@ module Samagotchi
|
|
|
1525
1523
|
# from here. Read under the event lock, as snapshot_frame and tail_frame:
|
|
1526
1524
|
# the state and its event_seq/event_id are one step of the event log.
|
|
1527
1525
|
def handle_state(_session_id, _body = nil)
|
|
1528
|
-
state = @
|
|
1526
|
+
state = @event_log.synchronize do
|
|
1529
1527
|
snap = @engine.session_state_snapshot
|
|
1530
1528
|
snap.merge(event_id: event_id(snap[:event_seq]))
|
|
1531
1529
|
end
|
|
@@ -1794,10 +1792,10 @@ module Samagotchi
|
|
|
1794
1792
|
return [{}, 422, { error: "relay_unverified", detail: "this session has no parent" }] unless parent_id
|
|
1795
1793
|
|
|
1796
1794
|
marker = { parent_id: parent_id, parent_short: parent_id[0, 8], relay_id: relay_id }
|
|
1797
|
-
return [{}, 409, { error: "question_not_pending", detail: "no pending question #{qid}" }] unless @
|
|
1795
|
+
return [{}, 409, { error: "question_not_pending", detail: "no pending question #{qid}" }] unless @question_desk.annotate(qid, relayed_to: marker)
|
|
1798
1796
|
|
|
1799
1797
|
Log.info(:bridge, "relay_opened", sid: @session_id, id: qid, parent: parent_id[0, 8])
|
|
1800
|
-
watch = RelayWatcher.start(
|
|
1798
|
+
watch = RelayWatcher.start(question_desk: @question_desk, question_id: qid, relay_id: relay_id,
|
|
1801
1799
|
parent_dir: Session.session_dir(parent_id, state_dir: @state_dir))
|
|
1802
1800
|
@mutex.synchronize { @relay_watchers = (@relay_watchers || []).select(&:alive?) << watch }
|
|
1803
1801
|
[{}, 200, { status: "opened", question_id: qid }]
|
|
@@ -1806,12 +1804,12 @@ module Samagotchi
|
|
|
1806
1804
|
# The parent's relay card closed unanswered: the mark goes, if it is
|
|
1807
1805
|
# still that relay's.
|
|
1808
1806
|
def relay_closed(qid, relay_id, reason)
|
|
1809
|
-
pending = @
|
|
1807
|
+
pending = @question_desk.pending
|
|
1810
1808
|
relay = pending&.relayed_to
|
|
1811
1809
|
unless pending && pending.id.to_s == qid && relay && (relay[:relay_id] || relay["relay_id"]).to_s == relay_id
|
|
1812
1810
|
return [{}, 409, { error: "question_not_pending", detail: "no question #{qid} relayed by #{relay_id}" }]
|
|
1813
1811
|
end
|
|
1814
|
-
return [{}, 409, { error: "question_not_pending", detail: "no pending question #{qid}" }] unless @
|
|
1812
|
+
return [{}, 409, { error: "question_not_pending", detail: "no pending question #{qid}" }] unless @question_desk.annotate(qid, relayed_to: nil, reason: reason)
|
|
1815
1813
|
|
|
1816
1814
|
Log.info(:bridge, "relay_closed", sid: @session_id, id: qid, reason: reason)
|
|
1817
1815
|
[{}, 200, { status: "closed", question_id: qid }]
|
|
@@ -1853,6 +1851,8 @@ module Samagotchi
|
|
|
1853
1851
|
403 => "Forbidden",
|
|
1854
1852
|
404 => "Not Found",
|
|
1855
1853
|
409 => "Conflict",
|
|
1854
|
+
413 => "Content Too Large",
|
|
1855
|
+
429 => "Too Many Requests",
|
|
1856
1856
|
500 => "Internal Server Error",
|
|
1857
1857
|
501 => "Not Implemented"
|
|
1858
1858
|
}.freeze
|
|
@@ -172,6 +172,15 @@ module Samagotchi
|
|
|
172
172
|
post("guardrails/wrong", { id: judgment_id }, read_body: true)
|
|
173
173
|
end
|
|
174
174
|
|
|
175
|
+
# POST /session/:id/blocks/fix: ask again, once, for a block the page
|
|
176
|
+
# couldn't draw in +turn_id+'s answer (Bridge::BlockFixRoute). 200 asked
|
|
177
|
+
# before, 202 asking, 400/409/413/429 refused; 404: a worker older than
|
|
178
|
+
# the route.
|
|
179
|
+
# @return [Response]
|
|
180
|
+
def fix_block(turn_id:, lang:, source:, reason:)
|
|
181
|
+
post("blocks/fix", { turn_id: turn_id, lang: lang, source: source, reason: reason }, read_body: true)
|
|
182
|
+
end
|
|
183
|
+
|
|
175
184
|
# POST /session/:id/turn/dequeue: take back a queued message (one that
|
|
176
185
|
# waits for the running turn's end) before it runs. 200 {status:
|
|
177
186
|
# "removed", enqueued_id, prompt}; 409 already_started (a turn took
|
|
@@ -26,7 +26,7 @@ module Samagotchi
|
|
|
26
26
|
"Plugin::Api" => { ctx: "Plugin::Context" },
|
|
27
27
|
"Plugin::Context" => { sessions: "Plugin::Sessions", context: "Plugin::AttachedContext", http: "Plugin::Http",
|
|
28
28
|
judgments: "Plugin::Judgments", cost: "SessionMetrics::Cost", usage: "SessionMetrics::Usage",
|
|
29
|
-
log: "Plugin::Context::Logger" }
|
|
29
|
+
log: "Plugin::Context::Logger", secrets: "Plugin::Secrets" }
|
|
30
30
|
}.freeze
|
|
31
31
|
ROOTS = { ctx: "Plugin::Context", chi: "Plugin::Api" }.freeze
|
|
32
32
|
|