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
@@ -11,7 +11,8 @@
11
11
  // notices. Kinds come from KINDS only (the server logs them, nothing else).
12
12
  // `cls` is a kind's own modifier (a row's status, a warn notice, a report
13
13
  // reply), `text` what it says, whitespace collapsed. Left out: lit's
14
- // comments, the copy button, the quiet-stretch dividers (`.time-gap`), and
14
+ // comments, the copy button, the quiet-stretch dividers (`.time-gap`), the
15
+ // notes rows (`.quiet-notes`, quiet_notes.js: their items are read), and
15
16
  // view state (folds, stacks, the motion classes): nothing a reader would
16
17
  // call a different turn.
17
18
  //
@@ -133,7 +134,7 @@ function otherItem(el) {
133
134
  // One child of #history as its items (a turn block's are several; a time
134
135
  // divider none).
135
136
  function childItems(el) {
136
- if (el.matches(".time-gap, .copy-btn")) return [];
137
+ if (el.matches(".time-gap, .copy-btn, .quiet-notes")) return [];
137
138
  if (el.matches(".turn-work")) {
138
139
  return [item("block", textOf(el.querySelector(":scope > summary .turn-steps"))),
139
140
  ...[...el.querySelectorAll(":scope > .gen")].flatMap(stepItems)];
@@ -88,6 +88,18 @@ main{flex:1;display:flex;flex-direction:column;gap:16px;padding:max(16px,var(--s
88
88
  #history>.bubble.folded{display:none}
89
89
  .answer-fold{display:flex;align-items:baseline;gap:0;align-self:stretch;max-width:100%;min-width:0;margin:0;padding:12px 14px;font:inherit;font-size:13px;font-weight:600;line-height:1.5;color:var(--text-muted);text-align:left;cursor:pointer;user-select:none;background:var(--bg-panel);border:1px solid var(--border);border-left:3px solid var(--accent-strong);border-radius:12px}
90
90
  @media(hover:hover){.answer-fold:hover .answer-fold-size{color:var(--text-2)}}
91
+ /* The notes row (quiet_notes.js): a stretch of background items folded, a sibling before them. */
92
+ #history>.quiet-hidden{display:none}
93
+ .quiet-notes{display:flex;align-items:baseline;gap:6px;align-self:stretch;max-width:100%;min-width:0;min-height:32px;margin:0;padding:5px 12px;font:inherit;font-size:13px;line-height:1.5;color:var(--text-muted);text-align:left;cursor:pointer;user-select:none;background:transparent;border:0;border-left:3px solid var(--text-faint);border-radius:0 8px 8px 0}
94
+ @media(hover:hover){.quiet-notes:hover{background:var(--bg-hover);color:var(--text-2)}}
95
+ .quiet-notes:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
96
+ .quiet-notes::before{content:"▸";color:var(--text-faint);flex:none}
97
+ .quiet-notes.open::before{content:"▾"}
98
+ .quiet-notes.warn{border-left-color:var(--warn)}
99
+ .qn-count{flex:none;font-weight:600;white-space:nowrap}
100
+ .qn-names{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
101
+ .qn-warn{flex:none;margin-left:auto;white-space:nowrap;color:var(--warn-text)}
102
+ @media(max-width:600px){.quiet-notes{min-height:44px;align-items:center}}
91
103
  .answer-fold:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
92
104
  .answer-fold::before{content:"▾\00a0";color:var(--accent);flex:none}
93
105
  .answer-fold.folded::before{content:"▸\00a0"}
@@ -1478,6 +1490,7 @@ body.has-session.composer-folded #infoBar{border-top-color:transparent}
1478
1490
  .peer-pop-legs{display:flex;flex-direction:column;gap:6px}
1479
1491
  .peer-pop-legs>div{display:flex;gap:8px}
1480
1492
  .peer-pop-legs>div>span:first-child{flex:0 0 78px;color:var(--text-muted)}
1493
+ .peer-pop-legs>div>span:last-child{min-width:0;overflow-wrap:anywhere;word-break:break-word}
1481
1494
  .peer-pop-legs .warn{color:var(--warn-text)}
1482
1495
  .peer-pop-list{display:flex;flex-direction:column}
1483
1496
  .peer-pop-list-head{font-size:12px;font-weight:700;color:var(--text-muted);padding-bottom:4px}
@@ -1990,6 +2003,12 @@ body.strip-hidden #topStrip{display:none}
1990
2003
  /* In the all view the pill is the view's own button, pressed (app.js applyView), whatever the strip's state: a click goes
1991
2004
  back to the chat (lists_view.js), as Esc does. */
1992
2005
  body.all-view #stripStatus{display:block}
2006
+ /* The pages pill (pages_wiring.js): the sessions pill's look and size, shown in every view while there is a page (not tied
2007
+ to the strip or the all view); hidden while there is none. */
2008
+ #pagesBtn{display:block;align-self:center;height:28px;min-height:0;padding:0 10px;font-size:12px;line-height:26px;white-space:nowrap;flex-shrink:0}
2009
+ #pagesBtn[hidden]{display:none}
2010
+ #pagesBtn:not([aria-pressed="true"]){border-color:var(--border-strong)}
2011
+ #pagesBtn:hover{filter:none}
1993
2012
  .strip-toggle[aria-pressed="true"]{color:var(--accent);background:var(--accent-bg);border-color:var(--accent-border)}
1994
2013
  .strip-toggle[aria-pressed="true"]:hover{color:var(--accent);border-color:var(--accent)}
1995
2014
  /* The strip's grip (strip_grip.js, app.js): the handle on the edge between the
@@ -2172,8 +2191,8 @@ a{color:var(--accent)}
2172
2191
  #notifyBtn::after{content:"";position:absolute;inset:-7px}
2173
2192
  #scope .scope-all{position:relative}
2174
2193
  #scope .scope-all::after{content:"";position:absolute;inset:-14px -4px -11px -3px}
2175
- #stripStatus{position:relative}
2176
- #stripStatus::after{content:"";position:absolute;inset:-2px 0}
2194
+ #stripStatus,#pagesBtn{position:relative}
2195
+ #stripStatus::after,#pagesBtn::after{content:"";position:absolute;inset:-2px 0}
2177
2196
  #newBtn{position:relative}
2178
2197
  #newBtn::after{content:"";position:absolute;inset:-3px}
2179
2198
  /* The pill: its box 40px tall and borderless (no border at all: a transparent one would put the ::before 1px inside, a
@@ -2243,6 +2262,7 @@ body.participant-view .bubble.user:not(.by-participant)>.origin-label{color:var(
2243
2262
  <button type="button" id="newBtn" class="brand" title="New session" aria-label="New session"><span class="brand-name">chi</span><span class="brand-new"><svg class="brand-plus" viewBox="0 0 12 12" aria-hidden="true"><path d="M6 1.5v9M1.5 6h9"/></svg><span class="brand-new-word">new session</span></span></button>
2244
2263
  <div id="scope" hidden></div>
2245
2264
  <button type="button" class="strip-toggle" id="stripStatus" title="All sessions, with search"><span class="pill-count">sessions</span></button>
2265
+ <button type="button" class="strip-toggle" id="pagesBtn" title="Pages" aria-pressed="false" hidden>pages</button>
2246
2266
  <span id="sessionTitleRow" hidden><span id="sessionTitle"></span><button type="button" id="titleRenameBtn" class="title-rename" title="Rename this session" aria-label="Rename this session" hidden>✎</button></span>
2247
2267
  <div id="allHead">
2248
2268
  <input id="filter" name="chi-filter" type="search" placeholder="Search preview, id or status" aria-label="Search sessions" autocomplete="off">
@@ -2315,7 +2335,9 @@ body.participant-view .bubble.user:not(.by-participant)>.origin-label{color:var(
2315
2335
  </div>
2316
2336
  </section>
2317
2337
  <section id="pageView" aria-label="Page"></section>
2338
+ <section id="pagesList" aria-label="Pages"></section>
2318
2339
  <section id="allView" aria-label="All sessions">
2340
+ <!-- Inert: an older cached app.js draws All sessions' Pages group into it (pages_group.js). Remove after 0.189.0. -->
2319
2341
  <div id="pagesGroup" hidden></div>
2320
2342
  <div id="allList"></div>
2321
2343
  <div id="allPath" hidden>Stored in <code></code></div>
@@ -20,7 +20,7 @@ export function notifyState({ hasNotification, secureContext, permission, wanted
20
20
  // an attached context change (context) or a finished background task
21
21
  // (task): nobody started it, so nobody watched it.
22
22
  const DONE_MIN_SECONDS = 10;
23
- const UNWATCHED_ORIGINS = ["delegate_report", "context", "task"];
23
+ const UNWATCHED_ORIGINS = ["delegate_report", "context", "peer", "task"];
24
24
  const VERDICT_REASON_MAX = 120;
25
25
 
26
26
  // The step-limit question (kind "continue": a turn ran out of iterations
@@ -14,10 +14,11 @@ export const CLAIM_ROOTS = ".bubble.output, .bubble.user .user-message, .gen-tex
14
14
  // current.dir, sessions; read only). +claimTarget()+: participants_panel.js's
15
15
  // (the people a link would open for, or null). +toast(text, action,
16
16
  // options)+, +copyText(text)+ (copy.js), +allViewOpen()+: the page's.
17
- // +home+: the home folder the page shows as ~ (body data-home). +win+: the
18
- // window (location, history).
17
+ // +pagesAreaOpen()+: the page's pages check (views.js's: a page at #/p/NAME
18
+ // or the Pages list at #/pages open). +home+: the home folder the page shows
19
+ // as ~ (body data-home). +win+: the window (location, history).
19
20
  export function createOwnerClaim({
20
- historyEl, store, claimTarget, toast, copyText, allViewOpen, home = "", win = globalThis,
21
+ historyEl, store, claimTarget, toast, copyText, allViewOpen, pagesAreaOpen = () => false, home = "", win = globalThis,
21
22
  }) {
22
23
  // A claim link waiting for its session to be drawn (selectFromHash sets it).
23
24
  let pendingClaim = null;
@@ -35,13 +36,14 @@ export function createOwnerClaim({
35
36
  // participant page shows it (the session's folder relative, home ~); a
36
37
  // quote not found marks its turn; neither found: the top, and a toast.
37
38
  // The hash is the session's again (select() already set it when it
38
- // switched), so the same link opened again is a hashchange.
39
+ // switched), so the same link opened again is a hashchange. Not while a
40
+ // pages view is open either: #/pages or #/p/NAME stays, as All sessions does.
39
41
  function showPending() {
40
42
  const claim = pendingClaim;
41
43
  if (!claim || claim.sessionId !== store.selected) return;
42
44
  pendingClaim = null;
43
45
  const { location, history } = win;
44
- if (!allViewOpen() && location.hash !== sessionHash(store.selected)) {
46
+ if (!allViewOpen() && !pagesAreaOpen() && location.hash !== sessionHash(store.selected)) {
45
47
  history.replaceState(null, "", location.pathname + location.search + sessionHash(store.selected));
46
48
  }
47
49
  if (showClaim(historyEl, claim, { roots: CLAIM_ROOTS, opts: opts() })) return;
@@ -5,7 +5,7 @@
5
5
  // whose href starts with #/p/: a plain left click (button 0, no modifier
6
6
  // key, not a ref link) opens the view and stops the new tab; a modified or
7
7
  // middle click keeps the browser's own way. Other in-page links are left as
8
- // they are. Installed only with the pages capability (app.js).
8
+ // they are. Installed only with the pages capability (pages_wiring.js).
9
9
  import { pageNameFromHash } from "./page_route.js";
10
10
 
11
11
  // The page a click on +event+ asks for, or null when it isn't one this
@@ -1,17 +1,20 @@
1
1
  // The page view, #/p/NAME (docs/pages.md, docs/internals/pages.md): in
2
2
  // place of the session area, a page's header (Back, the title, when it ran
3
- // and how long it took, Refresh, ⋯ with Delete) and its blocks. Opening
3
+ // and how long it took, Refresh, ⋯ with Delete) and its blocks. A page a
4
+ // bundle ships (+bundle+ in its JSON) has no ⋯: it can't be deleted, only
5
+ // its bundle uninstalled (Pages::FromBundle). Opening
4
6
  // reads the stored page (GET /api/pages/NAME) and runs nothing; Refresh
5
7
  // runs the script (POST …/run, answered when the run ended) and redraws
6
8
  // from its answer. A failed run keeps the last result, dimmed, under the
7
9
  // failure line; a script changed since the result says so. Nothing is
8
10
  // live: a run from a terminal shows after the next open.
9
11
  //
10
- // Made only with the pages capability (app.js; the null-control rule).
12
+ // Made only with the pages capability (pages_wiring.js; the null-control rule).
11
13
  // +root+: #pageView. +api+: data.js api. +toast(text)+, +copyText(text)+:
12
14
  // the page's. +back()+: Back (views.js closePage). +gone()+: the page was
13
15
  // removed (a Delete, or a Refresh answered "no such page"): views.js leaves
14
- // for All sessions. +backLabel()+: what Back says ("Sessions" or "Back").
16
+ // for the Pages list. +backLabel()+: what Back says ("Pages", "Back", or
17
+ // "Sessions" from All sessions).
15
18
  // +phone()+: the page's phone width now (the table's phone form). The view
16
19
  // keeps each table's sort and opened rows (page_table.js) until another
17
20
  // page opens.
@@ -23,6 +26,7 @@ import { tableTemplate } from "./page_table.js";
23
26
  import { nextSort } from "./page_table_model.js";
24
27
  import { changedText, errorCopy, failureParts, keptAfterFailure, metaText } from "./page_model.js";
25
28
 
29
+ // Mirrors Pages::NAME (lib/samagotchi/pages.rb).
26
30
  const PAGE_NAME = /^[a-z0-9][a-z0-9-]{0,39}$/;
27
31
 
28
32
  export function createPageView({
@@ -55,7 +59,7 @@ export function createPageView({
55
59
  async function open(name) {
56
60
  if (view?.name === name) return;
57
61
  const run = runs.get(name);
58
- const valid = PAGE_NAME.exec(name)?.[0] === name;
62
+ const valid = PAGE_NAME.test(name);
59
63
  view = { name, status: valid ? "loading" : "missing", page: null, error: null, running: !!run, stderr: "", menu: "closed", deleting: false,
60
64
  sorts: {}, opened: {} };
61
65
  draw();
@@ -157,12 +161,12 @@ export function createPageView({
157
161
  ${meta ? html`<span class="pg-meta">${meta}</span>` : nothing}
158
162
  ${ready ? html`<span class="pg-actions">
159
163
  <button type="button" class="pg-refresh primary" ?disabled=${v.running} @click=${refresh}>${v.running ? "Running…" : "Refresh"}</button>
160
- <span class="pg-more">
164
+ ${page.bundle ? nothing : html`<span class="pg-more">
161
165
  <button type="button" class="pg-menu-btn ghost" aria-label="More" title="More" aria-haspopup="menu"
162
166
  aria-expanded=${v.menu === "closed" ? "false" : "true"}
163
167
  @click=${() => set({ menu: v.menu === "closed" ? "open" : "closed" })}>⋯</button>
164
168
  ${menuTemplate(v)}
165
- </span>
169
+ </span>`}
166
170
  </span>` : nothing}
167
171
  </div>`;
168
172
  }
@@ -4,6 +4,12 @@
4
4
  #pageView{display:none;flex:1;min-height:0;min-width:0;flex-direction:column;gap:12px;overflow-y:auto;overflow-x:hidden;padding:2px 2px 24px}
5
5
  body.page-view #pageView{display:flex}
6
6
  body.page-view :is(#center,#topStrip,#stripGrip,#allView,#sessionTitleRow){display:none}
7
+ /* The Pages list, #/pages (pages_list.js): the same place, while body.pages-list; its cards in All sessions' grid. */
8
+ #pagesList{display:none;flex:1;min-height:0;min-width:0;flex-direction:column;gap:12px;overflow-y:auto;overflow-x:hidden;padding:2px 2px 24px}
9
+ body.pages-list #pagesList{display:flex}
10
+ body.pages-list :is(#center,#topStrip,#stripGrip,#allView,#sessionTitleRow){display:none}
11
+ .pl-cards{display:grid;grid-template-columns:repeat(auto-fill,minmax(260px,1fr));gap:12px;align-content:start}
12
+ .pl-cards .pg-card{padding:12px 14px;font-size:12.5px}
7
13
  .pg-head{display:flex;flex-wrap:wrap;align-items:center;gap:8px 12px}
8
14
  .pg-back{flex:none;padding:5px 10px;font-size:13px;color:var(--accent);background:transparent;border:1px solid transparent}
9
15
  @media(hover:hover){.pg-back:hover{filter:none;border-color:var(--border-strong)}}
@@ -64,8 +70,9 @@ button.pg-row-main:focus-visible{outline:2px solid var(--accent);outline-offset:
64
70
  /* The meta wraps under the title, on a line of its own. */
65
71
  .pg-meta{order:5;flex-basis:100%;white-space:normal}
66
72
  }
67
- /* All sessions' Pages group (pages_group.js): one row of cards that scrolls sideways, over a "Sessions" heading. Its own
68
- card rules: the session cards' are scoped to #allList .card. */
73
+ /* A page card (the Pages list's), and All sessions' former Pages group (pages_group.js, kept for an older cached app.js:
74
+ remove the group's rules after 0.189.0): one row of cards that scrolls sideways. Their own card rules: the session
75
+ cards' are scoped to #allList .card. */
69
76
  #pagesGroup{display:flex;flex-direction:column;gap:8px;flex:none;min-width:0}
70
77
  #pagesGroup[hidden]{display:none}
71
78
  .pg-group-label{font-size:11px;font-weight:600;line-height:1;letter-spacing:.06em;text-transform:uppercase;color:var(--text-faint)}
@@ -1,25 +1,21 @@
1
- // All sessions' Pages group (decision D1 A, docs/pages.md): above the
2
- // session cards, one row of page cards that scrolls sideways (it never takes
3
- // more than a row of a phone's first screen), each with its title and "ran 2
4
- // min ago", "failed 1 h ago" or "never ran"; a tap opens the page. A
5
- // "Sessions" heading sits over the session cards while the group shows. It
6
- // is read when All sessions opens (GET /api/pages) and filtered by the
7
- // search field (#filter) by name and title; with no page, or none the search
8
- // matches, the group and its heading are not there. Not live: a page made
9
- // meanwhile shows the next time All sessions opens.
1
+ // All sessions' former Pages group: KEPT for an older cached app.js until
2
+ // the next release (the mixed-cache rule, docs/internals/web-frontend.md):
3
+ // remove after 0.189.0, with index.html's inert #pagesGroup and pages.css's
4
+ // group rules. Pages have their own way in now: the top bar's pages pill
5
+ // and the Pages list (pages_list.js, pages_wiring.js); nothing new imports
6
+ // this. What it did: above the session cards, one row of page cards, read
7
+ // when All sessions opened (GET /api/pages) and filtered by the search
8
+ // field (#filter), under a "Pages" heading and over a "Sessions" one.
10
9
  //
11
- // Made only with the pages capability (app.js). +root+: #pagesGroup.
12
- // +filterEl+: #filter. +api+: data.js api. +open(name)+: views.js openPage.
10
+ // +root+: #pagesGroup. +filterEl+: #filter. +api+: data.js api.
11
+ // +open(name)+: views.js openPage. +model+: pages_model.js's list, read
12
+ // through instead of its own GET when given.
13
13
  import { html, nothing, render } from "./vendor/lit-html.js";
14
14
  import { createLatest } from "./latest.js";
15
- import { relativeTime } from "./format.js";
15
+ import { pageCardMeta } from "./pages_list.js";
16
16
 
17
- // A card's second line.
18
- export function pageCardMeta(page, now = Date.now()) {
19
- if (page.state === "failed" && page.failed_at) return `failed ${relativeTime(page.failed_at, now)}`;
20
- if (page.ran_at) return `ran ${relativeTime(page.ran_at, now)}`;
21
- return "never ran";
22
- }
17
+ // A card's second line (the list's).
18
+ export { pageCardMeta };
23
19
 
24
20
  // The pages the search shows: +query+ in the name or the title, any case.
25
21
  export function filterPages(pages, query = "") {
@@ -36,7 +32,7 @@ export function pagesGroupTemplate(pages, { open, now = Date.now() }) {
36
32
  <div class="pg-group-label">Sessions</div>`;
37
33
  }
38
34
 
39
- export function createPagesGroup({ root, filterEl, api, open, now = () => Date.now() }) {
35
+ export function createPagesGroup({ root, filterEl, api, open, model = null, now = () => Date.now() }) {
40
36
  let pages = [];
41
37
  const latest = createLatest();
42
38
 
@@ -47,6 +43,7 @@ export function createPagesGroup({ root, filterEl, api, open, now = () => Date.n
47
43
  }
48
44
 
49
45
  async function load() {
46
+ if (model) return model.load();
50
47
  const fresh = latest.begin();
51
48
  try {
52
49
  const answer = await api("/api/pages");
@@ -59,6 +56,11 @@ export function createPagesGroup({ root, filterEl, api, open, now = () => Date.n
59
56
  draw();
60
57
  }
61
58
 
59
+ // A failed read empties the group, as its own read does.
60
+ model?.subscribe((state) => {
61
+ pages = state.failed ? [] : state.pages;
62
+ draw();
63
+ });
62
64
  filterEl?.addEventListener("input", draw);
63
65
  draw();
64
66
  return { load, redraw: draw };
@@ -0,0 +1,73 @@
1
+ // The Pages list, #/pages (docs/pages.md): the one home of pages, opened by
2
+ // the top bar's pages pill (pages_wiring.js). In place of the session area,
3
+ // as a page is: Back, "Pages", and a card per page (its title, "ran 2 min
4
+ // ago", "failed 1 h ago" or "never ran") in /api/pages's order; a tap
5
+ // opens #/p/NAME. "Loading…" until the first answer ("Couldn't read the
6
+ // pages." when it failed), "No pages." when there is none. It draws the page's list (pages_model.js) and reads it again
7
+ // each time it opens. Also the list's hash, so views.js reads it where it
8
+ // reads a page's (a new module: page_route.js takes no new export).
9
+ //
10
+ // Made only with the pages capability (pages_wiring.js). +root+: #pagesList
11
+ // (null on an older cached index.html: nothing drawn). +model+:
12
+ // pages_model.js's. +open(name)+: views.js openPage. +back()+: views.js
13
+ // closePagesList.
14
+ import { html, nothing, render } from "./vendor/lit-html.js";
15
+ import { relativeTime } from "./format.js";
16
+
17
+ export const PAGES_HASH = "#/pages";
18
+
19
+ // Whether +hash+ is the Pages list for this viewer: never on a
20
+ // participant's page and never without the pages capability.
21
+ export function pagesListOpen(hash, { participant = null, canPages = false } = {}) {
22
+ return !participant && !!canPages && hash === PAGES_HASH;
23
+ }
24
+
25
+ // A card's second line.
26
+ export function pageCardMeta(page, now = Date.now()) {
27
+ if (page.state === "failed" && page.failed_at) return `failed ${relativeTime(page.failed_at, now)}`;
28
+ if (page.ran_at) return `ran ${relativeTime(page.ran_at, now)}`;
29
+ return "never ran";
30
+ }
31
+
32
+ // +state+: the model's ({pages, loaded}).
33
+ export function pagesListTemplate(state, { open, back, now = Date.now() }) {
34
+ let body;
35
+ if (!state.loaded && state.failed) body = html`<div class="pg-note">Couldn't read the pages.</div>`;
36
+ else if (!state.loaded) body = html`<div class="pg-note">Loading…</div>`;
37
+ else if (!state.pages.length) body = html`<div class="pg-note">No pages.</div>`;
38
+ else {
39
+ body = html`<div class="pl-cards" role="list">${state.pages.map((page) => html`<button type="button" class="pg-card"
40
+ role="listitem" data-page=${page.name} @click=${() => open(page.name)}><b>${page.title || page.name}</b><span
41
+ class="pg-card-meta">${pageCardMeta(page, now)}</span></button>`)}</div>`;
42
+ }
43
+ return html`<div class="pg-head">
44
+ <button type="button" class="pg-back ghost" @click=${back}>← Back</button>
45
+ <h1 class="pg-title">Pages</h1>
46
+ </div>${body}`;
47
+ }
48
+
49
+ export function createPagesList({ root, model, open, back, now = () => Date.now() }) {
50
+ let shown = false;
51
+
52
+ function draw() {
53
+ if (!root) return;
54
+ render(shown ? pagesListTemplate(model.state(), { open, back, now: now() }) : nothing, root);
55
+ }
56
+
57
+ model.subscribe(() => shown && draw());
58
+
59
+ return {
60
+ open() {
61
+ if (!shown) {
62
+ shown = true;
63
+ draw();
64
+ }
65
+ model.load();
66
+ },
67
+ close() {
68
+ if (!shown) return;
69
+ shown = false;
70
+ draw();
71
+ },
72
+ };
73
+ }
@@ -0,0 +1,54 @@
1
+ // The page's one list of pages (GET /api/pages, docs/pages.md): what the
2
+ // pages' ways in read (the top bar's pages pill and the Pages list,
3
+ // pages_wiring.js). DOM-free. +load()+ reads the list, or returns the read
4
+ // already out (one request at boot however many views ask); +reload()+
5
+ // always asks again (something changed: the hub's `pages` event, a
6
+ // reconnect, a Delete), and an answer older than the newest ask is dropped
7
+ // (createLatest). A failed read keeps the last list and +loaded+ as they
8
+ // were (chi web restarting doesn't hide the pill); +failed+ says the last
9
+ // read failed. Subscribers are called with the state after every answer.
10
+ //
11
+ // +api+: data.js api.
12
+ import { createLatest } from "./latest.js";
13
+
14
+ export function createPagesModel({ api }) {
15
+ let state = { pages: [], loaded: false, failed: false };
16
+ let inFlight = null;
17
+ const subscribers = new Set();
18
+ const latest = createLatest();
19
+
20
+ function publish(fields) {
21
+ state = { ...state, ...fields };
22
+ for (const fn of [...subscribers]) fn(state);
23
+ }
24
+
25
+ function reload() {
26
+ const fresh = latest.begin();
27
+ const ask = (async () => {
28
+ await null; // inFlight is set before the read starts, even when api throws at once
29
+ try {
30
+ const answer = await api("/api/pages");
31
+ if (!fresh()) return;
32
+ publish({ pages: Array.isArray(answer?.pages) ? answer.pages : [], loaded: true, failed: false });
33
+ } catch (_) {
34
+ if (!fresh()) return;
35
+ publish({ failed: true });
36
+ } finally {
37
+ if (fresh()) inFlight = null;
38
+ }
39
+ })();
40
+ inFlight = ask;
41
+ return ask;
42
+ }
43
+
44
+ function load() {
45
+ return inFlight || reload();
46
+ }
47
+
48
+ function subscribe(fn) {
49
+ subscribers.add(fn);
50
+ return () => subscribers.delete(fn);
51
+ }
52
+
53
+ return { load, reload, subscribe, state: () => state };
54
+ }
@@ -0,0 +1,66 @@
1
+ // The pages capability's parts on the main page, wired in one place so
2
+ // app.js keeps one call (docs/pages.md, docs/internals/pages.md): the page's
3
+ // list of pages (pages_model.js), the top bar's pages pill (#pagesBtn), the
4
+ // Pages list at #/pages (pages_list.js), the page view at #/p/NAME
5
+ // (page_view.js) and the #/p/ links opening a page in place (page_links.js).
6
+ // Null without the capability (the null-control rule): the pill stays
7
+ // withheld, nothing of pages is made and nothing is read.
8
+ //
9
+ // The list is read once at boot (one request: a view opened at boot shares
10
+ // it), again when All sessions, the Pages list or a page opens, after a
11
+ // Delete, and live: on the hub's `pages` event (a script made or removed)
12
+ // and on every snapshot (the hub has no replay): the first one loads (it
13
+ // shares the boot read while that is out, and covers a page made between the
14
+ // boot read and the stream), every later one, a reconnect, reads again. The
15
+ // pill shows while the list has a page, in every view; a failed read keeps it
16
+ // as it was. With an older cached views.js (no openPagesList) it stays
17
+ // hidden: it would open nothing.
18
+ //
19
+ // +viewer+: viewer.js. +$+: the page's querySelector. +api+: data.js api.
20
+ // +toast(text)+, +copyText(text)+: the page's. +turnRoots()+: the roots an
21
+ // answer's links are in. +views()+: views.js's (made after this, so a
22
+ // getter): openPage, closePage, leavePage, pageBackLabel, openPagesList,
23
+ // closePagesList.
24
+ import { createPagesModel } from "./pages_model.js";
25
+ import { createPageView } from "./page_view.js";
26
+ import { createPagesList } from "./pages_list.js";
27
+ import { installPageLinks } from "./page_links.js";
28
+
29
+ export function wirePages({ viewer, $, api, toast, copyText, turnRoots, views, win = window }) {
30
+ const pill = viewer.control($("#pagesBtn"), "pages");
31
+ if (!viewer.can("pages")) return null;
32
+ const model = createPagesModel({ api });
33
+ const open = (name) => views().openPage(name);
34
+ const view = createPageView({
35
+ root: $("#pageView"), api, toast, copyText, back: () => views().closePage(),
36
+ // Gone (a Delete, or no such page on Refresh): the list is read again, so the pill hides with the last page.
37
+ gone: () => { model.reload(); views().leavePage(); },
38
+ backLabel: () => views().pageBackLabel(), phone: () => !!win.matchMedia?.("(max-width: 600px)").matches,
39
+ });
40
+ // A page opened reads the list too (views.js calls open again while it is open: once per page).
41
+ const pageView = { ...view, open: (name) => { if (view.name() !== name) model.load(); return view.open(name); } };
42
+ const pagesList = createPagesList({ root: $("#pagesList"), model, open, back: () => views().closePagesList?.() });
43
+ installPageLinks([...turnRoots(), $("#pageView")], { open });
44
+
45
+ model.subscribe((state) => {
46
+ if (pill) pill.hidden = !state.pages.length || typeof views()?.openPagesList !== "function";
47
+ });
48
+ pill?.addEventListener("click", () => views().openPagesList?.());
49
+ model.load();
50
+
51
+ let snapshots = 0;
52
+ return {
53
+ pageView,
54
+ pagesList,
55
+ // All sessions just opened.
56
+ allViewOpened: () => model.load(),
57
+ // The hub's `pages` event.
58
+ changed: () => model.reload(),
59
+ // A hub snapshot: the first loads (sharing a read that is out), every later one is a reconnect.
60
+ snapshot: () => {
61
+ snapshots += 1;
62
+ if (snapshots > 1) model.reload();
63
+ else model.load();
64
+ },
65
+ };
66
+ }