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
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "../delivery"
4
- require_relative "../participant_mode"
4
+ require_relative "../worker_link"
5
5
  require_relative "../participant_messages"
6
6
  require_relative "../participants"
7
7
  require_relative "../session"
@@ -16,7 +16,7 @@ module Samagotchi
16
16
  # with their fresh children's spend; a worker that predates
17
17
  # participants' turns) and audited like one (participants.log,
18
18
  # verify_fresh); their child runs under the boundary their grant has
19
- # here now (ParticipantMode.live_for), saved on the child. See
19
+ # here now (Grants::PAIR_MODE), saved on the child. See
20
20
  # docs/internals/participants.md (Verify fresh).
21
21
  module VerifyFreshRoutes
22
22
  private
@@ -48,7 +48,7 @@ module Samagotchi
48
48
  return verify_fresh_refused(clicker, of, *refused, runner: runner)
49
49
  end
50
50
 
51
- mode = runner && ParticipantMode.live_for(session, runner, state_dir: default_state_dir)
51
+ mode = runner && Grants::PAIR_MODE
52
52
  child = VerifyFresh.start(id, of: of, author: runner&.author, mode: mode, state_dir: default_state_dir,
53
53
  spawn: fresh_spawn)
54
54
  audit_verify_fresh(clicker, of, status: "accepted", child: child.id, mode: mode&.to_s, runner: runner)
@@ -81,14 +81,15 @@ module Samagotchi
81
81
  def field_of(record, key) = record && (record[key] || record[key.to_s])
82
82
 
83
83
  # A live worker must support VerifyFresh's parent checks. For a
84
- # participant's child it must also have the review boundary.
84
+ # participant's child it must also run their turns under the owner's
85
+ # rules (Delivery::PAIR_FEATURE).
85
86
  def fresh_worker_refusal(session, participant: false)
86
- features = ParticipantMode.live_features(session, default_state_dir)
87
+ features = WorkerLink.live_features(session.id, state_dir: default_state_dir)
87
88
  return nil if features.nil?
88
89
  return [409, VerifyAnchor::UNAVAILABLE["error"], VerifyAnchor::UNAVAILABLE["detail"]] if VerifyAnchor.worker_unavailable?(features)
89
- return nil unless participant && !features.include?(Delivery::REVIEW_FEATURE)
90
+ return nil unless participant && !features.include?(Delivery::PAIR_FEATURE)
90
91
 
91
- [409, Delivery::REVIEW_UNAVAILABLE["error"], Delivery::REVIEW_UNAVAILABLE["detail"]]
92
+ [409, Delivery::PAIR_UNAVAILABLE["error"], Delivery::PAIR_UNAVAILABLE["detail"]]
92
93
  end
93
94
 
94
95
  def verify_fresh_refused(clicker, of, status, code, detail, runner: nil)
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../client_id"
4
+
5
+ module Samagotchi
6
+ module Web
7
+ # Who looks at chi web's main page and what they may do there: the
8
+ # page draws a control, and makes a call, only when +can+ names its
9
+ # capability (viewer.js; docs/internals/web-frontend.md, The viewer).
10
+ # Not Web::Capabilities: that one says what the page renders (markdown
11
+ # display) and whether this request's viewer can open a path in this
12
+ # machine's editor; this one says which of the owner's controls this
13
+ # viewer gets at all. The page reads it from <body> at boot
14
+ # (App#index_data_attributes, #body_data) and from a show's +viewer+.
15
+ #
16
+ # +kind+ "owner" | "participant"; +id+ and +name+ a participant's grant's
17
+ # (nil for the owner); +can+ the frozen list of capability names.
18
+ Viewer = Data.define(:kind, :id, :name, :can) do
19
+ # @param principal [Principal] the request's
20
+ # @return [Viewer] the owner's with every capability; a participant's
21
+ # with PARTICIPANT_CAPABILITIES only (what their routes serve).
22
+ def self.for(principal)
23
+ return Viewer::OWNER if principal.owner?
24
+
25
+ grant = principal.grant
26
+ new(kind: "participant", id: grant.id, name: grant.name, can: Viewer::PARTICIPANT_CAPABILITIES)
27
+ end
28
+
29
+ def owner? = kind == "owner"
30
+
31
+ # The client id chi web gives this viewer's turns (it replaces the
32
+ # body's): a participant's participant:<grant id> (ClientId.participant), so the
33
+ # page can tell its own turns; nil for the owner (each tab names its own).
34
+ def client_id = owner? ? nil : ClientId.participant(id)
35
+
36
+ def can?(name) = can.include?(name.to_s)
37
+
38
+ # The <body> data attributes the page reads at boot: the kind and the
39
+ # space-separated +can+ list (no names or ids: those come with a show).
40
+ def body_attributes = { "viewer-kind" => kind, "viewer-can" => can.join(" ") }
41
+
42
+ # A participant's carries their client id too.
43
+ def to_h = { kind: kind, id: id, name: name, can: can, **(owner? ? {} : { client_id: client_id }) }
44
+ end
45
+
46
+ # The capabilities the main page gates by, each named for what the
47
+ # owner does with it (the page's controls and calls behind each one:
48
+ # docs/internals/web-frontend.md, The viewer).
49
+ Viewer::CAPABILITIES = %w[
50
+ sessions models prompt_history push context working_set working_set_items participants_admin rename images
51
+ session_controls revise answer_cards render block_renderers stop_any_turn steer_any_turn
52
+ ].freeze
53
+
54
+ # A participant's capabilities: names of their own (the owner's are
55
+ # CAPABILITIES), each one a participant route serves. +send+: their
56
+ # composer (POST …/turn); +stop_own_turn+: the stage's Stop turn on a
57
+ # turn they started (POST …/cancel with its turn_id); +steer_own_turn+:
58
+ # a message sent while their own turn runs goes in at its next step
59
+ # (a worker with Delivery::OWN_TURN_STEER_FEATURE); +revise_own_turn+:
60
+ # Revise on the answer to their own message with only their own turns
61
+ # after it (POST …/turn with revises, ParticipantRevise). And of the owner's,
62
+ # those a participant's route serves filtered: +working_set+ (GET
63
+ # …/working_set, the project's files only) and +images+ (POST …/images
64
+ # within their cap and quota; GET …/images/<name> only for an image
65
+ # they uploaded; their turn takes only those: ParticipantImages).
66
+ Viewer::PARTICIPANT_CAPABILITIES = %w[send stop_own_turn steer_own_turn revise_own_turn working_set images].freeze
67
+
68
+ Viewer::OWNER = Viewer.new(kind: "owner", id: nil, name: nil, can: Viewer::CAPABILITIES)
69
+ end
70
+ end
@@ -0,0 +1,132 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../working_set"
4
+ require_relative "../llm_context_view"
5
+ require_relative "../session_setting"
6
+ require_relative "../memory_paths"
7
+ require_relative "participant_view"
8
+
9
+ module Samagotchi
10
+ module Web
11
+ # The working set's routes (web.working_set), mixed into App. See
12
+ # docs/internals/working-set.md (Delivery).
13
+ module WorkingSetRoutes
14
+ # What a working-set build reads off a session: its +messages+ (symbol
15
+ # keys at the top, as paired_runs reads them), the +strategy+ its
16
+ # llm_context summary names, its folder (+cwd+) and its +pins+.
17
+ Source = Data.define(:messages, :strategy, :cwd, :pins)
18
+
19
+ private
20
+
21
+ # GET /api/sessions/:id/working_set: the working set as the model has
22
+ # it ({items:, at:, running:}, WorkingSet), what the page overlays on its rows'
23
+ # set when it seeds a session's and when the expanded view opens (a
24
+ # file changed while the session sat idle). A live worker's snapshot
25
+ # messages are the truth, as #handle_show's, else the file's; the
26
+ # strategy is the session's llm_context summary's, the folder its working
27
+ # directory. running: a turn runs. The snapshot's messages lag it, so
28
+ # the answer is then the turn's last :working_set_updated (the
29
+ # snapshot's working_set, TurnAccumulator#working_set); before the
30
+ # turn emitted one, or from an older worker without the field, it is
31
+ # the session's messages, behind that turn's events, and the page
32
+ # keeps theirs (working_set.js newerSnapshot).
33
+ # A participant's is filtered (ParticipantView.working_set: the
34
+ # project's files only).
35
+ def handle_working_set(req, id)
36
+ session = @session_class.load(id, state_dir: default_state_dir)
37
+ set = working_set_payload(id, session)
38
+ return json_response(200, set) if principal(req).owner?
39
+
40
+ viewer = participant_viewer(req, session)
41
+ # Redacted as the stream's event is (ParticipantView.event): a file name may hold a secret's shape.
42
+ shown = Redact.deep(ParticipantView.working_set(set, viewer), viewer)
43
+ json_response(200, shown.merge("running" => set[:running] == true))
44
+ end
45
+
46
+ # GET /api/sessions/:id/working_set/item?kind=&key=[&now=1]: what the
47
+ # model has of one item, its timeline (WorkingSet.timeline_of: every
48
+ # run of it, in conversation order), the reader's and the popover
49
+ # peek's. Owner only. Always built from messages, unlike the set's
50
+ # route: the worker's snapshot messages (they lag a running turn's
51
+ # events; the page asks again on its next :working_set_updated), else
52
+ # the file's. Whole texts: the tool limits bound them (read's
53
+ # truncate_at_bytes, the ToolRunner cap). +now+ (a memory): what its
54
+ # latest read would answer now and the diff (WorkingSet.memory_now).
55
+ def handle_working_set_item(req, id)
56
+ kind = req.params["kind"].to_s
57
+ key = req.params["key"].to_s
58
+ unless [WorkingSet::FILE, WorkingSet::MEMORY, WorkingSet::URL].include?(kind) && !key.strip.empty?
59
+ return error_response(400, "invalid_item", "expected kind (file, memory or url) and key")
60
+ end
61
+
62
+ session = @session_class.load(id, state_dir: default_state_dir)
63
+ source = working_set_source(bridge_get_json(id, "snapshot"), session)
64
+ timeline = WorkingSet.timeline_of(source.messages, kind: kind, key: key, strategy: source.strategy, cwd: source.cwd)
65
+ body = timeline.to_h
66
+ if req.params["now"] == "1" && kind == WorkingSet::MEMORY
67
+ body[:now] = WorkingSet.memory_now(source.messages, key: key, strategy: source.strategy, cwd: source.cwd,
68
+ root: MemoryPaths.system_dir)&.to_h
69
+ end
70
+ json_response(200, body)
71
+ end
72
+
73
+ def working_set_payload(id, session)
74
+ live = bridge_get_json(id, "snapshot")
75
+ worker_snapshot = live && live["snapshot"]
76
+ running = !worker_snapshot&.dig("current_turn").nil?
77
+ turn_set = running && running_working_set(live["snapshot"]["working_set"])
78
+ return turn_set.merge(running: true) if turn_set
79
+
80
+ idle_set = idle_working_set(worker_snapshot && worker_snapshot["idle_working_set"]) unless running
81
+ return idle_set.merge(running: false) if idle_set
82
+
83
+ source = working_set_source(live, session)
84
+ snapshot = WorkingSet.build(source.messages, cwd: source.cwd, strategy: source.strategy, pins: source.pins)
85
+ snapshot.to_h.merge(running: running)
86
+ end
87
+
88
+ # What a build from messages reads (+live+: the worker's Bridge
89
+ # snapshot answer, nil without a worker): the worker's snapshot
90
+ # messages, as #handle_show reads them, else the file's; the strategy
91
+ # the worker's llm_context summary names, else the saved chip's.
92
+ # @return [Source]
93
+ def working_set_source(live, session)
94
+ messages = live_or_saved(live && live["snapshot"], "messages") { session.messages }
95
+ # A snapshot's entries have string keys (paired_runs reads symbols).
96
+ messages = Array(messages).map { |message| message.is_a?(Hash) ? message.transform_keys(&:to_sym) : message }
97
+ llm_context = SessionSetting::LLM_CONTEXT.snapshot_value(live && live["session_state_snapshot"]) do
98
+ SessionSetting::LLM_CONTEXT.saved_summary(session, registry: host_registry)
99
+ end
100
+ Source.new(messages: messages, strategy: working_set_strategy(llm_context), cwd: session.working_directory,
101
+ pins: session.working_set_pins)
102
+ end
103
+
104
+ # The running turn's set from a worker's snapshot ({"items", "at"}), as
105
+ # {items:, at:}; nil when it has none (none emitted yet, an older worker).
106
+ def running_working_set(set)
107
+ return nil unless set.is_a?(Hash) && set["items"].is_a?(Array) && set["at"].is_a?(Integer)
108
+
109
+ { items: set["items"], at: set["at"] }
110
+ end
111
+
112
+ # The worker's between-turns working set ({"items", "at", optional
113
+ # "adds"}); nil for an older or malformed snapshot.
114
+ def idle_working_set(set)
115
+ return nil unless set.is_a?(Hash) && set["items"].is_a?(Array) && set["at"].is_a?(Integer)
116
+ return nil if set.key?("adds") && !set["adds"].is_a?(Array)
117
+
118
+ { items: set["items"], at: set["at"] }.tap do |snapshot|
119
+ snapshot[:adds] = set["adds"] if set.key?("adds")
120
+ end
121
+ end
122
+
123
+ # The strategy an llm_context summary names ("none", "stale,forget"), as
124
+ # LLMContextView takes it; none when there is no chip.
125
+ def working_set_strategy(llm_context)
126
+ named = llm_context.is_a?(Hash) ? (llm_context[:strategy] || llm_context["strategy"]).to_s : ""
127
+ layers = named.split(",").map(&:strip).reject { |layer| layer.empty? || layer == "none" }
128
+ layers.empty? ? LLMContextView::NONE : layers.map(&:to_sym)
129
+ end
130
+ end
131
+ end
132
+ end
@@ -848,16 +848,22 @@ module Samagotchi
848
848
  end
849
849
 
850
850
  # Why the participant +author+ ("p:<id>") may run no more turns, nil
851
- # while they may (and for the owner): "revoked" when participants.json
852
- # has no active entry for them (revoked, expired, never invited),
853
- # "cap_reached" once they spent their cap (its own, else the config's;
854
- # their turns and the side requests they caused, Engine#metrics). The
855
- # owner hears the cap once per cap (#cap_notice).
851
+ # while they may (and for the owner): by ParticipantTurnMode, "outdated"
852
+ # for an older chi's read-only invite (an entry, or a Verify fresh child
853
+ # saved, without "pair": this chi runs no read-only turn), "revoked" when
854
+ # participants.json has no current entry for them (revoked, expired,
855
+ # never invited) or they aren't invited to this (the owner's own Verify
856
+ # fresh child), "cap_reached" once they spent their cap (its own,
857
+ # else the config's; their turns and the side requests they caused,
858
+ # Engine#metrics). The owner hears the cap once per cap (#cap_notice).
856
859
  def participant_refusal(author)
857
860
  author = Steer.sender_author(author) or return nil
858
861
 
859
862
  entry = participant_entry(author)
860
- return "revoked" unless entry&.active?(Time.now)
863
+ case ParticipantTurnMode.of(entry: entry, session: @session, now: Time.now)
864
+ when :outdated then return "outdated"
865
+ when :revoked, :not_invited then return "revoked"
866
+ end
861
867
 
862
868
  cap = participant_cap(entry)
863
869
  return nil if participant_spend(author) < cap
@@ -875,7 +881,7 @@ module Samagotchi
875
881
 
876
882
  entry = participant_entry(author)
877
883
  name = participant_label(author, entry&.name)
878
- return format(REVOKED_STOP, name: name) if reason == "revoked"
884
+ return format(REVOKED_STOP, name: name) if %w[revoked outdated].include?(reason)
879
885
 
880
886
  format(CAP_STOP, name: name, cap: participant_cap(entry))
881
887
  end
@@ -908,23 +914,19 @@ module Samagotchi
908
914
 
909
915
  # The owner's notice that a participant reached their cap: once per
910
916
  # participant and cap (a raised cap that is reached again says so again).
911
- # A pair participant's says turns (their turns act), a review one's
912
- # comments.
913
917
  def cap_notice(author, entry, cap)
914
918
  key = [author, cap]
915
919
  return if (@cap_noticed ||= Set.new).include?(key)
916
920
 
917
921
  @cap_noticed << key
918
922
  id = Steer.participant_id(author)
919
- text = ParticipantMode.for_turn(@session, entry) == ParticipantMode::PAIR ? PAIR_CAP_NOTICE : CAP_NOTICE
920
923
  @engine.announce(type: :hook_notice, hook: Engine::PARTICIPANTS_HOOK, level: :warn, between_turns: true,
921
- text: format(text, name: participant_label(author, entry.name), cap: cap, session: participants_id[0, 8], id: id))
924
+ text: format(CAP_NOTICE, name: participant_label(author, entry.name), cap: cap,
925
+ session: participants_id[0, 8], id: id))
922
926
  end
923
927
 
924
- CAP_NOTICE = "%<name>s reached their $%<cap>.2f review cap: their comments are refused until you raise it " \
928
+ CAP_NOTICE = "%<name>s reached their $%<cap>.2f cap: their turns stop and are refused until you raise it " \
925
929
  "(chi share cap %<session>s %<id>s AMOUNT)."
926
- PAIR_CAP_NOTICE = "%<name>s reached their $%<cap>.2f cap: their turns stop and are refused until you raise it " \
927
- "(chi share cap %<session>s %<id>s AMOUNT)."
928
930
  # The reason a pair turn stopped at its participant's cap (the turn's
929
931
  # "stopped the turn: …" notice).
930
932
  CAP_STOP = "%<name>s reached their $%<cap>.2f cap"
@@ -1412,8 +1414,9 @@ module Samagotchi
1412
1414
  @turn_enqueued_id = origin.enqueued_id
1413
1415
  verdicts_before = @session.verdicts.size
1414
1416
  @reporting_turn = reports_to_parent?(origin)
1415
- # A participant's review turn merges no input (#pending_input_drain).
1416
- @participant_turn = origin.participant?
1417
+ # Who wrote the running turn (a participant's author, nil for the
1418
+ # owner's): its drain merges only their own lines (#pending_input_drain).
1419
+ @turn_author = origin.participant_author
1417
1420
  restores = @turn_flow.restores
1418
1421
  begin
1419
1422
  @checkpoint.begin_turn(prompt: prompt, turn_id: turn_args[:id], enqueued_id: origin.enqueued_id, author: turn_author_fields(origin))
@@ -1603,15 +1606,12 @@ module Samagotchi
1603
1606
  # one (steering merges text only): skipped here, not a barrier, so a
1604
1607
  # line sent after it still goes in at this boundary. Only the files
1605
1608
  # claimed below reach @merged_this_turn and the checkpoint.
1606
- # A participant's review turn takes none: the owner's lines (the
1607
- # Bridge queues them, this covers a file it didn't see) run as the
1608
- # owner's own turns after it, never inside its review boundary.
1609
- files = if @participant_turn
1610
- []
1611
- else
1612
- SessionInbox.find_new_input_files(@session_dir).sort
1613
- .reject { |input_file| SessionInbox.waits_for_turn_end?(input_file) }
1614
- end
1609
+ # A participant's turn takes only their own lines: the owner's and
1610
+ # anyone else's (the Bridge queues them, this covers a file it didn't
1611
+ # see) run as their own turns after it, never inside its boundary;
1612
+ # and the owner's turn takes no participant's (SessionInbox.turn_end_data?).
1613
+ files = SessionInbox.find_new_input_files(@session_dir).sort
1614
+ .reject { |input_file| SessionInbox.waits_for_turn_end?(input_file, running_author: @turn_author) }
1615
1615
  merged = files.filter_map do |input_file|
1616
1616
  claimed_file = SessionInbox.claim_input_file(input_file)
1617
1617
  next unless claimed_file
@@ -1624,6 +1624,13 @@ module Samagotchi
1624
1624
  next
1625
1625
  end
1626
1626
  begin
1627
+ # A participant's line gets the checks their queued turn gets at
1628
+ # its start (#participant_refusal: the link, the cap): refused,
1629
+ # it is taken off as that turn would be, and never merged.
1630
+ if (author = input.origin.participant_author) && (reason = participant_refusal(author))
1631
+ refuse_participant_turn(input.origin, reason)
1632
+ next
1633
+ end
1627
1634
  prompt = input.prompt.to_s.strip
1628
1635
  # Which turn took it, on disk before its file goes (a wait for
1629
1636
  # it reads this, not an inference: ReplyWait::Poll#follow_merge).
@@ -1660,10 +1667,10 @@ module Samagotchi
1660
1667
  end
1661
1668
 
1662
1669
  # Whether a person's message waits that the next boundary merges
1663
- # (SessionInbox.steer_waiting?): a running task_wait ends on it. Never
1664
- # in a participant's turn, whose drain merges none.
1670
+ # (SessionInbox.steer_waiting?): a running task_wait ends on it. In a
1671
+ # participant's turn only their own.
1665
1672
  def input_waiting?
1666
- !@participant_turn && SessionInbox.steer_waiting?(@session_dir)
1673
+ SessionInbox.steer_waiting?(@session_dir, running_author: @turn_author)
1667
1674
  end
1668
1675
 
1669
1676
  # A participant's author fields (Engine#author_fields), {} for the owner.
@@ -240,13 +240,14 @@ module Samagotchi
240
240
  # for it, --new --wait, names it) and who sent it when no person did
241
241
  # (Session#first_turn_client_id: a delegating parent, a schedule). A
242
242
  # delegate child's task is its parent's even without the field (one
243
- # spawned by an older chi, or by a caller that names no sender). NONE
244
- # for a person's session from before either field.
243
+ # spawned by an older chi, or by a caller that names no sender). Else
244
+ # the owner's (ClientId::FIRST_PROMPT): a person's session saves no
245
+ # sender for its first prompt, nor did one from before either field.
245
246
  # @return [TurnOrigin]
246
247
  def initial_origin
247
248
  client_id = @session.first_turn_client_id
248
249
  client_id ||= ClientId.delegate(@session.parent_id) if @session.delegate? && @session.parent_id
249
- TurnOrigin.of(client_id: client_id, enqueued_id: @session.first_turn_id)
250
+ TurnOrigin.of(client_id: client_id || ClientId::FIRST_PROMPT, enqueued_id: @session.first_turn_id)
250
251
  end
251
252
 
252
253
  # The first prompt as a command (queued): the session was saved as
@@ -8,6 +8,7 @@ require_relative "compat"
8
8
  require_relative "worker_sidecar"
9
9
  require_relative "bridge_client"
10
10
  require_relative "chi_invocation"
11
+ require_relative "session_paths"
11
12
 
12
13
  module Samagotchi
13
14
  # Whether a session's worker can be reached, and how: the one question
@@ -145,6 +146,22 @@ module Samagotchi
145
146
  Live.new(sidecar: sidecar, owner: owner)
146
147
  end
147
148
 
149
+ # The features of session +session_id+'s live worker (WorkerSidecar#features,
150
+ # a read-only look), nil without a live worker or when it can't be read:
151
+ # what chi web and chi share check before they send it a participant's
152
+ # turn or invite anyone to it.
153
+ # @param state_dir [String, nil] the sessions' state dir (nil: the default)
154
+ # @return [Array<String>, nil]
155
+ def live_features(session_id, state_dir: nil)
156
+ return nil if session_id.to_s.empty?
157
+
158
+ dir = SessionPaths.session_dir(session_id, state_dir: state_dir || SessionPaths.default_state_dir)
159
+ link = resolve(dir, unlink: false)
160
+ link.live? ? link.features : nil
161
+ rescue StandardError # degrade-ok: an unreadable sidecar reads as no live worker (the callers' fallback)
162
+ nil
163
+ end
164
+
148
165
  # @return [BridgeClient, nil] a client for the session's Live worker
149
166
  def client(session_id, session_dir:, unlink: true, host: BridgeClient::HOST, read_timeout: BridgeClient::READ_TIMEOUT)
150
167
  link = resolve(session_dir, unlink: unlink)