samagotchi 0.171.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 (202) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +33 -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 +33 -14
  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 +56 -7
  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 +122 -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 +5 -47
  154. data/lib/samagotchi/web/public/data.js +2 -10
  155. data/lib/samagotchi/web/public/delivery_line.js +17 -60
  156. data/lib/samagotchi/web/public/index.html +10 -30
  157. data/lib/samagotchi/web/public/live_turn.js +1 -1
  158. data/lib/samagotchi/web/public/owner_claim.js +2 -2
  159. data/lib/samagotchi/web/public/participant_bar.js +4 -5
  160. data/lib/samagotchi/web/public/participant_names.js +14 -10
  161. data/lib/samagotchi/web/public/participants_panel.js +29 -62
  162. data/lib/samagotchi/web/public/reload_check.js +2 -3
  163. data/lib/samagotchi/web/public/send_turn.js +4 -23
  164. data/lib/samagotchi/web/public/session_actions.js +4 -45
  165. data/lib/samagotchi/web/public/session_fields.js +0 -1
  166. data/lib/samagotchi/web/public/session_state.js +0 -3
  167. data/lib/samagotchi/web/public/session_view.js +2 -3
  168. data/lib/samagotchi/web/public/settings_panel.js +4 -18
  169. data/lib/samagotchi/web/public/stage_answer.js +56 -0
  170. data/lib/samagotchi/web/public/stage_asking.js +1 -1
  171. data/lib/samagotchi/web/public/stage_cloud.js +93 -0
  172. data/lib/samagotchi/web/public/stage_dom.js +100 -0
  173. data/lib/samagotchi/web/public/stage_handoff.js +154 -0
  174. data/lib/samagotchi/web/public/stage_pager.js +59 -0
  175. data/lib/samagotchi/web/public/stage_quiet_clock.js +52 -0
  176. data/lib/samagotchi/web/public/stage_view.js +90 -411
  177. data/lib/samagotchi/web/public/start_page.js +12 -33
  178. data/lib/samagotchi/web/public/start_settings.js +24 -78
  179. data/lib/samagotchi/web/public/start_toolkit.js +6 -10
  180. data/lib/samagotchi/web/public/stream_handlers.js +1 -9
  181. data/lib/samagotchi/web/public/turn_block.js +3 -7
  182. data/lib/samagotchi/web/public/turn_events.js +15 -6
  183. data/lib/samagotchi/web/public/turn_pieces.js +3 -4
  184. data/lib/samagotchi/web/public/viewer_self.js +8 -2
  185. data/lib/samagotchi/web/qr.rb +1 -1
  186. data/lib/samagotchi/web/session_control.rb +3 -1
  187. data/lib/samagotchi/web/session_create.rb +5 -71
  188. data/lib/samagotchi/web/session_summary.rb +1 -4
  189. data/lib/samagotchi/web/verify_fresh_routes.rb +8 -7
  190. data/lib/samagotchi/web/viewer.rb +3 -3
  191. data/lib/samagotchi/worker.rb +16 -14
  192. data/lib/samagotchi/worker_inbound.rb +4 -3
  193. data/lib/samagotchi/worker_link.rb +17 -0
  194. data/lib/samagotchi/working_set.rb +87 -28
  195. metadata +13 -8
  196. data/lib/samagotchi/guardrails/review_boundary.rb +0 -99
  197. data/lib/samagotchi/participant_mode.rb +0 -86
  198. data/lib/samagotchi/review_apply.rb +0 -131
  199. data/lib/samagotchi/review_command.rb +0 -123
  200. data/lib/samagotchi/review_start.rb +0 -196
  201. data/lib/samagotchi/web/public/review_apply.js +0 -34
  202. data/lib/samagotchi/web/public/review_start.js +0 -55
@@ -23,8 +23,6 @@ export function emptySessionState() {
23
23
  parentId: null,
24
24
  // The previous link of its chain (its `continues`), or null.
25
25
  continues: null,
26
- // A review session's {subjects, from} (its `review`), or null.
27
- review: null,
28
26
  verdicts: [],
29
27
  // The Verify fresh checks running ({of, child_id}; the summary's verify_fresh).
30
28
  verifyFresh: [],
@@ -139,7 +137,6 @@ export function sessionStateFrom(data) {
139
137
  state.toolkitDefault = s.toolkit_default || null;
140
138
  state.parentId = s.parent_id || null;
141
139
  state.continues = s.continues || null;
142
- state.review = s.review || null;
143
140
  state.verdicts = Array.isArray(s.verdicts) ? s.verdicts : [];
144
141
  state.verifyFresh = Array.isArray(s.verify_fresh) ? s.verify_fresh : [];
145
142
  state.createdAt = s.created_at || null;
@@ -21,7 +21,6 @@
21
21
  // bubbles, cards bubbles.js, cards_place.js
22
22
  // markdown markdown_place.js (linkTextRefs, applyFinalMarkdown)
23
23
  // capabilities() the page's capabilities (card.js shownElsewhere, the history's cards)
24
- // can(name) the viewer's (viewer.js): the history's Apply row needs apply
25
24
  // page: {setReviseAnchor(anchor), renderInitRow(), setMarkdownWarning(warning),
26
25
  // setCapabilities(capabilities, editorUrl, viewer), findInTurnRoots(selector), appendToHistory(el),
27
26
  // setTurnRunning(running), mergeTurnTiming(id, timing), applySessionPayload(data, opts),
@@ -50,7 +49,7 @@ export function createSessionView(deps) {
50
49
  const {
51
50
  store, historyEl, getSession,
52
51
  liveTurn, turnDom, questionCards, shownTexts, objectives, featureGates, markdownTip, workingSet = null, wsPreview = null, gapClock,
53
- bubbles, cards, markdown, capabilities, can = () => true, page, later,
52
+ bubbles, cards, markdown, capabilities, page, later,
54
53
  } = deps;
55
54
  const { makeUserBubble, addEndBubble, showGuardrailWarning, showRecap } = bubbles;
56
55
  const { placeSnapshotCards, upsertCard } = cards;
@@ -200,7 +199,7 @@ export function createSessionView(deps) {
200
199
  // (its prompt has no drawing yet: a plain row, no miss).
201
200
  const historyOpts = (data = {}) => ({ thumbs: (images) => thumbsTemplate(store.selected, images), workingSet: !!workingSet,
202
201
  drawings: data.turn_drawings || null, cards: data.cards || null, capabilities: capabilities(),
203
- runningTurnId: data.current_turn?.turn_id || null, apply: can("apply") });
202
+ runningTurnId: data.current_turn?.turn_id || null });
204
203
 
205
204
  // A read drawn the way a reload draws it: the turn blocks only, nothing
206
205
  // placed after them (the opt-in reload check, reload_check.js, compares
@@ -2,14 +2,13 @@
2
2
  // (start_settings.js) over a new session's choices and by the session bar's
3
3
  // (settings_chip.js) over the open session's own: rows of segments
4
4
  // (Thinking, Context), the Context row's Apply and budget disclosure, a
5
- // line for a choice whose row is hidden, the review files' line. The caller
5
+ // line for a choice whose row is hidden. The caller
6
6
  // passes the rows it draws (settingsPanelTemplate's +rows+) and applies the
7
7
  // picks itself; the segments' data-act ("think:low", "ctx:stale",
8
8
  // "ctx-more") is the same for every caller.
9
9
 
10
10
  import { APPLIES, choiceWords, ownValues, strategyValue } from "./llm_context_chip.js";
11
11
  import { DEFAULT, followSegment, segments } from "./level_chip.js";
12
- import { reviewChipModel } from "./review_start.js";
13
12
  import { CONFIG as THINKING } from "./thinking_chip.js";
14
13
  import { html, nothing } from "./vendor/lit-html.js";
15
14
 
@@ -99,23 +98,10 @@ function rowTemplate(row) {
99
98
  : nothing}`;
100
99
  }
101
100
 
102
- // The review files' line: the files set (Edit, Remove), else Review files….
103
- function reviewTemplate(review, ruled) {
104
- const chip = review.length ? reviewChipModel(review) : null;
105
- return html`${ruled ? html`<hr class="sset-rule">` : nothing}<div class="sset-review">${chip
106
- ? html`<span class="sset-review-set" title=${chip.title}>Review: ${chip.text.replace(/^review: /, "")}</span
107
- ><button type="button" class="sset-link" data-act="review">Edit</button><button type="button" class="sset-link"
108
- data-act="review-clear">Remove</button>`
109
- : html`<button type="button" class="sset-link" data-act="review" title=${reviewChipModel([]).title}>Review files…</button>`}</div>`;
110
- }
111
-
112
101
  // The panel: its heading (+kind+ beside it, or none) and close button, the
113
- // +rows+ (rowTemplate's), the review files' line when +review+ is a list
114
- // (null: no review line; a rule above it when a row of segments is drawn),
115
- // and the +note+.
116
- export function settingsPanelTemplate({ heading, kind = null, rows = [], review = null, note }) {
117
- const segmented = rows.some((row) => row?.segments);
102
+ // +rows+ (rowTemplate's) and the +note+.
103
+ export function settingsPanelTemplate({ heading, kind = null, rows = [], note }) {
118
104
  return html`<div class="ctx-pop-head"><strong>${heading}</strong>${kind ? html`<span class="ctx-pop-kind">${kind}</span>` : nothing
119
105
  }<button type="button" class="ctx-pop-close" data-act="close" aria-label="Close">×</button></div
120
- >${rows.map(rowTemplate)}${review ? reviewTemplate(review, segmented) : nothing}<div class="ctx-pop-meta">${note}</div>`;
106
+ >${rows.map(rowTemplate)}<div class="ctx-pop-meta">${note}</div>`;
121
107
  }
@@ -0,0 +1,56 @@
1
+ // The stage's answer reading (stage_view.js): the answer read from its top
2
+ // at the end, an answered card's line kept in sight, and the prompt's first
3
+ // words. Its own module: new exports go in new modules (web-frontend.md).
4
+ // No state of its own: nothing to reset at emptySlots.
5
+
6
+ // The answer reads from its top: at the end it is scrolled to unless this
7
+ // much of it (about three lines) shows already.
8
+ const ANSWER_SEEN = 72;
9
+ // A short answer cut by less than this at its foot isn't scrolled to.
10
+ const ANSWER_CUT = 12;
11
+ // An answered card's one line.
12
+ const ANSWERED_LINE = 40;
13
+ // That line brought back sits this far under the scroller's edge.
14
+ const ANSWERED_GAP = 8;
15
+
16
+ // +stage+, +scroller+ and the +answer+, +extras+ and +tail+ slots
17
+ // (stage_dom.js); +frame+() the stage's frame (stage_frame.js); +promptEl+()
18
+ // the turn's prompt bubble, or null.
19
+ export function createStageAnswer({ stage, scroller, answer, extras, tail, frame, promptEl }) {
20
+ // The answer reads from its top (no tail pin in the stage): scrolled to
21
+ // (eased, once the stage has grown for it) when its top is out of sight
22
+ // or only its first line shows.
23
+ function showAnswerTop() {
24
+ frame().afterGrowth(() => {
25
+ if (!answer.firstChild || answer.hidden || stage.hidden) return;
26
+ const top = answer.offsetTop;
27
+ const seen = scroller.scrollTop + scroller.clientHeight - top;
28
+ if (top < scroller.scrollTop || seen < Math.min(answer.offsetHeight - ANSWER_CUT, ANSWER_SEEN)) frame().easeStage(Math.max(0, top - 8));
29
+ });
30
+ }
31
+
32
+ // A card stopped waiting and its slot has its cap and own scroll again: the
33
+ // slot's end shows (the answered card's line). The stage's scroll stays, so
34
+ // nothing moves at the tap (the frame holds, and the blank goes at the next
35
+ // step), unless the reader had scrolled down the tall card past its line:
36
+ // then the line comes back, to the view's top and no further.
37
+ function settleAnsweredCard() {
38
+ const slot = tail.firstChild ? tail : extras;
39
+ if (!slot.firstChild) return;
40
+ slot.scrollTop = slot.scrollHeight;
41
+ const end = slot.offsetTop + slot.offsetHeight;
42
+ // Strictly below: offsetTop/offsetHeight are rounded, so a scroll exactly
43
+ // at the line's edge can leave it a fraction of a pixel above the view.
44
+ if (scroller.scrollTop < end - ANSWERED_LINE) return;
45
+ scroller.scrollTop = Math.max(0, Math.min(slot.lastElementChild.offsetTop, end - ANSWERED_LINE) - ANSWERED_GAP);
46
+ }
47
+
48
+ // The prompt's first words (the working set's session tags say whose
49
+ // turn a step was).
50
+ function promptWords() {
51
+ const text = (promptEl()?.querySelector(".user-message")?.textContent || "").replace(/\s+/g, " ").trim();
52
+ return text.length > 60 ? `${text.slice(0, 59)}…` : text;
53
+ }
54
+
55
+ return { showAnswerTop, settleAnsweredCard, promptWords };
56
+ }
@@ -139,7 +139,7 @@ export function createStageAsking({
139
139
  if (!isOpened()) return;
140
140
  for (const el of [...extras.children]) {
141
141
  if (waitsInStage(el)) continue;
142
- // Out of the stage it waits behind nothing (stage_view.js drawDocked).
142
+ // Out of the stage it waits behind nothing (stage_pager.js drawDocked).
143
143
  el.classList.remove("ts-behind");
144
144
  const key = waitingKey(el);
145
145
  if (opener.has(key)) opener.settle(key, el);
@@ -0,0 +1,93 @@
1
+ // The stage's cloud (stage_view.js): the turn block's steps opened above
2
+ // the chip, newest first while the turn runs. The chip's toggle and its
3
+ // ticks, the headline's peek at the live step's reasoning, and a ref's "show
4
+ // in turn" (refs.js ref:reveal) open it. Its own module: new exports go in
5
+ // new modules (web-frontend.md).
6
+
7
+ // +stage+, +cloud+, +cloudBody+, +chip+, +headline+: stage_dom.js's nodes;
8
+ // +view+: the turn block (turn_block.js); +opener+: the turn opened in the
9
+ // history (stage_opened.js); +frame+(): the stage's frame (stage_frame.js);
10
+ // +isOpened+(): the turn is opened in the history; +isCollapsed+(),
11
+ // +setCollapsed+(on), +reveal+(): the stage's fold (stage_asking.js);
12
+ // +isHolding+(): the stage holds a turn; +isFolding+(), +finishFold+(): a
13
+ // turn sliding away (stage_view.js handOff).
14
+ export function createStageCloud({
15
+ stage, cloud, cloudBody, chip, headline, view, opener, frame, isOpened, isCollapsed, setCollapsed, reveal,
16
+ isHolding, isFolding, finishFold,
17
+ }) {
18
+ let open = false;
19
+
20
+ function setCloud(value) {
21
+ open = value;
22
+ cloudBody.hidden = !value || cloud.hidden;
23
+ // Closed by hand: its room isn't held (the stage shrinks back to what
24
+ // shows).
25
+ if (!value) frame().forgetContent();
26
+ cloud.classList.toggle("open", value);
27
+ stage.classList.toggle("cloud-open", value);
28
+ chip.setAttribute("aria-expanded", String(value));
29
+ frame().fitCap();
30
+ // The steps open right above the chip: in sight (the answer and the
31
+ // cards above them scroll up), once the stage has grown for them.
32
+ if (value) frame().afterGrowth(() => frame().reveal(cloudBody));
33
+ }
34
+
35
+ // Open the cloud at step +i+ (a hand toggle, as a click on its row), its
36
+ // reasoning too with +thinking+.
37
+ function openStep(i, { thinking = false } = {}) {
38
+ const step = view.step(i);
39
+ if (!step) return;
40
+ // An opened turn's step is in the history: shown there.
41
+ if (isOpened()) {
42
+ if (!step.el.open) step.summary.click();
43
+ opener.reveal(step.el);
44
+ return;
45
+ }
46
+ if (isCollapsed()) setCollapsed(false);
47
+ setCloud(true);
48
+ if (!step.el.open) step.summary.click();
49
+ const wrap = step.thinkingWrap;
50
+ if (thinking && wrap && !wrap.open) wrap.querySelector(":scope > summary")?.click();
51
+ requestAnimationFrame(() => step.el.scrollIntoView({ block: "nearest" }));
52
+ }
53
+
54
+ function peek() {
55
+ const { turn } = view.current();
56
+ if (turn?.gens.length) openStep(turn.gens.length - 1, { thinking: true });
57
+ }
58
+
59
+ chip.addEventListener("click", (e) => {
60
+ const tick = e.target.closest(".ts-tick");
61
+ if (tick) openStep(Number(tick.dataset.step));
62
+ else setCloud(!open);
63
+ });
64
+ headline.addEventListener("click", peek);
65
+ // A ref's "show in turn" (refs.js) names a row in the cloud: open the
66
+ // stage and the cloud to it, as a hand toggle would.
67
+ // A collapsed stage opens for this turn only: the kept choice
68
+ // (chi_stage_collapsed) is left alone and comes back at the release.
69
+ // The row's scroll into view (refs.js) waits for the stage's growth.
70
+ // A row of a turn sliding away is no longer the stage's: it goes into the
71
+ // history first (finishFold), and refs.js finds it there.
72
+ stage.addEventListener("ref:reveal", (e) => {
73
+ if (!cloudBody.contains(e.target)) return;
74
+ if (isFolding()) return finishFold();
75
+ if (!isHolding()) return;
76
+ if (isCollapsed()) reveal();
77
+ setCloud(true);
78
+ if (e.detail) e.detail.until = new Promise((resolve) => frame().afterGrowth(resolve));
79
+ });
80
+ headline.addEventListener("keydown", (e) => {
81
+ if (e.key === "Enter" || e.key === " ") {
82
+ e.preventDefault();
83
+ peek();
84
+ }
85
+ });
86
+
87
+ // The turn left the stage: the cloud closes.
88
+ function reset() {
89
+ setCloud(false);
90
+ }
91
+
92
+ return { setCloud, isOpen: () => open, reset };
93
+ }
@@ -0,0 +1,100 @@
1
+ // The stage's nodes (stage_view.js): the dock made a clear column (the
2
+ // stage's card, then the composer's: its rows moved into .dock-card, which
3
+ // wears the glass and the focus ring) and every slot of the stage, built
4
+ // once. Plain nodes, no behaviour: the listeners and the drawing are the
5
+ // stage's modules'. Its own module: new exports go in new modules
6
+ // (web-frontend.md).
7
+ import { node } from "./stage_slots.js";
8
+
9
+ // +dockEl+: the page's dock. Returns the nodes by name.
10
+ export function createStageDom(dockEl) {
11
+ document.body.classList.add("stage-view");
12
+ const card = node("div", "dock-card composer-card");
13
+ card.append(...dockEl.children);
14
+ dockEl.appendChild(card);
15
+ const stage = node("section", "turn-stage");
16
+ stage.id = "turnStage";
17
+ stage.hidden = true;
18
+ stage.setAttribute("aria-label", "Running turn");
19
+ dockEl.prepend(stage);
20
+
21
+ const status = node("div", "ts-status", stage);
22
+ const mark = node("span", "ts-mark", status);
23
+ const phaseEl = node("span", "ts-phase", status);
24
+ // Two cards docked: one at a time, "‹ 1 of 2 ›" (stage_pager.js).
25
+ const pager = node("span", "ts-pager", status);
26
+ pager.hidden = true;
27
+ const pagePrev = node("button", "ts-page", pager);
28
+ pagePrev.type = "button";
29
+ pagePrev.textContent = "‹";
30
+ pagePrev.setAttribute("aria-label", "The card before");
31
+ const pageLabel = node("span", "ts-page-label", pager);
32
+ const pageNext = node("button", "ts-page", pager);
33
+ pageNext.type = "button";
34
+ pageNext.textContent = "›";
35
+ pageNext.setAttribute("aria-label", "The next card");
36
+ const stepEl = node("span", "ts-step", status);
37
+ const timingSlot = node("span", "ts-timing", status);
38
+ const miniTool = node("span", "ts-mini", status);
39
+ const defer = node("span", "ts-defer", status);
40
+ defer.textContent = "moves up when you leave";
41
+ // Stop turn, at the row's right edge beside the fold: while the turn
42
+ // hasn't ended (refresh), in a collapsed stage too (the row stays).
43
+ const cancel = node("button", "ts-cancel", status);
44
+ cancel.type = "button";
45
+ cancel.title = "Cancel the running turn, whoever sent it";
46
+ cancel.textContent = "Stop turn";
47
+ cancel.hidden = true;
48
+ const fold = node("button", "ts-fold", status);
49
+ fold.type = "button";
50
+
51
+ const scroller = node("div", "ts-scroll", stage);
52
+ const promptSlot = node("div", "ts-prompt", scroller);
53
+ promptSlot.title = "Show the whole prompt";
54
+ const live = node("div", "ts-live", scroller);
55
+ const headline = node("div", "ts-headline", live);
56
+ headline.tabIndex = 0;
57
+ headline.setAttribute("role", "button");
58
+ headline.title = "Show this step's reasoning";
59
+ // A participant's turn waiting for the owner: one line under the headline,
60
+ // no card (stage_owner_wait.js).
61
+ const ownerWaitLine = node("div", "ts-owner-wait", live);
62
+ const now = node("div", "ts-now", live);
63
+ const trail = node("div", "ts-trail", live);
64
+ // (The working set's rail joins the live slots after the trail:
65
+ // working_set_rail.js.)
66
+ const extras = node("div", "ts-extras", scroller);
67
+ // While check-in's card is docked: the live step's text, streamed under
68
+ // the card (the steps are in the history then). The answer's own slot
69
+ // takes it over at the turn's end (placeAnswer, in one task).
70
+ const asksText = node("div", "ts-asks-text", scroller);
71
+ asksText.hidden = true;
72
+ const answer = node("div", "ts-answer", scroller);
73
+ const tail = node("div", "ts-tail", scroller);
74
+ // The cloud's steps, the scroller's last slot, right above the chip.
75
+ const cloudBody = node("div", "ts-cloud-body", scroller);
76
+ cloudBody.hidden = true;
77
+ // A 1 px mark at the tallest content's end (absolute: out of the flow),
78
+ // so the scroll extent never drops while the turn runs: content that
79
+ // shrinks never lowers a scrolled reader's scrollTop (the browser would
80
+ // clamp it, moving everything in sight), even in the layout before
81
+ // fitFrame runs.
82
+ const holdEl = node("div", "ts-hold", scroller);
83
+ holdEl.setAttribute("aria-hidden", "true");
84
+ // The footer: the chip, pinned to the stage's bottom edge as the status
85
+ // row is to its top.
86
+ const cloud = node("div", "ts-foot", stage);
87
+ const chip = node("button", "ts-chip", cloud);
88
+ chip.type = "button";
89
+ const chipLabel = node("span", "ts-chip-label", chip);
90
+ const ticks = node("span", "ts-ticks", chip);
91
+ // Below the turn's card: the lines queued for after it (placeKind
92
+ // "queued"), each to run as a turn of its own; they hand off last.
93
+ const queuedSlot = node("div", "ts-queued", stage);
94
+
95
+ return {
96
+ stage, status, mark, phaseEl, pager, pagePrev, pageLabel, pageNext, stepEl, timingSlot, miniTool, cancel, fold,
97
+ scroller, promptSlot, live, headline, ownerWaitLine, now, trail, extras, asksText, answer, tail, cloudBody, holdEl,
98
+ cloud, chip, chipLabel, ticks, queuedSlot,
99
+ };
100
+ }
@@ -0,0 +1,154 @@
1
+ // The stage's hand-off (stage_view.js): an ended turn's pieces go into the
2
+ // history's end in the turn block's order, the stage sliding away under the
3
+ // composer meanwhile (the fold). Its own module: new exports go in new
4
+ // modules (web-frontend.md). Its state (the turn sliding away) outlives
5
+ // emptySlots on purpose: the fold runs after the turn's release, and
6
+ // finishFold clears the stage; reset() (the view's reset) drops it.
7
+ import { openAfterEnd } from "./block_open.js";
8
+ import { handOffOrder } from "./stage_model.js";
9
+ import { reducedMotion } from "./stage_frame.js";
10
+ import { easeScrollTo } from "./scroll.js";
11
+ import { moveToHistoryEnd } from "./history_end.js";
12
+ import { turnElements } from "./turn_range.js";
13
+
14
+ // How long the stage slides away (dock_reserve.js eases the history's
15
+ // bottom reserve as long).
16
+ export const FOLD_MS = 420;
17
+ // What opens a turn in the history: its prompt (data-turn-id), else a
18
+ // wake's or a reminder's note or /continue no's line (data-opening-id).
19
+ // reload_check.js reads it too.
20
+ export const OPENING = "[data-turn-id], [data-opening-id]";
21
+
22
+ // +stage+ and the slots the pieces leave (stage_dom.js: +promptSlot+,
23
+ // +extras+, +answer+, +timingSlot+, +tail+, +queuedSlot+); +historyEl+ and
24
+ // +historyNearBottom+(): the history and its follow check; +view+: the turn
25
+ // block (turn_block.js); +opener+: the turn opened in the history
26
+ // (stage_opened.js); +waiting+: the waiting cards (stage_waiting.js);
27
+ // +workingSet+: the page's working set or null; +isHolding+(), +ended+()
28
+ // (the end's kind, null: running), +isOpened+(), +promptEl+(): the stage's
29
+ // turn; +blockPlace+(el), +promptWords+(), +isCollapsed+(): the stage's
30
+ // reads; +releaseTurn+(), +clearStage+(): its lifecycle; +onHandedOff+:
31
+ // createStageView's.
32
+ export function createStageHandoff({
33
+ stage, promptSlot, extras, answer, timingSlot, tail, queuedSlot, historyEl, historyNearBottom, view, opener, waiting,
34
+ workingSet, isHolding, ended, isOpened, promptEl, blockPlace, promptWords, isCollapsed, releaseTurn, clearStage,
35
+ onHandedOff,
36
+ }) {
37
+ let folding = null; // { pieces, block, endKind, wasAtEnd, animate, immediate, opened, prompt } while it slides
38
+ let foldTimer = null;
39
+
40
+ // A fold cut short (the stage shows again): the slide's class goes.
41
+ function cancelFold() {
42
+ clearTimeout(foldTimer);
43
+ foldTimer = null;
44
+ stage.classList.remove("leaving");
45
+ }
46
+
47
+ // The hand-off: everything the stage holds goes into the history's end in
48
+ // the turn block's order (prompt, block (chronological; its outline open,
49
+ // else collapsed), extras, answer, timing line, then the end line and
50
+ // what came after the end). The turn is released at once; the stage
51
+ // slides away under the composer still drawn (a transform: nothing on the
52
+ // page moves; app.js eases the history's bottom reserve meanwhile), then
53
+ // the pieces arrive in the history and it follows a reader who was at its
54
+ // end. Not with +immediate+ (another turn takes the stage at once), a
55
+ // reduced motion, a hidden page or a collapsed stage: all at once.
56
+ function handOff({ immediate = false } = {}) {
57
+ if (folding && immediate) finishFold();
58
+ if (!isHolding() || !ended()) return;
59
+ const { blockEl } = view.current();
60
+ view.chronological();
61
+ const place = blockPlace(blockEl);
62
+ // A finished turn's block draws as a reload draws it.
63
+ if (blockEl) delete blockEl.dataset.openedTurn;
64
+ // The block moves with the pieces from the cloud; an opened turn's is
65
+ // in the history already.
66
+ const block = place === "cloud" ? blockEl : null;
67
+ // The turn's set joins the page's turns (a plain one's too: the turns
68
+ // keep their places); its footprint takes the block's summary line,
69
+ // wherever the block is.
70
+ const footprint = workingSet ? workingSet.handOff(view.current().turn, promptWords()) : null;
71
+ if ((place === "cloud" || place === "history") && footprint) blockEl.querySelector(":scope > summary > .tw-sum")?.prepend(footprint);
72
+ const opened = isOpened();
73
+ const prompt = promptEl();
74
+ const pieces = handOffOrder({
75
+ // The prompt still in its slot (an opened turn's is in the history).
76
+ prompt: !opened && prompt?.parentNode === promptSlot ? prompt : null,
77
+ block,
78
+ extras: [...extras.children].map((el) => { el.classList.remove("ts-behind"); return el; }),
79
+ answer: [...answer.children],
80
+ timing: timingSlot.firstElementChild,
81
+ kept: [...tail.children],
82
+ queued: [...queuedSlot.children],
83
+ });
84
+ const animate = !immediate && !reducedMotion() && !document.hidden && !isCollapsed();
85
+ folding = { pieces, block: blockEl, endKind: ended(), wasAtEnd: historyNearBottom(), animate, immediate, opened, prompt };
86
+ releaseTurn();
87
+ if (!animate) return finishFold();
88
+ stage.classList.add("leaving");
89
+ foldTimer = setTimeout(finishFold, FOLD_MS);
90
+ }
91
+
92
+ // The fold's end (or a cut-short fold): the original pieces (their ref
93
+ // bindings, selection and late updates with them) move into the history.
94
+ function finishFold() {
95
+ const fold = folding;
96
+ if (!fold) return;
97
+ folding = null;
98
+ // The stage held it open; here it takes its place in the history:
99
+ // closed, unless the turn was cut short with an outline (block_open.js).
100
+ if (fold.block) fold.block.open = openAfterEnd(fold.endKind, fold.block.classList.contains("outline"));
101
+ // A piece removed while it slid (the old continue card the step-limit
102
+ // question replaces) stays gone.
103
+ fold.pieces = fold.pieces.filter((el) => el.isConnected);
104
+ moveToHistoryEnd(historyEl, fold.pieces, { animate: fold.animate });
105
+ // An opened turn's reasons go in the pieces' task (after them: the
106
+ // history is as tall as it gets, room to keep the reader's rows in
107
+ // place): each answered card's clamp says it now, as the turn reloads.
108
+ opener.end();
109
+ // The asking card's fold is this turn's: with the turn handed off, there
110
+ // is nothing for a reload to bring back.
111
+ waiting.clearFold();
112
+ clearStage();
113
+ if (fold.wasAtEnd) easeScrollTo(historyEl, () => historyEl.scrollHeight - historyEl.clientHeight, { reduce: !fold.animate || reducedMotion() });
114
+ // The turn's pieces are in the history now (the opt-in reload check,
115
+ // reload_check.js), the whole turn from its opening: an opened turn's
116
+ // earlier pieces went before, and a wake's or a reminder's turn has no
117
+ // prompt (its note opens it); +immediate+: another turn took the stage.
118
+ onHandedOff?.({ pieces: wholeTurn(fold), immediate: fold.immediate });
119
+ }
120
+
121
+ // A handed-off turn from its opening: its prompt, else the wake's note
122
+ // (data-opening-id) right before its first piece; the pieces as they are
123
+ // when the first is its opening, or nothing opens it.
124
+ function wholeTurn(fold) {
125
+ const first = fold.pieces[0];
126
+ if (first?.matches?.(OPENING) && !fold.opened) return fold.pieces;
127
+ let opening = fold.prompt?.isConnected && fold.prompt.parentElement === historyEl ? fold.prompt : null;
128
+ for (let el = (fold.block || first)?.previousElementSibling; !opening && el; el = el.previousElementSibling) {
129
+ if (el.matches("[data-turn-id]")) break;
130
+ if (el.matches("[data-opening-id]")) opening = el;
131
+ }
132
+ if (!opening) return fold.pieces;
133
+ // Its rows, notes too (a note that reached the history mid-turn is the turn's).
134
+ return [opening, ...turnElements(opening, { container: historyEl, boundary: null })];
135
+ }
136
+
137
+ return {
138
+ handOff,
139
+ finishFold,
140
+ cancelFold,
141
+ // A turn still sliding away gets into the history first.
142
+ settle() {
143
+ if (folding) finishFold();
144
+ },
145
+ isFolding: () => !!folding,
146
+ // Another session opens, or this one renders again: a turn still
147
+ // sliding away is dropped, never moved into whatever history shows now.
148
+ reset() {
149
+ folding = null;
150
+ clearTimeout(foldTimer);
151
+ foldTimer = null;
152
+ },
153
+ };
154
+ }
@@ -0,0 +1,59 @@
1
+ // The stage's pager (stage_view.js): one docked card at a time, the others
2
+ // wait behind it (.ts-behind), and "‹ 1 of 2 ›" in the status row says where
3
+ // the shown one is (stage_waiting.js shown, page). Its own module: new
4
+ // exports go in new modules (web-frontend.md).
5
+ import { waitingKey } from "./stage_waiting.js";
6
+
7
+ // +card+'s first control takes the focus (its summary is out of the tab
8
+ // order while it waits), else the card.
9
+ function focusIn(card) {
10
+ const control = card.querySelector('input:not([type="hidden"]), textarea, select, button:not([disabled]), a[href]');
11
+ if (control) control.focus();
12
+ else {
13
+ if (!card.hasAttribute("tabindex")) card.tabIndex = -1;
14
+ card.focus();
15
+ }
16
+ }
17
+
18
+ // +pager+, +pagePrev+, +pageLabel+, +pageNext+, +extras+: stage_dom.js's
19
+ // nodes; +waiting+: the stage's waiting cards (stage_waiting.js); +cardOf+(key)
20
+ // and +askingCard+(): stage_asking.js's; +refresh+(): the stage's redraw;
21
+ // +frame+(): the stage's frame (stage_frame.js).
22
+ export function createStagePager({ pager, pagePrev, pageLabel, pageNext, extras, waiting, cardOf, askingCard, refresh, frame }) {
23
+ // +shownCard+ (null: none docked) in front, the others behind it.
24
+ function drawDocked(shownCard) {
25
+ const keys = waiting.docked();
26
+ for (const key of keys) cardOf(key)?.classList.toggle("ts-behind", !!shownCard && cardOf(key) !== shownCard);
27
+ for (const el of extras.querySelectorAll(":scope > .ts-behind")) if (!keys.includes(waitingKey(el))) el.classList.remove("ts-behind");
28
+ // Focus in a card that went behind (a new card came in front of it):
29
+ // to the card that shows now.
30
+ const focused = document.activeElement;
31
+ if (shownCard && focused && [...extras.querySelectorAll(":scope > .ts-behind")].some((el) => el.contains(focused))) focusIn(shownCard);
32
+ const at = shownCard ? keys.indexOf(waitingKey(shownCard)) : -1;
33
+ pager.hidden = keys.length < 2 || at < 0;
34
+ if (pager.hidden) return;
35
+ const label = `${at + 1} of ${keys.length}`;
36
+ if (pageLabel.textContent !== label) pageLabel.textContent = label;
37
+ pagePrev.disabled = at === 0;
38
+ pageNext.disabled = at === keys.length - 1;
39
+ // A pager button the page just disabled keeps no focus: the other one has it.
40
+ if (document.activeElement === pagePrev && pagePrev.disabled) pageNext.focus();
41
+ else if (document.activeElement === pageNext && pageNext.disabled) pagePrev.focus();
42
+ }
43
+
44
+ for (const [button, by] of [[pagePrev, -1], [pageNext, 1]]) {
45
+ button.addEventListener("click", (e) => {
46
+ e.stopPropagation();
47
+ waiting.page(by);
48
+ refresh();
49
+ const shown = askingCard();
50
+ if (shown) frame().afterGrowth(() => frame().reveal(shown.parentElement));
51
+ });
52
+ }
53
+
54
+ function reset() {
55
+ pager.hidden = true;
56
+ }
57
+
58
+ return { drawDocked, reset };
59
+ }
@@ -0,0 +1,52 @@
1
+ // The quiet signal's clocks (stage_view.js; stage_stall.js draws the
2
+ // signal): the turn's last event, since when the stream isn't live, the
3
+ // grace after the page shows again. Its own module: new exports go in new
4
+ // modules, and an existing module's factory keeps its signature
5
+ // (web-frontend.md).
6
+ import { GRACE_MS, stallState } from "./stage_stall.js";
7
+
8
+ // +stall+: the drawer (stage_stall.js createStageStall). Its inputs
9
+ // (stage_view.js's): +clock+() the page's time, +streamLive+() the
10
+ // session's stream is open, +timingNote+() what the turn waits on
11
+ // (live_timing.js note), +serverGen+() a joiner's generation; +held+() the
12
+ // turn the stage holds (null: none), +ended+() it has ended, +pending+() a
13
+ // card blocks it.
14
+ export function createStageQuietClock({
15
+ stall, clock = () => Date.now(), streamLive = () => true, timingNote = () => "", serverGen = () => null,
16
+ held = () => null, ended = () => false, pending = () => false,
17
+ }) {
18
+ let lastEventAt = 0;
19
+ let downSince = null; // null: the stream is live
20
+ let graceUntil = 0;
21
+
22
+ // At +nowMs+ (the tick's, a refresh's): what the held turn's silence says.
23
+ function tick(nowMs, blocked = pending()) {
24
+ const turn = held();
25
+ if (!turn) return;
26
+ if (streamLive()) downSince = null;
27
+ else downSince ??= nowMs;
28
+ stall.draw(stallState({
29
+ turn, lastEventAt, nowMs, blocked, note: timingNote() || "", ended: ended(), graceUntil, serverGen: serverGen(),
30
+ streamDownMs: downSince === null ? null : nowMs - downSince,
31
+ }));
32
+ }
33
+
34
+ // A turn event arrived (a replay's too), or a turn started: the silence
35
+ // starts over, and a stall shown clears at once.
36
+ function alive() {
37
+ lastEventAt = clock();
38
+ if (stall.level()) tick(lastEventAt);
39
+ }
40
+
41
+ document.addEventListener("visibilitychange", () => {
42
+ if (!document.hidden) graceUntil = clock() + GRACE_MS;
43
+ });
44
+
45
+ // The turn left the stage: the signal goes, the stream's clock too.
46
+ function reset() {
47
+ stall.reset();
48
+ downSince = null;
49
+ }
50
+
51
+ return { tick, alive, reset };
52
+ }