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
@@ -16,46 +16,46 @@
16
16
  // rows so far go there (chi's line before its own question or approval as a
17
17
  // lead-in bubble), and the stage docks the card alone (the asking form)
18
18
  // until the answer. The step limit's card goes under its ended turn. The
19
- // rules are pure in stage_model.js; the live slots are drawn by
20
- // stage_slots.js, the cap and the held height are stage_frame.js's, the
21
- // in-use watch that times the hand-off is stage_watch.js's, the docked card
22
- // and the fold are stage_asking.js's, and the waiting cards as data are
23
- // stage_waiting.js's.
19
+ // rules are pure in stage_model.js; this module wires the stage (refresh,
20
+ // the lifecycle, the event API) and each feature is a module of its own
21
+ // (web-frontend.md, The stage's modules): the nodes are stage_dom.js's, the
22
+ // live slots stage_slots.js's, the cap and the held height stage_frame.js's,
23
+ // the in-use watch that times the hand-off stage_watch.js's, the hand-off
24
+ // itself stage_handoff.js's, the cloud stage_cloud.js's, the docked card and
25
+ // the fold stage_asking.js's, its pager stage_pager.js's, the waiting cards
26
+ // as data stage_waiting.js's, the answer's reading stage_answer.js's, the
27
+ // quiet signal stage_stall.js's and its clocks stage_quiet_clock.js's. A new
28
+ // stage feature goes into its own stage_<feature>.js, wired here in a few
29
+ // lines; each module resets itself from emptySlots.
24
30
  import { createTurnBlock } from "./turn_block.js";
25
- import { openAfterEnd } from "./block_open.js";
26
- import { chipLabelText, flashOf, handOffDue, handOffOrder, headlineOf, isPlain, liveSlots, MARKS, foldedRowText, phaseLabel, placeFor, placeKind, QUEUED_CHILDREN, stepSlotText } from "./stage_model.js";
31
+ import { chipLabelText, flashOf, handOffDue, headlineOf, isPlain, liveSlots, MARKS, foldedRowText, phaseLabel, placeFor, placeKind, QUEUED_CHILDREN, stepSlotText } from "./stage_model.js";
27
32
  import { createStageSlots, node } from "./stage_slots.js";
33
+ import { createStageDom } from "./stage_dom.js";
34
+ import { createStageAnswer } from "./stage_answer.js";
35
+ import { createStagePager } from "./stage_pager.js";
36
+ import { createStageCloud } from "./stage_cloud.js";
37
+ import { createStageHandoff } from "./stage_handoff.js";
38
+
39
+ // How long the stage slides away at the hand-off (dock_reserve.js).
40
+ export { FOLD_MS } from "./stage_handoff.js";
28
41
  import { createStageFrame, reducedMotion } from "./stage_frame.js";
29
42
  import { createInUseWatch } from "./stage_watch.js";
30
- import { createStageStall, GRACE_MS, stallState } from "./stage_stall.js";
43
+ import { createStageStall } from "./stage_stall.js";
44
+ import { createStageQuietClock } from "./stage_quiet_clock.js";
31
45
  import { formatDuration } from "./timing.js";
32
- import { easeScrollTo } from "./scroll.js";
33
46
  import { createWorkingSetRail } from "./working_set_rail.js";
34
- import { moveToHistoryEnd } from "./history_end.js";
35
- import { turnElements } from "./turn_range.js";
36
47
  import { createOpenedTurn } from "./stage_opened.js";
37
48
  import { askingRefs } from "./turn_model.js";
38
49
  import { ASKS, PENDING } from "./stage_cards.js";
39
50
  import { createStageAsking } from "./stage_asking.js";
40
51
  import { cardIn, createStageWaiting, isApprovalCard, isBlocking, waitingKey, waitingKind } from "./stage_waiting.js";
41
52
  import { stampRows } from "./working_set_view.js";
53
+ import { createOwnerWait } from "./stage_owner_wait.js";
42
54
 
43
55
  // The turn-end read (markdown, answer_display) settles within this, or the
44
56
  // hand-off stops waiting for it.
45
57
  const SETTLE_CAP_MS = 8000;
46
- export const FOLD_MS = 420;
47
58
  const FLASH_MS = 4000;
48
- // The answer reads from its top: at the end it is scrolled to unless this
49
- // much of it (about three lines) shows already.
50
- const ANSWER_SEEN = 72;
51
- // A short answer cut by less than this at its foot isn't scrolled to.
52
- const ANSWER_CUT = 12;
53
- // An answered card's one line.
54
- const ANSWERED_LINE = 40;
55
- // That line brought back sits this far under the scroller's edge.
56
- const ANSWERED_GAP = 8;
57
- // What opens a turn in the history (reload_check.js's OPENING).
58
- const OPENING = "[data-turn-id], [data-opening-id]";
59
59
 
60
60
  // +historyNearBottom+: the history's own follow check (the hand-off scrolls
61
61
  // the history only for a reader at its end). +dockEl+ and +centerEl+ size
@@ -68,53 +68,25 @@ const OPENING = "[data-turn-id], [data-opening-id]";
68
68
  // status row's Stop turn (a promise of the cancel request; the button is
69
69
  // disabled until it settles); null: no Stop turn. +onHandedOff+({pieces,
70
70
  // immediate}): a turn's pieces reached the history (reload_check.js).
71
- // The quiet signal's inputs (stage_stall.js): +clock+() the page's time,
71
+ // The quiet signal's inputs (stage_quiet_clock.js): +clock+() the page's time,
72
72
  // +streamLive+() the session's stream is open, +timingNote+() what the turn
73
73
  // waits on (live_timing.js note), +serverGen+() a joiner's generation.
74
+ // +canCancel+() whether this turn's Stop turn shows (a participant's own
75
+ // turn only); +ownerWait+() the kind a participant's view waits for the
76
+ // owner on, or null (stage_owner_wait.js).
74
77
  export function createStageView({
75
78
  historyEl, isNearBottom: historyNearBottom, sawText, sessionId, dockEl, centerEl, taskStop = null,
76
79
  renderMarkdown, linkRefs, workingSet = null, onCancel = null, onHandedOff = null,
77
80
  clock = () => Date.now(), streamLive = () => true, timingNote = () => "", serverGen = () => null,
81
+ canCancel = () => true, ownerWait = () => null,
78
82
  }) {
79
- // The dock becomes a clear column: the stage's card, then the composer's
80
- // (its rows moved into .dock-card, which wears the glass and the focus ring).
81
- document.body.classList.add("stage-view");
82
- const card = node("div", "dock-card composer-card");
83
- card.append(...dockEl.children);
84
- dockEl.appendChild(card);
85
- const stage = node("section", "turn-stage");
86
- stage.id = "turnStage";
87
- stage.hidden = true;
88
- stage.setAttribute("aria-label", "Running turn");
89
- dockEl.prepend(stage);
90
-
91
- const status = node("div", "ts-status", stage);
92
- const mark = node("span", "ts-mark", status);
93
- const phaseEl = node("span", "ts-phase", status);
94
- // Two cards docked: one at a time, "‹ 1 of 2 ›" (stage_waiting.js shown).
95
- const pager = node("span", "ts-pager", status);
96
- pager.hidden = true;
97
- const pagePrev = node("button", "ts-page", pager);
98
- pagePrev.type = "button";
99
- pagePrev.textContent = "‹";
100
- pagePrev.setAttribute("aria-label", "The card before");
101
- const pageLabel = node("span", "ts-page-label", pager);
102
- const pageNext = node("button", "ts-page", pager);
103
- pageNext.type = "button";
104
- pageNext.textContent = "›";
105
- pageNext.setAttribute("aria-label", "The next card");
106
- const stepEl = node("span", "ts-step", status);
107
- const timingSlot = node("span", "ts-timing", status);
108
- const miniTool = node("span", "ts-mini", status);
109
- const defer = node("span", "ts-defer", status);
110
- defer.textContent = "moves up when you leave";
111
- // Stop turn, at the row's right edge beside the fold: while the turn
112
- // hasn't ended (refresh), in a collapsed stage too (the row stays).
113
- const cancel = node("button", "ts-cancel", status);
114
- cancel.type = "button";
115
- cancel.title = "Cancel the running turn, whoever sent it";
116
- cancel.textContent = "Stop turn";
117
- cancel.hidden = true;
83
+ // The stage's nodes (stage_dom.js), then their behaviour here and in the
84
+ // stage's modules.
85
+ const {
86
+ stage, status, mark, phaseEl, pager, pagePrev, pageLabel, pageNext, stepEl, timingSlot, miniTool, cancel, fold,
87
+ scroller, promptSlot, live, headline, ownerWaitLine, now, trail, extras, asksText, answer, tail, cloudBody, holdEl,
88
+ cloud, chip, chipLabel, ticks, queuedSlot,
89
+ } = createStageDom(dockEl);
118
90
  cancel.addEventListener("click", async () => {
119
91
  if (!onCancel || cancel.disabled) return;
120
92
  cancel.disabled = true;
@@ -124,19 +96,7 @@ export function createStageView({
124
96
  cancel.disabled = false;
125
97
  }
126
98
  });
127
- const fold = node("button", "ts-fold", status);
128
- fold.type = "button";
129
-
130
- const scroller = node("div", "ts-scroll", stage);
131
- const promptSlot = node("div", "ts-prompt", scroller);
132
- promptSlot.title = "Show the whole prompt";
133
- const live = node("div", "ts-live", scroller);
134
- const headline = node("div", "ts-headline", live);
135
- headline.tabIndex = 0;
136
- headline.setAttribute("role", "button");
137
- headline.title = "Show this step's reasoning";
138
- const now = node("div", "ts-now", live);
139
- const trail = node("div", "ts-trail", live);
99
+ const drawOwnerWait = createOwnerWait({ stage, line: ownerWaitLine, phaseEl });
140
100
  // The working set's rail: out of the live slots' flow on their right (a
141
101
  // row under the trail in a narrow stage), so it never sets their height.
142
102
  const rail = workingSet
@@ -147,36 +107,12 @@ export function createStageView({
147
107
  // The server's states change the lines (never the fold): a redraw.
148
108
  workingSet.onServer(() => scheduleRefresh());
149
109
  }
150
- const extras = node("div", "ts-extras", scroller);
151
- // While check-in's card is docked: the live step's text, streamed under
152
- // the card (the steps are in the history then). The answer's own slot
153
- // takes it over at the turn's end (placeAnswer, in one task).
154
- const asksText = node("div", "ts-asks-text", scroller);
155
- asksText.hidden = true;
156
- const answer = node("div", "ts-answer", scroller);
157
- const tail = node("div", "ts-tail", scroller);
158
- // The cloud's steps, the scroller's last slot, right above the chip.
159
- const cloudBody = node("div", "ts-cloud-body", scroller);
160
- cloudBody.hidden = true;
161
- // A 1 px mark at the tallest content's end (absolute: out of the flow),
162
- // so the scroll extent never drops while the turn runs: content that
163
- // shrinks never lowers a scrolled reader's scrollTop (the browser would
164
- // clamp it, moving everything in sight), even in the layout before
165
- // fitFrame runs.
166
- const holdEl = node("div", "ts-hold", scroller);
167
- holdEl.setAttribute("aria-hidden", "true");
168
- // The footer: the chip, pinned to the stage's bottom edge as the status
169
- // row is to its top.
170
- const cloud = node("div", "ts-foot", stage);
171
- const chip = node("button", "ts-chip", cloud);
172
- chip.type = "button";
173
- const chipLabel = node("span", "ts-chip-label", chip);
174
- const ticks = node("span", "ts-ticks", chip);
175
- // Below the turn's card: the lines queued for after it (placeKind
176
- // "queued"), each to run as a turn of its own; they hand off last.
177
- const queuedSlot = node("div", "ts-queued", stage);
178
- // The quiet signal: its span in the now row, its head after the timing line.
179
- const stall = createStageStall({ stage, nowEl: now, timingSlot });
110
+ // The quiet signal: its span in the now row, its head after the timing
111
+ // line (stage_stall.js), its clocks (stage_quiet_clock.js).
112
+ const quiet = createStageQuietClock({
113
+ stall: createStageStall({ stage, nowEl: now, timingSlot }), clock, streamLive, timingNote, serverGen,
114
+ held: () => (holding ? view.current().turn : null), ended: () => !!endKind, pending: () => pendingCard(),
115
+ });
180
116
  const slotsView = createStageSlots({
181
117
  headline, now, trail, ticks, taskStop,
182
118
  gens: () => view.current().turn?.gens,
@@ -195,21 +131,14 @@ export function createStageView({
195
131
  let thinkingLine = null; // { step, text } from the live step's ticker
196
132
  let flash = null; // { kind, text, until } a notice or nudge in the trail
197
133
  let flashTimer = null; // its end's redraw
198
- let cloudOpen = false;
199
134
  let rafPending = false;
200
135
  // A turn the page knows runs (the session says running) with none in the
201
136
  // stage to show (a join without its current_turn): the status row alone,
202
137
  // so Stop turn stays in reach (runningWithoutTurn).
203
138
  let bare = false;
204
- let foldTimer = null;
205
139
  // The raised cap (stage_frame.js) of an approval too tall for the stage
206
140
  // (raiseForApproval), kept until the next step (releaseHeldWait).
207
141
  let roomCap = false;
208
- // The quiet signal's clocks: the turn's last event, since when the stream
209
- // isn't live (null: it is), the grace after the page shows again.
210
- let lastEventAt = 0;
211
- let downSince = null;
212
- let graceUntil = 0;
213
142
  // Where the turn's prompt and block are: the stage (its prompt slot, the
214
143
  // cloud or a plain turn's answer slot), or the history once the turn is
215
144
  // opened there (+opened+). Read through blockPlace and promptEl, never
@@ -225,6 +154,12 @@ export function createStageView({
225
154
  else delete stage.dataset.block;
226
155
  }
227
156
 
157
+ // The answer read from its top, an answered card's line, the prompt's
158
+ // first words (stage_answer.js).
159
+ const { showAnswerTop, settleAnsweredCard, promptWords } = createStageAnswer({
160
+ stage, scroller, answer, extras, tail, frame: () => frame, promptEl: () => promptEl,
161
+ });
162
+
228
163
  // The turn opened in the history while the model's own question waits
229
164
  // (stage_opened.js): its reasons, and the moves there.
230
165
  const opener = createOpenedTurn({
@@ -259,19 +194,23 @@ export function createStageView({
259
194
  });
260
195
 
261
196
  // The cards that wait in the stage, as data (stage_waiting.js): the
262
- // docked card, its fold, what blocks, the approval's cap read them.
263
- const waiting = createStageWaiting();
197
+ // docked card, its fold, what blocks, the approval's cap read them. The
198
+ // fold of the asking card is this tab's own, kept per session.
199
+ const waiting = createStageWaiting({ sessionId });
264
200
  let dockedAtLastRefresh = [];
265
201
  // The asking form and the stage's fold (stage_asking.js).
266
202
  const ask = createStageAsking({
267
203
  stage, extras, tail, scroller, fold, view, opener, frame: () => frame, isOpened: () => opened, setOpened,
268
- isEnded: () => !!endKind, promptEl: () => promptEl, setCloud, scheduleRefresh, pendingCard, waiting,
204
+ isEnded: () => !!endKind, promptEl: () => promptEl, setCloud: (open) => cloudView.setCloud(open), scheduleRefresh, pendingCard, waiting,
269
205
  });
270
206
  const {
271
207
  askingGen, asksOf, askedFor, askedText, docks, leadInText, openTurn, settleCards, askingCard, cardOf, foldedText, markOwnToggles,
272
208
  isCollapsed, drawCollapsed, applyCollapsed, setCollapsed,
273
209
  } = ask;
274
210
 
211
+ // The docked cards' pager (stage_pager.js).
212
+ const pagerView = createStagePager({ pager, pagePrev, pageLabel, pageNext, extras, waiting, cardOf, askingCard, refresh, frame: () => frame });
213
+
275
214
  // ── slots ────────────────────────────────────────────────────────────────
276
215
 
277
216
  // "history" (an opened turn's), "cloud", "answer" (a plain turn's in
@@ -314,48 +253,6 @@ export function createStageView({
314
253
  return !endKind && waiting.count({ asks: true }) > waiting.count();
315
254
  }
316
255
 
317
- // One docked card at a time: the others wait behind it (.ts-behind) and
318
- // the pager says where the shown one is.
319
- function drawDocked(shownCard) {
320
- const keys = waiting.docked();
321
- for (const key of keys) cardOf(key)?.classList.toggle("ts-behind", !!shownCard && cardOf(key) !== shownCard);
322
- for (const el of extras.querySelectorAll(":scope > .ts-behind")) if (!keys.includes(waitingKey(el))) el.classList.remove("ts-behind");
323
- // Focus in a card that went behind (a new card came in front of it):
324
- // to the card that shows now.
325
- const focused = document.activeElement;
326
- if (shownCard && focused && [...extras.querySelectorAll(":scope > .ts-behind")].some((el) => el.contains(focused))) focusIn(shownCard);
327
- const at = shownCard ? keys.indexOf(waitingKey(shownCard)) : -1;
328
- pager.hidden = keys.length < 2 || at < 0;
329
- if (pager.hidden) return;
330
- const label = `${at + 1} of ${keys.length}`;
331
- if (pageLabel.textContent !== label) pageLabel.textContent = label;
332
- pagePrev.disabled = at === 0;
333
- pageNext.disabled = at === keys.length - 1;
334
- // A pager button the page just disabled keeps no focus: the other one has it.
335
- if (document.activeElement === pagePrev && pagePrev.disabled) pageNext.focus();
336
- else if (document.activeElement === pageNext && pageNext.disabled) pagePrev.focus();
337
- }
338
-
339
- // +card+'s first control takes the focus (its summary is out of the tab
340
- // order while it waits), else the card.
341
- function focusIn(card) {
342
- const control = card.querySelector('input:not([type="hidden"]), textarea, select, button:not([disabled]), a[href]');
343
- if (control) control.focus();
344
- else {
345
- if (!card.hasAttribute("tabindex")) card.tabIndex = -1;
346
- card.focus();
347
- }
348
- }
349
- for (const [button, by] of [[pagePrev, -1], [pageNext, 1]]) {
350
- button.addEventListener("click", (e) => {
351
- e.stopPropagation();
352
- waiting.page(by);
353
- refresh();
354
- const shown = askingCard();
355
- if (shown) frame.afterGrowth(() => frame.reveal(shown.parentElement));
356
- });
357
- }
358
-
359
256
  // The text under a docked check-in card, patched only when it changes.
360
257
  function drawAsksText(text) {
361
258
  if (asksText.textContent !== text) asksText.textContent = text;
@@ -397,7 +294,7 @@ export function createStageView({
397
294
  stage.classList.toggle("asking-form", askingForm);
398
295
  // Its fold (stage_asking.js trackFold): that card's only.
399
296
  ask.trackFold();
400
- drawDocked(asking);
297
+ pagerView.drawDocked(asking);
401
298
  const collapsed = isCollapsed();
402
299
  drawCollapsed();
403
300
  const slots = liveSlots(turn, { narration, thinking, pending: blocked || asksCard(), ended: endKind, emptyAnswer });
@@ -415,7 +312,7 @@ export function createStageView({
415
312
  if (!blocked && wasWaiting) stage.style.setProperty("--live-held", `${live.offsetHeight}px`);
416
313
  stage.classList.toggle("plain", plain);
417
314
  stage.classList.toggle("ended", !!endKind);
418
- cancel.hidden = !onCancel || !!endKind;
315
+ cancel.hidden = !onCancel || !canCancel() || !!endKind;
419
316
  stage.classList.toggle("waiting", blocked);
420
317
  if (!blocked && wasWaiting) settleAnsweredCard();
421
318
  if (answeredWithCardLeft && !blocked) releaseHeldWait();
@@ -427,13 +324,14 @@ export function createStageView({
427
324
  const asksFront = !!asking && !blocked && !endKind && waiting.get(waiting.front())?.kind === "asks";
428
325
  const streaming = asksFront ? liveSlots(turn).phase : "";
429
326
  phaseEl.textContent = phaseLabel({ phase: slots.phase, asksFront, streaming });
327
+ drawOwnerWait(endKind ? null : ownerWait());
430
328
  stage.classList.toggle("asks-live", asksFront);
431
329
  drawAsksText(asksFront && gen?.text && !gen.answer ? String(gen.text).trim() : "");
432
330
 
433
331
  slotsView.drawHeadline(slots.headline);
434
332
  raiseForApproval(blocked);
435
333
  slotsView.drawNow(slots.tool, slots.quiet);
436
- drawStall(clock(), blocked);
334
+ quiet.tick(clock(), blocked);
437
335
  slotsView.drawTrail(slots.trail, flash);
438
336
  slotsView.drawTicks(slots.ticks);
439
337
  if (rail) {
@@ -469,7 +367,7 @@ export function createStageView({
469
367
  // The chip: a block in the cloud. An opened turn's steps line in the
470
368
  // history says the count: none here.
471
369
  cloud.hidden = place !== "cloud";
472
- cloudBody.hidden = !cloudOpen || cloud.hidden;
370
+ cloudBody.hidden = !cloudView.isOpen() || cloud.hidden;
473
371
  // Running: the step it is on. Ended: the count, unless the chip says it.
474
372
  stepEl.textContent = stepSlotText({ step: slots.step, ended: !!endKind, chipHidden: cloud.hidden });
475
373
  promptSlot.hidden = !promptSlot.firstChild;
@@ -478,29 +376,6 @@ export function createStageView({
478
376
  tail.hidden = !tail.firstChild;
479
377
  }
480
378
 
481
- // A card stopped waiting and its slot has its cap and own scroll again: the
482
- // slot's end shows (the answered card's line). The stage's scroll stays, so
483
- // nothing moves at the tap (the frame holds, and the blank goes at the next
484
- // step), unless the reader had scrolled down the tall card past its line:
485
- // then the line comes back, to the view's top and no further.
486
- function settleAnsweredCard() {
487
- const slot = tail.firstChild ? tail : extras;
488
- if (!slot.firstChild) return;
489
- slot.scrollTop = slot.scrollHeight;
490
- const end = slot.offsetTop + slot.offsetHeight;
491
- // Strictly below: offsetTop/offsetHeight are rounded, so a scroll exactly
492
- // at the line's edge can leave it a fraction of a pixel above the view.
493
- if (scroller.scrollTop < end - ANSWERED_LINE) return;
494
- scroller.scrollTop = Math.max(0, Math.min(slot.lastElementChild.offsetTop, end - ANSWERED_LINE) - ANSWERED_GAP);
495
- }
496
-
497
- // The prompt's first words (the working set's session tags say whose
498
- // turn a step was).
499
- function promptWords() {
500
- const text = (promptEl?.querySelector(".user-message")?.textContent || "").replace(/\s+/g, " ").trim();
501
- return text.length > 60 ? `${text.slice(0, 59)}\u2026` : text;
502
- }
503
-
504
379
  // The next step after a blocking card's answer, or an answer that leaves
505
380
  // another docked card in front: the frame shrinks (stage_frame.js
506
381
  // forgetContent; grow-only otherwise). What the wait held goes together:
@@ -518,31 +393,6 @@ export function createStageView({
518
393
  frame.forgetContent();
519
394
  }
520
395
 
521
- // ── the quiet signal (stage_stall.js) ─────────────────────────────────────
522
-
523
- // At +nowMs+ (the tick's, a refresh's): what the turn's silence says.
524
- function drawStall(nowMs, blocked = pendingCard()) {
525
- const { turn } = view.current();
526
- if (!holding || !turn) return;
527
- if (streamLive()) downSince = null;
528
- else downSince ??= nowMs;
529
- stall.draw(stallState({
530
- turn, lastEventAt, nowMs, blocked, note: timingNote() || "", ended: !!endKind, graceUntil, serverGen: serverGen(),
531
- streamDownMs: downSince === null ? null : nowMs - downSince,
532
- }));
533
- }
534
-
535
- // A turn event arrived (a replay's too): the silence starts over, and a
536
- // stall shown clears at once.
537
- function alive() {
538
- lastEventAt = clock();
539
- if (stall.level()) drawStall(lastEventAt);
540
- }
541
-
542
- document.addEventListener("visibilitychange", () => {
543
- if (!document.hidden) graceUntil = clock() + GRACE_MS;
544
- });
545
-
546
396
  // One line in the trail for FLASH_MS, ahead of the calls (a newer flash
547
397
  // takes its place).
548
398
  function flashTrail(what) {
@@ -553,58 +403,13 @@ export function createStageView({
553
403
  flashTimer = setTimeout(scheduleRefresh, FLASH_MS + 50);
554
404
  }
555
405
 
556
- // The answer reads from its top (no tail pin in the stage): scrolled to
557
- // (eased, once the stage has grown for it) when its top is out of sight
558
- // or only its first line shows.
559
- function showAnswerTop() {
560
- frame.afterGrowth(() => {
561
- if (!answer.firstChild || answer.hidden || stage.hidden) return;
562
- const top = answer.offsetTop;
563
- const seen = scroller.scrollTop + scroller.clientHeight - top;
564
- if (top < scroller.scrollTop || seen < Math.min(answer.offsetHeight - ANSWER_CUT, ANSWER_SEEN)) frame.easeStage(Math.max(0, top - 8));
565
- });
566
- }
567
-
568
406
  // ── collapse, cloud, peek ────────────────────────────────────────────────
569
407
 
570
- function setCloud(open) {
571
- cloudOpen = open;
572
- cloudBody.hidden = !open || cloud.hidden;
573
- // Closed by hand: its room isn't held (the stage shrinks back to what
574
- // shows).
575
- if (!open) frame.forgetContent();
576
- cloud.classList.toggle("open", open);
577
- stage.classList.toggle("cloud-open", open);
578
- chip.setAttribute("aria-expanded", String(open));
579
- frame.fitCap();
580
- // The steps open right above the chip: in sight (the answer and the
581
- // cards above them scroll up), once the stage has grown for them.
582
- if (open) frame.afterGrowth(() => frame.reveal(cloudBody));
583
- }
584
-
585
- // Open the cloud at step +i+ (a hand toggle, as a click on its row), its
586
- // reasoning too with +thinking+.
587
- function openStep(i, { thinking = false } = {}) {
588
- const step = view.step(i);
589
- if (!step) return;
590
- // An opened turn's step is in the history: shown there.
591
- if (opened) {
592
- if (!step.el.open) step.summary.click();
593
- opener.reveal(step.el);
594
- return;
595
- }
596
- if (isCollapsed()) setCollapsed(false);
597
- setCloud(true);
598
- if (!step.el.open) step.summary.click();
599
- const wrap = step.thinkingWrap;
600
- if (thinking && wrap && !wrap.open) wrap.querySelector(":scope > summary")?.click();
601
- requestAnimationFrame(() => step.el.scrollIntoView({ block: "nearest" }));
602
- }
603
-
604
- function peek() {
605
- const { turn } = view.current();
606
- if (turn?.gens.length) openStep(turn.gens.length - 1, { thinking: true });
607
- }
408
+ // The cloud and its toggles (stage_cloud.js).
409
+ const cloudView = createStageCloud({
410
+ stage, cloud, cloudBody, chip, headline, view, opener, frame: () => frame, isOpened: () => opened, isCollapsed, setCollapsed,
411
+ reveal: () => ask.reveal(), isHolding: () => holding, isFolding: () => handoff.isFolding(), finishFold: () => handoff.finishFold(),
412
+ });
608
413
 
609
414
  fold.addEventListener("click", (e) => {
610
415
  e.stopPropagation();
@@ -613,33 +418,6 @@ export function createStageView({
613
418
  status.addEventListener("click", (e) => {
614
419
  if (isCollapsed() && !e.target.closest("button, a")) setCollapsed(false);
615
420
  });
616
- chip.addEventListener("click", (e) => {
617
- const tick = e.target.closest(".ts-tick");
618
- if (tick) openStep(Number(tick.dataset.step));
619
- else setCloud(!cloudOpen);
620
- });
621
- headline.addEventListener("click", peek);
622
- // A ref's "show in turn" (refs.js) names a row in the cloud: open the
623
- // stage and the cloud to it, as a hand toggle would.
624
- // A collapsed stage opens for this turn only: the kept choice
625
- // (chi_stage_collapsed) is left alone and comes back at the release.
626
- // The row's scroll into view (refs.js) waits for the stage's growth.
627
- // A row of a turn sliding away is no longer the stage's: it goes into the
628
- // history first (finishFold), and refs.js finds it there.
629
- stage.addEventListener("ref:reveal", (e) => {
630
- if (!cloudBody.contains(e.target)) return;
631
- if (folding) return finishFold();
632
- if (!holding) return;
633
- if (isCollapsed()) ask.reveal();
634
- setCloud(true);
635
- if (e.detail) e.detail.until = new Promise((resolve) => frame.afterGrowth(resolve));
636
- });
637
- headline.addEventListener("keydown", (e) => {
638
- if (e.key === "Enter" || e.key === " ") {
639
- e.preventDefault();
640
- peek();
641
- }
642
- });
643
421
  promptSlot.addEventListener("click", (e) => {
644
422
  if (e.target.closest("button, a, img")) return;
645
423
  if (window.getSelection()?.isCollapsed === false) return;
@@ -653,7 +431,7 @@ export function createStageView({
653
431
  observed: [status, promptSlot, live, extras, asksText, answer, tail, cloudBody, cloud, queuedSlot],
654
432
  isHolding: () => holding,
655
433
  isCollapsed,
656
- isCloudOpen: () => cloudOpen,
434
+ isCloudOpen: () => cloudView.isOpen(),
657
435
  isRoomCap: () => roomCap,
658
436
  waitingCard: () => (stage.classList.contains("waiting") ? cardOf(waiting.front()) : null),
659
437
  });
@@ -666,20 +444,13 @@ export function createStageView({
666
444
  alsoWatched: () => opener.watched(),
667
445
  watching: () => holding && !!endKind,
668
446
  onTick: ({ inUse, quietSinceMs, nowMs }) => handOffDue({ ended: true, pending: !settled || pendingCard(), inUse, quietSinceMs, nowMs }),
669
- onDue: () => handOff(),
447
+ onDue: () => handoff.handOff(),
670
448
  });
671
449
 
672
450
  // ── show, clear, hand off ────────────────────────────────────────────────
673
451
 
674
- // A fold cut short (the stage shows again): the slide's class goes.
675
- function cancelFold() {
676
- clearTimeout(foldTimer);
677
- foldTimer = null;
678
- stage.classList.remove("leaving");
679
- }
680
-
681
452
  function show() {
682
- cancelFold();
453
+ handoff.cancelFold();
683
454
  const hidden = stage.hidden;
684
455
  stage.hidden = false;
685
456
  applyCollapsed();
@@ -709,8 +480,7 @@ export function createStageView({
709
480
  cancel.hidden = true;
710
481
  chipLabel.textContent = "";
711
482
  slotsView.reset();
712
- stall.reset();
713
- downSince = null;
483
+ quiet.reset();
714
484
  rail?.reset();
715
485
  promptSlot.classList.remove("full");
716
486
  thinkingLine = null;
@@ -722,9 +492,9 @@ export function createStageView({
722
492
  waiting.reset();
723
493
  dockedAtLastRefresh = [];
724
494
  miniTool.classList.remove("ts-mini-question");
725
- setCloud(false);
495
+ cloudView.reset();
726
496
  stage.classList.remove("plain", "ended", "waiting", "held-wait", "in-use", "asking-form", "asks-live");
727
- pager.hidden = true;
497
+ pagerView.reset();
728
498
  stage.style.removeProperty("--live-held");
729
499
  }
730
500
 
@@ -746,7 +516,7 @@ export function createStageView({
746
516
  // The stage empty and hidden, its frame free.
747
517
  function clearStage() {
748
518
  dropBare();
749
- cancelFold();
519
+ handoff.cancelFold();
750
520
  emptySlots();
751
521
  stage.hidden = true;
752
522
  frame.frameFree();
@@ -759,91 +529,12 @@ export function createStageView({
759
529
  clearStage();
760
530
  }
761
531
 
762
- // The hand-off: everything the stage holds goes into the history's end in
763
- // the turn block's order (prompt, block (chronological; its outline open,
764
- // else collapsed), extras, answer, timing line, then the end line and
765
- // what came after the end). The turn is released at once; the stage
766
- // slides away under the composer still drawn (a transform: nothing on the
767
- // page moves; app.js eases the history's bottom reserve meanwhile), then
768
- // the pieces arrive in the history and it follows a reader who was at its
769
- // end. Not with +immediate+ (another turn takes the stage at once), a
770
- // reduced motion, a hidden page or a collapsed stage: all at once.
771
- let folding = null; // { pieces, block, endKind, wasAtEnd, animate } while it slides
772
- function handOff({ immediate = false } = {}) {
773
- if (folding && immediate) finishFold();
774
- if (!holding || !endKind) return;
775
- const { blockEl } = view.current();
776
- view.chronological();
777
- const place = blockPlace(blockEl);
778
- // A finished turn's block draws as a reload draws it.
779
- if (blockEl) delete blockEl.dataset.openedTurn;
780
- // The block moves with the pieces from the cloud; an opened turn's is
781
- // in the history already.
782
- const block = place === "cloud" ? blockEl : null;
783
- // The turn's set joins the page's turns (a plain one's too: the turns
784
- // keep their places); its footprint takes the block's summary line,
785
- // wherever the block is.
786
- const footprint = workingSet ? workingSet.handOff(view.current().turn, promptWords()) : null;
787
- if ((place === "cloud" || place === "history") && footprint) blockEl.querySelector(":scope > summary > .tw-sum")?.prepend(footprint);
788
- const pieces = handOffOrder({
789
- // The prompt still in its slot (an opened turn's is in the history).
790
- prompt: !opened && promptEl?.parentNode === promptSlot ? promptEl : null,
791
- block,
792
- extras: [...extras.children].map((el) => { el.classList.remove("ts-behind"); return el; }),
793
- answer: [...answer.children],
794
- timing: timingSlot.firstElementChild,
795
- kept: [...tail.children],
796
- queued: [...queuedSlot.children],
797
- });
798
- const animate = !immediate && !reducedMotion() && !document.hidden && !isCollapsed();
799
- folding = { pieces, block: blockEl, endKind, wasAtEnd: historyNearBottom(), animate, immediate, opened, prompt: promptEl };
800
- releaseTurn();
801
- if (!animate) return finishFold();
802
- stage.classList.add("leaving");
803
- foldTimer = setTimeout(finishFold, FOLD_MS);
804
- }
805
-
806
- // The fold's end (or a cut-short fold): the original pieces (their ref
807
- // bindings, selection and late updates with them) move into the history.
808
- function finishFold() {
809
- const fold = folding;
810
- if (!fold) return;
811
- folding = null;
812
- // The stage held it open; here it takes its place in the history:
813
- // closed, unless the turn was cut short with an outline (block_open.js).
814
- if (fold.block) fold.block.open = openAfterEnd(fold.endKind, fold.block.classList.contains("outline"));
815
- // A piece removed while it slid (the old continue card the step-limit
816
- // question replaces) stays gone.
817
- fold.pieces = fold.pieces.filter((el) => el.isConnected);
818
- moveToHistoryEnd(historyEl, fold.pieces, { animate: fold.animate });
819
- // An opened turn's reasons go in the pieces' task (after them: the
820
- // history is as tall as it gets, room to keep the reader's rows in
821
- // place): each answered card's clamp says it now, as the turn reloads.
822
- opener.end();
823
- clearStage();
824
- if (fold.wasAtEnd) easeScrollTo(historyEl, () => historyEl.scrollHeight - historyEl.clientHeight, { reduce: !fold.animate || reducedMotion() });
825
- // The turn's pieces are in the history now (the opt-in reload check,
826
- // reload_check.js), the whole turn from its opening: an opened turn's
827
- // earlier pieces went before, and a wake's or a reminder's turn has no
828
- // prompt (its note opens it); +immediate+: another turn took the stage.
829
- onHandedOff?.({ pieces: wholeTurn(fold), immediate: fold.immediate });
830
- }
831
-
832
- // A handed-off turn from its opening: its prompt, else the wake's note
833
- // (data-opening-id) right before its first piece; the pieces as they are
834
- // when the first is its opening, or nothing opens it.
835
- function wholeTurn(fold) {
836
- const first = fold.pieces[0];
837
- if (first?.matches?.(OPENING) && !fold.opened) return fold.pieces;
838
- let opening = fold.prompt?.isConnected && fold.prompt.parentElement === historyEl ? fold.prompt : null;
839
- for (let el = (fold.block || first)?.previousElementSibling; !opening && el; el = el.previousElementSibling) {
840
- if (el.matches("[data-turn-id]")) break;
841
- if (el.matches("[data-opening-id]")) opening = el;
842
- }
843
- if (!opening) return fold.pieces;
844
- // Its rows, notes too (a note that reached the history mid-turn is the turn's).
845
- return [opening, ...turnElements(opening, { container: historyEl, boundary: null })];
846
- }
532
+ // The hand-off and its fold (stage_handoff.js).
533
+ const handoff = createStageHandoff({
534
+ stage, promptSlot, extras, answer, timingSlot, tail, queuedSlot, historyEl, historyNearBottom, view, opener, waiting,
535
+ workingSet, isHolding: () => holding, ended: () => endKind, isOpened: () => opened, promptEl: () => promptEl,
536
+ blockPlace, promptWords, isCollapsed, releaseTurn, clearStage, onHandedOff,
537
+ });
847
538
 
848
539
  function dropBare() {
849
540
  if (!bare) return;
@@ -856,13 +547,13 @@ export function createStageView({
856
547
 
857
548
  return {
858
549
  turnStarted(data) {
859
- if (folding) finishFold();
550
+ handoff.settle();
860
551
  dropBare();
861
552
  // A turn that never ended (released, not handed off) leaves its queued
862
553
  // lines waiting still: they stay.
863
554
  const waiting = [...queuedSlot.children].filter((el) => el !== data?.promptBubble);
864
555
  if (holding) {
865
- if (endKind) handOff({ immediate: true });
556
+ if (endKind) handoff.handOff({ immediate: true });
866
557
  else {
867
558
  // A turn that never ended (its end lost): its reasons, still
868
559
  // "asking you", go; what it moved into the history stays.
@@ -878,7 +569,7 @@ export function createStageView({
878
569
  settled = true;
879
570
  startedAt = Date.parse(data?.started_at) || Date.now();
880
571
  emptySlots();
881
- lastEventAt = clock();
572
+ quiet.alive();
882
573
  // The turn's prompt bubble (app.js found or drew it) sits in the prompt
883
574
  // line until the hand-off takes it back first.
884
575
  promptEl = data?.promptBubble || null;
@@ -893,28 +584,28 @@ export function createStageView({
893
584
  refresh();
894
585
  },
895
586
  generationStarted(data, now = null) {
896
- alive();
587
+ quiet.alive();
897
588
  releaseHeldWait();
898
589
  view.generationStarted(data, now);
899
590
  },
900
591
  chunk(data) {
901
- alive();
592
+ quiet.alive();
902
593
  return view.chunk(data);
903
594
  },
904
595
  generationRestarted(data) {
905
- alive();
596
+ quiet.alive();
906
597
  return view.generationRestarted(data);
907
598
  },
908
599
  generationCompleted() {
909
- alive();
600
+ quiet.alive();
910
601
  return view.generationCompleted();
911
602
  },
912
603
  toolStarted(data) {
913
- alive();
604
+ quiet.alive();
914
605
  return view.toolStarted(data);
915
606
  },
916
607
  toolCompleted(data) {
917
- alive();
608
+ quiet.alive();
918
609
  return view.toolCompleted(data);
919
610
  },
920
611
  turnEnded(args) {
@@ -944,13 +635,13 @@ export function createStageView({
944
635
  // A hook's notice and a plugin's nudge go into their step inside the
945
636
  // closed cloud; they flash in the trail meanwhile (flashOf).
946
637
  notice(data) {
947
- alive();
638
+ quiet.alive();
948
639
  const placed = view.notice(data);
949
640
  if (placed) flashTrail(flashOf("notice", data));
950
641
  return placed;
951
642
  },
952
643
  steer(data) {
953
- alive();
644
+ quiet.alive();
954
645
  const placed = view.steer(data);
955
646
  if (placed) flashTrail(flashOf("steer", data));
956
647
  return placed;
@@ -958,7 +649,7 @@ export function createStageView({
958
649
  // A warn card (a plugin's warning) stays in sight: false puts it in the
959
650
  // extras (place), not in its step inside the closed cloud.
960
651
  card(el) {
961
- alive();
652
+ quiet.alive();
962
653
  return !el.classList.contains("warn") && view.card(el);
963
654
  },
964
655
  flush: () => view.flush(),
@@ -972,7 +663,7 @@ export function createStageView({
972
663
  askingText: (pq) => askedText(pq),
973
664
  // A lead-in bubble drawn after this pairs them (askingRefs).
974
665
  questionRefs(refs) {
975
- alive();
666
+ quiet.alive();
976
667
  view.questionRefs(refs);
977
668
  scheduleRefresh();
978
669
  },
@@ -986,7 +677,7 @@ export function createStageView({
986
677
  // sliding away is dropped (its history is about to be drawn anew),
987
678
  // never moved into whatever history shows now.
988
679
  reset() {
989
- folding = null;
680
+ handoff.reset();
990
681
  release();
991
682
  },
992
683
  // The stage's extras while it holds a turn (a card, a prompt for its
@@ -995,7 +686,7 @@ export function createStageView({
995
686
  place(el) {
996
687
  // A turn still sliding away goes into the history first: what comes
997
688
  // now comes after it.
998
- if (folding) finishFold();
689
+ handoff.settle();
999
690
  const waits = !!(el.matches?.(PENDING) || el.querySelector?.(ASKS));
1000
691
  const kind = placeKind(el);
1001
692
  const where = placeFor({ live: holding, handingOff: false, kind, opened: opened && !endKind });
@@ -1007,7 +698,7 @@ export function createStageView({
1007
698
  return true;
1008
699
  }
1009
700
  if (where === "history") return false;
1010
- alive();
701
+ quiet.alive();
1011
702
  if (where === "queued") {
1012
703
  ensureQueuedSlot();
1013
704
  queuedSlot.appendChild(el);
@@ -1060,7 +751,7 @@ export function createStageView({
1060
751
  // the history's follow and the reload check as for any finished turn;
1061
752
  // the in-use watch and the end read don't hold it).
1062
753
  if (endKind && waitKind === "continue") {
1063
- handOff();
754
+ handoff.handOff();
1064
755
  return true;
1065
756
  }
1066
757
  // A waiting card and the asking form (refresh) land in one task.
@@ -1078,19 +769,19 @@ export function createStageView({
1078
769
  },
1079
770
  // Where the live timing line goes (the status row), null: the history.
1080
771
  timingHost: () => {
1081
- if (folding) finishFold();
772
+ handoff.settle();
1082
773
  return holding ? timingSlot : null;
1083
774
  },
1084
775
  // Before anything else goes into the history (app.js): a turn still
1085
776
  // sliding away gets there first.
1086
- settleFold: () => { if (folding) finishFold(); },
777
+ settleFold: () => handoff.settle(),
1087
778
  // A turn is in the stage (running, or ended and not handed off yet):
1088
779
  // nothing the turn does touches the history's scroll.
1089
780
  holds: () => holding,
1090
781
  // The page's turn runs but the stage holds none (+on+); false: it ended.
1091
782
  // A turn the stage holds draws its own row: nothing to do then.
1092
783
  runningWithoutTurn(on) {
1093
- if (holding || folding) return;
784
+ if (holding || handoff.isFolding()) return;
1094
785
  if (!on) {
1095
786
  if (bare) clearStage();
1096
787
  return;
@@ -1100,17 +791,19 @@ export function createStageView({
1100
791
  stage.classList.add("bare");
1101
792
  stage.dataset.phase = "running";
1102
793
  phaseEl.textContent = "running";
1103
- cancel.hidden = !onCancel;
794
+ cancel.hidden = !onCancel || !canCancel();
1104
795
  show();
1105
796
  },
1106
797
  // The page's clock ticks (app.js, the live timing's 200 ms / 1 s): the
1107
798
  // quiet signal and the running call's clock.
1108
799
  tick(nowMs = clock()) {
1109
- drawStall(nowMs);
800
+ quiet.tick(nowMs);
1110
801
  if (holding && asksCard() && liveSlots(view.current().turn).tool?.startedAt != null) scheduleRefresh();
1111
802
  },
1112
803
  callClockActive: () => holding && asksCard() && !!liveSlots(view.current().turn).tool,
1113
- handOff,
804
+ // An input of the hooks changed (a participant's ownerWait, canCancel).
805
+ redraw: () => scheduleRefresh(),
806
+ handOff: handoff.handOff,
1114
807
  // A card stopped waiting (stage_waiting.js keys; +only+: when it is that
1115
808
  // kind): question_cards.js's resolve path, cards_place.js upsertCard.
1116
809
  // The stage's redraw comes from the card's own change.