samagotchi 0.170.0 → 0.172.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +61 -1
- data/bin/chi +6 -4
- data/docs/architecture.md +26 -9
- data/docs/bundles.md +4 -21
- data/docs/cli.md +81 -97
- data/docs/configuration.md +12 -14
- data/docs/context.md +5 -6
- data/docs/guardrails.md +57 -96
- data/docs/hooks.md +3 -4
- data/docs/internals/attached-context.md +6 -7
- data/docs/internals/background-tasks.md +4 -3
- data/docs/internals/block-renderers.md +2 -2
- data/docs/internals/bridge.md +34 -7
- data/docs/internals/context-telemetry.md +13 -1
- data/docs/internals/engine.md +25 -13
- data/docs/internals/inline-widgets.md +1 -1
- data/docs/internals/objectives.md +4 -6
- data/docs/internals/participants.md +340 -249
- data/docs/internals/prompt-caching.md +4 -5
- data/docs/internals/web-frontend.md +185 -35
- data/docs/internals/working-set.md +71 -5
- data/docs/memory.md +1 -1
- data/docs/plugins.md +2 -16
- data/docs/releasing.md +2 -1
- data/docs/scheduling.md +1 -1
- data/docs/sessions.md +17 -48
- data/docs/sub-agent.md +2 -2
- data/docs/system-one.md +2 -2
- data/docs/testing.md +56 -7
- data/lib/samagotchi/bridge/card_store.rb +1 -0
- data/lib/samagotchi/bridge/sse_writer.rb +1 -0
- data/lib/samagotchi/bridge/turn_accumulator.rb +8 -5
- data/lib/samagotchi/bridge.rb +64 -20
- data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
- data/lib/samagotchi/bundle_command/test_run.rb +1 -0
- data/lib/samagotchi/bundle_command/try/run.rb +1 -0
- data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
- data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
- data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
- data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
- data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
- data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
- data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
- data/lib/samagotchi/bundles/system/manifest.yml +1 -1
- data/lib/samagotchi/client_id.rb +126 -67
- data/lib/samagotchi/command_intake.rb +15 -3
- data/lib/samagotchi/compat.rb +6 -1
- data/lib/samagotchi/context_candidate_choice.rb +1 -3
- data/lib/samagotchi/context_candidates.rb +3 -4
- data/lib/samagotchi/continue_offer.rb +9 -2
- data/lib/samagotchi/delivery.rb +36 -12
- data/lib/samagotchi/empty_answer_error.rb +5 -0
- data/lib/samagotchi/engine/cards.rb +3 -0
- data/lib/samagotchi/engine/context_judge.rb +5 -3
- data/lib/samagotchi/engine/events.rb +4 -0
- data/lib/samagotchi/engine/hooks.rb +3 -0
- data/lib/samagotchi/engine/messages.rb +5 -0
- data/lib/samagotchi/engine/model_settings.rb +2 -0
- data/lib/samagotchi/engine/models.rb +10 -0
- data/lib/samagotchi/engine/objectives.rb +6 -0
- data/lib/samagotchi/engine/participants.rb +18 -14
- data/lib/samagotchi/engine/plugins.rb +8 -0
- data/lib/samagotchi/engine/questions.rb +3 -0
- data/lib/samagotchi/engine/recap.rb +1 -0
- data/lib/samagotchi/engine/session.rb +4 -0
- data/lib/samagotchi/engine/side_requests.rb +9 -0
- data/lib/samagotchi/engine/steering.rb +2 -0
- data/lib/samagotchi/engine/toolkit.rb +5 -0
- data/lib/samagotchi/engine/turn_end.rb +7 -0
- data/lib/samagotchi/engine/turn_helpers.rb +8 -0
- data/lib/samagotchi/engine/verify.rb +2 -0
- data/lib/samagotchi/engine.rb +8 -3
- data/lib/samagotchi/event_fanout.rb +22 -1
- data/lib/samagotchi/grants.rb +33 -22
- data/lib/samagotchi/guardrail_wiring.rb +23 -18
- data/lib/samagotchi/guardrails/approval_use.rb +59 -0
- data/lib/samagotchi/guardrails/approvals.rb +9 -11
- data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
- data/lib/samagotchi/guardrails/gate.rb +10 -5
- data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
- data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
- data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
- data/lib/samagotchi/guardrails/review_paths.rb +2 -3
- data/lib/samagotchi/guardrails.rb +1 -1
- data/lib/samagotchi/image_store.rb +51 -12
- data/lib/samagotchi/kernel_loop.rb +7 -1
- data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
- data/lib/samagotchi/llm_context_stale.rb +4 -1
- data/lib/samagotchi/llm_context_strategy.rb +2 -0
- data/lib/samagotchi/objectives.rb +1 -4
- data/lib/samagotchi/objectives_audit.rb +1 -1
- data/lib/samagotchi/objectives_track.rb +1 -1
- data/lib/samagotchi/owner_prompt.rb +3 -6
- data/lib/samagotchi/parent_report.rb +1 -0
- data/lib/samagotchi/participant_messages.rb +48 -4
- data/lib/samagotchi/participant_revise.rb +61 -0
- data/lib/samagotchi/participant_turn_mode.rb +50 -0
- data/lib/samagotchi/participant_uploads.rb +98 -0
- data/lib/samagotchi/participants.rb +21 -12
- data/lib/samagotchi/participants_watch.rb +2 -2
- data/lib/samagotchi/plugin/api.rb +2 -2
- data/lib/samagotchi/plugin/attached_context.rb +1 -1
- data/lib/samagotchi/plugin/context.rb +0 -4
- data/lib/samagotchi/plugin/sessions.rb +1 -25
- data/lib/samagotchi/reply_wait/poll.rb +5 -1
- data/lib/samagotchi/revise_anchor.rb +9 -0
- data/lib/samagotchi/run_options.rb +2 -3
- data/lib/samagotchi/send_command.rb +15 -10
- data/lib/samagotchi/session.rb +0 -3
- data/lib/samagotchi/session_access/contract.rb +1 -10
- data/lib/samagotchi/session_access/local.rb +10 -23
- data/lib/samagotchi/session_access/remote.rb +9 -20
- data/lib/samagotchi/session_access.rb +12 -16
- data/lib/samagotchi/session_chain.rb +4 -6
- data/lib/samagotchi/session_commands.rb +6 -3
- data/lib/samagotchi/session_continue.rb +1 -2
- data/lib/samagotchi/session_inbox.rb +20 -12
- data/lib/samagotchi/session_manager/delivery.rb +32 -5
- data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
- data/lib/samagotchi/session_manager/listing.rb +5 -0
- data/lib/samagotchi/session_manager/spawn.rb +2 -2
- data/lib/samagotchi/session_manager/worker_command.rb +9 -0
- data/lib/samagotchi/session_participants.rb +12 -8
- data/lib/samagotchi/session_review.rb +5 -7
- data/lib/samagotchi/session_stores.rb +3 -1
- data/lib/samagotchi/share_command.rb +10 -12
- data/lib/samagotchi/share_terms.rb +41 -0
- data/lib/samagotchi/steer.rb +8 -14
- data/lib/samagotchi/system_prompt.rb +16 -25
- data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
- data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
- data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
- data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
- data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
- data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
- data/lib/samagotchi/terminal_ui.rb +1 -2
- data/lib/samagotchi/testing/sessions.rb +2 -10
- data/lib/samagotchi/tool_runner.rb +42 -9
- data/lib/samagotchi/toolkit.rb +4 -22
- data/lib/samagotchi/toolkit_suggest.rb +3 -3
- data/lib/samagotchi/tools/deferred_tools.rb +1 -0
- data/lib/samagotchi/tools/execute.rb +11 -45
- data/lib/samagotchi/tools/leftovers.rb +122 -0
- data/lib/samagotchi/tools/memory.rb +88 -25
- data/lib/samagotchi/tools/task_get.rb +1 -0
- data/lib/samagotchi/tools/task_list.rb +3 -2
- data/lib/samagotchi/tools/task_record.rb +6 -4
- data/lib/samagotchi/tools/task_runtime.rb +199 -20
- data/lib/samagotchi/tools/task_wait.rb +4 -3
- data/lib/samagotchi/turn_anchors.rb +0 -3
- data/lib/samagotchi/turn_context.rb +1 -1
- data/lib/samagotchi/turn_origin.rb +1 -1
- data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
- data/lib/samagotchi/turn_stages/objectives.rb +2 -2
- data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
- data/lib/samagotchi/turn_stages/verify_anchors.rb +13 -1
- data/lib/samagotchi/update_hint.rb +3 -1
- data/lib/samagotchi/verify_anchor.rb +6 -0
- data/lib/samagotchi/verify_fresh.rb +1 -1
- data/lib/samagotchi/verify_fresh_origin.rb +13 -11
- data/lib/samagotchi/version.rb +1 -1
- data/lib/samagotchi/web/app.rb +45 -82
- data/lib/samagotchi/web/participant_access.rb +139 -40
- data/lib/samagotchi/web/participant_images.rb +185 -0
- data/lib/samagotchi/web/participant_review.rb +22 -142
- data/lib/samagotchi/web/participant_show.rb +241 -0
- data/lib/samagotchi/web/participant_stream_filter.rb +25 -2
- data/lib/samagotchi/web/participant_view.rb +334 -27
- data/lib/samagotchi/web/participants_admin.rb +21 -40
- data/lib/samagotchi/web/public/annotate_bar.css +1 -2
- data/lib/samagotchi/web/public/annotate_bar.js +1 -2
- data/lib/samagotchi/web/public/app.js +126 -132
- data/lib/samagotchi/web/public/block_host.js +7 -4
- data/lib/samagotchi/web/public/bubbles.js +9 -1
- data/lib/samagotchi/web/public/cards_offscreen.js +69 -0
- data/lib/samagotchi/web/public/chip_popover.js +5 -4
- data/lib/samagotchi/web/public/claim_link.js +4 -4
- data/lib/samagotchi/web/public/composer.css +4 -6
- data/lib/samagotchi/web/public/composer_mode.js +11 -6
- data/lib/samagotchi/web/public/composer_mount.js +4 -4
- data/lib/samagotchi/web/public/ctx.js +12 -3
- data/lib/samagotchi/web/public/data.js +2 -10
- data/lib/samagotchi/web/public/delivery_line.js +49 -63
- data/lib/samagotchi/web/public/history_shape.js +5 -1
- data/lib/samagotchi/web/public/images.js +7 -1
- data/lib/samagotchi/web/public/index.html +50 -34
- data/lib/samagotchi/web/public/info_bar_model.js +2 -1
- data/lib/samagotchi/web/public/live_turn.js +1 -1
- data/lib/samagotchi/web/public/markdown_place.js +5 -4
- data/lib/samagotchi/web/public/owner_claim.js +5 -5
- data/lib/samagotchi/web/public/owner_verify.js +0 -2
- data/lib/samagotchi/web/public/participant_bar.js +107 -0
- data/lib/samagotchi/web/public/participant_main.js +289 -0
- data/lib/samagotchi/web/public/participant_model.js +5 -292
- data/lib/samagotchi/web/public/participant_names.js +17 -8
- data/lib/samagotchi/web/public/participant_revise.js +66 -0
- data/lib/samagotchi/web/public/participants_panel.js +32 -74
- data/lib/samagotchi/web/public/question_cards.js +14 -3
- data/lib/samagotchi/web/public/refs.js +53 -36
- data/lib/samagotchi/web/public/reload_check.js +70 -14
- data/lib/samagotchi/web/public/revise_stack.js +21 -10
- data/lib/samagotchi/web/public/send_turn.js +28 -29
- data/lib/samagotchi/web/public/session_actions.js +4 -45
- data/lib/samagotchi/web/public/session_fields.js +1 -2
- data/lib/samagotchi/web/public/session_state.js +12 -7
- data/lib/samagotchi/web/public/session_sync.js +9 -2
- data/lib/samagotchi/web/public/session_view.js +14 -5
- data/lib/samagotchi/web/public/settings_panel.js +4 -18
- data/lib/samagotchi/web/public/stage_answer.js +56 -0
- data/lib/samagotchi/web/public/stage_asking.js +7 -5
- data/lib/samagotchi/web/public/stage_cloud.js +93 -0
- data/lib/samagotchi/web/public/stage_dom.js +100 -0
- data/lib/samagotchi/web/public/stage_handoff.js +154 -0
- data/lib/samagotchi/web/public/stage_owner_wait.js +35 -0
- data/lib/samagotchi/web/public/stage_pager.js +59 -0
- data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
- data/lib/samagotchi/web/public/stage_view.js +103 -410
- data/lib/samagotchi/web/public/stage_waiting.js +74 -6
- data/lib/samagotchi/web/public/start_page.js +12 -33
- data/lib/samagotchi/web/public/start_settings.js +24 -78
- data/lib/samagotchi/web/public/start_toolkit.js +6 -10
- data/lib/samagotchi/web/public/storage.js +22 -0
- data/lib/samagotchi/web/public/stream_handlers.js +3 -10
- data/lib/samagotchi/web/public/tokens.css +1 -1
- data/lib/samagotchi/web/public/turn_block.js +2 -5
- data/lib/samagotchi/web/public/turn_events.js +30 -12
- data/lib/samagotchi/web/public/turn_html.js +9 -2
- data/lib/samagotchi/web/public/turn_pieces.js +3 -4
- data/lib/samagotchi/web/public/turn_range.js +1 -2
- data/lib/samagotchi/web/public/verify_target.js +2 -4
- data/lib/samagotchi/web/public/viewer.js +59 -0
- data/lib/samagotchi/web/public/viewer_self.js +59 -0
- data/lib/samagotchi/web/public/working_set.js +11 -5
- data/lib/samagotchi/web/public/working_set_view.js +53 -8
- data/lib/samagotchi/web/public/ws_cards.js +89 -0
- data/lib/samagotchi/web/public/ws_peek.js +106 -0
- data/lib/samagotchi/web/public/ws_preview.css +126 -0
- data/lib/samagotchi/web/public/ws_reader.js +319 -0
- data/lib/samagotchi/web/public/ws_timeline.js +200 -0
- data/lib/samagotchi/web/public/ws_way_in.js +25 -0
- data/lib/samagotchi/web/qr.rb +1 -1
- data/lib/samagotchi/web/session_control.rb +42 -15
- data/lib/samagotchi/web/session_create.rb +6 -72
- data/lib/samagotchi/web/session_show.rb +75 -28
- data/lib/samagotchi/web/session_summary.rb +1 -4
- data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
- data/lib/samagotchi/web/viewer.rb +70 -0
- data/lib/samagotchi/web/working_set_routes.rb +132 -0
- data/lib/samagotchi/worker.rb +35 -28
- data/lib/samagotchi/worker_inbound.rb +4 -3
- data/lib/samagotchi/worker_link.rb +17 -0
- data/lib/samagotchi/working_set.rb +322 -11
- metadata +32 -13
- data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
- data/lib/samagotchi/participant_mode.rb +0 -86
- data/lib/samagotchi/review_apply.rb +0 -131
- data/lib/samagotchi/review_command.rb +0 -123
- data/lib/samagotchi/review_start.rb +0 -196
- data/lib/samagotchi/web/public/participant.html +0 -218
- data/lib/samagotchi/web/public/participant.js +0 -719
- data/lib/samagotchi/web/public/participant_annotate.js +0 -76
- data/lib/samagotchi/web/public/review_apply.js +0 -34
- data/lib/samagotchi/web/public/review_history.js +0 -72
- data/lib/samagotchi/web/public/review_start.js +0 -55
- data/lib/samagotchi/web/public/tools_overview.js +0 -42
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../grants"
|
|
4
|
+
require_relative "../image_store"
|
|
5
|
+
require_relative "../participant_messages"
|
|
6
|
+
require_relative "../participant_uploads"
|
|
7
|
+
require_relative "../participants"
|
|
8
|
+
require_relative "../send_command"
|
|
9
|
+
require_relative "../session"
|
|
10
|
+
|
|
11
|
+
module Samagotchi
|
|
12
|
+
module Web
|
|
13
|
+
# A participant's images, mixed into App: their uploads (POST
|
|
14
|
+
# /api/sessions/:id/images) and reading their own back (GET
|
|
15
|
+
# …/images/<hash>.<ext>). Ownership is ParticipantUploads' record alone,
|
|
16
|
+
# written here, under @participant_images_mutex. Their turn's refs are
|
|
17
|
+
# checked by ParticipantAccess#participant_images_refusal. See
|
|
18
|
+
# docs/internals/participants.md (Participant images).
|
|
19
|
+
module ParticipantImages
|
|
20
|
+
# One upload's answer: +ref+ when accepted, else +status+, +code+ and
|
|
21
|
+
# +detail+; +bytes+ the stored size (for the audit line).
|
|
22
|
+
Upload = Data.define(:status, :code, :detail, :ref, :bytes) do
|
|
23
|
+
def self.refused(status, code, detail) = new(status: status, code: code, detail: detail, ref: nil, bytes: nil)
|
|
24
|
+
def self.accepted(ref) = new(status: 201, code: nil, detail: nil, ref: ref, bytes: ref[:bytes])
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
# The largest image a participant may upload, by its header: pixels
|
|
30
|
+
# and either side (a phone's 4032×3024 passes). Checked before any
|
|
31
|
+
# tool reads their bytes.
|
|
32
|
+
MAX_PARTICIPANT_PIXELS = 40_000_000
|
|
33
|
+
MAX_PARTICIPANT_SIDE = 12_000
|
|
34
|
+
|
|
35
|
+
def init_participant_images
|
|
36
|
+
@participant_images_mutex = Mutex.new
|
|
37
|
+
# One participant conversion (ImageStore.ingest, which may run sips
|
|
38
|
+
# or magick on their bytes) at a time, whoever's.
|
|
39
|
+
@participant_conversion_mutex = Mutex.new
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# POST /api/sessions/:id/images from a participant. Every upload
|
|
43
|
+
# counts as an attempt first (before the cap check). Refused, in
|
|
44
|
+
# order: their cap (429 cap_reached), their attempts (429
|
|
45
|
+
# too_many_image_attempts, refused ones counted), the size (413, as
|
|
46
|
+
# the owner's), not an image (422 bad_image), a bmp/tiff/heic (422
|
|
47
|
+
# unsupported_image: chi converts those with an external tool), no
|
|
48
|
+
# size in its header or over MAX_PARTICIPANT_PIXELS / _SIDE (422
|
|
49
|
+
# image_too_big), their quota (429 too_many_images; a file they own
|
|
50
|
+
# already is accepted again, no new entry). Every answer is audited,
|
|
51
|
+
# an exception too (internal_error, then raised). The reply is the ref
|
|
52
|
+
# without source, mime or bytes.
|
|
53
|
+
def participant_image_upload(req, id, session_dir)
|
|
54
|
+
grant = principal(req).grant
|
|
55
|
+
upload = begin
|
|
56
|
+
participant_upload(req, session_dir, grant)
|
|
57
|
+
rescue StandardError
|
|
58
|
+
audit_image(id, grant, Upload.refused(500, "internal_error", nil))
|
|
59
|
+
raise
|
|
60
|
+
end
|
|
61
|
+
audit_image(id, grant, upload)
|
|
62
|
+
return error_response(upload.status, upload.code, upload.detail) unless upload.ref
|
|
63
|
+
|
|
64
|
+
json_response(201, upload.ref.slice(:file, :name, :width, :height))
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def audit_image(id, grant, upload)
|
|
68
|
+
Participants.log(id, :image, state_dir: default_state_dir, participant: grant.id,
|
|
69
|
+
status: upload.ref ? "accepted" : "refused",
|
|
70
|
+
**{ reason: upload.code, file: upload.ref&.dig(:file), bytes: upload.bytes }.compact)
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
def participant_upload(req, dir, grant)
|
|
74
|
+
over = @participant_images_mutex.synchronize do
|
|
75
|
+
record = ParticipantUploads.read(dir)
|
|
76
|
+
tried = record.attempts_of(grant.id)
|
|
77
|
+
ParticipantUploads.attempt!(dir, grant.id, state: record) if tried < Grants::MAX_IMAGE_ATTEMPTS
|
|
78
|
+
tried >= Grants::MAX_IMAGE_ATTEMPTS
|
|
79
|
+
end
|
|
80
|
+
if (cap = participant_cap_refusal(grant))
|
|
81
|
+
return Upload.refused(*cap)
|
|
82
|
+
end
|
|
83
|
+
if over
|
|
84
|
+
return Upload.refused(429, "too_many_image_attempts", ParticipantMessages::TOO_MANY_IMAGE_ATTEMPTS)
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
bytes = image_upload_bytes(req)
|
|
88
|
+
return Upload.refused(413, "too_large", image_too_large_detail) unless bytes
|
|
89
|
+
|
|
90
|
+
participant_store(dir, grant, bytes, upload_name(req))
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def participant_store(dir, grant, bytes, name)
|
|
94
|
+
info = ImageHeader.read(bytes)
|
|
95
|
+
return Upload.refused(422, "bad_image", ParticipantMessages::BAD_IMAGE) unless info
|
|
96
|
+
if ImageHeader::CONVERTIBLE.include?(info.format)
|
|
97
|
+
return Upload.refused(422, "unsupported_image", ParticipantMessages::IMAGE_TYPE)
|
|
98
|
+
end
|
|
99
|
+
return Upload.refused(422, "image_too_big", ParticipantMessages::IMAGE_TOO_BIG) unless participant_size_ok?(info)
|
|
100
|
+
|
|
101
|
+
# At quota: only a file they own already, named as its bytes would
|
|
102
|
+
# be stored unchanged (a downscaled or re-encoded one is refused).
|
|
103
|
+
raw = ImageStore.file_name_for(bytes, info.format)
|
|
104
|
+
again = @participant_images_mutex.synchronize do
|
|
105
|
+
record = ParticipantUploads.read(dir)
|
|
106
|
+
next nil if record.count(grant.id) < Grants::MAX_IMAGES
|
|
107
|
+
|
|
108
|
+
record.owned?(raw, grant.id)
|
|
109
|
+
end
|
|
110
|
+
return Upload.accepted(ImageStore.ref_for(dir, file: raw, name: name)) if again
|
|
111
|
+
return too_many_images if again == false
|
|
112
|
+
|
|
113
|
+
participant_ingest(dir, grant, bytes, name)
|
|
114
|
+
rescue ImageStore::Error
|
|
115
|
+
Upload.refused(422, "bad_image", ParticipantMessages::BAD_IMAGE)
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
# Whether +info+'s header gives a size within the participant limits.
|
|
119
|
+
def participant_size_ok?(info)
|
|
120
|
+
return false unless info.width.is_a?(Integer) && info.height.is_a?(Integer) && info.width.positive? && info.height.positive?
|
|
121
|
+
|
|
122
|
+
[info.width, info.height].max <= MAX_PARTICIPANT_SIDE && info.width * info.height <= MAX_PARTICIPANT_PIXELS
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
# Stores their image, then records it: the quota checked again under
|
|
126
|
+
# the mutex (a race past it is refused here; the stored file may stay
|
|
127
|
+
# in images/, unreachable to them without a record).
|
|
128
|
+
def participant_ingest(dir, grant, bytes, name)
|
|
129
|
+
ref = @participant_conversion_mutex.synchronize { ImageStore.ingest(dir, bytes: bytes, name: name, source: "user") }
|
|
130
|
+
added = @participant_images_mutex.synchronize do
|
|
131
|
+
record = ParticipantUploads.read(dir)
|
|
132
|
+
next true if record.owned?(ref[:file], grant.id)
|
|
133
|
+
next false if record.count(grant.id) >= Grants::MAX_IMAGES
|
|
134
|
+
|
|
135
|
+
ParticipantUploads.add(dir, file: ref[:file], grant_id: grant.id, state: record)
|
|
136
|
+
end
|
|
137
|
+
added ? Upload.accepted(ref) : too_many_images
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
# A participant's turn's images: [nil, refs] to deliver (refs [] for
|
|
141
|
+
# none), or [[status, code, detail(, audit detail)], nil]. In this
|
|
142
|
+
# order, so nothing tells them which files exist: the shape (400
|
|
143
|
+
# bad_images), then ownership (403 not_permitted for any ref they
|
|
144
|
+
# didn't upload, there or not), only then the files (ImageStore.
|
|
145
|
+
# check_refs), then the model (422 no_vision in their words; the
|
|
146
|
+
# model's name and the /model hint only in the audit line).
|
|
147
|
+
def participant_images_refusal(grant, id, raw)
|
|
148
|
+
return [nil, []] if raw.nil? || raw == []
|
|
149
|
+
return [[400, "bad_images", ParticipantMessages::IMAGES_SHAPE], nil] unless participant_refs_shape?(raw)
|
|
150
|
+
|
|
151
|
+
dir = Session.session_dir(id, state_dir: default_state_dir)
|
|
152
|
+
record = @participant_images_mutex.synchronize { ParticipantUploads.read(dir) }
|
|
153
|
+
owned = raw.all? { |ref| record.owned?(ref["file"].to_s, grant.id) }
|
|
154
|
+
return [[403, "not_permitted", ParticipantMessages::NOT_PERMITTED], nil] unless owned
|
|
155
|
+
|
|
156
|
+
images = ImageStore.check_refs(dir, raw)
|
|
157
|
+
return [[400, "bad_images", ParticipantMessages::IMAGE_GONE, images], nil] if images.is_a?(String)
|
|
158
|
+
|
|
159
|
+
refusal = participant_vision_refusal(id)
|
|
160
|
+
refusal ? [[422, "no_vision", ParticipantMessages::NO_VISION, refusal], nil] : [nil, images]
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def participant_refs_shape?(raw)
|
|
164
|
+
raw.is_a?(Array) && raw.size <= ImageStore::MAX_TURN_REFS &&
|
|
165
|
+
raw.all? { |ref| ref.is_a?(Hash) && ref["file"].is_a?(String) && !ref.key?("path") }
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# chi send's refusal for this session's model, nil when it takes
|
|
169
|
+
# images or nobody knows (the Engine stays the backstop). The model's
|
|
170
|
+
# name can lag a live /model.
|
|
171
|
+
def participant_vision_refusal(id)
|
|
172
|
+
session = @session_class.load(id, state_dir: default_state_dir)
|
|
173
|
+
SendCommand.vision_refusal_for(session.model_name, typed: session.model_typed)
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
# Whether the request's participant uploaded <session_dir>/images/+name+
|
|
177
|
+
# (a bare "<hash>.<ext>"): the record alone (they had the bytes).
|
|
178
|
+
def participant_owns_image?(req, session_dir, name)
|
|
179
|
+
ParticipantUploads.owned?(session_dir, "#{ImageStore::DIR}/#{name}", principal(req).grant.id)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def too_many_images = Upload.refused(429, "too_many_images", ParticipantMessages::TOO_MANY_IMAGES)
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
|
@@ -5,46 +5,34 @@ require_relative "token"
|
|
|
5
5
|
require_relative "participant_stream_filter"
|
|
6
6
|
require_relative "../participant_exposure"
|
|
7
7
|
require_relative "../session_metrics"
|
|
8
|
-
require_relative "../review_start"
|
|
9
|
-
require_relative "../guardrails/review_paths"
|
|
10
|
-
require_relative "../participant_mode"
|
|
11
8
|
require_relative "../verify_fresh"
|
|
12
9
|
|
|
13
10
|
module Samagotchi
|
|
14
11
|
module Web
|
|
15
|
-
# What a participant reads, mixed into App
|
|
16
|
-
# (
|
|
17
|
-
#
|
|
18
|
-
#
|
|
19
|
-
#
|
|
20
|
-
# their own (nothing is "mine", no composer). See
|
|
21
|
-
# docs/internals/participants.md (What a participant sees).
|
|
12
|
+
# What a participant reads, mixed into App: the reads their main page's
|
|
13
|
+
# show is made of (ParticipantShow: the viewer, their usage, the
|
|
14
|
+
# messages as they may read them) and their stream, each through
|
|
15
|
+
# ParticipantView. See docs/internals/participants.md (What a
|
|
16
|
+
# participant sees).
|
|
22
17
|
module ParticipantReview
|
|
23
|
-
PARTICIPANT_VIEW = "participant"
|
|
24
18
|
# The stream's query keys a filtered stream passes to the Bridge: its
|
|
25
19
|
# cursor and its join. Not client_id (a stream named as a TUI's would
|
|
26
20
|
# count as one), not raw.
|
|
27
21
|
STREAM_KEYS = %w[from_seq snapshot].freeze
|
|
28
|
-
# How much of each review subject the page gets; the rest is cut
|
|
29
|
-
# (+truncated+).
|
|
30
|
-
SUBJECT_BYTES = 256 * 1024
|
|
31
|
-
MARKDOWN_EXT = %w[.md .markdown .mdown .mkd].freeze
|
|
32
22
|
|
|
33
23
|
private
|
|
34
24
|
|
|
35
|
-
# Whether this request reads as a participant
|
|
36
|
-
#
|
|
25
|
+
# Whether this request reads as a participant (their show, their
|
|
26
|
+
# filtered stream).
|
|
37
27
|
def participant_view?(req)
|
|
38
|
-
!principal(req).owner?
|
|
28
|
+
!principal(req).owner?
|
|
39
29
|
end
|
|
40
30
|
|
|
41
|
-
# Who reads: the participant (their author)
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
# cookie value.
|
|
31
|
+
# Who reads: the participant (their author). The session's folder
|
|
32
|
+
# places tool paths; the owner's token is never shown, nor (LAN) its
|
|
33
|
+
# loopback cookie value.
|
|
45
34
|
def participant_viewer(req, session)
|
|
46
|
-
author
|
|
47
|
-
Redact::Viewer.new(author: author, cwd: session.working_directory, secrets: owner_secrets)
|
|
35
|
+
Redact::Viewer.new(author: principal(req).grant.author, cwd: session.working_directory, secrets: owner_secrets)
|
|
48
36
|
end
|
|
49
37
|
|
|
50
38
|
def owner_secrets
|
|
@@ -52,82 +40,6 @@ module Samagotchi
|
|
|
52
40
|
[token, @lan && Token.loopback_cookie(token)].compact
|
|
53
41
|
end
|
|
54
42
|
|
|
55
|
-
# GET /api/sessions/:id/review: the participant page's data, always
|
|
56
|
-
# filtered (the owner gets the participant view of it).
|
|
57
|
-
# @return {viewer, session, subjects, participants, messages,
|
|
58
|
-
# current_turn, waiting_for_owner, queued: {mine, others}, last_event_id}
|
|
59
|
-
def handle_review(req, id)
|
|
60
|
-
session = @session_class.load(id, state_dir: default_state_dir)
|
|
61
|
-
features = ParticipantMode.live_features(session, default_state_dir)
|
|
62
|
-
mode = ParticipantMode.of(session, features: features).to_s
|
|
63
|
-
viewer = participant_viewer(req, session)
|
|
64
|
-
live = bridge_get_json(id, "snapshot")
|
|
65
|
-
turn_snapshot = live && live["snapshot"]
|
|
66
|
-
state = live && live["session_state_snapshot"]
|
|
67
|
-
raw = live_or_saved(turn_snapshot, "messages") { session.messages }
|
|
68
|
-
# No html here: ParticipantView drops a report and the owner's html; with_reply_html renders the redacted replies.
|
|
69
|
-
shown = messages_for_display(visible_replies(raw), parts: true, cwd: session.working_directory, markdown: false)
|
|
70
|
-
current = turn_snapshot && ParticipantView.current_turn(turn_snapshot["current_turn"], viewer)
|
|
71
|
-
automatic = automatic_turn_ids(id, session, shown, current, state)
|
|
72
|
-
waiting = !!(current&.dig("waiting_for_owner") || turn_snapshot&.dig("continue_offer") ||
|
|
73
|
-
(turn_snapshot && !turn_snapshot["current_turn"] && turn_snapshot["pending_question"]))
|
|
74
|
-
queued = turn_snapshot && snapshot_list(turn_snapshot, "queued")
|
|
75
|
-
status = displayed_status(session, state)
|
|
76
|
-
status = Session::STATUS_IDLE if status == Session::STATUS_RUNNING && current.nil?
|
|
77
|
-
json_response(200, {
|
|
78
|
-
viewer: review_viewer(req, id, session: session, state: state, queued: queued, viewer: viewer, mode: mode),
|
|
79
|
-
session: { id: session.id, title: participant_title(session), status: status,
|
|
80
|
-
verify_unavailable: VerifyAnchor.worker_unavailable?(features) },
|
|
81
|
-
subjects: review_subjects(session, viewer),
|
|
82
|
-
participants: participant_names(id).map { |entry| entry.slice(:id, :name, :kind) },
|
|
83
|
-
messages: with_reply_html(ParticipantView.messages(shown, viewer, automatic: automatic)),
|
|
84
|
-
current_turn: current,
|
|
85
|
-
waiting_for_owner: waiting,
|
|
86
|
-
queued: review_queue(queued || [], viewer),
|
|
87
|
-
verdicts: ParticipantView.verdicts(VerifyFresh.verdicts_of(session, Session.session_dir(id, state_dir: default_state_dir)), viewer),
|
|
88
|
-
# The verdicts a fresh check runs for now (their check_turn_id;
|
|
89
|
-
# never the child's id: a participant can't open it).
|
|
90
|
-
fresh_running: VerifyFresh.running(id, state_dir: default_state_dir).map { |row| row[:of] }.uniq,
|
|
91
|
-
last_event_id: turn_snapshot && turn_snapshot["event_id"]
|
|
92
|
-
})
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
# A review session's subjects as the page shows them (the Plan view):
|
|
96
|
-
# each {path, text, html (markdown only), truncated}, or {path,
|
|
97
|
-
# missing: true} for one that is gone or that ReviewPaths refuses now
|
|
98
|
-
# (it became a symlink out of the project, say): checked at every
|
|
99
|
-
# read, as a review turn's read is. The text is cut at SUBJECT_BYTES
|
|
100
|
-
# and redacted like everything else a participant gets; [] for a
|
|
101
|
-
# session that isn't a review session.
|
|
102
|
-
def review_subjects(session, viewer)
|
|
103
|
-
review = session.review or return []
|
|
104
|
-
root = ReviewStart.root_for(session.working_directory)
|
|
105
|
-
check = Guardrails::ReviewPaths.new(root: root)
|
|
106
|
-
review.subjects.map do |path|
|
|
107
|
-
# The real path is checked and read, the file with O_NOFOLLOW: a
|
|
108
|
-
# symlink swapped in for it after the check isn't followed. Only
|
|
109
|
-
# the last component: a folder swapped for a symlink in between
|
|
110
|
-
# still is, which takes write access to the project (the owner's).
|
|
111
|
-
real = File.realpath(File.expand_path(path, root))
|
|
112
|
-
next { path: path, missing: true } unless File.file?(real) && check.refusal([real]).nil?
|
|
113
|
-
|
|
114
|
-
review_subject(path, real, viewer)
|
|
115
|
-
rescue SystemCallError
|
|
116
|
-
{ path: path, missing: true }
|
|
117
|
-
end
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def review_subject(path, full, viewer)
|
|
121
|
-
raw = File.open(full, File::RDONLY | File::NOFOLLOW | File::BINARY) { |file| file.read(SUBJECT_BYTES + 1) }.to_s
|
|
122
|
-
text = Redact.text(raw.byteslice(0, SUBJECT_BYTES).force_encoding(Encoding::UTF_8).scrub, viewer)
|
|
123
|
-
entry = { path: path, text: text, truncated: raw.bytesize > SUBJECT_BYTES }
|
|
124
|
-
markdown = MARKDOWN_EXT.include?(File.extname(path).downcase)
|
|
125
|
-
entry[:html] = @markdown_renderer.render(text) if markdown && @markdown_renderer.available?
|
|
126
|
-
entry
|
|
127
|
-
rescue SystemCallError
|
|
128
|
-
{ path: path, missing: true }
|
|
129
|
-
end
|
|
130
|
-
|
|
131
43
|
# The saved messages with each model message's content as a
|
|
132
44
|
# participant may read it (ParticipantView.visible_raw: no cut-off
|
|
133
45
|
# thought or tool call left as text).
|
|
@@ -161,19 +73,15 @@ module Samagotchi
|
|
|
161
73
|
def delegate_child?(session) = !session.parent_id.to_s.empty?
|
|
162
74
|
|
|
163
75
|
# Who is reading: a participant (their id, name, whether they may
|
|
164
|
-
# comment: not once their cap is reached,
|
|
165
|
-
# grant's exposure shows it)
|
|
166
|
-
#
|
|
167
|
-
|
|
168
|
-
# its queue.
|
|
169
|
-
def review_viewer(req, id, session:, state:, queued:, viewer:, mode:)
|
|
170
|
-
return { kind: "owner", can_comment: false, mode: mode } if principal(req).owner?
|
|
171
|
-
|
|
76
|
+
# comment: not once their cap is reached, their grant's mode, and
|
|
77
|
+
# their +usage+ as their grant's exposure shows it). +state+ the live
|
|
78
|
+
# worker's session state (nil without one), +queued+ its queue.
|
|
79
|
+
def review_viewer(req, id, session:, state:, queued:, viewer:)
|
|
172
80
|
grant = principal(req).grant
|
|
173
81
|
capped = !participant_cap_refusal(grant).nil?
|
|
174
|
-
#
|
|
175
|
-
mode =
|
|
176
|
-
{ kind: "participant", id: grant.id, name: grant.name,
|
|
82
|
+
# Their turns act under the owner's rules (every active grant is pair).
|
|
83
|
+
mode = Grants::PAIR_MODE
|
|
84
|
+
{ kind: "participant", id: grant.id, name: grant.name, mode: mode, can_comment: !capped,
|
|
177
85
|
cap_reached: capped, session_id: id,
|
|
178
86
|
usage: review_usage(grant, session: session, state: state, queued: queued, viewer: viewer) }
|
|
179
87
|
end
|
|
@@ -231,14 +139,6 @@ module Samagotchi
|
|
|
231
139
|
nil
|
|
232
140
|
end
|
|
233
141
|
|
|
234
|
-
# The queue as a participant sees it: their own queued turns (the
|
|
235
|
-
# enqueued id, for dequeue, and the text), and how many others wait.
|
|
236
|
-
def review_queue(queued, viewer)
|
|
237
|
-
mine, others = queued.partition { |entry| entry.is_a?(Hash) && viewer.own?(entry["author"]) }
|
|
238
|
-
{ mine: mine.map { |entry| Redact.deep({ "enqueued_id" => entry["enqueued_id"].to_s, "prompt" => entry["prompt"].to_s }, viewer) },
|
|
239
|
-
others: others.size }
|
|
240
|
-
end
|
|
241
|
-
|
|
242
142
|
# Each reply's rendered markdown, from its filtered (redacted) text.
|
|
243
143
|
def with_reply_html(messages)
|
|
244
144
|
return messages unless @markdown_renderer.available?
|
|
@@ -250,33 +150,13 @@ module Samagotchi
|
|
|
250
150
|
end
|
|
251
151
|
end
|
|
252
152
|
|
|
253
|
-
# The page a participant's link opens, and the owner's participant
|
|
254
|
-
# view (/?as=participant#s=<id>): the participant page's shell; its
|
|
255
|
-
# script reads everything through the review route and the filtered
|
|
256
|
-
# stream.
|
|
257
|
-
def participant_shell(req)
|
|
258
|
-
path = File.join(@public_dir, "participant.html")
|
|
259
|
-
return error_response(500, "missing_page", "participant.html is missing") unless File.file?(path)
|
|
260
|
-
|
|
261
|
-
attrs = { "version" => Samagotchi::VERSION }
|
|
262
|
-
if principal(req).owner?
|
|
263
|
-
attrs["viewer"] = "owner"
|
|
264
|
-
else
|
|
265
|
-
grant = principal(req).grant
|
|
266
|
-
attrs.merge!("viewer" => "participant", "session" => grant.session_id, "name" => grant.name)
|
|
267
|
-
end
|
|
268
|
-
# The owner's participant view has no composer; a participant's
|
|
269
|
-
# selection quotes with the same presets as the owner's page.
|
|
270
|
-
attrs.merge!(annotate_presets_data) unless principal(req).owner?
|
|
271
|
-
# A block: a name's \& or \' is text, never a backreference.
|
|
272
|
-
html_response(200, File.read(path).sub("<body>") { "<body #{body_data(attrs)}>" })
|
|
273
|
-
end
|
|
274
|
-
|
|
275
153
|
# A filtered stream's frame filter (App#stream_filter), for the
|
|
276
154
|
# session +id+.
|
|
277
155
|
def participant_stream_filter(req, id)
|
|
278
156
|
session = @session_class.load(id, state_dir: default_state_dir)
|
|
279
|
-
|
|
157
|
+
grant = principal(req).grant
|
|
158
|
+
own = ->(enqueued_id) { own_turn?(grant, id, enqueued_id) }
|
|
159
|
+
ParticipantStreamFilter.new(participant_viewer(req, session), own_enqueued: own)
|
|
280
160
|
end
|
|
281
161
|
|
|
282
162
|
# A filtered stream's query: only its cursor and its join.
|
|
@@ -0,0 +1,241 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "json"
|
|
4
|
+
require_relative "participant_view"
|
|
5
|
+
require_relative "viewer"
|
|
6
|
+
require_relative "../bridge/turn_accumulator"
|
|
7
|
+
require_relative "../worker_link"
|
|
8
|
+
require_relative "../verify_anchor"
|
|
9
|
+
require_relative "../verify_fresh"
|
|
10
|
+
require_relative "capabilities"
|
|
11
|
+
|
|
12
|
+
module Samagotchi
|
|
13
|
+
module Web
|
|
14
|
+
# A participant's GET /api/sessions/:id, mixed into App: the main page's
|
|
15
|
+
# session read in the owner's shape (SessionShow#owner_show's keys), made
|
|
16
|
+
# from the same reads (SessionShow#show_reads) and every part passed
|
|
17
|
+
# through ParticipantView: the messages, each finished turn's drawing
|
|
18
|
+
# event by event, the turn in progress, the replay and the queue. What a
|
|
19
|
+
# participant never gets is there empty or nil (history, commands, cards,
|
|
20
|
+
# recap, the continue offer, the question, the warnings); the only keys
|
|
21
|
+
# the owner's show lacks are +viewer+'s own and the verdicts' (+fresh_running+,
|
|
22
|
+
# +session.verify_unavailable+). The light re-reads (?tail=1, ?timing=1,
|
|
23
|
+
# ?cards=1) answer in the owner's shape too, filtered; ?brief=1 is what
|
|
24
|
+
# SessionAccess::Remote#show reads with a participant's token.
|
|
25
|
+
# See docs/internals/participants.md (What a participant sees).
|
|
26
|
+
module ParticipantShow
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def participant_show_read(req, id)
|
|
30
|
+
return participant_show(id) if req.params["brief"] == "1"
|
|
31
|
+
return participant_cards_read(id) if req.params["cards"] == "1"
|
|
32
|
+
return participant_tail_read(req, id) if req.params["tail"] == "1"
|
|
33
|
+
return participant_timing_read(req, id) if req.params["timing"] == "1"
|
|
34
|
+
|
|
35
|
+
session = @session_class.load(id, state_dir: default_state_dir)
|
|
36
|
+
participant_main_show(req, show_reads(id, session))
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# The owner's show's keys, a participant's values (the class comment).
|
|
40
|
+
def participant_main_show(req, reads)
|
|
41
|
+
session = reads.session
|
|
42
|
+
viewer = participant_viewer(req, session)
|
|
43
|
+
turn_snapshot = reads.turn_snapshot
|
|
44
|
+
features = WorkerLink.live_features(session.id, state_dir: default_state_dir)
|
|
45
|
+
timing = participant_timing(reads.id, live_metrics: reads.state && reads.state["metrics"])
|
|
46
|
+
raw = visible_replies(reads.raw_messages)
|
|
47
|
+
drawn = drawn_lines(reads.id, raw, timing: {}, entries: [], cwd: session.working_directory,
|
|
48
|
+
skip_turn_id: reads.current_turn_id)
|
|
49
|
+
shown = messages_for_display(raw, parts: true, cwd: session.working_directory, markdown: false, drawings: drawn)
|
|
50
|
+
current = turn_snapshot && ParticipantView.current_turn(reads.current_turn, viewer)
|
|
51
|
+
automatic = automatic_turn_ids(reads.id, session, shown, current, reads.state)
|
|
52
|
+
messages = with_reply_html(ParticipantView.messages(shown, viewer, automatic: automatic))
|
|
53
|
+
queued = snapshot_list(turn_snapshot, "queued")
|
|
54
|
+
json_response(200, {
|
|
55
|
+
session: participant_session_json(reads, features, viewer),
|
|
56
|
+
history: [],
|
|
57
|
+
messages: messages,
|
|
58
|
+
**participant_drawings_field(drawn, messages, viewer),
|
|
59
|
+
failed_turn: nil,
|
|
60
|
+
current_turn: current,
|
|
61
|
+
replay: participant_replay(turn_snapshot, timing, viewer),
|
|
62
|
+
queued: participant_queued(queued, viewer),
|
|
63
|
+
queued_commands: [],
|
|
64
|
+
recap: nil,
|
|
65
|
+
saved_recap: nil,
|
|
66
|
+
continue_offer: nil,
|
|
67
|
+
guardrail_warning: nil,
|
|
68
|
+
plugin_warning: nil,
|
|
69
|
+
init_tasks: [],
|
|
70
|
+
cards: [],
|
|
71
|
+
commands: [],
|
|
72
|
+
# The owner's chi web's own setup notes: never a participant's.
|
|
73
|
+
markdown_warning: nil,
|
|
74
|
+
markdown_tip: nil,
|
|
75
|
+
**participant_capabilities,
|
|
76
|
+
viewer: participant_main_viewer(req, reads, queued: turn_snapshot && queued, viewer: viewer),
|
|
77
|
+
pending_question: nil,
|
|
78
|
+
last_event_seq: reads.event_seq,
|
|
79
|
+
last_event_id: reads.event_id,
|
|
80
|
+
timing: timing,
|
|
81
|
+
fresh_running: VerifyFresh.running(reads.id, state_dir: default_state_dir).map { |row| row[:of] }.uniq
|
|
82
|
+
})
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# The show's session: who it is (id, the participant title), its status
|
|
86
|
+
# as a participant reads it (idle when a worker says running with no
|
|
87
|
+
# turn), whether a worker runs it and what it can do (the page's
|
|
88
|
+
# delivery line, the stale-worker badge), its verdicts (filtered) and whether Verify fresh can run.
|
|
89
|
+
def participant_session_json(reads, features, viewer)
|
|
90
|
+
session = reads.session
|
|
91
|
+
status = displayed_status(session, reads.state)
|
|
92
|
+
status = Session::STATUS_IDLE if status == Session::STATUS_RUNNING && reads.turn_snapshot && reads.current_turn.nil?
|
|
93
|
+
dir = Session.session_dir(reads.id, state_dir: default_state_dir)
|
|
94
|
+
{ id: session.id, title: participant_title(session), status: status,
|
|
95
|
+
bridge_up: !reads.turn_snapshot.nil?, worker_features: Array(features),
|
|
96
|
+
verdicts: ParticipantView.verdicts(VerifyFresh.verdicts_of(session, dir), viewer),
|
|
97
|
+
verify_unavailable: VerifyAnchor.worker_unavailable?(features) }
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
# The show's capabilities (Capabilities, what the page renders): never
|
|
101
|
+
# the editor (it opens this machine's paths), whoever's machine it is.
|
|
102
|
+
def participant_capabilities
|
|
103
|
+
{ capabilities: Capabilities.for(@markdown_renderer, editor: false).to_h, editor_url: nil }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# The show's viewer: Viewer's (kind, id, name, can, client_id) with what
|
|
107
|
+
# #review_viewer says (their mode, cap,
|
|
108
|
+
# usage) and whether the session is stopped (only the owner's message
|
|
109
|
+
# starts it again).
|
|
110
|
+
def participant_main_viewer(req, reads, queued:, viewer:)
|
|
111
|
+
session = reads.session
|
|
112
|
+
review = review_viewer(req, reads.id, session: session, state: reads.state, queued: queued, viewer: viewer)
|
|
113
|
+
Viewer.for(principal(req)).to_h.merge(
|
|
114
|
+
review.slice(:mode, :cap_reached, :usage),
|
|
115
|
+
session_stopped: session.status == Session::STATUS_STOPPED,
|
|
116
|
+
waiting_for_owner: participant_waiting(reads.turn_snapshot)
|
|
117
|
+
)
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
# What the session waits for the owner on (ParticipantView.waiting_kind),
|
|
121
|
+
# nil when it doesn't: the running turn's question or approval, a
|
|
122
|
+
# Continue offer, or a question between turns.
|
|
123
|
+
def participant_waiting(turn_snapshot)
|
|
124
|
+
return nil unless turn_snapshot.is_a?(Hash)
|
|
125
|
+
|
|
126
|
+
turn = turn_snapshot["current_turn"]
|
|
127
|
+
pending = turn.is_a?(Hash) ? turn["pending_question"] : turn_snapshot["pending_question"]
|
|
128
|
+
return ParticipantView.waiting_kind(pending) if pending
|
|
129
|
+
return ParticipantView::WAITING_CONTINUE if turn_snapshot["continue_offer"]
|
|
130
|
+
|
|
131
|
+
nil
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
# {turn_drawings: {turn_id => {events:, end:}}} as the owner's
|
|
135
|
+
# (SessionShow#turn_drawings_field), each event through
|
|
136
|
+
# ParticipantView.event (a dropped one is left out) and the end
|
|
137
|
+
# through ParticipantView.drawing_end. No cards. Its start's prompt is
|
|
138
|
+
# the filtered messages' (ParticipantView.drawing_start): a turn they
|
|
139
|
+
# show no person's prompt for (an automatic turn, a wake, a delegate
|
|
140
|
+
# child's task; a made drawing's start has no origin to say so)
|
|
141
|
+
# starts with none.
|
|
142
|
+
def participant_drawings_field(lines, messages, viewer)
|
|
143
|
+
stamped = messages.to_set { |item| item["drawn"] }
|
|
144
|
+
shown = lines.select { |line| stamped.include?(line.turn_id) }
|
|
145
|
+
return {} if shown.empty?
|
|
146
|
+
|
|
147
|
+
prompts = messages.each_with_object({}) do |message, by_turn|
|
|
148
|
+
next unless message["role"] == "user" && message["turn_id"] && !message["automatic"] && !message["merged"]
|
|
149
|
+
|
|
150
|
+
by_turn[message["turn_id"]] = message["content"]
|
|
151
|
+
end
|
|
152
|
+
{ turn_drawings: shown.to_h do |line|
|
|
153
|
+
turn = Bridge::TurnAccumulator.from_wire(line.drawing)
|
|
154
|
+
events = participant_events(Bridge::TurnAccumulator.replay_events(turn, cards: false), viewer)
|
|
155
|
+
events = events.map { |event| ParticipantView.drawing_start(event, prompts[line.turn_id.to_s]) }
|
|
156
|
+
[line.turn_id, { events: events, end: ParticipantView.drawing_end(line.end || {}, line.turn_id) }]
|
|
157
|
+
end }
|
|
158
|
+
end
|
|
159
|
+
|
|
160
|
+
# The page's join (SessionShow#replay_events_for), each event through
|
|
161
|
+
# ParticipantView.event: another person's queued turn is a bare
|
|
162
|
+
# turn_enqueued (its entry holds their prompt).
|
|
163
|
+
def participant_replay(turn_snapshot, timing, viewer)
|
|
164
|
+
participant_events(replay_events_for(turn_snapshot, timing), viewer)
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def participant_events(events, viewer)
|
|
168
|
+
Array(events).filter_map { |event| ParticipantView.event(JSON.parse(JSON.generate(event)), viewer) }
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# The queue as the owner's show lists it, the viewer's own entries only
|
|
172
|
+
# (ParticipantView.queued_entry): another person's entry holds their
|
|
173
|
+
# prompt.
|
|
174
|
+
def participant_queued(queued, viewer)
|
|
175
|
+
queued.filter_map { |entry| ParticipantView.queued_entry(entry, viewer) }
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
# ?cards=1: a participant gets no cards.
|
|
179
|
+
def participant_cards_read(id)
|
|
180
|
+
return session_not_found(id) unless session_exists?(id)
|
|
181
|
+
|
|
182
|
+
json_response(200, { cards: [] })
|
|
183
|
+
end
|
|
184
|
+
|
|
185
|
+
# ?tail=1 as the owner's (SessionShow#handle_tail_read): the session's
|
|
186
|
+
# id and status, the turn's answer through ParticipantView (the last
|
|
187
|
+
# one, or ?turn_id='s), the timing filtered. Read from the live
|
|
188
|
+
# snapshot (the messages a participant may read are filtered as the
|
|
189
|
+
# full show's), else the file.
|
|
190
|
+
def participant_tail_read(req, id)
|
|
191
|
+
session = @session_class.load(id, state_dir: default_state_dir)
|
|
192
|
+
reads = show_reads(id, session)
|
|
193
|
+
viewer = participant_viewer(req, session)
|
|
194
|
+
turn_id = req.params["turn_id"].to_s.strip
|
|
195
|
+
raw = visible_replies(reads.raw_messages)
|
|
196
|
+
shown = messages_for_display(raw, parts: true, cwd: session.working_directory, markdown: false)
|
|
197
|
+
messages = ParticipantView.messages(shown, viewer)
|
|
198
|
+
answer = participant_answer(messages, turn_id.empty? ? nil : turn_id)
|
|
199
|
+
status = displayed_status(session, reads.state)
|
|
200
|
+
status = Session::STATUS_IDLE if status == Session::STATUS_RUNNING && reads.turn_snapshot && reads.current_turn.nil?
|
|
201
|
+
json_response(200, {
|
|
202
|
+
tail: true,
|
|
203
|
+
session: { id: id, status: status, used_memory_names: [], prompt_notes: [] },
|
|
204
|
+
messages: with_reply_html([answer].compact),
|
|
205
|
+
# The owner's chi web's own setup notes: never a participant's.
|
|
206
|
+
markdown_warning: nil,
|
|
207
|
+
markdown_tip: nil,
|
|
208
|
+
**participant_capabilities,
|
|
209
|
+
timing: participant_timing(id, live_metrics: reads.state && reads.state["metrics"])
|
|
210
|
+
})
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
# The answer of the turn +turn_id+ (the reply after its prompt), else
|
|
214
|
+
# the last reply with text.
|
|
215
|
+
def participant_answer(messages, turn_id)
|
|
216
|
+
if turn_id
|
|
217
|
+
start = messages.index { |message| message["role"] == "user" && message["turn_id"] == turn_id }
|
|
218
|
+
return nil unless start
|
|
219
|
+
|
|
220
|
+
turn = messages.drop(start + 1).take_while { |message| message["role"] != "user" || message["merged"] }
|
|
221
|
+
return turn.reverse.find { |message| message["role"] == "assistant" && !message["content"].strip.empty? }
|
|
222
|
+
end
|
|
223
|
+
messages.reverse.find { |message| message["role"] == "assistant" && !message["content"].strip.empty? }
|
|
224
|
+
end
|
|
225
|
+
|
|
226
|
+
# ?timing=1: the whole timing, filtered.
|
|
227
|
+
def participant_timing_read(_req, id)
|
|
228
|
+
return session_not_found(id) unless session_exists?(id)
|
|
229
|
+
|
|
230
|
+
state = bridge_get_json(id, "state")
|
|
231
|
+
json_response(200, { timing: participant_timing(id, live_metrics: state && state["session_state_snapshot"]&.fetch("metrics", nil)) })
|
|
232
|
+
end
|
|
233
|
+
|
|
234
|
+
# The show's timing (SessionShow#timing_payload) as a participant may
|
|
235
|
+
# read it (ParticipantView.timing: durations and speeds only).
|
|
236
|
+
def participant_timing(id, live_metrics:)
|
|
237
|
+
ParticipantView.timing(timing_payload(id, live_metrics: live_metrics))
|
|
238
|
+
end
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
end
|