samagotchi 0.171.0 → 0.172.1

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 (209) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +46 -1
  3. data/bin/chi +6 -4
  4. data/docs/architecture.md +24 -7
  5. data/docs/bundles.md +4 -21
  6. data/docs/cli.md +50 -80
  7. data/docs/configuration.md +11 -13
  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/bridge.md +29 -3
  14. data/docs/internals/engine.md +21 -9
  15. data/docs/internals/objectives.md +3 -5
  16. data/docs/internals/participants.md +118 -143
  17. data/docs/internals/prompt-caching.md +4 -5
  18. data/docs/internals/web-frontend.md +70 -27
  19. data/docs/internals/working-set.md +7 -5
  20. data/docs/memory.md +1 -1
  21. data/docs/plugins.md +2 -16
  22. data/docs/scheduling.md +1 -1
  23. data/docs/sessions.md +15 -49
  24. data/docs/sub-agent.md +2 -2
  25. data/docs/system-one.md +2 -2
  26. data/docs/testing.md +81 -8
  27. data/lib/samagotchi/bridge/card_store.rb +1 -0
  28. data/lib/samagotchi/bridge/sse_writer.rb +1 -0
  29. data/lib/samagotchi/bridge/turn_accumulator.rb +0 -1
  30. data/lib/samagotchi/bridge.rb +26 -9
  31. data/lib/samagotchi/bridge_client/event_stream.rb +2 -0
  32. data/lib/samagotchi/bundle_command/test_run.rb +1 -0
  33. data/lib/samagotchi/bundle_command/try/run.rb +1 -0
  34. data/lib/samagotchi/bundles/coordinator/manifest.yml +4 -5
  35. data/lib/samagotchi/bundles/coordinator/plugin.rb +6 -79
  36. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +1 -16
  37. data/lib/samagotchi/bundles/github-pr/manifest.yml +2 -2
  38. data/lib/samagotchi/bundles/github-pr/plugin.rb +0 -1
  39. data/lib/samagotchi/bundles/inline-widgets/manifest.yml +2 -2
  40. data/lib/samagotchi/bundles/inline-widgets/plugin.rb +4 -5
  41. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  42. data/lib/samagotchi/client_id.rb +126 -67
  43. data/lib/samagotchi/command_intake.rb +15 -3
  44. data/lib/samagotchi/compat.rb +6 -1
  45. data/lib/samagotchi/context_candidate_choice.rb +1 -3
  46. data/lib/samagotchi/context_candidates.rb +3 -4
  47. data/lib/samagotchi/continue_offer.rb +9 -2
  48. data/lib/samagotchi/delivery.rb +23 -12
  49. data/lib/samagotchi/empty_answer_error.rb +5 -0
  50. data/lib/samagotchi/engine/cards.rb +3 -0
  51. data/lib/samagotchi/engine/context_judge.rb +5 -3
  52. data/lib/samagotchi/engine/events.rb +4 -0
  53. data/lib/samagotchi/engine/hooks.rb +3 -0
  54. data/lib/samagotchi/engine/messages.rb +5 -0
  55. data/lib/samagotchi/engine/model_settings.rb +2 -0
  56. data/lib/samagotchi/engine/models.rb +10 -0
  57. data/lib/samagotchi/engine/objectives.rb +6 -0
  58. data/lib/samagotchi/engine/participants.rb +18 -14
  59. data/lib/samagotchi/engine/plugins.rb +8 -0
  60. data/lib/samagotchi/engine/questions.rb +3 -0
  61. data/lib/samagotchi/engine/recap.rb +1 -0
  62. data/lib/samagotchi/engine/session.rb +4 -0
  63. data/lib/samagotchi/engine/side_requests.rb +9 -0
  64. data/lib/samagotchi/engine/steering.rb +2 -0
  65. data/lib/samagotchi/engine/toolkit.rb +5 -0
  66. data/lib/samagotchi/engine/turn_end.rb +7 -0
  67. data/lib/samagotchi/engine/turn_helpers.rb +8 -0
  68. data/lib/samagotchi/engine/verify.rb +2 -0
  69. data/lib/samagotchi/engine.rb +3 -2
  70. data/lib/samagotchi/grants.rb +28 -22
  71. data/lib/samagotchi/guardrail_wiring.rb +23 -18
  72. data/lib/samagotchi/guardrails/approval_use.rb +59 -0
  73. data/lib/samagotchi/guardrails/approvals.rb +9 -11
  74. data/lib/samagotchi/guardrails/child_boundary.rb +1 -0
  75. data/lib/samagotchi/guardrails/gate.rb +10 -5
  76. data/lib/samagotchi/guardrails/implicit_writers.rb +1 -0
  77. data/lib/samagotchi/guardrails/pair_boundary.rb +6 -6
  78. data/lib/samagotchi/guardrails/participant_refusal.rb +33 -0
  79. data/lib/samagotchi/guardrails/review_paths.rb +2 -3
  80. data/lib/samagotchi/guardrails.rb +1 -1
  81. data/lib/samagotchi/llm_context_strategy.rb +2 -0
  82. data/lib/samagotchi/objectives.rb +1 -4
  83. data/lib/samagotchi/objectives_audit.rb +1 -1
  84. data/lib/samagotchi/objectives_track.rb +1 -1
  85. data/lib/samagotchi/owner_prompt.rb +3 -6
  86. data/lib/samagotchi/parent_report.rb +1 -0
  87. data/lib/samagotchi/participant_messages.rb +9 -4
  88. data/lib/samagotchi/participant_turn_mode.rb +50 -0
  89. data/lib/samagotchi/participants.rb +21 -12
  90. data/lib/samagotchi/participants_watch.rb +2 -2
  91. data/lib/samagotchi/plugin/api.rb +2 -2
  92. data/lib/samagotchi/plugin/attached_context.rb +1 -1
  93. data/lib/samagotchi/plugin/context.rb +0 -4
  94. data/lib/samagotchi/plugin/sessions.rb +1 -25
  95. data/lib/samagotchi/run_options.rb +2 -3
  96. data/lib/samagotchi/send_command.rb +10 -10
  97. data/lib/samagotchi/session.rb +0 -3
  98. data/lib/samagotchi/session_access/contract.rb +1 -10
  99. data/lib/samagotchi/session_access/local.rb +10 -23
  100. data/lib/samagotchi/session_access/remote.rb +9 -20
  101. data/lib/samagotchi/session_access.rb +12 -16
  102. data/lib/samagotchi/session_chain.rb +4 -6
  103. data/lib/samagotchi/session_commands.rb +6 -3
  104. data/lib/samagotchi/session_continue.rb +1 -2
  105. data/lib/samagotchi/session_manager/delivery.rb +25 -5
  106. data/lib/samagotchi/session_manager/lifecycle.rb +12 -0
  107. data/lib/samagotchi/session_manager/listing.rb +5 -0
  108. data/lib/samagotchi/session_manager/spawn.rb +2 -2
  109. data/lib/samagotchi/session_manager/worker_command.rb +9 -0
  110. data/lib/samagotchi/session_participants.rb +12 -8
  111. data/lib/samagotchi/session_review.rb +5 -7
  112. data/lib/samagotchi/session_stores.rb +3 -3
  113. data/lib/samagotchi/share_command.rb +10 -12
  114. data/lib/samagotchi/share_terms.rb +41 -0
  115. data/lib/samagotchi/steer.rb +8 -14
  116. data/lib/samagotchi/system_prompt.rb +16 -25
  117. data/lib/samagotchi/terminal_ui/attach_launcher.rb +2 -1
  118. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +1 -0
  119. data/lib/samagotchi/terminal_ui/attached_loop/queueing.rb +1 -0
  120. data/lib/samagotchi/terminal_ui/attached_loop/sending.rb +2 -0
  121. data/lib/samagotchi/terminal_ui/formatting.rb +15 -17
  122. data/lib/samagotchi/terminal_ui/participant_commands.rb +2 -2
  123. data/lib/samagotchi/terminal_ui/status_row.rb +2 -2
  124. data/lib/samagotchi/terminal_ui.rb +1 -2
  125. data/lib/samagotchi/testing/sessions.rb +2 -10
  126. data/lib/samagotchi/tool_runner.rb +8 -9
  127. data/lib/samagotchi/toolkit.rb +4 -22
  128. data/lib/samagotchi/toolkit_suggest.rb +3 -3
  129. data/lib/samagotchi/tools/deferred_tools.rb +1 -0
  130. data/lib/samagotchi/tools/execute.rb +11 -45
  131. data/lib/samagotchi/tools/leftovers.rb +126 -0
  132. data/lib/samagotchi/tools/memory.rb +70 -26
  133. data/lib/samagotchi/tools/task_get.rb +1 -0
  134. data/lib/samagotchi/tools/task_list.rb +3 -2
  135. data/lib/samagotchi/tools/task_record.rb +6 -4
  136. data/lib/samagotchi/tools/task_runtime.rb +199 -20
  137. data/lib/samagotchi/tools/task_wait.rb +4 -3
  138. data/lib/samagotchi/turn_context.rb +1 -1
  139. data/lib/samagotchi/turn_origin.rb +1 -1
  140. data/lib/samagotchi/turn_stages/context_judge.rb +4 -4
  141. data/lib/samagotchi/turn_stages/objectives.rb +2 -2
  142. data/lib/samagotchi/turn_stages/toolkit_suggestion.rb +2 -2
  143. data/lib/samagotchi/update_hint.rb +3 -1
  144. data/lib/samagotchi/verify_fresh.rb +1 -1
  145. data/lib/samagotchi/verify_fresh_origin.rb +13 -11
  146. data/lib/samagotchi/version.rb +1 -1
  147. data/lib/samagotchi/web/app.rb +3 -5
  148. data/lib/samagotchi/web/participant_access.rb +14 -18
  149. data/lib/samagotchi/web/participant_review.rb +3 -4
  150. data/lib/samagotchi/web/participant_show.rb +4 -3
  151. data/lib/samagotchi/web/participant_view.rb +3 -3
  152. data/lib/samagotchi/web/participants_admin.rb +21 -40
  153. data/lib/samagotchi/web/public/app.js +7 -48
  154. data/lib/samagotchi/web/public/card_kinds.js +51 -0
  155. data/lib/samagotchi/web/public/cards_place.js +13 -2
  156. data/lib/samagotchi/web/public/data.js +2 -10
  157. data/lib/samagotchi/web/public/delivery_line.js +17 -60
  158. data/lib/samagotchi/web/public/index.html +10 -30
  159. data/lib/samagotchi/web/public/live_turn.js +1 -1
  160. data/lib/samagotchi/web/public/owner_claim.js +2 -2
  161. data/lib/samagotchi/web/public/participant_bar.js +4 -5
  162. data/lib/samagotchi/web/public/participant_names.js +14 -10
  163. data/lib/samagotchi/web/public/participants_panel.js +29 -62
  164. data/lib/samagotchi/web/public/question_cards.js +29 -14
  165. data/lib/samagotchi/web/public/reload_check.js +2 -3
  166. data/lib/samagotchi/web/public/send_turn.js +4 -23
  167. data/lib/samagotchi/web/public/session_actions.js +4 -45
  168. data/lib/samagotchi/web/public/session_fields.js +0 -1
  169. data/lib/samagotchi/web/public/session_state.js +0 -3
  170. data/lib/samagotchi/web/public/session_sync.js +4 -2
  171. data/lib/samagotchi/web/public/session_view.js +2 -3
  172. data/lib/samagotchi/web/public/settings_panel.js +4 -18
  173. data/lib/samagotchi/web/public/stage_answer.js +56 -0
  174. data/lib/samagotchi/web/public/stage_asking.js +30 -11
  175. data/lib/samagotchi/web/public/stage_cloud.js +93 -0
  176. data/lib/samagotchi/web/public/stage_dom.js +100 -0
  177. data/lib/samagotchi/web/public/stage_handoff.js +154 -0
  178. data/lib/samagotchi/web/public/stage_pager.js +59 -0
  179. data/lib/samagotchi/web/public/stage_place.js +144 -0
  180. data/lib/samagotchi/web/public/stage_probe.js +64 -0
  181. data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
  182. data/lib/samagotchi/web/public/stage_view.js +133 -506
  183. data/lib/samagotchi/web/public/stage_waiting.js +30 -13
  184. data/lib/samagotchi/web/public/start_page.js +12 -33
  185. data/lib/samagotchi/web/public/start_settings.js +24 -78
  186. data/lib/samagotchi/web/public/start_toolkit.js +6 -10
  187. data/lib/samagotchi/web/public/stream_handlers.js +1 -9
  188. data/lib/samagotchi/web/public/turn_block.js +3 -7
  189. data/lib/samagotchi/web/public/turn_events.js +15 -6
  190. data/lib/samagotchi/web/public/turn_pieces.js +3 -4
  191. data/lib/samagotchi/web/public/viewer_self.js +8 -2
  192. data/lib/samagotchi/web/qr.rb +1 -1
  193. data/lib/samagotchi/web/session_control.rb +3 -1
  194. data/lib/samagotchi/web/session_create.rb +5 -71
  195. data/lib/samagotchi/web/session_summary.rb +1 -4
  196. data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
  197. data/lib/samagotchi/web/viewer.rb +3 -3
  198. data/lib/samagotchi/worker.rb +16 -14
  199. data/lib/samagotchi/worker_inbound.rb +4 -3
  200. data/lib/samagotchi/worker_link.rb +17 -0
  201. data/lib/samagotchi/working_set.rb +87 -28
  202. metadata +16 -8
  203. data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
  204. data/lib/samagotchi/participant_mode.rb +0 -86
  205. data/lib/samagotchi/review_apply.rb +0 -131
  206. data/lib/samagotchi/review_command.rb +0 -123
  207. data/lib/samagotchi/review_start.rb +0 -196
  208. data/lib/samagotchi/web/public/review_apply.js +0 -34
  209. data/lib/samagotchi/web/public/review_start.js +0 -55
@@ -16,47 +16,47 @@
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, where a row or a card goes stage_place.js's,
25
+ // the cloud stage_cloud.js's, the docked card and the fold stage_asking.js's,
26
+ // its pager stage_pager.js's, the waiting cards as data stage_waiting.js's,
27
+ // the answer's reading stage_answer.js's, the quiet signal stage_stall.js's
28
+ // and its clocks stage_quiet_clock.js's, the perf probe stage_probe.js's. A new
29
+ // stage feature goes into its own stage_<feature>.js, wired here in a few
30
+ // lines; each module resets itself from emptySlots.
24
31
  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";
32
+ import { chipLabelText, flashOf, handOffDue, headlineOf, isPlain, liveSlots, MARKS, foldedRowText, phaseLabel, QUEUED_CHILDREN, stepSlotText } from "./stage_model.js";
27
33
  import { createStageSlots, node } from "./stage_slots.js";
34
+ import { createStageDom } from "./stage_dom.js";
35
+ import { createStageAnswer } from "./stage_answer.js";
36
+ import { createStagePager } from "./stage_pager.js";
37
+ import { createStageCloud } from "./stage_cloud.js";
38
+ import { createStageHandoff } from "./stage_handoff.js";
39
+
40
+ // How long the stage slides away at the hand-off (dock_reserve.js).
41
+ export { FOLD_MS } from "./stage_handoff.js";
28
42
  import { createStageFrame, reducedMotion } from "./stage_frame.js";
29
43
  import { createInUseWatch } from "./stage_watch.js";
30
- import { createStageStall, GRACE_MS, stallState } from "./stage_stall.js";
44
+ import { createStageStall } from "./stage_stall.js";
45
+ import { createStageQuietClock } from "./stage_quiet_clock.js";
31
46
  import { formatDuration } from "./timing.js";
32
- import { easeScrollTo } from "./scroll.js";
33
47
  import { createWorkingSetRail } from "./working_set_rail.js";
34
- import { moveToHistoryEnd } from "./history_end.js";
35
- import { turnElements } from "./turn_range.js";
36
48
  import { createOpenedTurn } from "./stage_opened.js";
37
- import { askingRefs } from "./turn_model.js";
38
- import { ASKS, PENDING } from "./stage_cards.js";
39
49
  import { createStageAsking } from "./stage_asking.js";
40
- import { cardIn, createStageWaiting, isApprovalCard, isBlocking, waitingKey, waitingKind } from "./stage_waiting.js";
50
+ import { cardIn, createStageWaiting, waitingKey } from "./stage_waiting.js";
41
51
  import { stampRows } from "./working_set_view.js";
42
52
  import { createOwnerWait } from "./stage_owner_wait.js";
53
+ import { measured } from "./stage_probe.js";
54
+ import { createStagePlace } from "./stage_place.js";
43
55
 
44
56
  // The turn-end read (markdown, answer_display) settles within this, or the
45
57
  // hand-off stops waiting for it.
46
58
  const SETTLE_CAP_MS = 8000;
47
- export const FOLD_MS = 420;
48
59
  const FLASH_MS = 4000;
49
- // The answer reads from its top: at the end it is scrolled to unless this
50
- // much of it (about three lines) shows already.
51
- const ANSWER_SEEN = 72;
52
- // A short answer cut by less than this at its foot isn't scrolled to.
53
- const ANSWER_CUT = 12;
54
- // An answered card's one line.
55
- const ANSWERED_LINE = 40;
56
- // That line brought back sits this far under the scroller's edge.
57
- const ANSWERED_GAP = 8;
58
- // What opens a turn in the history (reload_check.js's OPENING).
59
- const OPENING = "[data-turn-id], [data-opening-id]";
60
60
 
61
61
  // +historyNearBottom+: the history's own follow check (the hand-off scrolls
62
62
  // the history only for a reader at its end). +dockEl+ and +centerEl+ size
@@ -69,57 +69,26 @@ const OPENING = "[data-turn-id], [data-opening-id]";
69
69
  // status row's Stop turn (a promise of the cancel request; the button is
70
70
  // disabled until it settles); null: no Stop turn. +onHandedOff+({pieces,
71
71
  // immediate}): a turn's pieces reached the history (reload_check.js).
72
- // The quiet signal's inputs (stage_stall.js): +clock+() the page's time,
72
+ // The quiet signal's inputs (stage_quiet_clock.js): +clock+() the page's time,
73
73
  // +streamLive+() the session's stream is open, +timingNote+() what the turn
74
74
  // waits on (live_timing.js note), +serverGen+() a joiner's generation.
75
75
  // +canCancel+() whether this turn's Stop turn shows (a participant's own
76
76
  // turn only); +ownerWait+() the kind a participant's view waits for the
77
- // owner on, or null (stage_owner_wait.js).
77
+ // owner on, or null (stage_owner_wait.js). +createWaiting+: the registry's
78
+ // factory (stage_waiting.js; a test passes an older one's shape).
78
79
  export function createStageView({
79
80
  historyEl, isNearBottom: historyNearBottom, sawText, sessionId, dockEl, centerEl, taskStop = null,
80
81
  renderMarkdown, linkRefs, workingSet = null, onCancel = null, onHandedOff = null,
81
82
  clock = () => Date.now(), streamLive = () => true, timingNote = () => "", serverGen = () => null,
82
- canCancel = () => true, ownerWait = () => null,
83
+ canCancel = () => true, ownerWait = () => null, createWaiting = createStageWaiting,
83
84
  }) {
84
- // The dock becomes a clear column: the stage's card, then the composer's
85
- // (its rows moved into .dock-card, which wears the glass and the focus ring).
86
- document.body.classList.add("stage-view");
87
- const card = node("div", "dock-card composer-card");
88
- card.append(...dockEl.children);
89
- dockEl.appendChild(card);
90
- const stage = node("section", "turn-stage");
91
- stage.id = "turnStage";
92
- stage.hidden = true;
93
- stage.setAttribute("aria-label", "Running turn");
94
- dockEl.prepend(stage);
95
-
96
- const status = node("div", "ts-status", stage);
97
- const mark = node("span", "ts-mark", status);
98
- const phaseEl = node("span", "ts-phase", status);
99
- // Two cards docked: one at a time, "‹ 1 of 2 ›" (stage_waiting.js shown).
100
- const pager = node("span", "ts-pager", status);
101
- pager.hidden = true;
102
- const pagePrev = node("button", "ts-page", pager);
103
- pagePrev.type = "button";
104
- pagePrev.textContent = "‹";
105
- pagePrev.setAttribute("aria-label", "The card before");
106
- const pageLabel = node("span", "ts-page-label", pager);
107
- const pageNext = node("button", "ts-page", pager);
108
- pageNext.type = "button";
109
- pageNext.textContent = "›";
110
- pageNext.setAttribute("aria-label", "The next card");
111
- const stepEl = node("span", "ts-step", status);
112
- const timingSlot = node("span", "ts-timing", status);
113
- const miniTool = node("span", "ts-mini", status);
114
- const defer = node("span", "ts-defer", status);
115
- defer.textContent = "moves up when you leave";
116
- // Stop turn, at the row's right edge beside the fold: while the turn
117
- // hasn't ended (refresh), in a collapsed stage too (the row stays).
118
- const cancel = node("button", "ts-cancel", status);
119
- cancel.type = "button";
120
- cancel.title = "Cancel the running turn, whoever sent it";
121
- cancel.textContent = "Stop turn";
122
- cancel.hidden = true;
85
+ // The stage's nodes (stage_dom.js), then their behaviour here and in the
86
+ // stage's modules.
87
+ const {
88
+ stage, status, mark, phaseEl, pager, pagePrev, pageLabel, pageNext, stepEl, timingSlot, miniTool, cancel, fold,
89
+ scroller, promptSlot, live, headline, ownerWaitLine, now, trail, extras, asksText, answer, tail, cloudBody, holdEl,
90
+ cloud, chip, chipLabel, ticks, queuedSlot,
91
+ } = createStageDom(dockEl);
123
92
  cancel.addEventListener("click", async () => {
124
93
  if (!onCancel || cancel.disabled) return;
125
94
  cancel.disabled = true;
@@ -129,21 +98,7 @@ export function createStageView({
129
98
  cancel.disabled = false;
130
99
  }
131
100
  });
132
- const fold = node("button", "ts-fold", status);
133
- fold.type = "button";
134
-
135
- const scroller = node("div", "ts-scroll", stage);
136
- const promptSlot = node("div", "ts-prompt", scroller);
137
- promptSlot.title = "Show the whole prompt";
138
- const live = node("div", "ts-live", scroller);
139
- const headline = node("div", "ts-headline", live);
140
- headline.tabIndex = 0;
141
- headline.setAttribute("role", "button");
142
- headline.title = "Show this step's reasoning";
143
- // A participant's turn waiting for the owner: one line under the headline, no card.
144
- const drawOwnerWait = createOwnerWait({ stage, line: node("div", "ts-owner-wait", live), phaseEl });
145
- const now = node("div", "ts-now", live);
146
- const trail = node("div", "ts-trail", live);
101
+ const drawOwnerWait = createOwnerWait({ stage, line: ownerWaitLine, phaseEl });
147
102
  // The working set's rail: out of the live slots' flow on their right (a
148
103
  // row under the trail in a narrow stage), so it never sets their height.
149
104
  const rail = workingSet
@@ -154,36 +109,12 @@ export function createStageView({
154
109
  // The server's states change the lines (never the fold): a redraw.
155
110
  workingSet.onServer(() => scheduleRefresh());
156
111
  }
157
- const extras = node("div", "ts-extras", scroller);
158
- // While check-in's card is docked: the live step's text, streamed under
159
- // the card (the steps are in the history then). The answer's own slot
160
- // takes it over at the turn's end (placeAnswer, in one task).
161
- const asksText = node("div", "ts-asks-text", scroller);
162
- asksText.hidden = true;
163
- const answer = node("div", "ts-answer", scroller);
164
- const tail = node("div", "ts-tail", scroller);
165
- // The cloud's steps, the scroller's last slot, right above the chip.
166
- const cloudBody = node("div", "ts-cloud-body", scroller);
167
- cloudBody.hidden = true;
168
- // A 1 px mark at the tallest content's end (absolute: out of the flow),
169
- // so the scroll extent never drops while the turn runs: content that
170
- // shrinks never lowers a scrolled reader's scrollTop (the browser would
171
- // clamp it, moving everything in sight), even in the layout before
172
- // fitFrame runs.
173
- const holdEl = node("div", "ts-hold", scroller);
174
- holdEl.setAttribute("aria-hidden", "true");
175
- // The footer: the chip, pinned to the stage's bottom edge as the status
176
- // row is to its top.
177
- const cloud = node("div", "ts-foot", stage);
178
- const chip = node("button", "ts-chip", cloud);
179
- chip.type = "button";
180
- const chipLabel = node("span", "ts-chip-label", chip);
181
- const ticks = node("span", "ts-ticks", chip);
182
- // Below the turn's card: the lines queued for after it (placeKind
183
- // "queued"), each to run as a turn of its own; they hand off last.
184
- const queuedSlot = node("div", "ts-queued", stage);
185
- // The quiet signal: its span in the now row, its head after the timing line.
186
- const stall = createStageStall({ stage, nowEl: now, timingSlot });
112
+ // The quiet signal: its span in the now row, its head after the timing
113
+ // line (stage_stall.js), its clocks (stage_quiet_clock.js).
114
+ const quiet = createStageQuietClock({
115
+ stall: createStageStall({ stage, nowEl: now, timingSlot }), clock, streamLive, timingNote, serverGen,
116
+ held: () => (holding ? view.current().turn : null), ended: () => !!endKind, pending: () => pendingCard(),
117
+ });
187
118
  const slotsView = createStageSlots({
188
119
  headline, now, trail, ticks, taskStop,
189
120
  gens: () => view.current().turn?.gens,
@@ -202,21 +133,14 @@ export function createStageView({
202
133
  let thinkingLine = null; // { step, text } from the live step's ticker
203
134
  let flash = null; // { kind, text, until } a notice or nudge in the trail
204
135
  let flashTimer = null; // its end's redraw
205
- let cloudOpen = false;
206
136
  let rafPending = false;
207
137
  // A turn the page knows runs (the session says running) with none in the
208
138
  // stage to show (a join without its current_turn): the status row alone,
209
139
  // so Stop turn stays in reach (runningWithoutTurn).
210
140
  let bare = false;
211
- let foldTimer = null;
212
141
  // The raised cap (stage_frame.js) of an approval too tall for the stage
213
142
  // (raiseForApproval), kept until the next step (releaseHeldWait).
214
143
  let roomCap = false;
215
- // The quiet signal's clocks: the turn's last event, since when the stream
216
- // isn't live (null: it is), the grace after the page shows again.
217
- let lastEventAt = 0;
218
- let downSince = null;
219
- let graceUntil = 0;
220
144
  // Where the turn's prompt and block are: the stage (its prompt slot, the
221
145
  // cloud or a plain turn's answer slot), or the history once the turn is
222
146
  // opened there (+opened+). Read through blockPlace and promptEl, never
@@ -232,6 +156,12 @@ export function createStageView({
232
156
  else delete stage.dataset.block;
233
157
  }
234
158
 
159
+ // The answer read from its top, an answered card's line, the prompt's
160
+ // first words (stage_answer.js).
161
+ const { showAnswerTop, settleAnsweredCard, promptWords } = createStageAnswer({
162
+ stage, scroller, answer, extras, tail, frame: () => frame, promptEl: () => promptEl,
163
+ });
164
+
235
165
  // The turn opened in the history while the model's own question waits
236
166
  // (stage_opened.js): its reasons, and the moves there.
237
167
  const opener = createOpenedTurn({
@@ -268,18 +198,21 @@ export function createStageView({
268
198
  // The cards that wait in the stage, as data (stage_waiting.js): the
269
199
  // docked card, its fold, what blocks, the approval's cap read them. The
270
200
  // fold of the asking card is this tab's own, kept per session.
271
- const waiting = createStageWaiting({ sessionId });
201
+ const waiting = createWaiting({ sessionId });
272
202
  let dockedAtLastRefresh = [];
273
203
  // The asking form and the stage's fold (stage_asking.js).
274
204
  const ask = createStageAsking({
275
205
  stage, extras, tail, scroller, fold, view, opener, frame: () => frame, isOpened: () => opened, setOpened,
276
- isEnded: () => !!endKind, promptEl: () => promptEl, setCloud, scheduleRefresh, pendingCard, waiting,
206
+ isEnded: () => !!endKind, promptEl: () => promptEl, setCloud: (open) => cloudView.setCloud(open), scheduleRefresh, pendingCard, waiting,
277
207
  });
278
208
  const {
279
- askingGen, asksOf, askedFor, askedText, docks, leadInText, openTurn, settleCards, askingCard, cardOf, foldedText, markOwnToggles,
209
+ askedText, settleCards, askingCard, cardOf, foldedText, markOwnToggles,
280
210
  isCollapsed, drawCollapsed, applyCollapsed, setCollapsed,
281
211
  } = ask;
282
212
 
213
+ // The docked cards' pager (stage_pager.js).
214
+ const pagerView = createStagePager({ pager, pagePrev, pageLabel, pageNext, extras, waiting, cardOf, askingCard, refresh, frame: () => frame });
215
+
283
216
  // ── slots ────────────────────────────────────────────────────────────────
284
217
 
285
218
  // "history" (an opened turn's), "cloud", "answer" (a plain turn's in
@@ -296,11 +229,10 @@ export function createStageView({
296
229
  requestAnimationFrame(refresh);
297
230
  }
298
231
 
299
- // A card that blocks the turn waits (stage_waiting.js, checked against
300
- // the slots first: a replay answers a card and starts the next step in one
301
- // task, before the refresh).
232
+ // A card that blocks the turn waits (stage_waiting.js: every way a card
233
+ // stops waiting settles it at once, also a replay's answer and next step
234
+ // in one task, before the refresh).
302
235
  function pendingCard() {
303
- ask.reconcileWaiting();
304
236
  return waiting.count() > 0;
305
237
  }
306
238
 
@@ -322,48 +254,6 @@ export function createStageView({
322
254
  return !endKind && waiting.count({ asks: true }) > waiting.count();
323
255
  }
324
256
 
325
- // One docked card at a time: the others wait behind it (.ts-behind) and
326
- // the pager says where the shown one is.
327
- function drawDocked(shownCard) {
328
- const keys = waiting.docked();
329
- for (const key of keys) cardOf(key)?.classList.toggle("ts-behind", !!shownCard && cardOf(key) !== shownCard);
330
- for (const el of extras.querySelectorAll(":scope > .ts-behind")) if (!keys.includes(waitingKey(el))) el.classList.remove("ts-behind");
331
- // Focus in a card that went behind (a new card came in front of it):
332
- // to the card that shows now.
333
- const focused = document.activeElement;
334
- if (shownCard && focused && [...extras.querySelectorAll(":scope > .ts-behind")].some((el) => el.contains(focused))) focusIn(shownCard);
335
- const at = shownCard ? keys.indexOf(waitingKey(shownCard)) : -1;
336
- pager.hidden = keys.length < 2 || at < 0;
337
- if (pager.hidden) return;
338
- const label = `${at + 1} of ${keys.length}`;
339
- if (pageLabel.textContent !== label) pageLabel.textContent = label;
340
- pagePrev.disabled = at === 0;
341
- pageNext.disabled = at === keys.length - 1;
342
- // A pager button the page just disabled keeps no focus: the other one has it.
343
- if (document.activeElement === pagePrev && pagePrev.disabled) pageNext.focus();
344
- else if (document.activeElement === pageNext && pageNext.disabled) pagePrev.focus();
345
- }
346
-
347
- // +card+'s first control takes the focus (its summary is out of the tab
348
- // order while it waits), else the card.
349
- function focusIn(card) {
350
- const control = card.querySelector('input:not([type="hidden"]), textarea, select, button:not([disabled]), a[href]');
351
- if (control) control.focus();
352
- else {
353
- if (!card.hasAttribute("tabindex")) card.tabIndex = -1;
354
- card.focus();
355
- }
356
- }
357
- for (const [button, by] of [[pagePrev, -1], [pageNext, 1]]) {
358
- button.addEventListener("click", (e) => {
359
- e.stopPropagation();
360
- waiting.page(by);
361
- refresh();
362
- const shown = askingCard();
363
- if (shown) frame.afterGrowth(() => frame.reveal(shown.parentElement));
364
- });
365
- }
366
-
367
257
  // The text under a docked check-in card, patched only when it changes.
368
258
  function drawAsksText(text) {
369
259
  if (asksText.textContent !== text) asksText.textContent = text;
@@ -374,7 +264,12 @@ export function createStageView({
374
264
  // the card's own task, never a frame apart.
375
265
  new MutationObserver(() => refresh()).observe(extras, { childList: true, subtree: true });
376
266
 
267
+ // Timed when the perf probe is on (stage_probe.js).
377
268
  function refresh() {
269
+ measured("stage:refresh", drawRefresh);
270
+ }
271
+
272
+ function drawRefresh() {
378
273
  rafPending = false;
379
274
  const { turn, blockEl } = view.current();
380
275
  if (!holding || !turn) return;
@@ -394,7 +289,6 @@ export function createStageView({
394
289
  const thinking = thinkingLine && thinkingLine.step === turn.gens.length ? thinkingLine.text : "";
395
290
  // A question blocks the turn: its card takes the live slots' room. An
396
291
  // asking card's turn goes on: the status waits, the slots stay.
397
- ask.reconcileWaiting();
398
292
  settleCards();
399
293
  markOwnToggles();
400
294
  const blocked = pendingCard();
@@ -405,7 +299,7 @@ export function createStageView({
405
299
  stage.classList.toggle("asking-form", askingForm);
406
300
  // Its fold (stage_asking.js trackFold): that card's only.
407
301
  ask.trackFold();
408
- drawDocked(asking);
302
+ pagerView.drawDocked(asking);
409
303
  const collapsed = isCollapsed();
410
304
  drawCollapsed();
411
305
  const slots = liveSlots(turn, { narration, thinking, pending: blocked || asksCard(), ended: endKind, emptyAnswer });
@@ -442,7 +336,7 @@ export function createStageView({
442
336
  slotsView.drawHeadline(slots.headline);
443
337
  raiseForApproval(blocked);
444
338
  slotsView.drawNow(slots.tool, slots.quiet);
445
- drawStall(clock(), blocked);
339
+ quiet.tick(clock(), blocked);
446
340
  slotsView.drawTrail(slots.trail, flash);
447
341
  slotsView.drawTicks(slots.ticks);
448
342
  if (rail) {
@@ -478,36 +372,15 @@ export function createStageView({
478
372
  // The chip: a block in the cloud. An opened turn's steps line in the
479
373
  // history says the count: none here.
480
374
  cloud.hidden = place !== "cloud";
481
- cloudBody.hidden = !cloudOpen || cloud.hidden;
375
+ cloudBody.hidden = !cloudView.isOpen() || cloud.hidden;
482
376
  // Running: the step it is on. Ended: the count, unless the chip says it.
483
377
  stepEl.textContent = stepSlotText({ step: slots.step, ended: !!endKind, chipHidden: cloud.hidden });
484
378
  promptSlot.hidden = !promptSlot.firstChild;
485
379
  extras.hidden = !extras.firstChild;
486
380
  answer.hidden = !answer.firstChild;
487
381
  tail.hidden = !tail.firstChild;
488
- }
489
-
490
- // A card stopped waiting and its slot has its cap and own scroll again: the
491
- // slot's end shows (the answered card's line). The stage's scroll stays, so
492
- // nothing moves at the tap (the frame holds, and the blank goes at the next
493
- // step), unless the reader had scrolled down the tall card past its line:
494
- // then the line comes back, to the view's top and no further.
495
- function settleAnsweredCard() {
496
- const slot = tail.firstChild ? tail : extras;
497
- if (!slot.firstChild) return;
498
- slot.scrollTop = slot.scrollHeight;
499
- const end = slot.offsetTop + slot.offsetHeight;
500
- // Strictly below: offsetTop/offsetHeight are rounded, so a scroll exactly
501
- // at the line's edge can leave it a fraction of a pixel above the view.
502
- if (scroller.scrollTop < end - ANSWERED_LINE) return;
503
- scroller.scrollTop = Math.max(0, Math.min(slot.lastElementChild.offsetTop, end - ANSWERED_LINE) - ANSWERED_GAP);
504
- }
505
-
506
- // The prompt's first words (the working set's session tags say whose
507
- // turn a step was).
508
- function promptWords() {
509
- const text = (promptEl?.querySelector(".user-message")?.textContent || "").replace(/\s+/g, " ").trim();
510
- return text.length > 60 ? `${text.slice(0, 59)}\u2026` : text;
382
+ // The registry against the slots: a debug check only (stage_asking.js).
383
+ ask.reconcileWaiting();
511
384
  }
512
385
 
513
386
  // The next step after a blocking card's answer, or an answer that leaves
@@ -527,31 +400,6 @@ export function createStageView({
527
400
  frame.forgetContent();
528
401
  }
529
402
 
530
- // ── the quiet signal (stage_stall.js) ─────────────────────────────────────
531
-
532
- // At +nowMs+ (the tick's, a refresh's): what the turn's silence says.
533
- function drawStall(nowMs, blocked = pendingCard()) {
534
- const { turn } = view.current();
535
- if (!holding || !turn) return;
536
- if (streamLive()) downSince = null;
537
- else downSince ??= nowMs;
538
- stall.draw(stallState({
539
- turn, lastEventAt, nowMs, blocked, note: timingNote() || "", ended: !!endKind, graceUntil, serverGen: serverGen(),
540
- streamDownMs: downSince === null ? null : nowMs - downSince,
541
- }));
542
- }
543
-
544
- // A turn event arrived (a replay's too): the silence starts over, and a
545
- // stall shown clears at once.
546
- function alive() {
547
- lastEventAt = clock();
548
- if (stall.level()) drawStall(lastEventAt);
549
- }
550
-
551
- document.addEventListener("visibilitychange", () => {
552
- if (!document.hidden) graceUntil = clock() + GRACE_MS;
553
- });
554
-
555
403
  // One line in the trail for FLASH_MS, ahead of the calls (a newer flash
556
404
  // takes its place).
557
405
  function flashTrail(what) {
@@ -562,58 +410,13 @@ export function createStageView({
562
410
  flashTimer = setTimeout(scheduleRefresh, FLASH_MS + 50);
563
411
  }
564
412
 
565
- // The answer reads from its top (no tail pin in the stage): scrolled to
566
- // (eased, once the stage has grown for it) when its top is out of sight
567
- // or only its first line shows.
568
- function showAnswerTop() {
569
- frame.afterGrowth(() => {
570
- if (!answer.firstChild || answer.hidden || stage.hidden) return;
571
- const top = answer.offsetTop;
572
- const seen = scroller.scrollTop + scroller.clientHeight - top;
573
- if (top < scroller.scrollTop || seen < Math.min(answer.offsetHeight - ANSWER_CUT, ANSWER_SEEN)) frame.easeStage(Math.max(0, top - 8));
574
- });
575
- }
576
-
577
413
  // ── collapse, cloud, peek ────────────────────────────────────────────────
578
414
 
579
- function setCloud(open) {
580
- cloudOpen = open;
581
- cloudBody.hidden = !open || cloud.hidden;
582
- // Closed by hand: its room isn't held (the stage shrinks back to what
583
- // shows).
584
- if (!open) frame.forgetContent();
585
- cloud.classList.toggle("open", open);
586
- stage.classList.toggle("cloud-open", open);
587
- chip.setAttribute("aria-expanded", String(open));
588
- frame.fitCap();
589
- // The steps open right above the chip: in sight (the answer and the
590
- // cards above them scroll up), once the stage has grown for them.
591
- if (open) frame.afterGrowth(() => frame.reveal(cloudBody));
592
- }
593
-
594
- // Open the cloud at step +i+ (a hand toggle, as a click on its row), its
595
- // reasoning too with +thinking+.
596
- function openStep(i, { thinking = false } = {}) {
597
- const step = view.step(i);
598
- if (!step) return;
599
- // An opened turn's step is in the history: shown there.
600
- if (opened) {
601
- if (!step.el.open) step.summary.click();
602
- opener.reveal(step.el);
603
- return;
604
- }
605
- if (isCollapsed()) setCollapsed(false);
606
- setCloud(true);
607
- if (!step.el.open) step.summary.click();
608
- const wrap = step.thinkingWrap;
609
- if (thinking && wrap && !wrap.open) wrap.querySelector(":scope > summary")?.click();
610
- requestAnimationFrame(() => step.el.scrollIntoView({ block: "nearest" }));
611
- }
612
-
613
- function peek() {
614
- const { turn } = view.current();
615
- if (turn?.gens.length) openStep(turn.gens.length - 1, { thinking: true });
616
- }
415
+ // The cloud and its toggles (stage_cloud.js).
416
+ const cloudView = createStageCloud({
417
+ stage, cloud, cloudBody, chip, headline, view, opener, frame: () => frame, isOpened: () => opened, isCollapsed, setCollapsed,
418
+ reveal: () => ask.reveal(), isHolding: () => holding, isFolding: () => handoff.isFolding(), finishFold: () => handoff.finishFold(),
419
+ });
617
420
 
618
421
  fold.addEventListener("click", (e) => {
619
422
  e.stopPropagation();
@@ -622,33 +425,6 @@ export function createStageView({
622
425
  status.addEventListener("click", (e) => {
623
426
  if (isCollapsed() && !e.target.closest("button, a")) setCollapsed(false);
624
427
  });
625
- chip.addEventListener("click", (e) => {
626
- const tick = e.target.closest(".ts-tick");
627
- if (tick) openStep(Number(tick.dataset.step));
628
- else setCloud(!cloudOpen);
629
- });
630
- headline.addEventListener("click", peek);
631
- // A ref's "show in turn" (refs.js) names a row in the cloud: open the
632
- // stage and the cloud to it, as a hand toggle would.
633
- // A collapsed stage opens for this turn only: the kept choice
634
- // (chi_stage_collapsed) is left alone and comes back at the release.
635
- // The row's scroll into view (refs.js) waits for the stage's growth.
636
- // A row of a turn sliding away is no longer the stage's: it goes into the
637
- // history first (finishFold), and refs.js finds it there.
638
- stage.addEventListener("ref:reveal", (e) => {
639
- if (!cloudBody.contains(e.target)) return;
640
- if (folding) return finishFold();
641
- if (!holding) return;
642
- if (isCollapsed()) ask.reveal();
643
- setCloud(true);
644
- if (e.detail) e.detail.until = new Promise((resolve) => frame.afterGrowth(resolve));
645
- });
646
- headline.addEventListener("keydown", (e) => {
647
- if (e.key === "Enter" || e.key === " ") {
648
- e.preventDefault();
649
- peek();
650
- }
651
- });
652
428
  promptSlot.addEventListener("click", (e) => {
653
429
  if (e.target.closest("button, a, img")) return;
654
430
  if (window.getSelection()?.isCollapsed === false) return;
@@ -662,7 +438,7 @@ export function createStageView({
662
438
  observed: [status, promptSlot, live, extras, asksText, answer, tail, cloudBody, cloud, queuedSlot],
663
439
  isHolding: () => holding,
664
440
  isCollapsed,
665
- isCloudOpen: () => cloudOpen,
441
+ isCloudOpen: () => cloudView.isOpen(),
666
442
  isRoomCap: () => roomCap,
667
443
  waitingCard: () => (stage.classList.contains("waiting") ? cardOf(waiting.front()) : null),
668
444
  });
@@ -675,20 +451,13 @@ export function createStageView({
675
451
  alsoWatched: () => opener.watched(),
676
452
  watching: () => holding && !!endKind,
677
453
  onTick: ({ inUse, quietSinceMs, nowMs }) => handOffDue({ ended: true, pending: !settled || pendingCard(), inUse, quietSinceMs, nowMs }),
678
- onDue: () => handOff(),
454
+ onDue: () => handoff.handOff(),
679
455
  });
680
456
 
681
457
  // ── show, clear, hand off ────────────────────────────────────────────────
682
458
 
683
- // A fold cut short (the stage shows again): the slide's class goes.
684
- function cancelFold() {
685
- clearTimeout(foldTimer);
686
- foldTimer = null;
687
- stage.classList.remove("leaving");
688
- }
689
-
690
459
  function show() {
691
- cancelFold();
460
+ handoff.cancelFold();
692
461
  const hidden = stage.hidden;
693
462
  stage.hidden = false;
694
463
  applyCollapsed();
@@ -718,8 +487,7 @@ export function createStageView({
718
487
  cancel.hidden = true;
719
488
  chipLabel.textContent = "";
720
489
  slotsView.reset();
721
- stall.reset();
722
- downSince = null;
490
+ quiet.reset();
723
491
  rail?.reset();
724
492
  promptSlot.classList.remove("full");
725
493
  thinkingLine = null;
@@ -729,11 +497,12 @@ export function createStageView({
729
497
  markOwnToggles();
730
498
  opener.reset();
731
499
  waiting.reset();
500
+ placer.reset();
732
501
  dockedAtLastRefresh = [];
733
502
  miniTool.classList.remove("ts-mini-question");
734
- setCloud(false);
503
+ cloudView.reset();
735
504
  stage.classList.remove("plain", "ended", "waiting", "held-wait", "in-use", "asking-form", "asks-live");
736
- pager.hidden = true;
505
+ pagerView.reset();
737
506
  stage.style.removeProperty("--live-held");
738
507
  }
739
508
 
@@ -755,7 +524,7 @@ export function createStageView({
755
524
  // The stage empty and hidden, its frame free.
756
525
  function clearStage() {
757
526
  dropBare();
758
- cancelFold();
527
+ handoff.cancelFold();
759
528
  emptySlots();
760
529
  stage.hidden = true;
761
530
  frame.frameFree();
@@ -768,94 +537,18 @@ export function createStageView({
768
537
  clearStage();
769
538
  }
770
539
 
771
- // The hand-off: everything the stage holds goes into the history's end in
772
- // the turn block's order (prompt, block (chronological; its outline open,
773
- // else collapsed), extras, answer, timing line, then the end line and
774
- // what came after the end). The turn is released at once; the stage
775
- // slides away under the composer still drawn (a transform: nothing on the
776
- // page moves; app.js eases the history's bottom reserve meanwhile), then
777
- // the pieces arrive in the history and it follows a reader who was at its
778
- // end. Not with +immediate+ (another turn takes the stage at once), a
779
- // reduced motion, a hidden page or a collapsed stage: all at once.
780
- let folding = null; // { pieces, block, endKind, wasAtEnd, animate } while it slides
781
- function handOff({ immediate = false } = {}) {
782
- if (folding && immediate) finishFold();
783
- if (!holding || !endKind) return;
784
- const { blockEl } = view.current();
785
- view.chronological();
786
- const place = blockPlace(blockEl);
787
- // A finished turn's block draws as a reload draws it.
788
- if (blockEl) delete blockEl.dataset.openedTurn;
789
- // The block moves with the pieces from the cloud; an opened turn's is
790
- // in the history already.
791
- const block = place === "cloud" ? blockEl : null;
792
- // The turn's set joins the page's turns (a plain one's too: the turns
793
- // keep their places); its footprint takes the block's summary line,
794
- // wherever the block is.
795
- const footprint = workingSet ? workingSet.handOff(view.current().turn, promptWords()) : null;
796
- if ((place === "cloud" || place === "history") && footprint) blockEl.querySelector(":scope > summary > .tw-sum")?.prepend(footprint);
797
- const pieces = handOffOrder({
798
- // The prompt still in its slot (an opened turn's is in the history).
799
- prompt: !opened && promptEl?.parentNode === promptSlot ? promptEl : null,
800
- block,
801
- extras: [...extras.children].map((el) => { el.classList.remove("ts-behind"); return el; }),
802
- answer: [...answer.children],
803
- timing: timingSlot.firstElementChild,
804
- kept: [...tail.children],
805
- queued: [...queuedSlot.children],
806
- });
807
- const animate = !immediate && !reducedMotion() && !document.hidden && !isCollapsed();
808
- folding = { pieces, block: blockEl, endKind, wasAtEnd: historyNearBottom(), animate, immediate, opened, prompt: promptEl };
809
- releaseTurn();
810
- if (!animate) return finishFold();
811
- stage.classList.add("leaving");
812
- foldTimer = setTimeout(finishFold, FOLD_MS);
813
- }
814
-
815
- // The fold's end (or a cut-short fold): the original pieces (their ref
816
- // bindings, selection and late updates with them) move into the history.
817
- function finishFold() {
818
- const fold = folding;
819
- if (!fold) return;
820
- folding = null;
821
- // The stage held it open; here it takes its place in the history:
822
- // closed, unless the turn was cut short with an outline (block_open.js).
823
- if (fold.block) fold.block.open = openAfterEnd(fold.endKind, fold.block.classList.contains("outline"));
824
- // A piece removed while it slid (the old continue card the step-limit
825
- // question replaces) stays gone.
826
- fold.pieces = fold.pieces.filter((el) => el.isConnected);
827
- moveToHistoryEnd(historyEl, fold.pieces, { animate: fold.animate });
828
- // An opened turn's reasons go in the pieces' task (after them: the
829
- // history is as tall as it gets, room to keep the reader's rows in
830
- // place): each answered card's clamp says it now, as the turn reloads.
831
- opener.end();
832
- // The asking card's fold is this turn's: with the turn handed off, there
833
- // is nothing for a reload to bring back.
834
- waiting.clearFold();
835
- clearStage();
836
- if (fold.wasAtEnd) easeScrollTo(historyEl, () => historyEl.scrollHeight - historyEl.clientHeight, { reduce: !fold.animate || reducedMotion() });
837
- // The turn's pieces are in the history now (the opt-in reload check,
838
- // reload_check.js), the whole turn from its opening: an opened turn's
839
- // earlier pieces went before, and a wake's or a reminder's turn has no
840
- // prompt (its note opens it); +immediate+: another turn took the stage.
841
- onHandedOff?.({ pieces: wholeTurn(fold), immediate: fold.immediate });
842
- }
540
+ // The hand-off and its fold (stage_handoff.js).
541
+ const handoff = createStageHandoff({
542
+ stage, promptSlot, extras, answer, timingSlot, tail, queuedSlot, historyEl, historyNearBottom, view, opener, waiting,
543
+ workingSet, isHolding: () => holding, ended: () => endKind, isOpened: () => opened, promptEl: () => promptEl,
544
+ blockPlace, promptWords, isCollapsed, releaseTurn, clearStage, onHandedOff,
545
+ });
843
546
 
844
- // A handed-off turn from its opening: its prompt, else the wake's note
845
- // (data-opening-id) right before its first piece; the pieces as they are
846
- // when the first is its opening, or nothing opens it.
847
- function wholeTurn(fold) {
848
- const first = fold.pieces[0];
849
- if (first?.matches?.(OPENING) && !fold.opened) return fold.pieces;
850
- let opening = fold.prompt?.isConnected && fold.prompt.parentElement === historyEl ? fold.prompt : null;
851
- for (let el = (fold.block || first)?.previousElementSibling; !opening && el; el = el.previousElementSibling) {
852
- if (el.matches("[data-turn-id]")) break;
853
- if (el.matches("[data-opening-id]")) opening = el;
854
- }
855
- if (!opening) return fold.pieces;
856
- // Its rows, notes too (a note that reached the history mid-turn is the turn's).
857
- return [opening, ...turnElements(opening, { container: historyEl, boundary: null })];
858
- }
547
+ // Where a row or a card goes while the stage holds a turn (stage_place.js).
548
+ const placer = createStagePlace({
549
+ historyEl, extras, tail, queuedSlot, ensureQueuedSlot, handoff, quiet, view, opener, waiting, ask, frame: () => frame, refresh,
550
+ scheduleRefresh, holding: () => holding, opened: () => opened, endKind: () => endKind, promptEl: () => promptEl, historyNearBottom,
551
+ });
859
552
 
860
553
  function dropBare() {
861
554
  if (!bare) return;
@@ -868,13 +561,13 @@ export function createStageView({
868
561
 
869
562
  return {
870
563
  turnStarted(data) {
871
- if (folding) finishFold();
564
+ handoff.settle();
872
565
  dropBare();
873
566
  // A turn that never ended (released, not handed off) leaves its queued
874
567
  // lines waiting still: they stay.
875
568
  const waiting = [...queuedSlot.children].filter((el) => el !== data?.promptBubble);
876
569
  if (holding) {
877
- if (endKind) handOff({ immediate: true });
570
+ if (endKind) handoff.handOff({ immediate: true });
878
571
  else {
879
572
  // A turn that never ended (its end lost): its reasons, still
880
573
  // "asking you", go; what it moved into the history stays.
@@ -890,7 +583,7 @@ export function createStageView({
890
583
  settled = true;
891
584
  startedAt = Date.parse(data?.started_at) || Date.now();
892
585
  emptySlots();
893
- lastEventAt = clock();
586
+ quiet.alive();
894
587
  // The turn's prompt bubble (app.js found or drew it) sits in the prompt
895
588
  // line until the hand-off takes it back first.
896
589
  promptEl = data?.promptBubble || null;
@@ -905,28 +598,28 @@ export function createStageView({
905
598
  refresh();
906
599
  },
907
600
  generationStarted(data, now = null) {
908
- alive();
601
+ quiet.alive();
909
602
  releaseHeldWait();
910
603
  view.generationStarted(data, now);
911
604
  },
912
605
  chunk(data) {
913
- alive();
606
+ quiet.alive();
914
607
  return view.chunk(data);
915
608
  },
916
609
  generationRestarted(data) {
917
- alive();
610
+ quiet.alive();
918
611
  return view.generationRestarted(data);
919
612
  },
920
613
  generationCompleted() {
921
- alive();
614
+ quiet.alive();
922
615
  return view.generationCompleted();
923
616
  },
924
617
  toolStarted(data) {
925
- alive();
618
+ quiet.alive();
926
619
  return view.toolStarted(data);
927
620
  },
928
621
  toolCompleted(data) {
929
- alive();
622
+ quiet.alive();
930
623
  return view.toolCompleted(data);
931
624
  },
932
625
  turnEnded(args) {
@@ -956,13 +649,13 @@ export function createStageView({
956
649
  // A hook's notice and a plugin's nudge go into their step inside the
957
650
  // closed cloud; they flash in the trail meanwhile (flashOf).
958
651
  notice(data) {
959
- alive();
652
+ quiet.alive();
960
653
  const placed = view.notice(data);
961
654
  if (placed) flashTrail(flashOf("notice", data));
962
655
  return placed;
963
656
  },
964
657
  steer(data) {
965
- alive();
658
+ quiet.alive();
966
659
  const placed = view.steer(data);
967
660
  if (placed) flashTrail(flashOf("steer", data));
968
661
  return placed;
@@ -970,7 +663,7 @@ export function createStageView({
970
663
  // A warn card (a plugin's warning) stays in sight: false puts it in the
971
664
  // extras (place), not in its step inside the closed cloud.
972
665
  card(el) {
973
- alive();
666
+ quiet.alive();
974
667
  return !el.classList.contains("warn") && view.card(el);
975
668
  },
976
669
  flush: () => view.flush(),
@@ -984,7 +677,7 @@ export function createStageView({
984
677
  askingText: (pq) => askedText(pq),
985
678
  // A lead-in bubble drawn after this pairs them (askingRefs).
986
679
  questionRefs(refs) {
987
- alive();
680
+ quiet.alive();
988
681
  view.questionRefs(refs);
989
682
  scheduleRefresh();
990
683
  },
@@ -998,111 +691,29 @@ export function createStageView({
998
691
  // sliding away is dropped (its history is about to be drawn anew),
999
692
  // never moved into whatever history shows now.
1000
693
  reset() {
1001
- folding = null;
694
+ handoff.reset();
1002
695
  release();
1003
696
  },
1004
- // The stage's extras while it holds a turn (a card, a prompt for its
1005
- // next step, a command, a note, the end line), its queued slot for a
1006
- // line queued for after it; false: the history takes it.
1007
- place(el) {
1008
- // A turn still sliding away goes into the history first: what comes
1009
- // now comes after it.
1010
- if (folding) finishFold();
1011
- const waits = !!(el.matches?.(PENDING) || el.querySelector?.(ASKS));
1012
- const kind = placeKind(el);
1013
- const where = placeFor({ live: holding, handingOff: false, kind, opened: opened && !endKind });
1014
- // A late recap (idle-only, it describes the chat before this turn)
1015
- // goes above an opened turn, not inside it.
1016
- const above = kind === "recap" && opened ? [promptEl, view.current().blockEl].find((x) => x?.parentElement === historyEl) : null;
1017
- if (above) {
1018
- historyEl.insertBefore(el, above);
1019
- return true;
1020
- }
1021
- if (where === "history") return false;
1022
- alive();
1023
- if (where === "queued") {
1024
- ensureQueuedSlot();
1025
- queuedSlot.appendChild(el);
1026
- scheduleRefresh();
1027
- return true;
1028
- }
1029
- // An opened turn's row: the history's end, in arrival order; a card
1030
- // that stopped waiting before it (a replay answers it and steers in
1031
- // one task, before the refresh) goes there first.
1032
- if (where === "opened") {
1033
- ask.reconcileWaiting();
1034
- settleCards();
1035
- opener.add(el);
1036
- scheduleRefresh();
1037
- return true;
1038
- }
1039
- // A card that waits for the user docks: the turn opens in the
1040
- // history, a lead-in lands there in this task, the card stays here.
1041
- const docked = docks(el);
1042
- const key = waitingKey(el);
1043
- // A card drawn again with its key (the same question) keeps its lead-in.
1044
- const kept = docked && opener.has(key);
1045
- const reason = docked && !kept ? leadInText(el) : "";
1046
- // Read before the turn's pieces make the history taller: with no
1047
- // lead-in to read, a reader at the end follows the turn's move.
1048
- const atEnd = docked ? historyNearBottom() : false;
1049
- if (docked) openTurn({ follow: atEnd && !reason });
1050
- const slot = endKind ? tail : extras;
1051
- const stick = slot.scrollHeight - slot.scrollTop - slot.clientHeight < 24;
1052
- // The stage's order (stage_waiting.js): a card that blocks goes before
1053
- // the asking ones, as a reload places them.
1054
- const behind = isBlocking(waitingKind(el)) ? [...slot.children].find((x) => waitingKind(x) === "asks") : null;
1055
- slot.insertBefore(el, behind || null);
1056
- slot.hidden = false;
1057
- // The card is the stage's to keep (stage_waiting.js): docked when it
1058
- // waits for the user (its lead-in is the opener's, by the same key).
1059
- const waitKind = waitingKind(el);
1060
- if (waitKind) waiting.arrive(key, { kind: waitKind, docked, approval: isApprovalCard(el) });
1061
- if (stick || waits) slot.scrollTop = slot.scrollHeight;
1062
- if (kept) opener.relink(key, el);
1063
- if (reason) {
1064
- const { turn } = view.current();
1065
- opener.ask({
1066
- card: el, key, text: reason, refs: askingRefs(turn) || askingGen(asksOf(el))?.refs || null, step: turn?.gens.length || 0,
1067
- turnId: promptEl?.dataset.turnId || null, questionId: el.dataset.qid ?? null, follow: atEnd,
1068
- });
1069
- }
1070
- // The step limit: its card goes under its ended turn in the
1071
- // conversation at once, as a reload draws it (the hand-off: its fold,
1072
- // the history's follow and the reload check as for any finished turn;
1073
- // the in-use watch and the end read don't hold it).
1074
- if (endKind && waitKind === "continue") {
1075
- handOff();
1076
- return true;
1077
- }
1078
- // A waiting card and the asking form (refresh) land in one task.
1079
- if (waits) refresh();
1080
- else scheduleRefresh();
1081
- // The asking form: the stage shrinks to the card; an answer can also
1082
- // shrink it to a remaining card, or release it at the next step.
1083
- if (docked) frame.forgetContent();
1084
- if (waits && el.matches?.(".bubble.question")) askedFor(el);
1085
- // A card that waits for the user is brought into the stage's view: in
1086
- // a short window the prompt and headline fill it and the card sat
1087
- // below, out of sight (once the stage has grown for it).
1088
- if (waits) frame.afterGrowth(() => frame.reveal(slot));
1089
- return true;
1090
- },
697
+ // The stage's extras while it holds a turn, its queued slot, an opened
698
+ // turn's history end; false: the history takes it (stage_place.js).
699
+ place: (el) => placer.place(el),
700
+ // What a card about to be placed waits as (card_kinds.js; stage_place.js).
701
+ expectWaiting: (key, info) => placer.expect(key, info),
1091
702
  // Where the live timing line goes (the status row), null: the history.
1092
703
  timingHost: () => {
1093
- if (folding) finishFold();
704
+ handoff.settle();
1094
705
  return holding ? timingSlot : null;
1095
706
  },
1096
707
  // Before anything else goes into the history (app.js): a turn still
1097
708
  // sliding away gets there first.
1098
- settleFold: () => { if (folding) finishFold(); },
709
+ settleFold: () => handoff.settle(),
1099
710
  // A turn is in the stage (running, or ended and not handed off yet):
1100
711
  // nothing the turn does touches the history's scroll.
1101
712
  holds: () => holding,
1102
713
  // The page's turn runs but the stage holds none (+on+); false: it ended.
1103
714
  // A turn the stage holds draws its own row: nothing to do then.
1104
715
  runningWithoutTurn(on) {
1105
- if (holding || folding) return;
716
+ if (holding || handoff.isFolding()) return;
1106
717
  if (!on) {
1107
718
  if (bare) clearStage();
1108
719
  return;
@@ -1118,13 +729,13 @@ export function createStageView({
1118
729
  // The page's clock ticks (app.js, the live timing's 200 ms / 1 s): the
1119
730
  // quiet signal and the running call's clock.
1120
731
  tick(nowMs = clock()) {
1121
- drawStall(nowMs);
732
+ quiet.tick(nowMs);
1122
733
  if (holding && asksCard() && liveSlots(view.current().turn).tool?.startedAt != null) scheduleRefresh();
1123
734
  },
1124
735
  callClockActive: () => holding && asksCard() && !!liveSlots(view.current().turn).tool,
1125
736
  // An input of the hooks changed (a participant's ownerWait, canCancel).
1126
737
  redraw: () => scheduleRefresh(),
1127
- handOff,
738
+ handOff: handoff.handOff,
1128
739
  // A card stopped waiting (stage_waiting.js keys; +only+: when it is that
1129
740
  // kind): question_cards.js's resolve path, cards_place.js upsertCard.
1130
741
  // The stage's redraw comes from the card's own change.
@@ -1136,6 +747,22 @@ export function createStageView({
1136
747
  opener.settle(key, card);
1137
748
  return true;
1138
749
  },
750
+ // A plugin card redrawn in place that still waits (cards_place.js
751
+ // upsertCard; +info+: card_kinds.js cardWait): its entry patched, or, for
752
+ // a card in the stage that waits again (its actions back), arrived anew;
753
+ // the stage redrawn at once (a new title changes no child: the extras'
754
+ // observer doesn't see it).
755
+ // An older stage_waiting.js (a mixed cache) has no update: arrive again,
756
+ // which keeps an entry's order.
757
+ updateWaiting: (key, info) => {
758
+ const was = waiting.get(key);
759
+ if (!waiting.update?.(key, info)) {
760
+ const card = cardIn(extras, key) || cardIn(tail, key);
761
+ if (!info?.kind || (!was && !card)) return;
762
+ waiting.arrive(key, { kind: info.kind, docked: was ? was.docked : ask.docks(card), approval: !!was?.approval, summary: info.summary });
763
+ }
764
+ refresh();
765
+ },
1139
766
  // The waiting cards as data (the tests' and the harness's read).
1140
767
  waiting: { list: () => waiting.list(), front: () => waiting.front(), docked: () => waiting.docked(), folded: () => waiting.folded(), count: (o) => waiting.count(o) },
1141
768
  root: stage,