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
@@ -0,0 +1,144 @@
1
+ // Where a row or a card goes while the stage holds a turn (stage_view.js's
2
+ // place, wired there): the stage's extras or its tail, its queued slot, the
3
+ // history's end of a turn opened there, or the history itself (false). A card
4
+ // that waits for the user docks: the turn opens in the history (stage_asking.js
5
+ // openTurn), its lead-in lands there in the card's task (stage_opened.js), the
6
+ // card waits in the stage (stage_waiting.js arrive). The step limit's card
7
+ // hands the turn off at once (stage_handoff.js). What a card waits as comes
8
+ // from its data (card_kinds.js, +expect+ right before its place); the node's
9
+ // own read (waitingKind) is the cross-check, and the fallback for a card
10
+ // that came without it (an older question_cards.js, a test's bare card).
11
+ import { placeFor, placeKind } from "./stage_model.js";
12
+ import { askingRefs } from "./turn_model.js";
13
+ import { ASKS, PENDING } from "./stage_cards.js";
14
+ import { isApprovalCard, isBlocking, waitingKey, waitingKind } from "./stage_waiting.js";
15
+ import { warnDiffers } from "./card_kinds.js";
16
+
17
+ // +historyEl+, +extras+, +tail+, +queuedSlot+: the history and the stage's
18
+ // slots; +ensureQueuedSlot()+ puts the queued slot back in the stage.
19
+ // +handoff+ (stage_handoff.js), +quiet+ (stage_quiet_clock.js), +view+ (the
20
+ // turn block), +opener+ (stage_opened.js), +waiting+ (stage_waiting.js) and
21
+ // +ask+ (stage_asking.js, whole: the docking, the lead-in, the opened row).
22
+ // +frame()+: the stage's frame (stage_frame.js); +refresh()+ and
23
+ // +scheduleRefresh()+ the stage's; +holding()+, +opened()+, +endKind()+ and
24
+ // +promptEl()+ the stage's turn; +historyNearBottom()+ the history's follow
25
+ // check.
26
+ export function createStagePlace({
27
+ historyEl, extras, tail, queuedSlot, ensureQueuedSlot, handoff, quiet, view, opener, waiting, ask, frame, refresh,
28
+ scheduleRefresh, holding, opened, endKind, promptEl, historyNearBottom,
29
+ }) {
30
+ // key → what the card about to be placed waits as ({kind, approval,
31
+ // summary}): taken by its place, whatever the outcome (a history card's
32
+ // too), cleared with the stage's slots (reset).
33
+ const expected = new Map();
34
+
35
+ // The card's wait: its data, else the node's read; the two checked
36
+ // against each other.
37
+ function waitOf(el, key) {
38
+ const data = key ? expected.get(key) : undefined;
39
+ if (key) expected.delete(key);
40
+ const dom = { kind: waitingKind(el), approval: isApprovalCard(el) };
41
+ if (!data) return { ...dom, summary: null };
42
+ if ((data.kind ?? null) !== dom.kind || !!data.approval !== dom.approval) warnDiffers("kind", key, data, dom);
43
+ return { kind: data.kind ?? null, approval: !!data.approval, summary: data.summary ?? null };
44
+ }
45
+
46
+ // The stage's extras while it holds a turn (a card, a prompt for its
47
+ // next step, a command, a note, the end line), its queued slot for a
48
+ // line queued for after it; false: the history takes it.
49
+ function place(el) {
50
+ // A turn still sliding away goes into the history first: what comes
51
+ // now comes after it.
52
+ handoff.settle();
53
+ const key = waitingKey(el);
54
+ const wait = waitOf(el, key);
55
+ const waits = !!(el.matches?.(PENDING) || el.querySelector?.(ASKS));
56
+ const kind = placeKind(el);
57
+ const isOpened = opened();
58
+ const ended = endKind();
59
+ const where = placeFor({ live: holding(), handingOff: false, kind, opened: isOpened && !ended });
60
+ // A late recap (idle-only, it describes the chat before this turn)
61
+ // goes above an opened turn, not inside it.
62
+ const above = kind === "recap" && isOpened ? [promptEl(), view.current().blockEl].find((x) => x?.parentElement === historyEl) : null;
63
+ if (above) {
64
+ historyEl.insertBefore(el, above);
65
+ return true;
66
+ }
67
+ if (where === "history") return false;
68
+ quiet.alive();
69
+ if (where === "queued") {
70
+ ensureQueuedSlot();
71
+ queuedSlot.appendChild(el);
72
+ scheduleRefresh();
73
+ return true;
74
+ }
75
+ // An opened turn's row: the history's end, in arrival order; a card
76
+ // that stopped waiting before it (a replay answers it and steers in
77
+ // one task, before the refresh) goes there first.
78
+ if (where === "opened") {
79
+ ask.settleCards();
80
+ opener.add(el);
81
+ scheduleRefresh();
82
+ return true;
83
+ }
84
+ // A card that waits for the user docks: the turn opens in the
85
+ // history, a lead-in lands there in this task, the card stays here.
86
+ const docked = ask.docks(el);
87
+ // A card drawn again with its key (the same question) keeps its lead-in.
88
+ const kept = docked && opener.has(key);
89
+ const reason = docked && !kept ? ask.leadInText(el) : "";
90
+ // Read before the turn's pieces make the history taller: with no
91
+ // lead-in to read, a reader at the end follows the turn's move.
92
+ const atEnd = docked ? historyNearBottom() : false;
93
+ if (docked) ask.openTurn({ follow: atEnd && !reason });
94
+ const slot = ended ? tail : extras;
95
+ const stick = slot.scrollHeight - slot.scrollTop - slot.clientHeight < 24;
96
+ // The stage's order (stage_waiting.js): a card that blocks goes before
97
+ // the asking ones, as a reload places them.
98
+ const behind = isBlocking(wait.kind) ? [...slot.children].find((x) => waiting.get(waitingKey(x))?.kind === "asks") : null;
99
+ slot.insertBefore(el, behind || null);
100
+ slot.hidden = false;
101
+ // The card is the stage's to keep (stage_waiting.js): docked when it
102
+ // waits for the user (its lead-in is the opener's, by the same key).
103
+ const waitKind = wait.kind;
104
+ if (waitKind) waiting.arrive(key, { kind: waitKind, docked, approval: wait.approval, summary: wait.summary });
105
+ if (stick || waits) slot.scrollTop = slot.scrollHeight;
106
+ if (kept) opener.relink(key, el);
107
+ if (reason) {
108
+ const { turn } = view.current();
109
+ opener.ask({
110
+ card: el, key, text: reason, refs: askingRefs(turn) || ask.askingGen(ask.asksOf(el))?.refs || null, step: turn?.gens.length || 0,
111
+ turnId: promptEl()?.dataset.turnId || null, questionId: el.dataset.qid ?? null, follow: atEnd,
112
+ });
113
+ }
114
+ // The step limit: its card goes under its ended turn in the
115
+ // conversation at once, as a reload draws it (the hand-off: its fold,
116
+ // the history's follow and the reload check as for any finished turn;
117
+ // the in-use watch and the end read don't hold it).
118
+ if (ended && waitKind === "continue") {
119
+ handoff.handOff();
120
+ return true;
121
+ }
122
+ // A waiting card and the asking form (refresh) land in one task.
123
+ if (waits) refresh();
124
+ else scheduleRefresh();
125
+ // The asking form: the stage shrinks to the card; an answer can also
126
+ // shrink it to a remaining card, or release it at the next step.
127
+ if (docked) frame().forgetContent();
128
+ if (waits && el.matches?.(".bubble.question")) ask.askedFor(el);
129
+ // A card that waits for the user is brought into the stage's view: in
130
+ // a short window the prompt and headline fill it and the card sat
131
+ // below, out of sight (once the stage has grown for it).
132
+ if (waits) frame().afterGrowth(() => frame().reveal(slot));
133
+ return true;
134
+ }
135
+
136
+ return {
137
+ place,
138
+ // +key+'s card is placed next; +info+: card_kinds.js's wait.
139
+ expect(key, info) {
140
+ if (key && info) expected.set(key, info);
141
+ },
142
+ reset: () => expected.clear(),
143
+ };
144
+ }
@@ -0,0 +1,64 @@
1
+ // The stage's perf probe (script/stage-perf-probe, docs/testing.md): off
2
+ // unless the page was opened with ?stage_probe=1 (kept for the tab in
3
+ // sessionStorage, chi_stage_probe, so a reload or a hash jump keeps it;
4
+ // ?stage_probe=0 drops it) or <body data-stage-probe="1"> (read at each call:
5
+ // Web Inspector can set it after the page loaded). Probing, +measured+
6
+ // times its function as a performance measure, and the page gets
7
+ // globalThis.chiStageProbe.report(): each name's {n, p50, p95, max} in ms,
8
+ // the measures cleared after. Off, +measured+ only runs the function.
9
+ const KEY = "chi_stage_probe";
10
+ let kept = null;
11
+
12
+ // The tab's flag, read once per page load (storage can throw: off then).
13
+ function keptOn() {
14
+ if (kept !== null) return kept;
15
+ kept = false;
16
+ try {
17
+ const asked = new URLSearchParams(globalThis.location?.search || "").get("stage_probe");
18
+ if (asked === "1") sessionStorage.setItem(KEY, "1");
19
+ else if (asked === "0") sessionStorage.removeItem(KEY);
20
+ kept = sessionStorage.getItem(KEY) === "1";
21
+ } catch (_) {
22
+ kept = false;
23
+ }
24
+ return kept;
25
+ }
26
+
27
+ export const probing = () => globalThis.document?.body?.dataset?.stageProbe === "1" || keptOn();
28
+
29
+ const at = (sorted, q) => sorted[Math.min(sorted.length - 1, Math.floor(q * sorted.length))];
30
+ const round = (ms) => Math.round(ms * 1000) / 1000;
31
+
32
+ // Each name's measures since the last report or clear: past MAX_MEASURES the
33
+ // name's measures are cleared (a page left probing for hours stays small).
34
+ const MAX_MEASURES = 10_000;
35
+ const counts = new Map();
36
+
37
+ export function report() {
38
+ counts.clear();
39
+ const byName = {};
40
+ for (const m of performance.getEntriesByType("measure")) (byName[m.name] ||= []).push(m.duration);
41
+ performance.clearMeasures();
42
+ performance.clearMarks();
43
+ return Object.fromEntries(Object.entries(byName).map(([name, list]) => {
44
+ const sorted = list.sort((a, b) => a - b);
45
+ return [name, { n: sorted.length, p50: round(at(sorted, 0.5)), p95: round(at(sorted, 0.95)), max: round(sorted[sorted.length - 1]) }];
46
+ }));
47
+ }
48
+
49
+ export function measured(name, fn) {
50
+ if (!probing()) return fn();
51
+ globalThis.chiStageProbe ||= { report };
52
+ const start = performance.now();
53
+ try {
54
+ return fn();
55
+ } finally {
56
+ // The probe never breaks the page: a measure the browser refuses is lost.
57
+ try {
58
+ const n = (counts.get(name) || 0) + 1;
59
+ if (n > MAX_MEASURES) performance.clearMeasures(name);
60
+ counts.set(name, n > MAX_MEASURES ? 1 : n);
61
+ performance.measure(name, { start, end: performance.now() });
62
+ } catch (_) { /* not measured */ }
63
+ }
64
+ }
@@ -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
+ }