samagotchi 0.187.0 → 0.189.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 (184) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +96 -1
  3. data/bin/chi +2 -0
  4. data/docs/architecture.md +13 -7
  5. data/docs/bundle-authoring.md +1 -1
  6. data/docs/bundles.md +139 -120
  7. data/docs/cli.md +46 -8
  8. data/docs/configuration.md +4 -4
  9. data/docs/context.md +18 -162
  10. data/docs/guardrails.md +6 -3
  11. data/docs/hooks.md +2 -1
  12. data/docs/internals/attached-context.md +19 -37
  13. data/docs/internals/bridge.md +1 -0
  14. data/docs/internals/bundles.md +3 -3
  15. data/docs/internals/engine.md +2 -2
  16. data/docs/internals/llm-context-forget.md +5 -6
  17. data/docs/internals/objectives.md +1 -0
  18. data/docs/internals/pages.md +71 -21
  19. data/docs/internals/peers.md +149 -0
  20. data/docs/internals/remote-sessions.md +7 -4
  21. data/docs/internals/session-lifecycle.md +19 -6
  22. data/docs/internals/web-frontend.md +63 -39
  23. data/docs/internals/web-push.md +12 -6
  24. data/docs/memory.md +1 -1
  25. data/docs/pages.md +70 -16
  26. data/docs/peers.md +144 -0
  27. data/docs/plugins.md +77 -28
  28. data/docs/releasing.md +1 -1
  29. data/docs/scheduling.md +71 -8
  30. data/docs/sessions.md +12 -8
  31. data/docs/sub-agent.md +5 -5
  32. data/docs/testing.md +1 -1
  33. data/lib/samagotchi/bridge.rb +3 -3
  34. data/lib/samagotchi/bundle_command/api_calls.rb +2 -1
  35. data/lib/samagotchi/bundle_command/check.rb +1 -1
  36. data/lib/samagotchi/bundle_manifest.rb +34 -5
  37. data/lib/samagotchi/bundles/claude-peer/manifest.yml +11 -18
  38. data/lib/samagotchi/bundles/claude-peer/plugin.rb +22 -16
  39. data/lib/samagotchi/bundles/claude-peer/scripts/chi-peer-SKILL.md +48 -39
  40. data/lib/samagotchi/bundles/claude-peer/scripts/pair +15 -73
  41. data/lib/samagotchi/bundles/claude-peer/skill_claude_peer.md +16 -16
  42. data/lib/samagotchi/bundles/codex-peer/manifest.yml +11 -15
  43. data/lib/samagotchi/bundles/codex-peer/plugin.rb +35 -24
  44. data/lib/samagotchi/bundles/codex-peer/scripts/chi-peer-SKILL.md +51 -40
  45. data/lib/samagotchi/bundles/codex-peer/scripts/pair +18 -54
  46. data/lib/samagotchi/bundles/codex-peer/skill_codex_peer.md +16 -16
  47. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  48. data/lib/samagotchi/bundles/schedules/manifest.yml +11 -0
  49. data/lib/samagotchi/bundles/schedules/scripts/schedules.rb +159 -0
  50. data/lib/samagotchi/bundles/skills/manifest.yml +2 -2
  51. data/lib/samagotchi/bundles/skills/plugin.rb +14 -8
  52. data/lib/samagotchi/bundles/system/manifest.yml +3 -3
  53. data/lib/samagotchi/bundles/system/self_map.md +2 -2
  54. data/lib/samagotchi/bundles/system/skill_pages.md +9 -4
  55. data/lib/samagotchi/client_id.rb +15 -1
  56. data/lib/samagotchi/config.rb +8 -7
  57. data/lib/samagotchi/context_absorber.rb +23 -277
  58. data/lib/samagotchi/context_sources.rb +17 -69
  59. data/lib/samagotchi/engine/messages.rb +12 -1
  60. data/lib/samagotchi/engine/plugins.rb +1 -1
  61. data/lib/samagotchi/engine.rb +7 -6
  62. data/lib/samagotchi/engine_peers.rb +0 -3
  63. data/lib/samagotchi/guardrail_wiring.rb +2 -0
  64. data/lib/samagotchi/guardrails/chi_dirs.rb +4 -3
  65. data/lib/samagotchi/guardrails/parent_approvals.rb +6 -5
  66. data/lib/samagotchi/guardrails/protected_paths.rb +6 -3
  67. data/lib/samagotchi/inline_widgets.rb +11 -3
  68. data/lib/samagotchi/installed_bundles/manifest.rb +8 -1
  69. data/lib/samagotchi/log_line.rb +1 -1
  70. data/lib/samagotchi/memory_bundle/asset_installer.rb +3 -2
  71. data/lib/samagotchi/memory_bundle/builder.rb +1 -0
  72. data/lib/samagotchi/memory_bundle/installed_copy.rb +1 -1
  73. data/lib/samagotchi/note_move.rb +3 -1
  74. data/lib/samagotchi/page_command.rb +25 -13
  75. data/lib/samagotchi/pages/builder.rb +7 -8
  76. data/lib/samagotchi/pages/bundled.rb +83 -0
  77. data/lib/samagotchi/pages/launch.rb +22 -4
  78. data/lib/samagotchi/pages/runner.rb +7 -2
  79. data/lib/samagotchi/pages/runtime.rb +83 -0
  80. data/lib/samagotchi/pages/store.rb +29 -7
  81. data/lib/samagotchi/pages.rb +47 -3
  82. data/lib/samagotchi/peer_command.rb +192 -0
  83. data/lib/samagotchi/peer_continue.rb +105 -0
  84. data/lib/samagotchi/peer_exchanges.rb +3 -3
  85. data/lib/samagotchi/peer_mailbox.rb +360 -0
  86. data/lib/samagotchi/peer_note.rb +208 -0
  87. data/lib/samagotchi/peer_receipts.rb +247 -0
  88. data/lib/samagotchi/peer_registry.rb +44 -0
  89. data/lib/samagotchi/peer_roster.rb +79 -0
  90. data/lib/samagotchi/peer_say.rb +168 -0
  91. data/lib/samagotchi/plugin/api.rb +29 -21
  92. data/lib/samagotchi/plugin/context.rb +7 -0
  93. data/lib/samagotchi/plugin/loader.rb +3 -3
  94. data/lib/samagotchi/plugin/peer.rb +175 -64
  95. data/lib/samagotchi/plugin/peers.rb +70 -0
  96. data/lib/samagotchi/run_options.rb +3 -2
  97. data/lib/samagotchi/schedule_command.rb +88 -39
  98. data/lib/samagotchi/schedule_display.rb +101 -0
  99. data/lib/samagotchi/schedules/busy_part.rb +146 -0
  100. data/lib/samagotchi/schedules/edit_part.rb +120 -0
  101. data/lib/samagotchi/schedules/history_part.rb +51 -0
  102. data/lib/samagotchi/schedules/listing_part.rb +52 -0
  103. data/lib/samagotchi/schedules/notices_part.rb +116 -0
  104. data/lib/samagotchi/schedules/protection_part.rb +103 -0
  105. data/lib/samagotchi/schedules/recurrence.rb +103 -0
  106. data/lib/samagotchi/schedules.rb +134 -330
  107. data/lib/samagotchi/session_access/contract.rb +18 -1
  108. data/lib/samagotchi/session_access/local.rb +140 -14
  109. data/lib/samagotchi/session_access/remote.rb +6 -3
  110. data/lib/samagotchi/session_access/remote_peers.rb +75 -0
  111. data/lib/samagotchi/session_chain/delete.rb +7 -5
  112. data/lib/samagotchi/session_continue.rb +12 -0
  113. data/lib/samagotchi/session_inbox_delivery.rb +5 -2
  114. data/lib/samagotchi/session_lifecycle/allowed.rb +2 -1
  115. data/lib/samagotchi/session_lifecycle.rb +3 -2
  116. data/lib/samagotchi/session_manager/delivery.rb +5 -3
  117. data/lib/samagotchi/session_manager/removal.rb +18 -9
  118. data/lib/samagotchi/session_manager/spawn.rb +2 -1
  119. data/lib/samagotchi/session_manager.rb +1 -0
  120. data/lib/samagotchi/session_protection.rb +99 -0
  121. data/lib/samagotchi/session_retention.rb +21 -6
  122. data/lib/samagotchi/session_stores.rb +17 -0
  123. data/lib/samagotchi/sessions_command.rb +4 -0
  124. data/lib/samagotchi/steer.rb +1 -1
  125. data/lib/samagotchi/system_prompt.rb +1 -1
  126. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  127. data/lib/samagotchi/terminal_ui/formatting.rb +4 -1
  128. data/lib/samagotchi/testing/boundary.rb +17 -13
  129. data/lib/samagotchi/testing/bundle.rb +40 -5
  130. data/lib/samagotchi/testing/install.rb +4 -4
  131. data/lib/samagotchi/testing/recording_api.rb +2 -1
  132. data/lib/samagotchi/tools/cancel_schedule.rb +5 -2
  133. data/lib/samagotchi/tools/context_read.rb +0 -14
  134. data/lib/samagotchi/tools/list_schedules.rb +8 -3
  135. data/lib/samagotchi/turn_outcome.rb +41 -0
  136. data/lib/samagotchi/version.rb +1 -1
  137. data/lib/samagotchi/web/app.rb +2 -13
  138. data/lib/samagotchi/web/message_display.rb +4 -2
  139. data/lib/samagotchi/web/page_routes.rb +13 -8
  140. data/lib/samagotchi/web/peer_routes.rb +133 -0
  141. data/lib/samagotchi/web/public/app.js +23 -29
  142. data/lib/samagotchi/web/public/bubbles.js +2 -2
  143. data/lib/samagotchi/web/public/cards_place.js +20 -1
  144. data/lib/samagotchi/web/public/context_chips.js +45 -46
  145. data/lib/samagotchi/web/public/history_reveal.js +4 -1
  146. data/lib/samagotchi/web/public/history_shape.js +3 -2
  147. data/lib/samagotchi/web/public/index.html +24 -2
  148. data/lib/samagotchi/web/public/notify.js +1 -1
  149. data/lib/samagotchi/web/public/owner_claim.js +7 -5
  150. data/lib/samagotchi/web/public/page_links.js +1 -1
  151. data/lib/samagotchi/web/public/page_view.js +10 -6
  152. data/lib/samagotchi/web/public/pages.css +9 -2
  153. data/lib/samagotchi/web/public/pages_group.js +21 -19
  154. data/lib/samagotchi/web/public/pages_list.js +73 -0
  155. data/lib/samagotchi/web/public/pages_model.js +54 -0
  156. data/lib/samagotchi/web/public/pages_wiring.js +66 -0
  157. data/lib/samagotchi/web/public/peer_chip.js +60 -74
  158. data/lib/samagotchi/web/public/peer_data.js +13 -11
  159. data/lib/samagotchi/web/public/peer_links.js +12 -7
  160. data/lib/samagotchi/web/public/peer_mailbox_api.js +20 -0
  161. data/lib/samagotchi/web/public/peer_note.js +7 -3
  162. data/lib/samagotchi/web/public/peer_text.js +4 -4
  163. data/lib/samagotchi/web/public/quiet_notes.js +199 -0
  164. data/lib/samagotchi/web/public/revise_stack.js +4 -3
  165. data/lib/samagotchi/web/public/session_view.js +2 -2
  166. data/lib/samagotchi/web/public/stream_handlers.js +3 -2
  167. data/lib/samagotchi/web/public/turn_block.js +7 -3
  168. data/lib/samagotchi/web/public/turn_events.js +6 -0
  169. data/lib/samagotchi/web/public/views.js +77 -23
  170. data/lib/samagotchi/web/push/attention.rb +3 -2
  171. data/lib/samagotchi/web/push/notifier.rb +4 -3
  172. data/lib/samagotchi/web/server.rb +3 -1
  173. data/lib/samagotchi/web/session_access_routes.rb +3 -20
  174. data/lib/samagotchi/web/session_hub.rb +64 -19
  175. data/lib/samagotchi/worker/leave.rb +3 -0
  176. data/lib/samagotchi/worker/mail.rb +73 -0
  177. data/lib/samagotchi/worker/startup.rb +3 -0
  178. data/lib/samagotchi/worker/wake_turns.rb +20 -5
  179. data/lib/samagotchi/worker.rb +12 -6
  180. data/lib/samagotchi/worker_inbound/mail.rb +214 -0
  181. data/lib/samagotchi/worker_inbound.rb +43 -46
  182. data/lib/samagotchi/worker_wakes.rb +26 -21
  183. metadata +35 -2
  184. data/lib/samagotchi/peer_sources.rb +0 -58
@@ -1,11 +1,12 @@
1
- // A paired peer in the session bar: its two context sources (<name>-inbox,
2
- // <name>-outbox) drawn as one chip that says how the pairing stands
3
- // (paired, waiting, answered, can't send, answers only), and that chip's
4
- // popover (how chi and the peer reach each other, the last exchanges, View
1
+ // A paired peer in the session bar: one chip per peer the peers model
2
+ // (peer_data.js, GET …/peers: the session's mailbox) names as paired, that
3
+ // says how the pairing stands (paired, waiting, answered, can't send,
4
+ // answers only), and that chip's popover (how chi and the peer reach each
5
+ // other, the last exchanges with what chi did with each message, View
5
6
  // latest answer, Status card, Unpair). Pure templates and models: the
6
- // chips' controller (context_chips.js) draws them, the exchanges are the
7
- // peers model's (peer_data.js, GET …/peers). docs/internals/web-frontend.md,
8
- // The peer exchange. Its own module: new exports go in new modules.
7
+ // chips' controller (context_chips.js) draws them. docs/internals/
8
+ // web-frontend.md, The peer exchange. Its own module: new exports go in new
9
+ // modules.
9
10
 
10
11
  import { html, nothing } from "./vendor/lit-html.js";
11
12
  import { spanText, verdictWord } from "./peer_exchange.js";
@@ -18,46 +19,29 @@ export const peerKey = (name) => `peer:${name}`;
18
19
  // How many exchanges the popover lists (the newest).
19
20
  export const POPOVER_EXCHANGES = 8;
20
21
 
21
- // GET …/context's rows split: the peer sources (a row with `peer` and its
22
- // `leg`) grouped per peer, {key, name, label, inbox, outbox} (a leg's row
23
- // or null; a muted leg stays in its group), in the order their first leg
24
- // comes; and the rest, the ordinary rows, as they were.
25
- export function peerGroups(rows) {
26
- const peers = new Map();
27
- const rest = [];
28
- for (const row of rows || []) {
29
- if (!row) continue;
30
- if (!row.peer || !["inbox", "outbox"].includes(row.leg)) {
31
- rest.push(row);
32
- continue;
33
- }
34
- let group = peers.get(row.peer);
35
- if (!group) {
36
- group = { key: peerKey(row.peer), name: row.peer, label: row.label || row.peer, inbox: null, outbox: null };
37
- peers.set(row.peer, group);
38
- }
39
- group[row.leg] = row;
40
- }
41
- return { peers: [...peers.values()], rest };
22
+ // The peers model's rows as chips' groups: {key, name, label, peer} for each
23
+ // paired one (its link.json: paired_at), in the model's order. A peer that
24
+ // was unpaired keeps its messages but has no chip.
25
+ export function peerGroups(peers) {
26
+ return (peers || []).filter((peer) => peer?.name && peer.paired_at)
27
+ .map((peer) => ({ key: peerKey(peer.name), name: peer.name, label: peer.label || peer.name, peer }));
42
28
  }
43
29
 
44
- // A leg that counts: there and not muted.
45
- const live = (leg) => (leg && !leg.muted ? leg : null);
46
- // The group's legs as the chip reads them, and whether it shows at all
47
- // (a group whose legs are all muted is gone from the bar, as a muted row is).
48
- export const peerShown = (group) => !!(live(group.inbox) || live(group.outbox));
30
+ // A message no turn has read yet: no receipt (chi hasn't taken it), or one
31
+ // that holds it as a note (held, noted).
32
+ const unread = (message) => !message?.receipt || ["held", "noted"].includes(message.receipt.state);
49
33
 
50
34
  // The sends among +exchanges+ (newest first): an unclaimed answer has no id.
51
35
  const sends = (exchanges) => (exchanges || []).filter((ex) => ex?.id);
52
36
  const ASKS = ["validate", "question"];
37
+ const messagesOf = (group) => group?.peer?.messages || [];
53
38
 
54
39
  // The newest send failed and the peer hasn't paired again since: pairing
55
- // again (the pair script) pushes the inbox, which moves its row's
56
- // fetched_at past the failed send's time.
40
+ // again writes a new link (paired_at) after the failed send's time.
57
41
  export function cantSend(group, exchanges) {
58
42
  const newest = sends(exchanges)[0];
59
43
  if (newest?.state !== "failed") return false;
60
- const paired = Date.parse(live(group.inbox)?.fetched_at || "");
44
+ const paired = Date.parse(group?.peer?.paired_at || "");
61
45
  const failed = Date.parse(newest.at || "");
62
46
  return !(paired > failed);
63
47
  }
@@ -67,26 +51,24 @@ const since = (iso, now) => {
67
51
  };
68
52
 
69
53
  // The chip of +group+: the first state that applies.
70
- // - answers-only: no inbox leg (chi can't send);
54
+ // - answers-only: the pairing has no address (chi can't send);
71
55
  // - cant-send: the newest send failed (the transport refused it);
72
56
  // - waiting: the newest validate or question send has no answer (its age the send's);
73
- // - answered: the outbox has a text the agent hasn't read (its age the text's), with the dot;
57
+ // - answered: its newest message no turn has read (its age the message's), with the dot;
74
58
  // - paired otherwise.
75
- // +exchanges+: the peer's from GET …/peers, newest first ([] when not read,
76
- // an older chi web, a peer the read doesn't name: nothing then says failed).
59
+ // +exchanges+: the peer's from GET …/peers, newest first.
77
60
  // @return {key, name, state, label, title, dot}
78
61
  export function peerChipModel(group, exchanges = [], now = Date.now()) {
79
- const inbox = live(group.inbox);
80
- const outbox = live(group.outbox);
81
62
  const all = sends(exchanges);
82
63
  const ask = all.find((ex) => ASKS.includes(ex.kind));
64
+ const newest = messagesOf(group)[0];
83
65
  let state = "paired";
84
66
  let words = "paired";
85
- let why = "paired: chi can send to it and it answers into its outbox";
86
- if (!inbox) {
67
+ let why = "paired: chi can send to it and it answers with chi peer say";
68
+ if (!group.peer?.can_send) {
87
69
  state = "answers-only";
88
70
  words = "answers only";
89
- why = "no inbox attached here: it answers, chi can't send to it";
71
+ why = "paired with no address chi can reach: it answers, chi can't send to it";
90
72
  } else if (cantSend(group, exchanges)) {
91
73
  state = "cant-send";
92
74
  words = "can't send";
@@ -97,11 +79,11 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
97
79
  words = age === null ? "waiting" : `waiting ${spanText(age)}`;
98
80
  why = [ask.kind, ask.title].filter(Boolean).join(" · ") || "a send";
99
81
  why = `waiting for its answer to ${why}`;
100
- } else if (outbox?.unread && outbox.has_text) {
101
- const age = since(outbox.fetched_at, now);
82
+ } else if (newest && unread(newest)) {
83
+ const age = since(newest.at, now);
102
84
  state = "answered";
103
85
  words = age === null ? "answered" : `answered ${spanText(age)}`;
104
- why = "an answer the agent hasn't read yet";
86
+ why = "a message the agent hasn't read yet";
105
87
  }
106
88
  return { key: group.key, name: group.key, state, label: `${group.label} · ${words}`, title: why,
107
89
  dot: state === "answered", peer: true };
@@ -109,58 +91,62 @@ export function peerChipModel(group, exchanges = [], now = Date.now()) {
109
91
 
110
92
  // An exchange's state as the popover's list says it: "waiting 2m",
111
93
  // "changes · 11m", "ok · 4m", "answered · 4m", "no answer expected",
112
- // "not sent"; +kind+ its tint.
94
+ // "not sent", then what chi did with the answer's message (+words+, the
95
+ // server's: "held · peer turns", "read", "completed"); +kind+ its tint.
113
96
  // @return {text, kind}
114
- export function exchangeState(ex, now = Date.now()) {
97
+ export function exchangeState(ex, now = Date.now(), words = null) {
115
98
  if (ex.state === "failed") return { text: "not sent", kind: "failed" };
116
99
  if (ex.state === "none_expected") return { text: "no answer expected", kind: "none" };
117
100
  if (ex.state === "waiting") {
118
101
  const age = since(ex.at, now);
119
102
  return { text: age === null ? "waiting" : `waiting ${spanText(age)}`, kind: "waiting" };
120
103
  }
121
- const took = ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
104
+ const took = ex.id && ex.answer?.at ? since(ex.at, Date.parse(ex.answer.at)) : null;
122
105
  const verdict = verdictWord(ex.answer?.verdict);
123
106
  const word = verdict || "answered";
124
- return { text: took === null ? word : `${word} · ${spanText(took)}`, kind: verdict || "answered" };
107
+ const text = [took === null ? word : `${word} · ${spanText(took)}`, words].filter(Boolean).join(" · ");
108
+ return { text, kind: verdict || "answered" };
125
109
  }
126
110
 
127
111
  // The popover's inside for +group+ (scene 2). +exchanges+ newest first;
128
- // +text+ the outbox's text once View latest answer read it (null before);
129
- // +status+ the status card's command ("/claude") when the session has it,
130
- // else null; +drawn+(id) whether that send's row is in the conversation
131
- // (its list row then jumps to it).
112
+ // +text+ the newest message's text once View latest answer read it (null
113
+ // before); +status+ the status card's command ("/claude") when the session
114
+ // has it, else null; +drawn+(id) whether that send's row is in the
115
+ // conversation (its list row then jumps to it).
132
116
  export function peerPopoverTemplate(group, exchanges = [], { text = null, status = null, drawn = () => false, now = Date.now() } = {}) {
133
- const inbox = live(group.inbox);
134
- const outbox = live(group.outbox);
117
+ const peer = group.peer || {};
118
+ const messages = messagesOf(group);
119
+ const words = new Map(messages.map((m) => [String(m.serial), m.words]));
135
120
  const all = sends(exchanges);
136
121
  const lastSent = all.find((ex) => ex.state !== "failed");
137
- const sendsLine = !inbox
138
- ? "can't: no inbox attached here"
139
- : cantSend(group, exchanges)
122
+ const failing = peer.can_send && cantSend(group, exchanges);
123
+ const sendsLine = !peer.can_send
124
+ ? "can't: it paired with no address chi can reach"
125
+ : failing
140
126
  ? `the last send didn't go through: pair again from ${group.label}`
141
- : [`to its inbox (${group.inbox.name})`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
142
- const answersLine = outbox
143
- ? [`into ${outbox.name}`, outbox.fetched_at ? `last ${wallClock(outbox.fetched_at, now)}` : "nothing yet"].join(" · ")
144
- : "no outbox attached";
145
- const list = all.slice(0, POPOVER_EXCHANGES);
127
+ : [`to ${peer.address}`, lastSent ? `last sent ${wallClock(lastSent.at, now)}` : ""].filter(Boolean).join(" · ");
128
+ const answersLine = messages.length ? `with chi peer say · last ${wallClock(messages[0].at, now)}` : "nothing yet";
129
+ const list = (exchanges || []).filter((ex) => ex?.id || ex?.answer).slice(0, POPOVER_EXCHANGES);
146
130
  const rowInside = (ex) => {
147
- const state = exchangeState(ex, now);
148
- return html`<span class="pp-x-at">${wallClock(ex.at, now)}</span><span class="pp-x-what">${
149
- [ex.kind, ex.title].filter(Boolean).join(" · ") || "a send"}</span><span class=${`pp-x-state ${state.kind}`}>${state.text}</span>`;
131
+ const serial = ex.answer?.serial;
132
+ const state = exchangeState(ex, now, serial === undefined || serial === null ? null : words.get(String(serial)));
133
+ const what = ex.id ? [ex.kind, ex.title].filter(Boolean).join(" · ") || "a send"
134
+ : [`#${serial ?? "?"}`, ex.answer?.summary].filter(Boolean).join(" · ");
135
+ return html`<span class="pp-x-at">${wallClock(ex.id ? ex.at : ex.answer?.at || ex.at, now)}</span><span class="pp-x-what">${
136
+ what}</span><span class=${`pp-x-state ${state.kind}`}>${state.text}</span>`;
150
137
  };
151
- const legs = [group.inbox, group.outbox].filter(Boolean);
152
138
  return html`<div class="ctx-pop-head"><strong>${group.label}</strong
153
139
  ><span class="ctx-pop-kind">paired peer</span
154
140
  ><button type="button" class="ctx-pop-close" data-act="close" aria-label="Close">×</button></div
155
- ><div class="peer-pop-legs"><div><span>chi sends</span><span class=${inbox && cantSend(group, exchanges) ? "warn" : ""}>${sendsLine}</span></div
141
+ ><div class="peer-pop-legs"><div><span>chi sends</span><span class=${failing ? "warn" : ""}>${sendsLine}</span></div
156
142
  ><div><span>it answers</span><span>${answersLine}</span></div></div
157
- >${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (drawn(ex.id)
143
+ >${list.length ? html`<div class="peer-pop-list"><div class="peer-pop-list-head">Exchanges</div>${list.map((ex) => (ex.id && drawn(ex.id)
158
144
  ? html`<button type="button" class="peer-pop-x" data-act="jump" data-peer-id=${ex.id}>${rowInside(ex)}</button>`
159
145
  : html`<div class="peer-pop-x">${rowInside(ex)}</div>`))}</div>` : nothing
160
146
  }<pre class="ctx-pop-text" ?hidden=${text === null}>${text ?? ""}</pre
161
- ><div class="ctx-pop-actions">${outbox?.has_text && text === null ? html`<button type="button" data-act="view">View latest answer</button>` : nothing
147
+ ><div class="ctx-pop-actions">${messages.length && text === null ? html`<button type="button" data-act="view">View latest answer</button>` : nothing
162
148
  }${status ? html`<button type="button" data-act="status">Status card (${status})</button>` : nothing
163
- }${legs.length ? html`<button type="button" class="ctx-pop-detach" data-act="unpair" title=${`Detach ${legs.map((l) => l.name).join(" and ")}`}>Unpair</button>` : nothing}</div>`;
149
+ }<button type="button" class="ctx-pop-detach" data-act="unpair" title="End the pairing (its messages stay)">Unpair</button></div>`;
164
150
  }
165
151
 
166
152
  // The peer's status card command ("/claude") when the session's commands
@@ -1,15 +1,17 @@
1
1
  // The open session's peers as the page knows them: GET …/peers (owner only,
2
- // read from the session's files: its peer sources and the exchange log), and
3
- // one small model of it that the conversation (peer_links.js) and, later,
4
- // the peer chips share. docs/internals/web-frontend.md, The peer exchange.
2
+ // read from the session's files: its mailbox, the receipts and the exchange
3
+ // log), and one small model of it that the conversation (peer_links.js) and
4
+ // the peer chips (context_chips.js, peer_chip.js) share. docs/internals/web-frontend.md, The peer exchange.
5
5
  // Its own module: new exports go in new modules (web-frontend.md).
6
6
 
7
7
  import { api } from "./data.js";
8
8
  import { createLatest } from "./latest.js";
9
9
 
10
- // GET /api/sessions/:id/peers → {peers: [{name, label, inbox, outbox,
11
- // exchanges: [newest first]}]}. An older chi web has no route: its 404 is
12
- // an empty list here, on its own (any other failure throws).
10
+ // GET /api/sessions/:id/peers → {peers: [{name, label, paired_at, from,
11
+ // address, can_send, messages: [newest first, {serial, summary, re, at,
12
+ // wake, receipt, words}], exchanges: [newest first]}]}. An older chi web
13
+ // has no route: its 404 is an empty list here, on its own (any other
14
+ // failure throws).
13
15
  export async function listPeers(id, opts = {}) {
14
16
  try {
15
17
  return await api(`/api/sessions/${encodeURIComponent(id)}/peers`, opts);
@@ -26,9 +28,9 @@ export const WAITING_TICK_MS = 60_000;
26
28
  // last: 20): a full list may leave older sends out.
27
29
  export const EXCHANGES_CAP = 20;
28
30
 
29
- // The model. +sessionId+() the open session; +hasPeer+() whether it has a
30
- // peer at all (a context row with `peer`, a drawn send or answer): without
31
- // one, refresh asks nothing. +onChange+() after each read for the open
31
+ // The model. +sessionId+() the open session; +hasPeer+() whether to read
32
+ // at all (the page: always, GET …/peers is the peer chip's only feed):
33
+ // without, refresh asks nothing. +onChange+() after each read for the open
32
34
  // session (also a failed one: what is drawn may follow it). +list+: the
33
35
  // fetch (listPeers). +win+: focus and the waiting tick.
34
36
  //
@@ -97,8 +99,8 @@ export function createPeers({ sessionId, hasPeer = () => false, onChange = () =>
97
99
  }
98
100
 
99
101
  // The send answer +serial+ of peer +name+ answers: the exchange its
100
- // serial (its outbox revision's, the note's) is in, when that one is a
101
- // send's; else the send +re+ names (a second answer to one send is an
102
+ // serial (its mailbox message's, or an older outbox revision's: the
103
+ // note's) is in, when that one is a send's; else the send +re+ names (a second answer to one send is an
102
104
  // unclaimed row of its own on the server); null for an answer no send
103
105
  // claims.
104
106
  function answer(name, serial, re = null) {
@@ -29,21 +29,24 @@ export function upTemplate(exchange) {
29
29
  aria-label=${`${what}: go to the send`}>${what}<span aria-hidden="true"> ↑</span></button>`;
30
30
  }
31
31
 
32
- // "Read the full answer": the newest answer of a peer only (its outbox keeps
33
- // one text), and not one whose bubble holds its text (data-peer-inline).
32
+ // "Read the full answer": not one whose bubble holds its text
33
+ // (data-peer-inline); for an older context answer, its peer's newest only
34
+ // (its outbox kept one text); for a mailbox message, each (it reads its own).
34
35
  export function fullTemplate(newest) {
35
36
  return newest ? html`<button type="button" class="peer-full" data-peer-full="1">Read the full answer</button>` : nothing;
36
37
  }
37
38
 
38
39
  // +peers+ the model (peer_data.js); +roots+() the elements a turn's rows can
39
40
  // be in (the history and the stage); +running+() whether a turn runs;
40
- // +showText+(source) opens that context source's text (context_chips.js);
41
+ // +showText+(source) opens that context source's text, +showMessage+(name,
42
+ // serial) a peer's mailbox message (context_chips.js);
41
43
  // +renderMarkdown+(text) a text's sanitized markdown HTML or null
42
44
  // (markdown_place.js; none: the peer's text stays plain); +behavior+() the
43
45
  // scroll's.
44
46
  //
45
47
  // @return {draw(), drawn(), hasPeer(), hasSend(id), showSend(id), reset()}
46
- export function createPeerLinks({ peers, roots, running = () => false, showText = () => {}, renderMarkdown = null, behavior = smooth } = {}) {
48
+ export function createPeerLinks({ peers, roots, running = () => false, showText = () => {}, showMessage = () => {}, renderMarkdown = null,
49
+ behavior = smooth } = {}) {
47
50
  const all = (selector) => roots().filter(Boolean).flatMap((root) => [...root.querySelectorAll(selector)]);
48
51
  // The peer texts already asked for (each is asked once; a failed read
49
52
  // keeps the plain text).
@@ -86,7 +89,8 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
86
89
  const up = note.querySelector(":scope > .peer-head > .peer-up-slot");
87
90
  if (up) render(upTemplate(answerOf(note)), up);
88
91
  const full = note.querySelector(":scope > .peer-foot > .peer-full-slot");
89
- const last = newest.get(note.dataset.peerSource || "") === note && !!note.dataset.peerSource;
92
+ const mailbox = !!note.dataset.peerBox;
93
+ const last = mailbox || (newest.get(note.dataset.peerSource || "") === note && !!note.dataset.peerSource);
90
94
  if (full) render(fullTemplate(last && !note.dataset.peerInline), full);
91
95
  const text = note.querySelector(":scope > .peer-text[data-peer-md]:not(.markdown)");
92
96
  if (text) renderText(text);
@@ -156,8 +160,9 @@ export function createPeerLinks({ peers, roots, running = () => false, showText
156
160
  if (slot) toAnswer(slot);
157
161
  } else if (jump?.dataset.peerJump === "send") toSend(jump);
158
162
  else if (e.target.closest?.("[data-peer-full]")) {
159
- const source = e.target.closest(ANSWERS)?.dataset.peerSource;
160
- if (source) showText(source);
163
+ const note = e.target.closest(ANSWERS);
164
+ if (note?.dataset.peerBox) showMessage(note.dataset.peerName, note.dataset.peerSerial, note.dataset.peerBox);
165
+ else if (note?.dataset.peerSource) showText(note.dataset.peerSource);
161
166
  }
162
167
  }
163
168
  for (const root of roots().filter(Boolean)) root.addEventListener("click", onClick);
@@ -0,0 +1,20 @@
1
+ // A session's peer mailbox through chi web, for the peer chip
2
+ // (context_chips.js): one message's text, and Unpair. Owner only (chi web's
3
+ // …/peers routes). docs/internals/web-frontend.md, The peer exchange.
4
+ // Its own module: new exports go in new modules (web-frontend.md).
5
+
6
+ import { api } from "./data.js";
7
+
8
+ const base = (id, name) => `/api/sessions/${encodeURIComponent(id)}/peers/${encodeURIComponent(name)}`;
9
+
10
+ // GET …/peers/NAME/messages/SERIAL → {serial, text, summary, re, at, wake, receipt}.
11
+ export function getPeerMessage(id, name, serial, box = null, opts = {}) {
12
+ if (box && typeof box === "object") { opts = box; box = null; }
13
+ const query = box ? `?box=${encodeURIComponent(box)}` : "";
14
+ return api(`${base(id, name)}/messages/${encodeURIComponent(serial)}${query}`, opts);
15
+ }
16
+
17
+ // DELETE …/peers/NAME (Unpair) → {unpaired}: the pairing ends, its messages stay.
18
+ export function unpairPeer(id, name, opts = {}) {
19
+ return api(base(id, name), { ...opts, method: "DELETE" });
20
+ }
@@ -5,7 +5,9 @@
5
5
  // classes `bubble note` and `turn-start`, data-opening-id, the time title):
6
6
  // TURN_PROMPT (bubbles.js), card_stamps.js, stage_handoff.js OPENING,
7
7
  // reload_check.js and revise_stack.js (its `.note-text`, the summary) read
8
- // it as any wake note. The ↑ to the send and "Read the full answer" are
8
+ // it as any wake note. A mailbox message's note has a `box` in its peer
9
+ // hash (data-peer-box); an older context answer's has none. The ↑ to the
10
+ // send and "Read the full answer" are
9
11
  // slots peer_links.js fills (data-peer-slot: history_shape.js leaves them
10
12
  // out, so live and an offscreen reload read alike). A note whose peer hash
11
13
  // says `inline` holds the peer's text (peer_text.js): the bubble shows it
@@ -52,7 +54,9 @@ export function peerNoteTemplate(note) {
52
54
  const name = peer.label || peer.peer || "Your peer";
53
55
  const cls = note.turn_start ? "bubble note turn-start peer-answer" : "bubble note peer-answer";
54
56
  const opening = note.turn_start && note.turn_id ? String(note.turn_id) : nothing;
55
- const source = note.context_source || (peer.peer ? `${peer.peer}-outbox` : "");
57
+ // A mailbox message's note (its peer hash has a box) reads its text by
58
+ // its number; an older context answer's by its outbox source.
59
+ const source = note.context_source || (peer.peer && !peer.box ? `${peer.peer}-outbox` : "");
56
60
  const attr = (value) => (value === null || value === undefined || value === "" ? nothing : String(value));
57
61
  const block = peer.inline ? peerBlock(note.content) : null;
58
62
  // Sanitized on the server (MarkdownRenderer), as an answer's html.
@@ -61,5 +65,5 @@ export function peerNoteTemplate(note) {
61
65
  : rendered ? html`<div class="peer-text markdown md-compact" data-peer-md=${block.text}>${unsafeHTML(rendered)}</div>`
62
66
  : html`<div class="peer-text" data-peer-md=${block.text}>${block.text}</div>`;
63
67
  const told = block ? block.rest : (note.content ?? "");
64
- return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
68
+ return html`<div class=${cls} title=${fullTime(note.at) || nothing} data-opening-id=${opening} data-peer-source=${attr(source)} data-peer-name=${attr(peer.peer)} data-peer-re=${attr(peer.re)} data-peer-serial=${attr(peer.serial)} data-peer-box=${attr(peer.box)} data-peer-inline=${block ? "1" : nothing}><div class="peer-head"><span class="peer-who">${name} answered</span><span class="peer-up-slot" data-peer-slot="up"></span>${verdictTemplate(peer)}</div><div class="note-text">${peerSummaryText(note)}</div>${body}<div class="peer-foot"><span class="peer-full-slot" data-peer-slot="full"></span><details class="peer-told"><summary>what chi told the agent</summary><div class="peer-told-text">${told}</div></details></div></div>`;
65
69
  }
@@ -1,4 +1,4 @@
1
- // A peer's text in its answer note (ContextAbsorber.peer_block): the note's
1
+ // A peer's text in its answer note (PeerNote.block): the note's
2
2
  // content holds it between a `[PEER MESSAGE from <Peer>]` line and an
3
3
  // `[END PEER MESSAGE]` line, each of its lines that opened like one of chi's
4
4
  // frames led by "(quoted) " (PEER_FORGED_OPENINGS, the Ruby side's list:
@@ -7,7 +7,7 @@
7
7
  // docs/internals/web-frontend.md, The peer exchange.
8
8
  // Its own module: new exports go in new modules (web-frontend.md).
9
9
 
10
- // ContextAbsorber::PEER_FORGED_OPENINGS (Steer::FORGED_OPENINGS and chi's
10
+ // PeerNote::FORGED_OPENINGS (Steer::FORGED_OPENINGS and chi's
11
11
  // system frames, those with their colon, and the tool-result frames).
12
12
  export const PEER_FORGED_OPENINGS = Object.freeze([
13
13
  "Review contribution", "Steer", "Automatic input", "Delegate report", "CONTEXT NOTE", "TASK FINISHED",
@@ -15,8 +15,8 @@ export const PEER_FORGED_OPENINGS = Object.freeze([
15
15
  "SYSTEM:", "END REMINDERS", "CONTEXT:", "WORKING SET:",
16
16
  "tool results", "images from tool results",
17
17
  ]);
18
- // ContextAbsorber::PEER_FORGED_LINES: chi's unbracketed lines it quotes.
19
- export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:"]);
18
+ // PeerNote::FORGED_LINES: chi's unbracketed lines it quotes.
19
+ export const PEER_FORGED_LINES = Object.freeze(["chi started this turn", "Updated:", "Attached:", "Message #"]);
20
20
 
21
21
  const escape = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
22
22
  // An opening that ends in a word character must end there (\b).
@@ -0,0 +1,199 @@
1
+ // The history's background items fold into one closed row per stretch:
2
+ // anything nobody says in the conversation (a plugin's or chi's own line
3
+ // shown on its own, a context note that starts nothing, an objectives line,
4
+ // a plugin's nudge) folds; what someone says stays (the prompt, the answer,
5
+ // a peer's answer, a note that opens a wake turn). A stretch is a run of
6
+ // those items side by side among #history's children; the turn's timing
7
+ // line passes through one, anything else ends it. Its row reads
8
+ // "▸ 3 notes · claude-inbox, source-links, skills" (and "⚠ N" for its warn
9
+ // lines), closed; a click (Enter/Space: it is a button) opens it and the
10
+ // items show under it as they are.
11
+ //
12
+ // Modelled on answer_fold.js: the row is a sibling just before the
13
+ // stretch's first item, never a wrapper (the bubbles keep their place,
14
+ // copy text and observers); the items are hidden by a class
15
+ // (`.quiet-hidden`, CSS). Each row is tied to its first item (rowOf /
16
+ // firstOf), so a row something was placed after (a recap, a snapshot's
17
+ // card) goes back before its item, open or not. The open state is the
18
+ // row's class only, never stored: a re-render draws the rows closed.
19
+ //
20
+ // One observer on #history (its own children only) settles every path that
21
+ // fills it; a settle that changes nothing writes nothing (the other
22
+ // observers on #history see class and subtree writes).
23
+ //
24
+ // A module of its own: static JS is cached at unversioned URLs, and a new
25
+ // export on an old module would blank the page.
26
+ import { html, render, nothing } from "./vendor/lit-html.js";
27
+
28
+ // What folds: a direct child of #history that is one of these.
29
+ export const QUIET = ".bubble.hook-notice:not(.empty-answer), .bubble.note:not(.turn-start):not(.peer-answer), .bubble.steer-row";
30
+ // What a stretch passes over (it stays visible).
31
+ export const PASS = ".turn-timing";
32
+ export const ROW_CLASS = "quiet-notes";
33
+ export const HIDDEN_CLASS = "quiet-hidden";
34
+
35
+ const ARRIVE = "ts-arrive";
36
+ const ROW = `.${ROW_CLASS}`;
37
+
38
+ // The stretches over +children+ (an element list, in order): [{ items }].
39
+ // Pure.
40
+ export function quietRuns(children) {
41
+ const runs = [];
42
+ let run = null;
43
+ for (const el of children) {
44
+ if (el.nodeType !== 1 || el.matches(ROW) || el.matches(PASS)) continue;
45
+ if (!el.matches(QUIET)) {
46
+ run = null;
47
+ continue;
48
+ }
49
+ if (!run) runs.push((run = { items: [] }));
50
+ run.items.push(el);
51
+ }
52
+ return runs;
53
+ }
54
+
55
+ const squash = (text) => String(text ?? "").replace(/\s+/g, " ").trim();
56
+
57
+ // One item's name on the row.
58
+ function itemName(el) {
59
+ if (el.matches(".objectives-entry")) return "objectives";
60
+ if (el.matches(".steer-row")) {
61
+ const said = squash(el.querySelector("summary")?.textContent);
62
+ return said.endsWith(" nudged the model") ? said.slice(0, -" nudged the model".length) : "nudge";
63
+ }
64
+ if (el.matches(".hook-notice")) {
65
+ // "<bundle or hook>: text"; chi's own lines ("↻ empty answer, asking
66
+ // again") name no one.
67
+ const text = squash(el.textContent);
68
+ const at = text.indexOf(": ");
69
+ const who = at > 0 ? text.slice(0, at) : "";
70
+ return /^[A-Za-z0-9][\w./-]*$/.test(who) ? who : "chi";
71
+ }
72
+ const line = squash(el.querySelector(".note-line")?.textContent) || "note";
73
+ if (line === "note") return "context";
74
+ const from = line.replace(/^note from /, "").replace(/^context /, "");
75
+ // Another session's note (ContextNote.label): "session <id6> (<~cwd>)".
76
+ const session = /^(session \S+) \(.*\)$/.exec(from);
77
+ return session ? session[1] : from;
78
+ }
79
+
80
+ // A row's words: how many items, their names (unique, in order) and how
81
+ // many are warnings (the `warn` class, never the text). Pure.
82
+ export function rowLabel(items) {
83
+ const names = [];
84
+ let warn = 0;
85
+ for (const el of items) {
86
+ const name = itemName(el);
87
+ if (!names.includes(name)) names.push(name);
88
+ if (el.classList.contains("warn")) warn += 1;
89
+ }
90
+ return { count: items.length, names, warn };
91
+ }
92
+
93
+ export function rowTemplate({ count, names, warn }) {
94
+ return html`<span class="qn-count">${count} ${count === 1 ? "note" : "notes"}</span><span class="qn-names">· ${names.join(", ")}</span>${warn > 0 ? html`<span class="qn-warn">⚠ ${warn}</span>` : nothing}`;
95
+ }
96
+
97
+ // +el+ hidden in a closed row: that row opens. Nothing otherwise. A jump
98
+ // goes through history_reveal.js revealInHistory, which calls this.
99
+ export function revealQuiet(el) {
100
+ const hidden = el?.closest?.(`.${HIDDEN_CLASS}`);
101
+ if (!hidden) return;
102
+ let row = hidden.previousElementSibling;
103
+ while (row && !row.matches(ROW)) row = row.previousElementSibling;
104
+ if (row && !row.classList.contains("open")) row.click();
105
+ }
106
+
107
+ // @param historyEl the history (#history)
108
+ // @return {settle()} (the observer runs it; specs call it)
109
+ export function installQuietNotes({ historyEl }) {
110
+ // Each stretch's first item's row, and each row's first item.
111
+ const rowOf = new WeakMap();
112
+ const firstOf = new WeakMap();
113
+ // The words a row was last drawn with.
114
+ const drawn = new WeakMap();
115
+
116
+ function makeRow(first) {
117
+ const row = historyEl.ownerDocument.createElement("button");
118
+ row.type = "button";
119
+ row.className = ROW_CLASS;
120
+ row.setAttribute("aria-expanded", "false");
121
+ // The stretch's arrival plays on its row (its items are hidden).
122
+ if (first.classList.contains(ARRIVE)) {
123
+ row.classList.add(ARRIVE);
124
+ row.addEventListener("animationend", () => row.classList.remove(ARRIVE), { once: true });
125
+ }
126
+ return row;
127
+ }
128
+
129
+ // A row already in the stretch's span (between its items, or just before
130
+ // its first) that no other stretch took: the stretch's first item changed.
131
+ function rowInSpan(items, taken) {
132
+ const last = items[items.length - 1];
133
+ const before = items[0].previousElementSibling;
134
+ if (before?.matches(ROW) && firstOf.has(before) && !taken.has(before)) return before;
135
+ for (let el = items[0]; el && el !== last; el = el.nextElementSibling) {
136
+ if (el.matches(ROW) && firstOf.has(el) && !taken.has(el)) return el;
137
+ }
138
+ return null;
139
+ }
140
+
141
+ function drawRow(row, items) {
142
+ const label = rowLabel(items);
143
+ const words = JSON.stringify(label);
144
+ if (drawn.get(row) !== words) {
145
+ render(rowTemplate(label), row);
146
+ drawn.set(row, words);
147
+ }
148
+ const open = row.classList.contains("open");
149
+ const expanded = open ? "true" : "false";
150
+ if (row.getAttribute("aria-expanded") !== expanded) row.setAttribute("aria-expanded", expanded);
151
+ if (row.classList.contains("warn") !== label.warn > 0) row.classList.toggle("warn", label.warn > 0);
152
+ for (const item of items) {
153
+ if (item.classList.contains(HIDDEN_CLASS) === !open) continue;
154
+ item.classList.toggle(HIDDEN_CLASS, !open);
155
+ // A hidden element never fires animationend: its slide-up would play
156
+ // the first time the row opens.
157
+ if (!open && item.classList.contains(ARRIVE)) item.classList.remove(ARRIVE);
158
+ }
159
+ }
160
+
161
+ function settle() {
162
+ const taken = new Set();
163
+ const shown = new Set();
164
+ for (const { items } of quietRuns(historyEl.children)) {
165
+ const first = items[0];
166
+ let row = rowOf.get(first);
167
+ if (!row || row.parentElement !== historyEl || taken.has(row)) row = rowInSpan(items, taken);
168
+ if (!row) row = makeRow(first);
169
+ if (firstOf.get(row) !== first) {
170
+ firstOf.set(row, first);
171
+ rowOf.set(first, row);
172
+ }
173
+ if (first.previousElementSibling !== row) historyEl.insertBefore(row, first);
174
+ taken.add(row);
175
+ drawRow(row, items);
176
+ for (const item of items) shown.add(item);
177
+ }
178
+ for (const row of historyEl.querySelectorAll(`:scope > ${ROW}`)) if (!taken.has(row)) row.remove();
179
+ // An item no stretch holds any more (it changed kind): shown again.
180
+ for (const el of historyEl.querySelectorAll(`:scope > .${HIDDEN_CLASS}`)) if (!shown.has(el)) el.classList.remove(HIDDEN_CLASS);
181
+ }
182
+
183
+ const ours = (n) => n.nodeType !== 1 || n.classList.contains(ROW_CLASS);
184
+ new MutationObserver((records) => {
185
+ if (records.some((r) => [...r.addedNodes, ...r.removedNodes].some((n) => !ours(n)))) settle();
186
+ }).observe(historyEl, { childList: true });
187
+ settle();
188
+
189
+ historyEl.addEventListener("click", (e) => {
190
+ const row = e.target.closest?.(ROW);
191
+ if (!row || row.parentElement !== historyEl || !firstOf.has(row)) return;
192
+ e.preventDefault();
193
+ const open = !row.classList.contains("open");
194
+ row.classList.toggle("open", open);
195
+ settle();
196
+ });
197
+
198
+ return { settle };
199
+ }