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.
Files changed (267) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +61 -1
  3. data/bin/chi +6 -4
  4. data/docs/architecture.md +26 -9
  5. data/docs/bundles.md +4 -21
  6. data/docs/cli.md +81 -97
  7. data/docs/configuration.md +12 -14
  8. data/docs/context.md +5 -6
  9. data/docs/guardrails.md +57 -96
  10. data/docs/hooks.md +3 -4
  11. data/docs/internals/attached-context.md +6 -7
  12. data/docs/internals/background-tasks.md +4 -3
  13. data/docs/internals/block-renderers.md +2 -2
  14. data/docs/internals/bridge.md +34 -7
  15. data/docs/internals/context-telemetry.md +13 -1
  16. data/docs/internals/engine.md +25 -13
  17. data/docs/internals/inline-widgets.md +1 -1
  18. data/docs/internals/objectives.md +4 -6
  19. data/docs/internals/participants.md +340 -249
  20. data/docs/internals/prompt-caching.md +4 -5
  21. data/docs/internals/web-frontend.md +185 -35
  22. data/docs/internals/working-set.md +71 -5
  23. data/docs/memory.md +1 -1
  24. data/docs/plugins.md +2 -16
  25. data/docs/releasing.md +2 -1
  26. data/docs/scheduling.md +1 -1
  27. data/docs/sessions.md +17 -48
  28. data/docs/sub-agent.md +2 -2
  29. data/docs/system-one.md +2 -2
  30. data/docs/testing.md +56 -7
  31. data/lib/samagotchi/bridge/card_store.rb +1 -0
  32. data/lib/samagotchi/bridge/sse_writer.rb +1 -0
  33. data/lib/samagotchi/bridge/turn_accumulator.rb +8 -5
  34. data/lib/samagotchi/bridge.rb +64 -20
  35. data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
  36. data/lib/samagotchi/bundle_command/test_run.rb +1 -0
  37. data/lib/samagotchi/bundle_command/try/run.rb +1 -0
  38. data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
  39. data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
  40. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
  41. data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
  42. data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
  43. data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
  44. data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
  45. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  46. data/lib/samagotchi/client_id.rb +126 -67
  47. data/lib/samagotchi/command_intake.rb +15 -3
  48. data/lib/samagotchi/compat.rb +6 -1
  49. data/lib/samagotchi/context_candidate_choice.rb +1 -3
  50. data/lib/samagotchi/context_candidates.rb +3 -4
  51. data/lib/samagotchi/continue_offer.rb +9 -2
  52. data/lib/samagotchi/delivery.rb +36 -12
  53. data/lib/samagotchi/empty_answer_error.rb +5 -0
  54. data/lib/samagotchi/engine/cards.rb +3 -0
  55. data/lib/samagotchi/engine/context_judge.rb +5 -3
  56. data/lib/samagotchi/engine/events.rb +4 -0
  57. data/lib/samagotchi/engine/hooks.rb +3 -0
  58. data/lib/samagotchi/engine/messages.rb +5 -0
  59. data/lib/samagotchi/engine/model_settings.rb +2 -0
  60. data/lib/samagotchi/engine/models.rb +10 -0
  61. data/lib/samagotchi/engine/objectives.rb +6 -0
  62. data/lib/samagotchi/engine/participants.rb +18 -14
  63. data/lib/samagotchi/engine/plugins.rb +8 -0
  64. data/lib/samagotchi/engine/questions.rb +3 -0
  65. data/lib/samagotchi/engine/recap.rb +1 -0
  66. data/lib/samagotchi/engine/session.rb +4 -0
  67. data/lib/samagotchi/engine/side_requests.rb +9 -0
  68. data/lib/samagotchi/engine/steering.rb +2 -0
  69. data/lib/samagotchi/engine/toolkit.rb +5 -0
  70. data/lib/samagotchi/engine/turn_end.rb +7 -0
  71. data/lib/samagotchi/engine/turn_helpers.rb +8 -0
  72. data/lib/samagotchi/engine/verify.rb +2 -0
  73. data/lib/samagotchi/engine.rb +8 -3
  74. data/lib/samagotchi/event_fanout.rb +22 -1
  75. data/lib/samagotchi/grants.rb +33 -22
  76. data/lib/samagotchi/guardrail_wiring.rb +23 -18
  77. data/lib/samagotchi/guardrails/approval_use.rb +59 -0
  78. data/lib/samagotchi/guardrails/approvals.rb +9 -11
  79. data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
  80. data/lib/samagotchi/guardrails/gate.rb +10 -5
  81. data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
  82. data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
  83. data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
  84. data/lib/samagotchi/guardrails/review_paths.rb +2 -3
  85. data/lib/samagotchi/guardrails.rb +1 -1
  86. data/lib/samagotchi/image_store.rb +51 -12
  87. data/lib/samagotchi/kernel_loop.rb +7 -1
  88. data/lib/samagotchi/llm/stray_tool_call.rb +7 -4
  89. data/lib/samagotchi/llm_context_stale.rb +4 -1
  90. data/lib/samagotchi/llm_context_strategy.rb +2 -0
  91. data/lib/samagotchi/objectives.rb +1 -4
  92. data/lib/samagotchi/objectives_audit.rb +1 -1
  93. data/lib/samagotchi/objectives_track.rb +1 -1
  94. data/lib/samagotchi/owner_prompt.rb +3 -6
  95. data/lib/samagotchi/parent_report.rb +1 -0
  96. data/lib/samagotchi/participant_messages.rb +48 -4
  97. data/lib/samagotchi/participant_revise.rb +61 -0
  98. data/lib/samagotchi/participant_turn_mode.rb +50 -0
  99. data/lib/samagotchi/participant_uploads.rb +98 -0
  100. data/lib/samagotchi/participants.rb +21 -12
  101. data/lib/samagotchi/participants_watch.rb +2 -2
  102. data/lib/samagotchi/plugin/api.rb +2 -2
  103. data/lib/samagotchi/plugin/attached_context.rb +1 -1
  104. data/lib/samagotchi/plugin/context.rb +0 -4
  105. data/lib/samagotchi/plugin/sessions.rb +1 -25
  106. data/lib/samagotchi/reply_wait/poll.rb +5 -1
  107. data/lib/samagotchi/revise_anchor.rb +9 -0
  108. data/lib/samagotchi/run_options.rb +2 -3
  109. data/lib/samagotchi/send_command.rb +15 -10
  110. data/lib/samagotchi/session.rb +0 -3
  111. data/lib/samagotchi/session_access/contract.rb +1 -10
  112. data/lib/samagotchi/session_access/local.rb +10 -23
  113. data/lib/samagotchi/session_access/remote.rb +9 -20
  114. data/lib/samagotchi/session_access.rb +12 -16
  115. data/lib/samagotchi/session_chain.rb +4 -6
  116. data/lib/samagotchi/session_commands.rb +6 -3
  117. data/lib/samagotchi/session_continue.rb +1 -2
  118. data/lib/samagotchi/session_inbox.rb +20 -12
  119. data/lib/samagotchi/session_manager/delivery.rb +32 -5
  120. data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
  121. data/lib/samagotchi/session_manager/listing.rb +5 -0
  122. data/lib/samagotchi/session_manager/spawn.rb +2 -2
  123. data/lib/samagotchi/session_manager/worker_command.rb +9 -0
  124. data/lib/samagotchi/session_participants.rb +12 -8
  125. data/lib/samagotchi/session_review.rb +5 -7
  126. data/lib/samagotchi/session_stores.rb +3 -1
  127. data/lib/samagotchi/share_command.rb +10 -12
  128. data/lib/samagotchi/share_terms.rb +41 -0
  129. data/lib/samagotchi/steer.rb +8 -14
  130. data/lib/samagotchi/system_prompt.rb +16 -25
  131. data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
  132. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
  133. data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
  134. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
  135. data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
  136. data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
  137. data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
  138. data/lib/samagotchi/terminal_ui.rb +1 -2
  139. data/lib/samagotchi/testing/sessions.rb +2 -10
  140. data/lib/samagotchi/tool_runner.rb +42 -9
  141. data/lib/samagotchi/toolkit.rb +4 -22
  142. data/lib/samagotchi/toolkit_suggest.rb +3 -3
  143. data/lib/samagotchi/tools/deferred_tools.rb +1 -0
  144. data/lib/samagotchi/tools/execute.rb +11 -45
  145. data/lib/samagotchi/tools/leftovers.rb +122 -0
  146. data/lib/samagotchi/tools/memory.rb +88 -25
  147. data/lib/samagotchi/tools/task_get.rb +1 -0
  148. data/lib/samagotchi/tools/task_list.rb +3 -2
  149. data/lib/samagotchi/tools/task_record.rb +6 -4
  150. data/lib/samagotchi/tools/task_runtime.rb +199 -20
  151. data/lib/samagotchi/tools/task_wait.rb +4 -3
  152. data/lib/samagotchi/turn_anchors.rb +0 -3
  153. data/lib/samagotchi/turn_context.rb +1 -1
  154. data/lib/samagotchi/turn_origin.rb +1 -1
  155. data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
  156. data/lib/samagotchi/turn_stages/objectives.rb +2 -2
  157. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
  158. data/lib/samagotchi/turn_stages/verify_anchors.rb +13 -1
  159. data/lib/samagotchi/update_hint.rb +3 -1
  160. data/lib/samagotchi/verify_anchor.rb +6 -0
  161. data/lib/samagotchi/verify_fresh.rb +1 -1
  162. data/lib/samagotchi/verify_fresh_origin.rb +13 -11
  163. data/lib/samagotchi/version.rb +1 -1
  164. data/lib/samagotchi/web/app.rb +45 -82
  165. data/lib/samagotchi/web/participant_access.rb +139 -40
  166. data/lib/samagotchi/web/participant_images.rb +185 -0
  167. data/lib/samagotchi/web/participant_review.rb +22 -142
  168. data/lib/samagotchi/web/participant_show.rb +241 -0
  169. data/lib/samagotchi/web/participant_stream_filter.rb +25 -2
  170. data/lib/samagotchi/web/participant_view.rb +334 -27
  171. data/lib/samagotchi/web/participants_admin.rb +21 -40
  172. data/lib/samagotchi/web/public/annotate_bar.css +1 -2
  173. data/lib/samagotchi/web/public/annotate_bar.js +1 -2
  174. data/lib/samagotchi/web/public/app.js +126 -132
  175. data/lib/samagotchi/web/public/block_host.js +7 -4
  176. data/lib/samagotchi/web/public/bubbles.js +9 -1
  177. data/lib/samagotchi/web/public/cards_offscreen.js +69 -0
  178. data/lib/samagotchi/web/public/chip_popover.js +5 -4
  179. data/lib/samagotchi/web/public/claim_link.js +4 -4
  180. data/lib/samagotchi/web/public/composer.css +4 -6
  181. data/lib/samagotchi/web/public/composer_mode.js +11 -6
  182. data/lib/samagotchi/web/public/composer_mount.js +4 -4
  183. data/lib/samagotchi/web/public/ctx.js +12 -3
  184. data/lib/samagotchi/web/public/data.js +2 -10
  185. data/lib/samagotchi/web/public/delivery_line.js +49 -63
  186. data/lib/samagotchi/web/public/history_shape.js +5 -1
  187. data/lib/samagotchi/web/public/images.js +7 -1
  188. data/lib/samagotchi/web/public/index.html +50 -34
  189. data/lib/samagotchi/web/public/info_bar_model.js +2 -1
  190. data/lib/samagotchi/web/public/live_turn.js +1 -1
  191. data/lib/samagotchi/web/public/markdown_place.js +5 -4
  192. data/lib/samagotchi/web/public/owner_claim.js +5 -5
  193. data/lib/samagotchi/web/public/owner_verify.js +0 -2
  194. data/lib/samagotchi/web/public/participant_bar.js +107 -0
  195. data/lib/samagotchi/web/public/participant_main.js +289 -0
  196. data/lib/samagotchi/web/public/participant_model.js +5 -292
  197. data/lib/samagotchi/web/public/participant_names.js +17 -8
  198. data/lib/samagotchi/web/public/participant_revise.js +66 -0
  199. data/lib/samagotchi/web/public/participants_panel.js +32 -74
  200. data/lib/samagotchi/web/public/question_cards.js +14 -3
  201. data/lib/samagotchi/web/public/refs.js +53 -36
  202. data/lib/samagotchi/web/public/reload_check.js +70 -14
  203. data/lib/samagotchi/web/public/revise_stack.js +21 -10
  204. data/lib/samagotchi/web/public/send_turn.js +28 -29
  205. data/lib/samagotchi/web/public/session_actions.js +4 -45
  206. data/lib/samagotchi/web/public/session_fields.js +1 -2
  207. data/lib/samagotchi/web/public/session_state.js +12 -7
  208. data/lib/samagotchi/web/public/session_sync.js +9 -2
  209. data/lib/samagotchi/web/public/session_view.js +14 -5
  210. data/lib/samagotchi/web/public/settings_panel.js +4 -18
  211. data/lib/samagotchi/web/public/stage_answer.js +56 -0
  212. data/lib/samagotchi/web/public/stage_asking.js +7 -5
  213. data/lib/samagotchi/web/public/stage_cloud.js +93 -0
  214. data/lib/samagotchi/web/public/stage_dom.js +100 -0
  215. data/lib/samagotchi/web/public/stage_handoff.js +154 -0
  216. data/lib/samagotchi/web/public/stage_owner_wait.js +35 -0
  217. data/lib/samagotchi/web/public/stage_pager.js +59 -0
  218. data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
  219. data/lib/samagotchi/web/public/stage_view.js +103 -410
  220. data/lib/samagotchi/web/public/stage_waiting.js +74 -6
  221. data/lib/samagotchi/web/public/start_page.js +12 -33
  222. data/lib/samagotchi/web/public/start_settings.js +24 -78
  223. data/lib/samagotchi/web/public/start_toolkit.js +6 -10
  224. data/lib/samagotchi/web/public/storage.js +22 -0
  225. data/lib/samagotchi/web/public/stream_handlers.js +3 -10
  226. data/lib/samagotchi/web/public/tokens.css +1 -1
  227. data/lib/samagotchi/web/public/turn_block.js +2 -5
  228. data/lib/samagotchi/web/public/turn_events.js +30 -12
  229. data/lib/samagotchi/web/public/turn_html.js +9 -2
  230. data/lib/samagotchi/web/public/turn_pieces.js +3 -4
  231. data/lib/samagotchi/web/public/turn_range.js +1 -2
  232. data/lib/samagotchi/web/public/verify_target.js +2 -4
  233. data/lib/samagotchi/web/public/viewer.js +59 -0
  234. data/lib/samagotchi/web/public/viewer_self.js +59 -0
  235. data/lib/samagotchi/web/public/working_set.js +11 -5
  236. data/lib/samagotchi/web/public/working_set_view.js +53 -8
  237. data/lib/samagotchi/web/public/ws_cards.js +89 -0
  238. data/lib/samagotchi/web/public/ws_peek.js +106 -0
  239. data/lib/samagotchi/web/public/ws_preview.css +126 -0
  240. data/lib/samagotchi/web/public/ws_reader.js +319 -0
  241. data/lib/samagotchi/web/public/ws_timeline.js +200 -0
  242. data/lib/samagotchi/web/public/ws_way_in.js +25 -0
  243. data/lib/samagotchi/web/qr.rb +1 -1
  244. data/lib/samagotchi/web/session_control.rb +42 -15
  245. data/lib/samagotchi/web/session_create.rb +6 -72
  246. data/lib/samagotchi/web/session_show.rb +75 -28
  247. data/lib/samagotchi/web/session_summary.rb +1 -4
  248. data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
  249. data/lib/samagotchi/web/viewer.rb +70 -0
  250. data/lib/samagotchi/web/working_set_routes.rb +132 -0
  251. data/lib/samagotchi/worker.rb +35 -28
  252. data/lib/samagotchi/worker_inbound.rb +4 -3
  253. data/lib/samagotchi/worker_link.rb +17 -0
  254. data/lib/samagotchi/working_set.rb +322 -11
  255. metadata +32 -13
  256. data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
  257. data/lib/samagotchi/participant_mode.rb +0 -86
  258. data/lib/samagotchi/review_apply.rb +0 -131
  259. data/lib/samagotchi/review_command.rb +0 -123
  260. data/lib/samagotchi/review_start.rb +0 -196
  261. data/lib/samagotchi/web/public/participant.html +0 -218
  262. data/lib/samagotchi/web/public/participant.js +0 -719
  263. data/lib/samagotchi/web/public/participant_annotate.js +0 -76
  264. data/lib/samagotchi/web/public/review_apply.js +0 -34
  265. data/lib/samagotchi/web/public/review_history.js +0 -72
  266. data/lib/samagotchi/web/public/review_start.js +0 -55
  267. data/lib/samagotchi/web/public/tools_overview.js +0 -42
@@ -46,8 +46,30 @@ module Samagotchi
46
46
  PEM_BEGIN = /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/
47
47
  PEM_END = /-----END [A-Z0-9 ]*PRIVATE KEY-----/
48
48
 
49
- def initialize(viewer)
49
+ # The enqueued ids of the viewer's own turns: those whose
50
+ # :turn_enqueued this stream sent them as theirs, and any +known+
51
+ # names (chi web's record of who sent a turn, ParticipantAccess#own_turn?).
52
+ class OwnEnqueued
53
+ MAX_IDS = 1000
54
+
55
+ def initialize(known)
56
+ @known = known
57
+ @ids = []
58
+ end
59
+
60
+ def add(id)
61
+ @ids << id
62
+ @ids.shift while @ids.size > MAX_IDS
63
+ end
64
+
65
+ def include?(id) = @ids.include?(id) || (!@known.nil? && @known.call(id) == true)
66
+ end
67
+
68
+ # @param own_enqueued [#call, nil] (enqueued_id) -> whether the viewer
69
+ # sent that turn, for one this stream didn't see queued
70
+ def initialize(viewer, own_enqueued: nil)
50
71
  @viewer = viewer
72
+ @own = OwnEnqueued.new(own_enqueued)
51
73
  @buffer = +""
52
74
  @skipping = false
53
75
  @held = +""
@@ -97,7 +119,8 @@ module Samagotchi
97
119
  end
98
120
  end
99
121
  event = parse(data.join("\n")) or return ""
100
- shown = ParticipantView.event(event, @viewer) or return ""
122
+ shown = ParticipantView.event(event, @viewer, own_enqueued: @own) or return ""
123
+ @own.add(shown["enqueued_id"]) if shown["type"] == "turn_enqueued" && shown["enqueued_id"]
101
124
  id = nil unless id && EVENT_ID.match?(id)
102
125
  return chunk(event, shown, id) if shown["type"] == "generation_chunk"
103
126
 
@@ -3,10 +3,11 @@
3
3
  require_relative "../output_formatter"
4
4
  require_relative "../steer"
5
5
  require_relative "../turn_origin"
6
- require_relative "../participant_mode"
7
6
  require_relative "../revise_anchor"
8
7
  require_relative "../verify_anchor"
9
8
  require_relative "../redact"
9
+ require_relative "../file_ref"
10
+ require_relative "../image_store"
10
11
 
11
12
  module Samagotchi
12
13
  module Web
@@ -16,7 +17,10 @@ module Samagotchi
16
17
  # waiting states. Verdict cards expose only their anchor, verdict,
17
18
  # reason and author. Never: tool output or arguments, thinking, context
18
19
  # notes, plugin steers, automatic input, other cards, commands,
19
- # questions' internals, memories, the working set, cost or timing, host paths.
20
+ # questions' internals, memories, the working set's memories and URLs,
21
+ # cost, tokens per model, host paths. Numbers they read: the context's
22
+ # fill, the speed, the turn times (#context_status, #generation_completed,
23
+ # #timing).
20
24
  #
21
25
  # Every read a participant reaches goes through here: the stream's
22
26
  # events (#event, ParticipantStreamFilter), the review route's
@@ -28,8 +32,9 @@ module Samagotchi
28
32
  # home folder), defence in depth only: the allowlist is the control.
29
33
  # Pure: no I/O; keys of what comes in may be strings (JSON) or symbols.
30
34
  module ParticipantView
31
- # A turn's participant_mode values the view passes on (ParticipantMode).
32
- PARTICIPANT_MODES = [ParticipantMode::REVIEW.to_s, ParticipantMode::PAIR.to_s].freeze
35
+ # A turn's participant_mode values the view passes on: "pair", and an
36
+ # older chi's "review" (its saved turns keep their stamp).
37
+ PARTICIPANT_MODES = %w[review pair].freeze
33
38
 
34
39
  # The stream's event types a participant gets, each rebuilt by the
35
40
  # method named. Every other type is dropped.
@@ -42,20 +47,28 @@ module Samagotchi
42
47
  # fields only (#verdict_record).
43
48
  "verdict_recorded" => :verdict_recorded,
44
49
  "generation_chunk" => :generation_chunk,
50
+ # A generation's end: its decode speed only (the composer's tok/s).
51
+ "generation_completed" => :generation_completed,
52
+ # The context's fill (the composer's ctx %): its numbers only.
53
+ "context_status" => :context_status,
54
+ # The files chi works with (the stage's working set): the project's
55
+ # files only (#working_set).
56
+ "working_set_updated" => :working_set_updated,
45
57
  "generation_retrying" => :generation_retrying,
46
58
  "tool_call_started" => :tool_call_started,
47
59
  "tool_call_completed" => :tool_call_completed,
48
- # Queue changes: the page re-reads its review data, which has the
49
- # queue as the participant may see it.
50
- "turn_enqueued" => :bare,
51
- "turn_dequeued" => :bare,
52
- "input_merged" => :bare,
60
+ # Queue changes: the viewer's own in the owner's shape (the main
61
+ # page matches its bubbles by them), anyone else's bare (their
62
+ # prompts are theirs).
63
+ "turn_enqueued" => :turn_enqueued,
64
+ "turn_dequeued" => :turn_dequeued,
65
+ "input_merged" => :input_merged,
53
66
  # The owner's question, approval or Continue offer: "waiting for the
54
- # owner", nothing of it.
55
- "question_requested" => :bare,
67
+ # owner" and for what kind of thing (#waiting_kind), nothing of it.
68
+ "question_requested" => :question_requested,
56
69
  "question_answered" => :bare,
57
70
  "question_cancelled" => :bare,
58
- "continue_offered" => :bare,
71
+ "continue_offered" => :continue_offered,
59
72
  "continue_resolved" => :bare,
60
73
  # The cursor can't be replayed (or a join): the page re-reads.
61
74
  "snapshot" => :bare,
@@ -68,12 +81,12 @@ module Samagotchi
68
81
  # named is dropped all the same.
69
82
  DROPPED = %w[
70
83
  after_generation after_tool_call after_turn answer_display before_generation before_tool_call before_turn
71
- card command_output command_queued command_ran context_added context_status empty_answer_retry
72
- generation_cancelled generation_completed generation_progress generation_started guardrail_warning hook_notice
84
+ card command_output command_queued command_ran context_added empty_answer_retry
85
+ generation_cancelled generation_progress generation_started guardrail_warning hook_notice
73
86
  llm_context_edited merged_input objectives_updated participants_changed pending_input_merged plugin_init_finished
74
87
  plugin_init_started plugin_init_wait prompt_restored question question_relay reasoning_history_refused
75
88
  recap_ready reminder_injected sampling_refused session_end session_start side_usage steer_cut thinking_dropped
76
- thinking_refused tool_dispatch_completed tool_dispatch_started used_memories_updated working_set_updated
89
+ thinking_refused tool_dispatch_completed tool_dispatch_started used_memories_updated
77
90
  ].freeze
78
91
 
79
92
  # A merged input line's saved sources a participant sees: a person's
@@ -88,16 +101,53 @@ module Samagotchi
88
101
 
89
102
  # @param event [Hash] a client event (ClientEvent's shape; string keys
90
103
  # from the stream's JSON)
104
+ # @param own_enqueued [#include?] the enqueued ids of the viewer's own
105
+ # turns (a :turn_dequeued names no author; ParticipantStreamFilter's)
91
106
  # @return [Hash, nil] what a participant gets, string keys; nil: nothing
92
- def event(event, viewer)
107
+ def event(event, viewer, own_enqueued: [])
93
108
  return nil unless event.is_a?(Hash)
94
109
 
95
110
  type = field(event, :type).to_s
96
111
  method = EVENTS[type] or return nil
97
- out = send(method, event, viewer)
112
+ out = method == :turn_dequeued ? turn_dequeued(event, viewer, own_enqueued) : send(method, event, viewer)
98
113
  out && Redact.deep(out, viewer)
99
114
  end
100
115
 
116
+ # A turn queued: the viewer's own with its enqueued id, client id,
117
+ # prompt, delivery, author and image refs; anyone else's bare.
118
+ def turn_enqueued(event, viewer)
119
+ entry = queued_entry(event, viewer) or return bare(event, viewer)
120
+
121
+ { "type" => "turn_enqueued", **seq(event), **entry }
122
+ end
123
+
124
+ # A queued turn taken back or refused: the viewer's own with its
125
+ # enqueued id (never the client that took it back); anyone else's bare.
126
+ def turn_dequeued(event, viewer, own_enqueued)
127
+ enqueued_id = field(event, :enqueued_id)
128
+ return bare(event, viewer) unless enqueued_id.is_a?(String) && !enqueued_id.empty? && own_enqueued.include?(enqueued_id)
129
+
130
+ { "type" => "turn_dequeued", **seq(event), "enqueued_id" => enqueued_id }
131
+ end
132
+
133
+ # Input merged into the running turn: the viewer's own lines (their
134
+ # origins' enqueued ids) and how many, bare when none is theirs. The
135
+ # others' (a delegate's report, another person's line) are not named
136
+ # nor counted. No text: it comes with the turn's messages.
137
+ def input_merged(event, viewer)
138
+ own = Array(field(event, :origins)).filter_map do |origin|
139
+ next unless origin.is_a?(Hash) && viewer.own?(field(origin, :author))
140
+
141
+ enqueued_id = field(origin, :enqueued_id)
142
+ next unless enqueued_id.is_a?(String) && !enqueued_id.empty?
143
+
144
+ { "enqueued_id" => enqueued_id, "client_id" => field(origin, :client_id).to_s, "author" => field(origin, :author).to_s }
145
+ end
146
+ return bare(event, viewer) if own.empty?
147
+
148
+ { "type" => "input_merged", **seq(event), "count" => own.size, "origins" => own }
149
+ end
150
+
101
151
  # A turn's start: who wrote it, and its prompt when a person sent it
102
152
  # (an automatic turn's prompt, a reminder's or a task's, is not
103
153
  # shown: a person is the owner's web or TUI, chi send, a participant,
@@ -108,6 +158,8 @@ module Samagotchi
108
158
  author = field(event, :author) || origin.author
109
159
  out = { "type" => "turn_started", **id_fields(event), **time_field(event) }
110
160
  out.merge!(author_fields(author, field(event, :author_name), viewer, field(event, :participant_mode)))
161
+ images = image_refs(field(event, :images), author, viewer)
162
+ out["images"] = images if images
111
163
  prompt = field(event, :prompt)
112
164
  if field(event, :continue)
113
165
  out["continue"] = true
@@ -119,14 +171,30 @@ module Samagotchi
119
171
  out
120
172
  end
121
173
 
174
+ # A drawing's filtered turn_started (#event's) with +prompt+, the
175
+ # filtered messages' prompt of its turn; nil: an automatic turn's
176
+ # start, no prompt (a Continue's has none either way). Any other event
177
+ # as it is.
178
+ def drawing_start(event, prompt)
179
+ return event unless event["type"] == "turn_started"
180
+
181
+ out = event.except("prompt", "automatic")
182
+ return out if out["continue"]
183
+ return out.merge("prompt" => prompt) if prompt
184
+
185
+ out.except("continue").merge("automatic" => true)
186
+ end
187
+
122
188
  def turn_ended(event, viewer)
123
189
  out = { "type" => field(event, :type).to_s, **id_fields(event) }
124
190
  verdict = verdict_record(field(event, :verdict), viewer)
125
191
  out["verdict"] = verdict if verdict
126
- # The turn a completed Revise turn revised (as on its prompt; the
127
- # page doesn't draw stacks yet).
192
+ # The turn a completed Revise turn revised (as on its prompt), and
193
+ # whether it was a Verify turn (no Revise on its answer): a boolean,
194
+ # never the quote.
128
195
  revises = ReviseAnchor.from(field(event, :revises))
129
196
  out["revises"] = revises.to_file if revises
197
+ out["verify"] = true if field(event, :verify) == true
130
198
  out
131
199
  end
132
200
 
@@ -172,6 +240,82 @@ module Samagotchi
172
240
  { "type" => "generation_chunk", "text" => text, **iteration(event) }
173
241
  end
174
242
 
243
+ # A generation's end: its decode speed ({decode_tps, source}, the
244
+ # composer's tok/s). Never the session's tokens, cost or models
245
+ # (+tokens+, +model_segments+), nor the served model.
246
+ def generation_completed(event, _viewer)
247
+ out = { "type" => "generation_completed", **iteration(event) }
248
+ speed = field(event, :speed)
249
+ tps = speed.is_a?(Hash) ? field(speed, :decode_tps) : nil
250
+ if tps.is_a?(Numeric) && tps.to_f.finite?
251
+ source = field(speed, :source).to_s
252
+ out["speed"] = { "decode_tps" => tps, **(SPEED_SOURCES.include?(source) ? { "source" => source } : {}) }
253
+ end
254
+ out
255
+ end
256
+
257
+ SPEED_SOURCES = %w[server estimate].freeze
258
+
259
+ # The usage numbers of a context_status a participant reads: its fill
260
+ # and the token counts.
261
+ CONTEXT_USAGE_KEYS = %w[estimated_pct estimated_used_tokens estimated_remaining_tokens window_tokens].freeze
262
+
263
+ # The context's fill (ContextStatus#observe, Engine's switched
264
+ # context): usage's percentage and token counts (usage null: unknown
265
+ # after a model switch), the bucket, and the window after a switch.
266
+ # Never +status+ (the model-facing line, with its guidance), the
267
+ # counts' source or the window's.
268
+ def context_status(event, _viewer)
269
+ out = { "type" => "context_status", **iteration(event) }
270
+ if event.key?("usage") || event.key?(:usage)
271
+ usage = field(event, :usage)
272
+ out["usage"] = usage.is_a?(Hash) ? pick(usage, CONTEXT_USAGE_KEYS) : nil
273
+ end
274
+ bucket = field(event, :bucket)
275
+ out["bucket"] = bucket if bucket.is_a?(String) && bucket.match?(/\A(?:under|\d+plus)\d*\z/)
276
+ if event.key?("context_window_tokens") || event.key?(:context_window_tokens)
277
+ window = field(event, :context_window_tokens)
278
+ out["context_window_tokens"] = window.is_a?(Integer) ? window : nil
279
+ end
280
+ out
281
+ end
282
+
283
+ def working_set_updated(event, viewer)
284
+ { "type" => "working_set_updated", **seq(event), **working_set(event, viewer) }
285
+ end
286
+
287
+ # The working set's states and ops a participant's item may carry
288
+ # (WorkingSet::Item).
289
+ WORKING_SET_STATES = %w[live stale forgotten dropped].freeze
290
+ WORKING_SET_OPS = %w[read edit].freeze
291
+
292
+ # A working set (WorkingSet::Snapshot#to_h: the event's and GET
293
+ # …/working_set's) as a participant may read it: the file items inside
294
+ # the project, each key made project-relative, with its state, tokens,
295
+ # ops, count and pinned; +at+ (the page keeps the newer). Never a
296
+ # memory or URL item, a path outside the project, +first_id+,
297
+ # +has_drop+ or the pending +adds+.
298
+ def working_set(set, viewer)
299
+ items = Array(field(set, :items)).filter_map { |item| working_set_item(item, viewer) }
300
+ at = field(set, :at)
301
+ { "items" => items, **(at.is_a?(Integer) ? { "at" => at } : {}) }
302
+ end
303
+
304
+ def working_set_item(item, viewer)
305
+ return nil unless item.is_a?(Hash) && field(item, :kind).to_s == FileRef::KIND
306
+
307
+ key = project_path(field(item, :key), viewer.cwd, home: viewer.home) or return nil
308
+ state = field(item, :state).to_s
309
+ out = { "kind" => FileRef::KIND, "key" => key, "state" => WORKING_SET_STATES.include?(state) ? state : "live",
310
+ "ops" => Array(field(item, :ops)).map(&:to_s).select { |op| WORKING_SET_OPS.include?(op) }.uniq,
311
+ "pinned" => field(item, :pinned) == true }
312
+ %i[tokens count].each do |name|
313
+ value = field(item, name)
314
+ out[name.to_s] = value if value.is_a?(Integer) && value >= 0
315
+ end
316
+ out
317
+ end
318
+
175
319
  # A dropped stream asked again: its text so far is void when restarted.
176
320
  def generation_retrying(event, _viewer)
177
321
  out = { "type" => "generation_retrying", **iteration(event) }
@@ -183,14 +327,42 @@ module Samagotchi
183
327
  { "type" => "tool_call_started", **tool_row(field(event, :tool), field(event, :ref), viewer), **call_ids(event) }
184
328
  end
185
329
 
330
+ # Its status word, flat and as the owner's +activity+ ({status} only:
331
+ # never its action or params) for the main page's row (activity.js).
186
332
  def tool_call_completed(event, _viewer)
187
333
  out = { "type" => "tool_call_completed", **call_ids(event) }
188
334
  activity = field(event, :activity)
189
335
  status = activity.is_a?(Hash) ? field(activity, :status).to_s : ""
190
- out["status"] = status if STATUSES.include?(status)
336
+ out.merge!("status" => status, "activity" => { "status" => status }) if STATUSES.include?(status)
191
337
  out
192
338
  end
193
339
 
340
+ # A question for the owner: its kind (#waiting_kind) only.
341
+ def question_requested(event, _viewer)
342
+ { "type" => "question_requested", **seq(event), "kind" => waiting_kind(field(event, :pending_question)) }
343
+ end
344
+
345
+ # The owner's Continue offer: its kind only.
346
+ def continue_offered(event, _viewer)
347
+ { "type" => "continue_offered", **seq(event), "kind" => WAITING_CONTINUE }
348
+ end
349
+
350
+ WAITING_APPROVAL = "approval"
351
+ WAITING_QUESTION = "question"
352
+ WAITING_CONTINUE = "continue"
353
+
354
+ # What a turn waits for the owner on, derived from a pending question
355
+ # (PendingQuestion#to_h, a snapshot's): "approval" for a guardrail's
356
+ # approval or a relayed one, "continue" for the step-limit question,
357
+ # "question" for anything else (the model's own, a hook's, none).
358
+ def waiting_kind(pending)
359
+ return WAITING_QUESTION unless pending.is_a?(Hash)
360
+ return WAITING_APPROVAL if field(pending, :kind).to_s == "approval" || field(pending, :relay).is_a?(Hash)
361
+ return WAITING_CONTINUE if field(pending, :kind).to_s == "continue"
362
+
363
+ WAITING_QUESTION
364
+ end
365
+
194
366
  # Its type alone (and the cursor it was sent at).
195
367
  def bare(event, _viewer)
196
368
  { "type" => field(event, :type).to_s, **seq(event) }
@@ -242,14 +414,17 @@ module Samagotchi
242
414
  out["content"] = field(message, :content).to_s
243
415
  out["turn_id"] = turn_id.to_s if turn_id
244
416
  out["merged"] = true if merged
245
- # The turn it revised (a Revise stack; the page doesn't draw it yet).
417
+ # The turn it revised (a Revise stack), and whether it was a Verify
418
+ # turn (VerifyAnchor.marks?, ParticipantRevise's test too): a
419
+ # boolean only, never the quote.
246
420
  revises = ReviseAnchor.from(field(message, :revises))
247
421
  out["revises"] = revises.to_file if revises && turn_id && !merged
422
+ out["verify"] = true if turn_id && !merged && VerifyAnchor.marks?(field(message, :verify))
248
423
  out.merge!(author_fields(field(message, :author), field(message, :author_name), viewer,
249
424
  field(message, :participant_mode)))
250
- images = field(message, :images)
251
- out["images"] = images.size if images.is_a?(Array) && !images.empty?
252
- out
425
+ images = image_refs(field(message, :images), field(message, :author), viewer)
426
+ out["images"] = images if images
427
+ out.merge!(drawn_field(message))
253
428
  end
254
429
 
255
430
  # A wake turn's start (its prompt a delegate's report or a context
@@ -275,9 +450,33 @@ module Samagotchi
275
450
 
276
451
  out = { "role" => "assistant", "content" => content, **time_field(message) }
277
452
  out["tools"] = rows unless rows.empty?
453
+ out.merge!(drawn_field(message))
454
+ end
455
+
456
+ # The turn whose drawing draws this message (MessageDisplay's +drawn+
457
+ # stamp: a turn id), for the main page's show; none on the review read.
458
+ def drawn_field(message)
459
+ drawn = field(message, :drawn)
460
+ drawn.is_a?(String) && !drawn.empty? ? { "drawn" => drawn } : {}
461
+ end
462
+
463
+ # A finished turn's drawing end (Bridge::TurnDrawings' end, its
464
+ # END_KEYS) as a participant may read it: how it ended, its turn and
465
+ # duration, and how many steps a failure kept. Never a failure's
466
+ # summary, message or class (they may name a host path), a cancel's
467
+ # reason or who stopped it.
468
+ def drawing_end(raw, turn_id)
469
+ out = { "type" => (TURN_ENDS.include?(field(raw, :type).to_s) ? field(raw, :type).to_s : "turn_completed"),
470
+ "turn_id" => turn_id.to_s }
471
+ duration = field(raw, :duration_ms)
472
+ out["duration_ms"] = duration if duration.is_a?(Integer)
473
+ kept = field(raw, :kept_steps)
474
+ out["kept_steps"] = kept if kept.is_a?(Integer)
278
475
  out
279
476
  end
280
477
 
478
+ TURN_ENDS = %w[turn_completed turn_canceled turn_failed].freeze
479
+
281
480
  # The turn in progress (TurnAccumulator#current_turn, string keys
282
481
  # from the Bridge's snapshot) as a participant sees it: its start
283
482
  # (#turn_started's fields), the reply's text so far, its tool rows,
@@ -303,18 +502,126 @@ module Samagotchi
303
502
  end
304
503
  end
305
504
  out = started.merge("text" => text, "tools" => tools)
306
- out["waiting_for_owner"] = true if field(turn, :pending_question)
505
+ # The running generation's phase (Bridge#with_generation: the main
506
+ # page's delivery-line clock): whether it only thinks so far, and its
507
+ # age; nothing of what it thinks.
508
+ generation = field(turn, :generation)
509
+ if generation.is_a?(Hash)
510
+ gen = {}
511
+ gen["thinking_only"] = field(generation, :thinking_only) if [true, false].include?(field(generation, :thinking_only))
512
+ gen["age"] = field(generation, :age) if field(generation, :age).is_a?(Numeric)
513
+ out["generation"] = gen
514
+ end
515
+ pending = field(turn, :pending_question)
516
+ out.merge!("waiting_for_owner" => true, "kind" => waiting_kind(pending)) if pending
307
517
  Redact.deep(out, viewer)
308
518
  end
309
519
 
520
+ # A queued entry (a Bridge snapshot's +queued+ row) when it is the
521
+ # viewer's own, in the owner's shape: its enqueued id, client id,
522
+ # prompt, delivery, author and image refs (#image_refs). nil for
523
+ # another person's (their prompt).
524
+ def queued_entry(entry, viewer)
525
+ return nil unless entry.is_a?(Hash) && viewer.own?(field(entry, :author))
526
+
527
+ out = { "enqueued_id" => field(entry, :enqueued_id).to_s, "client_id" => field(entry, :client_id).to_s,
528
+ "prompt" => field(entry, :prompt).to_s, "author" => field(entry, :author).to_s }
529
+ delivery = field(entry, :delivery).to_s
530
+ out["delivery"] = delivery if DELIVERIES.include?(delivery)
531
+ images = image_refs(field(entry, :images), field(entry, :author), viewer)
532
+ out["images"] = images if images
533
+ Redact.deep(out, viewer)
534
+ end
535
+
536
+ # A message's or a turn's images as a participant sees them: the
537
+ # viewer's own as refs ({file, name, width, height}: what the page's
538
+ # thumbnails load, the files they uploaded), only refs to a stored
539
+ # image (ImageStore::REF_RE); anyone else's as a count. Never source,
540
+ # bytes or mime. nil for none.
541
+ def image_refs(images, author, viewer)
542
+ return nil unless images.is_a?(Array) && !images.empty?
543
+ return images.size unless viewer.own?(author)
544
+
545
+ refs = images.filter_map do |ref|
546
+ file = field(ref, :file)
547
+ next unless file.is_a?(String) && ImageStore::REF_RE.match?(file)
548
+
549
+ out = { "file" => file, "name" => File.basename(field(ref, :name).to_s) }
550
+ %i[width height].each { |key| out[key.to_s] = field(ref, key) if field(ref, key).is_a?(Integer) }
551
+ out
552
+ end
553
+ refs.empty? ? nil : refs
554
+ end
555
+
556
+ DELIVERIES = %w[queue next_step].freeze
557
+
558
+ # A turn record's fields a participant reads: its id and times (the
559
+ # turn time) and its speed. Never its cost, tokens, model, author,
560
+ # cancel reason or failure.
561
+ TURN_RECORD_KEYS = %w[id started_at completed_at duration_ms decode_tps tps_source].freeze
562
+ # The running turn's (SessionMetrics#active_turn_snapshot).
563
+ ACTIVE_TURN_KEYS = %w[id started_at duration_ms].freeze
564
+
565
+ # The show's timing (SessionShow#timing_from) as a participant may
566
+ # read it: the session's clock, each turn's duration and speed, the
567
+ # running turn's start, and the context's fill (its token counts).
568
+ # Never tool_records (guardrail judgment ids, System 1 labels),
569
+ # active_tools, tokens, model_segments (cost per model), memory_index
570
+ # (memory names) or side (side requests' cost).
571
+ def timing(payload)
572
+ return nil unless payload.is_a?(Hash)
573
+
574
+ records = Array(field(payload, :turn_records)).filter_map { |record| pick(record, TURN_RECORD_KEYS) }
575
+ context = field(payload, :context)
576
+ {
577
+ "started_at" => string_or_nil(field(payload, :started_at)),
578
+ "last_activity_at" => string_or_nil(field(payload, :last_activity_at)),
579
+ "session_duration_ms" => number_or_nil(field(payload, :session_duration_ms)),
580
+ "turn_records" => records,
581
+ "tool_records" => [],
582
+ "turn_count" => number_or_nil(field(payload, :turn_count)),
583
+ "active_turn" => pick(field(payload, :active_turn), ACTIVE_TURN_KEYS),
584
+ "active_tools" => [],
585
+ "context" => pick(context, %w[used_tokens window_tokens]),
586
+ "tokens" => nil,
587
+ "model_segments" => [],
588
+ "memory_index" => nil,
589
+ "side" => nil
590
+ }
591
+ end
592
+
593
+ # +keys+ of +hash+ whose values are plain (a string or a number), nil
594
+ # for a hash with none.
595
+ def pick(hash, keys)
596
+ return nil unless hash.is_a?(Hash)
597
+
598
+ out = keys.each_with_object({}) do |key, kept|
599
+ value = field(hash, key.to_sym)
600
+ kept[key] = value if value.is_a?(String) || (value.is_a?(Numeric) && value.to_f.finite?)
601
+ end
602
+ out.empty? ? nil : out
603
+ end
604
+
605
+ def string_or_nil(value) = value.is_a?(String) ? value : nil
606
+
607
+ def number_or_nil(value) = value.is_a?(Numeric) && value.to_f.finite? ? value : nil
608
+
310
609
  # A tool row: the tool's name, and for a file tool the path relative
311
610
  # to the session's folder when it is inside it. Never its arguments.
611
+ # The row's +ref+ is the owner's shape (FileRef#to_h: kind, path, the
612
+ # lines) with the relative path, for the main page's refs.js.
312
613
  def tool_row(tool, ref, viewer)
313
614
  row = { "tool" => tool.to_s }
314
615
  path = ref.is_a?(Hash) ? field(ref, :path) : nil
315
616
  relative = project_path(path, viewer.cwd, home: viewer.home)
316
- row["path"] = relative if relative
317
- row
617
+ return row unless relative
618
+
619
+ file = { "kind" => FileRef::KIND, "path" => relative }
620
+ %i[line end_line].each do |key|
621
+ value = field(ref, key)
622
+ file[key.to_s] = value if value.is_a?(Integer) && value.positive?
623
+ end
624
+ row.merge("ref" => file)
318
625
  end
319
626
 
320
627
  # +path+ relative to +cwd+ when it is inside it, else nil. None when
@@ -2,17 +2,16 @@
2
2
 
3
3
  require_relative "../grants"
4
4
  require_relative "qr"
5
- require_relative "../review_start"
6
- require_relative "../review_apply"
5
+ require_relative "../share_terms"
7
6
  require_relative "../session_metrics"
8
7
  require_relative "../participants"
9
8
  require_relative "../session"
9
+ require_relative "link_codes"
10
10
 
11
11
  module Samagotchi
12
12
  module Web
13
13
  # The owner's routes for a session's participants, mixed into App:
14
- # invite, list, revoke, a participant's cap, and the review Apply (the
15
- # owner's turn quoting a review turn's proposal). What a participant
14
+ # invite, list, revoke and a participant's cap. What a participant
16
15
  # may do themselves is ParticipantAccess's. See
17
16
  # docs/internals/participants.md.
18
17
  module ParticipantsAdmin
@@ -21,12 +20,11 @@ module Samagotchi
21
20
  # GET /api/sessions/:id/participants, GET /api/participants: the
22
21
  # grants (one session's, or every one), in every state, never a token
23
22
  # or digest. One session's also carries +warning+: the share warning
24
- # for a session that isn't a review session (ReviewStart.share_warning,
25
- # null for one), which the panel shows before its Invite; and
26
- # +identity_header+: the header this chi web's auth proxy names the
27
- # signed-in user in (null without one), for the Invite's email field;
28
- # +mode+: what a participant invited now gets, "pair" or "review"
29
- # (ReviewStart.share_terms), for the panel's words; +link_base+: where
23
+ # (ShareTerms#warning: what a participant's turns do there, or why no
24
+ # one can be invited until the session restarts), which the panel shows
25
+ # before its Invite; +identity_header+: the header this chi web's auth
26
+ # proxy names the signed-in user in (null without one), for the
27
+ # Invite's email field; and +link_base+: where
30
28
  # a participant's link opens (participant_base), for a claim link
31
29
  # (claim_link.js) the participant's browser opens with its cookie.
32
30
  def handle_participants_list(req, id = nil)
@@ -34,8 +32,8 @@ module Samagotchi
34
32
  grants = @grants.list(session_id: id).map { |grant| grant.public_h(now) }
35
33
  return json_response(200, { participants: grants }) unless id
36
34
 
37
- terms = ReviewStart.share_terms(Session.load(id, state_dir: default_state_dir), state_dir: default_state_dir)
38
- json_response(200, { participants: with_spend(id, grants), warning: terms.warning, mode: terms.mode.to_s,
35
+ terms = ShareTerms.of(Session.load(id, state_dir: default_state_dir), state_dir: default_state_dir)
36
+ json_response(200, { participants: with_spend(id, grants), warning: terms.warning,
39
37
  identity_header: @public_url&.identity_header, link_base: participant_base(req) })
40
38
  rescue Grants::Unwritable => e
41
39
  grants_unwritable(e)
@@ -57,12 +55,12 @@ module Samagotchi
57
55
  end
58
56
  end
59
57
 
60
- # POST /api/sessions/:id/participants {name, kind?, ttl? (seconds),
58
+ # POST /api/sessions/:id/participants {name, ttl? (seconds),
61
59
  # cap? (dollars), identity? (an email: the grant is bound to it, only
62
60
  # behind an auth proxy with an identity header)}: a new grant. The
63
61
  # answer carries the token and the link once (chi keeps only its
64
- # digest), and +warning+ for a session that isn't a review session
65
- # (ReviewStart.share_warning; null for one).
62
+ # digest), and +warning+ (ShareTerms#warning); 409 worker_too_old
63
+ # while the session's live worker would refuse their turns.
66
64
  def handle_participants_invite(req, id)
67
65
  body = body_hash(req) or return error_response(400, "invalid_json", "invalid JSON body")
68
66
  ttl = body["ttl"]
@@ -71,12 +69,14 @@ module Samagotchi
71
69
  cap = body["cap"]
72
70
  return error_response(400, "invalid_participant", Grants::CAP_RANGE) unless cap.nil? || Grants.cap?(cap)
73
71
 
74
- # The grant is stamped pair only when the warning that comes back says
75
- # their turns act (one read: ReviewStart.share_terms).
76
- terms = ReviewStart.share_terms(Session.load(id, state_dir: default_state_dir), state_dir: default_state_dir)
77
- issued = Participants.invite(@grants, session_id: id, name: body["name"], kind: (body["kind"] || "human").to_s,
78
- ttl: ttl, cap: cap, identity: body["identity"], public_url: @public_url,
79
- mode: terms.grant_mode, state_dir: default_state_dir)
72
+ # No link while the live worker would refuse every turn of theirs
73
+ # (one read: ShareTerms.of, the warning that comes back).
74
+ terms = ShareTerms.of(Session.load(id, state_dir: default_state_dir), state_dir: default_state_dir)
75
+ return error_response(409, "worker_too_old", terms.refusal) if terms.refusal
76
+
77
+ # An older chi --remote share --agent's kind is ignored.
78
+ issued = Participants.invite(@grants, session_id: id, name: body["name"], ttl: ttl, cap: cap, identity: body["identity"], public_url: @public_url,
79
+ state_dir: default_state_dir)
80
80
  link = participant_link(req, id, issued.token, expires_at: issued.grant.expires_at)
81
81
  json_response(201, { participant: issued.grant.public_h(@grants.now), token: issued.token,
82
82
  link: link, lan: !@lan.nil? || !@public_url.nil?, qr_svg: QR.svg(link), warning: terms.warning })
@@ -86,25 +86,6 @@ module Samagotchi
86
86
  grants_unwritable(e)
87
87
  end
88
88
 
89
- # GET /api/sessions/:id/apply[?turn=T] (owner): the owner's turn that
90
- # applies review turn T's proposal (ReviewApply: the reply quoted
91
- # under one line of the owner's), for the composer: never sent from
92
- # here. Without turn, the latest review turn with a reply. Read from
93
- # the live worker's messages, else the saved ones.
94
- # @return 200 {text, turn_id, author, author_name}; 404 not_found (no
95
- # such turn, or no review turn); 400 not_review_turn; 409 no_reply
96
- def handle_apply(req, id)
97
- session = @session_class.load(id, state_dir: default_state_dir)
98
- live = bridge_get_json(id, "snapshot")
99
- messages = live_or_saved(live && live["snapshot"], "messages") { session.messages }
100
- proposal = ReviewApply.find(messages, req.params["turn"])
101
- json_response(200, { text: ReviewApply.compose(proposal), turn_id: proposal.turn_id, author: proposal.author,
102
- author_name: proposal.author_name })
103
- rescue ReviewApply::Refused => e
104
- status = { not_found: 404, not_review_turn: 400, no_reply: 409 }.fetch(e.code)
105
- error_response(status, e.code.to_s, e.message)
106
- end
107
-
108
89
  # DELETE /api/sessions/:id/participants[?id=p_…], DELETE
109
90
  # /api/participants: revokes one participant, a session's or every
110
91
  # one's; their open streams end within a poll.
@@ -1,6 +1,5 @@
1
1
  /* The selection bubble (annotate_bar.js): Annotate + the presets (web.annotate_presets), Copy link last where the page
2
- has one, small pills with a gap (they wrap on a narrow page). Both pages link it: the owner's (app.js) and the
3
- participant page (participant.js). Colours are tokens.css's. */
2
+ has one, small pills with a gap (they wrap on a narrow page). index.html links it (app.js makes the bar). Colours are tokens.css's. */
4
3
  #annotateBar{position:fixed;z-index:20;display:flex;flex-wrap:wrap;gap:4px;max-width:calc(100vw - 16px)}
5
4
  #annotateBar[hidden]{display:none}
6
5
  #annotateBar button{font-size:12px;padding:4px 10px;border-radius:999px;box-shadow:0 2px 8px var(--shadow-strong)}