samagotchi 0.165.0 → 0.166.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 (39) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +37 -1
  3. data/docs/cli.md +6 -2
  4. data/docs/configuration.md +3 -1
  5. data/docs/guardrails.md +6 -2
  6. data/docs/internals/bridge.md +6 -0
  7. data/docs/internals/remote-sessions.md +1 -1
  8. data/docs/internals/web-frontend.md +36 -3
  9. data/docs/releasing.md +12 -3
  10. data/docs/sessions.md +4 -4
  11. data/docs/testing.md +12 -3
  12. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  13. data/lib/samagotchi/compat.rb +8 -10
  14. data/lib/samagotchi/guardrails/approvals.rb +36 -9
  15. data/lib/samagotchi/plugin/sessions.rb +4 -1
  16. data/lib/samagotchi/reply_wait/poll.rb +4 -15
  17. data/lib/samagotchi/reply_wait.rb +0 -23
  18. data/lib/samagotchi/run_options.rb +1 -1
  19. data/lib/samagotchi/session_access/remote.rb +4 -19
  20. data/lib/samagotchi/session_inbox.rb +1 -1
  21. data/lib/samagotchi/tools/delegate.rb +2 -4
  22. data/lib/samagotchi/version.rb +1 -1
  23. data/lib/samagotchi/web/public/app.js +15 -8
  24. data/lib/samagotchi/web/public/auth_lost.js +3 -1
  25. data/lib/samagotchi/web/public/bar_fold.js +1 -1
  26. data/lib/samagotchi/web/public/chip_popover.js +5 -1
  27. data/lib/samagotchi/web/public/delivery_line.js +19 -4
  28. data/lib/samagotchi/web/public/index.html +122 -41
  29. data/lib/samagotchi/web/public/info_bar.js +1 -1
  30. data/lib/samagotchi/web/public/info_bar_model.js +6 -1
  31. data/lib/samagotchi/web/public/live_timing.js +3 -0
  32. data/lib/samagotchi/web/public/resync_reach.js +59 -0
  33. data/lib/samagotchi/web/public/session_fields.js +1 -0
  34. data/lib/samagotchi/web/public/session_state.js +5 -0
  35. data/lib/samagotchi/web/public/stage_frame.js +4 -2
  36. data/lib/samagotchi/web/public/stage_stall.js +134 -0
  37. data/lib/samagotchi/web/public/stage_view.js +75 -6
  38. data/lib/samagotchi/web/token.rb +2 -2
  39. metadata +3 -1
@@ -0,0 +1,134 @@
1
+ // The stage's quiet signal (stage_view.js): a running turn that has sent no
2
+ // event for a while says so, since the dot blinks the same whether the
3
+ // model works or the line went dead (the Bridge's idle `: ping` never
4
+ // reaches EventSource's JS, and a running tool sends nothing between its
5
+ // start and end). The code says "stall" (stallOf, .ts-stall, data-stall);
6
+ // the user reads "quiet" (liveSlots' `quiet` and stage_model's QUIET_MS are
7
+ // other things). The rule is pure (stallOf, stallState); createStageStall
8
+ // draws it: a right-aligned span in the now row, its short text in the
9
+ // status row for a collapsed or plain stage, and the running call's clock.
10
+ // Its own module: new exports go in new modules (web-frontend.md).
11
+ import { clockText, genAge } from "./delivery_line.js";
12
+ import { node } from "./stage_slots.js";
13
+
14
+ // The levels' thresholds (ms) per kind: A, streamed or between steps; B,
15
+ // no first token yet in the step (a long prompt read, hidden reasoning).
16
+ export const STALL_MS = { A: [30000, 90000], B: [45000, 180000] };
17
+ // The stream down this long reads "reconnecting…" (a resync's own blink
18
+ // between workerGone and the new stream stays unsaid).
19
+ export const NET_MS = 1000;
20
+ // After the page shows again (visibilitychange): a background tab's
21
+ // stream may still be catching up.
22
+ export const GRACE_MS = 5000;
23
+ // The running call's clock shows from this far into it.
24
+ export const TOOL_CLOCK_MS = 10000;
25
+
26
+ const NONE = Object.freeze({ level: 0, long: "", short: "" });
27
+ const NET = Object.freeze({ level: "net", long: "reconnecting… · events paused", short: "reconnecting…" });
28
+
29
+ // The signal for a running turn: level 0 (nothing to say), 1, 2 or "net",
30
+ // and its texts (+long+ the stage's, +short+ a phone's and the status
31
+ // row's). A stream not live wins (no count: the events, not the model, are
32
+ // what paused); an explained silence says nothing: a running tool (its
33
+ // clock speaks), a blocking card, a timing note (a retry, plugins' setup),
34
+ // the end, the grace after a return.
35
+ export function stallOf({ kind = "A", silentMs = 0, toolRunning = false, blocked = false, note = "", streamLive = true, ended = false, graceUntil = 0, nowMs = 0 } = {}) {
36
+ if (ended) return NONE;
37
+ if (!streamLive) return NET;
38
+ if (toolRunning || blocked || note || nowMs < graceUntil) return NONE;
39
+ const b = kind === "B";
40
+ const [l1, l2] = STALL_MS[b ? "B" : "A"];
41
+ if (!(silentMs >= l1)) return NONE;
42
+ const clock = clockText(silentMs / 1000);
43
+ const short = b ? `thinking quietly ${clock}` : `quiet ${clock}`;
44
+ return { level: silentMs >= l2 ? 2 : 1, long: `${short} · ${b ? "no tokens yet" : "no new events"}`, short };
45
+ }
46
+
47
+ // B while the step has had no token (no gen yet, or an open one with no
48
+ // text, thinking or call); else A (a closed step: between steps).
49
+ export function stallKind(gen) {
50
+ if (!gen) return "B";
51
+ return !gen.closed && !gen.text && !gen.thinking && !gen.tools?.length ? "B" : "A";
52
+ }
53
+
54
+ // The running call's clock ("0:12") from TOOL_CLOCK_MS on; "" before, or
55
+ // without a start (a replayed call the page didn't time).
56
+ export function toolClockText(startedAt, nowMs) {
57
+ if (startedAt == null) return "";
58
+ const ms = nowMs - startedAt;
59
+ return ms >= TOOL_CLOCK_MS ? clockText(ms / 1000) : "";
60
+ }
61
+
62
+ // What a tick shows for +turn+ (turn_model.js's): the stall (stallOf) and
63
+ // the running call's clock (+toolClock+). +lastEventAt+: the page's clock
64
+ // at the turn's last event; +streamDownMs+: how long the stream hasn't been
65
+ // live (null: it is); +serverGen+: the snapshot's generation (a joiner's
66
+ // step clock, delivery_line.js genAge): before the first token a joiner
67
+ // counts from it, not from its own replay.
68
+ export function stallState({ turn, lastEventAt, nowMs, blocked = false, note = "", streamDownMs = null, ended = false, graceUntil = 0, serverGen = null }) {
69
+ const gens = turn?.gens || [];
70
+ const gen = gens[gens.length - 1];
71
+ const running = gens.flatMap((g) => g.tools || []).reverse().find((row) => row.status === "running") || null;
72
+ const kind = stallKind(gen);
73
+ let silentMs = nowMs - lastEventAt;
74
+ if (kind === "B") {
75
+ const age = genAge(gen, serverGen, nowMs);
76
+ if (age !== null) silentMs = Math.max(silentMs, age * 1000);
77
+ }
78
+ const streamLive = streamDownMs == null || streamDownMs < NET_MS;
79
+ const stall = stallOf({ kind, silentMs, toolRunning: !!running, blocked, note, streamLive, ended, graceUntil, nowMs });
80
+ return { ...stall, toolClock: running && !ended ? toolClockText(running.startedAt, nowMs) : "" };
81
+ }
82
+
83
+ // The drawer. +stage+: the stage's root (.stalled, data-stall: the hooks);
84
+ // +nowEl+: the now row (stage_slots.js drawNow rebuilds it: each draw puts
85
+ // its spans back when they were taken out); +timingSlot+: the status row's
86
+ // timing line, the head goes after it. A draw writes only when a text
87
+ // changes, so the 1 Hz tick under the threshold writes nothing.
88
+ export function createStageStall({ stage, nowEl, timingSlot }) {
89
+ const stallEl = node("span", "ts-stall");
90
+ const longEl = node("span", "long", stallEl);
91
+ const shortEl = node("span", "short", stallEl);
92
+ const clockEl = node("span", "ts-tool-clock");
93
+ const head = node("span", "ts-stall-head");
94
+ timingSlot.after(head);
95
+ let shown = { key: null, clock: null, level: 0 };
96
+
97
+ function drawLevel({ level, long, short }) {
98
+ const key = `${level}|${long}`;
99
+ if (shown.key === key) return;
100
+ shown.key = key;
101
+ shown.level = level;
102
+ stage.classList.toggle("stalled", !!level);
103
+ if (level) {
104
+ stage.dataset.stall = String(level);
105
+ nowEl.dataset.stall = String(level);
106
+ } else {
107
+ delete stage.dataset.stall;
108
+ delete nowEl.dataset.stall;
109
+ }
110
+ longEl.textContent = long;
111
+ shortEl.textContent = short;
112
+ head.textContent = short;
113
+ }
114
+
115
+ function draw(state) {
116
+ drawLevel(state);
117
+ if (state.level && stallEl.parentNode !== nowEl) nowEl.appendChild(stallEl);
118
+ else if (!state.level && stallEl.parentNode) stallEl.remove();
119
+ const clock = state.toolClock || "";
120
+ if (shown.clock !== clock) {
121
+ shown.clock = clock;
122
+ clockEl.textContent = clock;
123
+ }
124
+ if (clock && clockEl.parentNode !== nowEl) nowEl.insertBefore(clockEl, nowEl.querySelector(":scope > .task-stop"));
125
+ else if (!clock && clockEl.parentNode) clockEl.remove();
126
+ }
127
+
128
+ function reset() {
129
+ draw({ ...NONE, toolClock: "" });
130
+ shown = { key: null, clock: null, level: 0 };
131
+ }
132
+
133
+ return { draw, reset, level: () => shown.level };
134
+ }
@@ -20,6 +20,7 @@ import { chipLabelText, flashOf, handOffDue, handOffOrder, headlineOf, isPlain,
20
20
  import { createStageSlots, node } from "./stage_slots.js";
21
21
  import { createStageFrame, reducedMotion } from "./stage_frame.js";
22
22
  import { createInUseWatch } from "./stage_watch.js";
23
+ import { createStageStall, GRACE_MS, stallState } from "./stage_stall.js";
23
24
  import { formatDuration } from "./timing.js";
24
25
  import { easeScrollTo } from "./scroll.js";
25
26
  import { readStored, writeStored } from "./storage.js";
@@ -69,9 +70,13 @@ function readCollapsed() {
69
70
  // status row's Stop turn (a promise of the cancel request; the button is
70
71
  // disabled until it settles); null: no Stop turn. +onHandedOff+({pieces,
71
72
  // immediate}): a turn's pieces reached the history (reload_check.js).
73
+ // The quiet signal's inputs (stage_stall.js): +clock+() the page's time,
74
+ // +streamLive+() the session's stream is open, +timingNote+() what the turn
75
+ // waits on (live_timing.js note), +serverGen+() a joiner's generation.
72
76
  export function createStageView({
73
77
  historyEl, isNearBottom: historyNearBottom, sawText, sessionId, dockEl, centerEl, taskStop = null,
74
78
  renderMarkdown, linkRefs, workingSet = null, onCancel = null, onHandedOff = null,
79
+ clock = () => Date.now(), streamLive = () => true, timingNote = () => "", serverGen = () => null,
75
80
  }) {
76
81
  // The dock becomes a clear column: the stage's card, then the composer's
77
82
  // (its rows moved into .dock-card, which wears the glass and the focus ring).
@@ -168,6 +173,8 @@ export function createStageView({
168
173
  // Below the turn's card: the lines queued for after it (placeKind
169
174
  // "queued"), each to run as a turn of its own; they hand off last.
170
175
  const queuedSlot = node("div", "ts-queued", stage);
176
+ // The quiet signal: its span in the now row, its head after the timing line.
177
+ const stall = createStageStall({ stage, nowEl: now, timingSlot });
171
178
  const slotsView = createStageSlots({
172
179
  headline, asking, now, trail, ticks, taskStop, renderMarkdown, linkRefs,
173
180
  gens: () => view.current().turn?.gens,
@@ -212,6 +219,11 @@ export function createStageView({
212
219
  // grows to it), and whether the room's card is in the stage's view.
213
220
  let arrived = null;
214
221
  let cardInSight = true;
222
+ // The quiet signal's clocks: the turn's last event, since when the stream
223
+ // isn't live (null: it is), the grace after the page shows again.
224
+ let lastEventAt = 0;
225
+ let downSince = null;
226
+ let graceUntil = 0;
215
227
 
216
228
  const view = createTurnBlock({
217
229
  historyEl,
@@ -378,6 +390,7 @@ export function createStageView({
378
390
  raiseForApproval(blocked);
379
391
  drawPill();
380
392
  slotsView.drawNow(slots.tool, slots.quiet);
393
+ drawStall(clock(), blocked);
381
394
  slotsView.drawTrail(slots.trail, flash);
382
395
  slotsView.drawTicks(slots.ticks);
383
396
  if (rail) {
@@ -446,6 +459,31 @@ export function createStageView({
446
459
  frame.forgetContent();
447
460
  }
448
461
 
462
+ // ── the quiet signal (stage_stall.js) ─────────────────────────────────────
463
+
464
+ // At +nowMs+ (the tick's, a refresh's): what the turn's silence says.
465
+ function drawStall(nowMs, blocked = pendingCard()) {
466
+ const { turn } = view.current();
467
+ if (!holding || !turn) return;
468
+ if (streamLive()) downSince = null;
469
+ else downSince ??= nowMs;
470
+ stall.draw(stallState({
471
+ turn, lastEventAt, nowMs, blocked, note: timingNote() || "", ended: !!endKind, graceUntil, serverGen: serverGen(),
472
+ streamDownMs: downSince === null ? null : nowMs - downSince,
473
+ }));
474
+ }
475
+
476
+ // A turn event arrived (a replay's too): the silence starts over, and a
477
+ // stall shown clears at once.
478
+ function alive() {
479
+ lastEventAt = clock();
480
+ if (stall.level()) drawStall(lastEventAt);
481
+ }
482
+
483
+ document.addEventListener("visibilitychange", () => {
484
+ if (!document.hidden) graceUntil = clock() + GRACE_MS;
485
+ });
486
+
449
487
  // One line in the trail for FLASH_MS, ahead of the calls (a newer flash
450
488
  // takes its place).
451
489
  function flashTrail(what) {
@@ -631,6 +669,8 @@ export function createStageView({
631
669
  cancel.hidden = true;
632
670
  chipLabel.textContent = "";
633
671
  slotsView.reset();
672
+ stall.reset();
673
+ downSince = null;
634
674
  rail?.reset();
635
675
  promptSlot.classList.remove("full");
636
676
  thinkingLine = null;
@@ -771,6 +811,7 @@ export function createStageView({
771
811
  settled = true;
772
812
  startedAt = Date.parse(data?.started_at) || Date.now();
773
813
  emptySlots();
814
+ lastEventAt = clock();
774
815
  // The turn's prompt bubble (app.js found or drew it) sits in the prompt
775
816
  // line until the hand-off takes it back first.
776
817
  if (data?.promptBubble) promptSlot.appendChild(data.promptBubble);
@@ -784,14 +825,30 @@ export function createStageView({
784
825
  refresh();
785
826
  },
786
827
  generationStarted(data, now = null) {
828
+ alive();
787
829
  releaseHeldWait();
788
830
  view.generationStarted(data, now);
789
831
  },
790
- chunk: (data) => view.chunk(data),
791
- generationRestarted: (data) => view.generationRestarted(data),
792
- generationCompleted: () => view.generationCompleted(),
793
- toolStarted: (data) => view.toolStarted(data),
794
- toolCompleted: (data) => view.toolCompleted(data),
832
+ chunk(data) {
833
+ alive();
834
+ return view.chunk(data);
835
+ },
836
+ generationRestarted(data) {
837
+ alive();
838
+ return view.generationRestarted(data);
839
+ },
840
+ generationCompleted() {
841
+ alive();
842
+ return view.generationCompleted();
843
+ },
844
+ toolStarted(data) {
845
+ alive();
846
+ return view.toolStarted(data);
847
+ },
848
+ toolCompleted(data) {
849
+ alive();
850
+ return view.toolCompleted(data);
851
+ },
795
852
  turnEnded(args) {
796
853
  const kept = view.turnEnded(args) || [];
797
854
  if (!holding) return kept;
@@ -816,18 +873,23 @@ export function createStageView({
816
873
  // A hook's notice and a plugin's nudge go into their step inside the
817
874
  // closed cloud; they flash in the trail meanwhile (flashOf).
818
875
  notice(data) {
876
+ alive();
819
877
  const placed = view.notice(data);
820
878
  if (placed) flashTrail(flashOf("notice", data));
821
879
  return placed;
822
880
  },
823
881
  steer(data) {
882
+ alive();
824
883
  const placed = view.steer(data);
825
884
  if (placed) flashTrail(flashOf("steer", data));
826
885
  return placed;
827
886
  },
828
887
  // A warn card (a plugin's warning) stays in sight: false puts it in the
829
888
  // extras (place), not in its step inside the closed cloud.
830
- card: (el) => !el.classList.contains("warn") && view.card(el),
889
+ card(el) {
890
+ alive();
891
+ return !el.classList.contains("warn") && view.card(el);
892
+ },
831
893
  flush: () => view.flush(),
832
894
  answerReady() {
833
895
  settled = true;
@@ -837,6 +899,7 @@ export function createStageView({
837
899
  askingText: () => view.askingText(),
838
900
  // The asking slot pairs them on its next draw.
839
901
  questionRefs(refs) {
902
+ alive();
840
903
  view.questionRefs(refs);
841
904
  scheduleRefresh();
842
905
  },
@@ -860,6 +923,7 @@ export function createStageView({
860
923
  if (folding) finishFold();
861
924
  const where = placeFor({ live: holding, handingOff: false, kind: placeKind(el) });
862
925
  if (where === "history") return false;
926
+ alive();
863
927
  if (where === "queued") {
864
928
  ensureQueuedSlot();
865
929
  queuedSlot.appendChild(el);
@@ -917,6 +981,11 @@ export function createStageView({
917
981
  cancel.hidden = !onCancel;
918
982
  show();
919
983
  },
984
+ // The page's clock ticks (app.js, the live timing's 200 ms / 1 s): the
985
+ // quiet signal and the running call's clock.
986
+ tick(nowMs = clock()) {
987
+ drawStall(nowMs);
988
+ },
920
989
  handOff,
921
990
  root: stage,
922
991
  scrollEl: scroller,
@@ -10,10 +10,10 @@ require_relative "../redacted_inspect"
10
10
 
11
11
  module Samagotchi
12
12
  module Web
13
- # The access token of `chi web` on the LAN (web.host: lan): anyone
13
+ # The one access token of `chi web` (LAN link, --tunnel, public_url/proxy): anyone
14
14
  # who has it can run commands as you, so it lives in one file only you
15
15
  # can read, $XDG_STATE_HOME/samagotchi/web-token (0600). It is made on
16
- # the first LAN start and kept, so a phone's bookmark survives
16
+ # the first start that needs it and kept, so a phone's bookmark survives
17
17
  # restarts; `chi web --new-token` replaces it. A loopback-only chi web
18
18
  # never reads it. The owner's cookie for this machine's 127.0.0.1
19
19
  # holds #loopback_cookie, never the token: a server on any other local
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: samagotchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.165.0
4
+ version: 0.166.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -1011,6 +1011,7 @@ files:
1011
1011
  - lib/samagotchi/web/public/refs.js
1012
1012
  - lib/samagotchi/web/public/reload_check.js
1013
1013
  - lib/samagotchi/web/public/restore.js
1014
+ - lib/samagotchi/web/public/resync_reach.js
1014
1015
  - lib/samagotchi/web/public/review_apply.js
1015
1016
  - lib/samagotchi/web/public/review_history.js
1016
1017
  - lib/samagotchi/web/public/review_start.js
@@ -1036,6 +1037,7 @@ files:
1036
1037
  - lib/samagotchi/web/public/stage_frame.js
1037
1038
  - lib/samagotchi/web/public/stage_model.js
1038
1039
  - lib/samagotchi/web/public/stage_slots.js
1040
+ - lib/samagotchi/web/public/stage_stall.js
1039
1041
  - lib/samagotchi/web/public/stage_view.js
1040
1042
  - lib/samagotchi/web/public/stage_watch.js
1041
1043
  - lib/samagotchi/web/public/stale_params.js