samagotchi 0.139.0 → 0.140.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 (75) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +62 -1
  3. data/README.md +2 -1
  4. data/docs/architecture.md +6 -4
  5. data/docs/cli.md +4 -2
  6. data/docs/configuration.md +36 -2
  7. data/docs/context.md +185 -44
  8. data/docs/guardrails.md +11 -0
  9. data/docs/internals/background-tasks.md +1 -1
  10. data/docs/internals/engine.md +7 -1
  11. data/docs/internals/prompt-caching.md +10 -1
  12. data/docs/internals/tool-guardrails.md +1 -1
  13. data/docs/plugins.md +35 -17
  14. data/docs/sessions.md +3 -2
  15. data/lib/samagotchi/bridge/turn_accumulator.rb +4 -1
  16. data/lib/samagotchi/bundle_command.rb +21 -21
  17. data/lib/samagotchi/bundles/dev/manifest.yml +2 -2
  18. data/lib/samagotchi/bundles/github-pr/manifest.yml +4 -4
  19. data/lib/samagotchi/bundles/github-pr/plugin.rb +72 -176
  20. data/lib/samagotchi/bundles/mcp/manifest.yml +2 -2
  21. data/lib/samagotchi/bundles/mcp/plugin.rb +8 -3
  22. data/lib/samagotchi/bundles/system/manifest.yml +2 -2
  23. data/lib/samagotchi/bundles/system/self_map.md +2 -2
  24. data/lib/samagotchi/cancellation_controller.rb +4 -4
  25. data/lib/samagotchi/config.rb +8 -0
  26. data/lib/samagotchi/context_candidate_choice.rb +154 -0
  27. data/lib/samagotchi/context_candidates.rb +259 -0
  28. data/lib/samagotchi/context_candidates_log.rb +90 -0
  29. data/lib/samagotchi/context_command.rb +33 -8
  30. data/lib/samagotchi/context_fetch.rb +2 -2
  31. data/lib/samagotchi/context_judge.rb +194 -0
  32. data/lib/samagotchi/context_report_command.rb +161 -0
  33. data/lib/samagotchi/context_sources.rb +67 -23
  34. data/lib/samagotchi/cut.rb +21 -0
  35. data/lib/samagotchi/cut_policy.rb +6 -5
  36. data/lib/samagotchi/engine.rb +189 -32
  37. data/lib/samagotchi/hook_routes.rb +3 -2
  38. data/lib/samagotchi/kernel_loop.rb +8 -6
  39. data/lib/samagotchi/launch_mode.rb +5 -2
  40. data/lib/samagotchi/llm/chat_loop.rb +3 -2
  41. data/lib/samagotchi/llm/openai_chat.rb +1 -1
  42. data/lib/samagotchi/memory_bundle/status.rb +13 -3
  43. data/lib/samagotchi/owner_prompt.rb +39 -0
  44. data/lib/samagotchi/plugin/api.rb +1 -0
  45. data/lib/samagotchi/plugin/attached_context.rb +91 -11
  46. data/lib/samagotchi/plugin/context.rb +7 -1
  47. data/lib/samagotchi/project_env.rb +16 -2
  48. data/lib/samagotchi/prompt_warmup.rb +9 -1
  49. data/lib/samagotchi/session_access/contract.rb +2 -0
  50. data/lib/samagotchi/session_access/local.rb +10 -0
  51. data/lib/samagotchi/session_access/remote.rb +14 -0
  52. data/lib/samagotchi/session_access.rb +5 -0
  53. data/lib/samagotchi/session_commands.rb +28 -5
  54. data/lib/samagotchi/side_judgments.rb +84 -0
  55. data/lib/samagotchi/steer_cut.rb +2 -1
  56. data/lib/samagotchi/terminal_ui.rb +15 -0
  57. data/lib/samagotchi/testing/boundary.rb +3 -1
  58. data/lib/samagotchi/testing/host.rb +4 -1
  59. data/lib/samagotchi/toolkit_report_command.rb +2 -1
  60. data/lib/samagotchi/toolkit_suggest.rb +3 -18
  61. data/lib/samagotchi/turn_note.rb +6 -6
  62. data/lib/samagotchi/version.rb +1 -1
  63. data/lib/samagotchi/web/app.rb +10 -3
  64. data/lib/samagotchi/web/auto_update.rb +8 -1
  65. data/lib/samagotchi/web/public/app.js +11 -3
  66. data/lib/samagotchi/web/public/composer.css +1 -1
  67. data/lib/samagotchi/web/public/index.html +10 -9
  68. data/lib/samagotchi/web/public/notifications.js +10 -1
  69. data/lib/samagotchi/web/public/revise_stack.js +6 -5
  70. data/lib/samagotchi/web/public/update_notice.js +21 -8
  71. data/lib/samagotchi/web/session_access_routes.rb +1 -1
  72. data/lib/samagotchi/web/session_hub.rb +5 -2
  73. data/lib/samagotchi/web/update_check.rb +14 -8
  74. data/lib/samagotchi/worker.rb +5 -0
  75. metadata +9 -1
@@ -41,6 +41,7 @@ require_relative "../config"
41
41
  require_relative "../output_formatter"
42
42
  require_relative "../image_store"
43
43
  require_relative "../message_time"
44
+ require_relative "../context_candidates"
44
45
  require_relative "../context_note"
45
46
  require_relative "../context_sources"
46
47
  require_relative "../context_providers"
@@ -1016,7 +1017,7 @@ module Samagotchi
1016
1017
  def handle_info(req)
1017
1018
  return participant_info unless principal(req).owner?
1018
1019
 
1019
- chi_state = @hub&.chi_state || { version: Samagotchi::VERSION, installed: nil, available: nil, update_blocked: nil, auto: "off" }
1020
+ chi_state = @hub&.chi_state || { version: Samagotchi::VERSION, installed: nil, available: nil, update_blocked: nil, auto: "off", restart: "idle" }
1020
1021
  json_response(200, { app: "chi-web", **chi_state, pid: Process.pid,
1021
1022
  cwd: Dir.pwd, start_dir: start_dir, features: INFO_FEATURES, lan: @lan && @lan[:ip],
1022
1023
  proxy: @public_url && { identity_header: @public_url.identity_header } })
@@ -1396,14 +1397,20 @@ module Samagotchi
1396
1397
  # (chi context ls from another machine, SessionAccess::Remote): each
1397
1398
  # source as it was added too, its command included (#context_row), and
1398
1399
  # a project's source a session's own of its name shadows, marked
1399
- # shadowed.
1400
+ # shadowed, and the session's candidates with their status.
1400
1401
  def handle_context_list(req, id)
1401
1402
  session = @session_class.load(id, state_dir: default_state_dir)
1402
1403
  own = ContextSources.session_location(id, state_dir: default_state_dir)
1403
1404
  subs = own.subscriptions
1404
1405
  full = truthy_param?(req.params["full"])
1405
1406
  rows = attached_context(session, shadowed: full).map { |attached| context_row(attached, own, subs, full: full) }
1406
- json_response(200, { session_id: id, sources: rows, can_add_url: ContextProviders.any? })
1407
+ body = { session_id: id, sources: rows, can_add_url: ContextProviders.any? }
1408
+ # The candidates too, for chi context ls (no UI shows them yet).
1409
+ if full
1410
+ body[:candidates] = ContextCandidates.with_status(id, project_root: session.project_root, state_dir: default_state_dir)
1411
+ .map { |candidate, status| candidate.to_h.merge("name" => candidate.name, "status" => status) }
1412
+ end
1413
+ json_response(200, body)
1407
1414
  end
1408
1415
 
1409
1416
  # GET /api/sessions/:id/context/:name: one source with its text
@@ -47,6 +47,10 @@ module Samagotchi
47
47
  RETRY_FOR = 24 * 3600
48
48
  # The phases a run is still in.
49
49
  OPEN_PHASES = %w[installed workers web].freeze
50
+ # How long stop waits for the thread: long enough for a preflight
51
+ # (Server::PREFLIGHT_TIMEOUT, 20 s; a healthy one takes about a
52
+ # second) to end on its own, so a stop never abandons one mid-way.
53
+ STOP_WAIT = 25
50
54
 
51
55
  # @param hub [SessionHub]
52
56
  # @param ops [UpdateOps]
@@ -87,7 +91,7 @@ module Samagotchi
87
91
  @wakeup << :stop
88
92
  thread = @thread
89
93
  @thread = nil
90
- thread&.join(5.0)
94
+ thread&.join(STOP_WAIT)
91
95
  nil
92
96
  end
93
97
 
@@ -226,6 +230,9 @@ module Samagotchi
226
230
  return if busy_turn?
227
231
 
228
232
  problem = @preflight&.call(record[:version])
233
+ # Stopped meanwhile (chi web is shutting down): no exec, no record
234
+ # of one; the next start takes it up again.
235
+ return if @stopped
229
236
  return restart_failed(record, problem, "auto_restart_preflight_failed") if problem
230
237
 
231
238
  save(record.merge(phase: "web"))
@@ -578,7 +578,8 @@ function noticeVersions(data) {
578
578
  Object.assign(versions, mergeVersionState(versions, data));
579
579
  const notice = versionNotice({ loaded: document.body.dataset.version, served: versions.served,
580
580
  installed: versions.installed, available: versions.available,
581
- updateBlocked: versions.updateBlocked, auto: versions.auto, shown: versionNoticesShown });
581
+ updateBlocked: versions.updateBlocked, auto: versions.auto, restart: versions.restart,
582
+ autoInstall: versions.autoInstall, shown: versionNoticesShown });
582
583
  store.touch("infoBar"); // the worker badge compares with the newest installed
583
584
  if (!notice) return;
584
585
  versionNoticesShown.add(notice.key);
@@ -607,7 +608,12 @@ function noticeVersions(data) {
607
608
  function updateFrame(data, { snapshot = false } = {}) {
608
609
  const wasOpen = updateSheet.isOpen();
609
610
  if (!updateSheet.receive(data)) return;
610
- if (data.operation === "install" && data.state === "done" && data.version) noticeVersions({ installed: data.version });
611
+ // Every install frame, not only the done one: the hub's `chi` frame with
612
+ // the new installed version comes before it, and an automatic run's
613
+ // running frame is what keeps that from offering the Restart toast.
614
+ if (data.operation === "install") {
615
+ noticeVersions({ ...(data.state === "done" && data.version ? { installed: data.version } : {}), chi_update: data });
616
+ }
611
617
  const label = `${data.auto ? "Automatic " : ""}${data.operation === "install" ? "chi update" : "worker restart"}`
612
618
  .replace(/^./, (c) => c.toUpperCase());
613
619
  if (!wasOpen && data.state === "running") {
@@ -822,7 +828,9 @@ function claimOpts() {
822
828
  // active (it opens in the browser their review link was opened in: one
823
829
  // link per browser, the cookie), else this page's own.
824
830
  function claimLinkFor(source, range) {
825
- if (!store.selected || !["answer", "step", "user"].includes(source.kind) || !historyEl.contains(source.root)) return null;
831
+ // Not in a Revise stack's switched copy (revise_switch.js: view only, no claim key; Verify isn't offered there either).
832
+ if (!store.selected || !["answer", "step", "user"].includes(source.kind) || !historyEl.contains(source.root)
833
+ || source.root.closest("[data-stack-view]")) return null;
826
834
  const turnId = turnIdOf(historyEl, source.root);
827
835
  if (!turnId) return null;
828
836
  const claim = { turnId, ...selectionClaim(source.root, range, claimOpts()) };
@@ -31,7 +31,7 @@ body.has-session #composer{flex-wrap:wrap;row-gap:6px}
31
31
  .revise-chip.refused{border-color:var(--warn-border);background:var(--warn-bg);color:var(--warn-text)}
32
32
  .revise-chip-text{min-width:0;overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
33
33
  .revise-chip-clear{flex:none;display:inline-flex;align-items:center;justify-content:center;width:20px;height:20px;min-height:0;padding:0;border:0;border-radius:999px;background:transparent;color:var(--text-muted);font-size:11px;cursor:pointer}
34
- .revise-chip-clear:hover{background:var(--bg-raised);color:var(--text-bright);filter:none}
34
+ .revise-chip-clear:hover{filter:none}@media(hover:hover){.revise-chip-clear:hover{background:var(--bg-raised);color:var(--text-bright)}}
35
35
  .chip{display:inline-flex;align-items:center;gap:6px;background:var(--accent-bg);border:1px solid var(--border-strong);border-radius:8px;padding:3px 4px 3px 3px;font-size:12px;max-width:220px}
36
36
  .chip img{width:32px;height:32px;object-fit:cover;border-radius:5px}
37
37
  .chip .chip-name{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
@@ -61,7 +61,7 @@ main{flex:1;display:flex;flex-direction:column;gap:16px;padding:16px;overflow:au
61
61
  /* Folded: the bubble hidden, an answer or a delegate report (#history's: outranks .bubble.user's display). */
62
62
  #history>.bubble.folded{display:none}
63
63
  .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}
64
- .answer-fold:hover .answer-fold-size{color:var(--text-2)}
64
+ @media(hover:hover){.answer-fold:hover .answer-fold-size{color:var(--text-2)}}
65
65
  .answer-fold:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
66
66
  .answer-fold::before{content:"▾\00a0";color:var(--accent);flex:none}
67
67
  .answer-fold.folded::before{content:"▸\00a0"}
@@ -92,7 +92,7 @@ main{flex:1;display:flex;flex-direction:column;gap:16px;padding:16px;overflow:au
92
92
  .revise-row.stacked{height:auto;margin-top:-6px;justify-content:flex-start}
93
93
  .revise-btn{min-height:0;height:22px;padding:0 8px;border-radius:6px;font-size:11px;line-height:20px;background:var(--bg-raised);border:1px solid var(--border-strong);color:var(--text-muted);cursor:pointer;opacity:0;transition:opacity .12s ease,color .12s ease}
94
94
  .revise-row:not(.stacked) .revise-btn{position:absolute;right:14px;top:-10px;z-index:1;height:20px;line-height:18px}
95
- .revise-btn:hover:not(:disabled){color:var(--text-bright);filter:none}
95
+ .revise-btn:hover:not(:disabled){filter:none}@media(hover:hover){.revise-btn:hover:not(:disabled){color:var(--text-bright)}}
96
96
  .revise-btn:disabled{cursor:default}
97
97
  .revise-btn.armed,.revise-btn.armed:hover:not(:disabled){opacity:1;color:var(--on-solid);background:var(--accent-strong);border-color:var(--accent-strong)}
98
98
  .bubble.output:hover+.revise-row .revise-btn,.revise-row:hover .revise-btn,.revise-row.stacked .revise-btn,.revise-btn:focus-visible{opacity:1}
@@ -110,7 +110,7 @@ main{flex:1;display:flex;flex-direction:column;gap:16px;padding:16px;overflow:au
110
110
  /* The ‹ N/M › switcher (revise_switch.js): an earlier version's answer read in the top one's place; its copy dashed, the top hidden. */
111
111
  .stack-switch{display:inline-flex;align-items:center;gap:2px;font-size:12px;color:var(--text-2);font-variant-numeric:tabular-nums}
112
112
  .stack-switch-btn{min-height:0;height:22px;min-width:24px;padding:0 6px;border-radius:6px;border:1px solid var(--border-strong);background:var(--bg-raised);color:var(--text-2);font:inherit;font-size:14px;line-height:20px;cursor:pointer}
113
- .stack-switch-btn:hover:not([aria-disabled="true"]){color:var(--text-bright);filter:none}
113
+ .stack-switch-btn:hover:not([aria-disabled="true"]){filter:none}@media(hover:hover){.stack-switch-btn:hover:not([aria-disabled="true"]){color:var(--text-bright)}}
114
114
  .stack-switch-btn:focus-visible{outline:2px solid var(--accent-ring);outline-offset:1px}
115
115
  .stack-switch-btn[aria-disabled="true"]{opacity:.4;cursor:default}
116
116
  .stack-switch-n{min-width:2.6em;text-align:center}
@@ -277,12 +277,12 @@ button.system-one-wrong,.system-one-wrong.marked{flex:none;padding:1px 6px;borde
277
277
  /* A reloaded row's ✂ mark (an LLM context edit on its output), one line above the output. */
278
278
  .activity-edit{flex:1 1 100%;min-width:0;margin:2px 0 0 var(--row-gutter);font-size:12px;color:var(--text-3);overflow:hidden;text-overflow:ellipsis;white-space:nowrap;cursor:help}
279
279
  .activity-diff>summary{cursor:pointer;font-size:12px;color:var(--text-muted);width:max-content;font-variant-numeric:tabular-nums}
280
- .activity-diff>summary:hover{color:var(--text-2)}
280
+ @media(hover:hover){.activity-diff>summary:hover{color:var(--text-2)}}
281
281
  .activity-diff[open]>summary{margin-bottom:4px}
282
282
  /* A read of the file as-is: its numbered lines, closed under the preview (read_view.js); open, they stand for it. */
283
283
  .activity-read{flex:1 1 100%;min-width:0;margin:2px 0 2px var(--row-gutter)}
284
284
  .activity-read>summary{cursor:pointer;font-size:12px;color:var(--text-muted);width:max-content;font-variant-numeric:tabular-nums}
285
- .activity-read>summary:hover{color:var(--text-2)}
285
+ @media(hover:hover){.activity-read>summary:hover{color:var(--text-2)}}
286
286
  .activity-read[open]>summary{margin-bottom:4px}
287
287
  .activity-row:has(>.activity-read[open])>.activity-output{display:none}
288
288
  .read-lines{margin:0;padding:4px 0;background:var(--bg-inset);border:1px solid var(--border);border-radius:6px;font-size:12px;line-height:1.45;overflow:auto;max-height:420px}
@@ -305,7 +305,7 @@ button.system-one-wrong,.system-one-wrong.marked{flex:none;padding:1px 6px;borde
305
305
  .has-steps .activity-command-cwd{flex:1 1 auto;min-width:0;margin:0;overflow-wrap:anywhere}
306
306
  .activity-command-cwd>.activity-command-sep{font-family:inherit;color:var(--text-faint)}
307
307
  .activity-command-raw{flex:none;display:inline-flex;align-items:center;height:18px;padding:0 7px;border:1px solid var(--border-strong);border-radius:9px;font-size:11px;color:var(--text-muted);cursor:pointer;user-select:none;-webkit-user-select:none}
308
- .activity-command-raw:hover{color:var(--text-2)}
308
+ @media(hover:hover){.activity-command-raw:hover{color:var(--text-2)}}
309
309
  .activity-command-raw input{position:absolute;opacity:0;width:0;height:0;margin:0}
310
310
  .activity-command-raw:has(input:focus-visible){outline:2px solid var(--accent);outline-offset:1px}
311
311
  .activity-command-raw:has(input:checked){color:var(--accent);border-color:var(--accent-border);background:var(--accent-tint)}
@@ -618,7 +618,7 @@ button.system-one-wrong,.system-one-wrong.marked{flex:none;padding:1px 6px;borde
618
618
  .bubble.output,.bubble.user{position:relative}
619
619
  .copy-btn{position:absolute;top:6px;right:6px;z-index:1;display:inline-flex;align-items:center;justify-content:center;height:24px;min-width:24px;padding:0 5px;border-radius:6px;background:var(--bg-raised);border:1px solid var(--border-strong);color:var(--text-muted);font-size:11px;line-height:1;white-space:nowrap;user-select:none;-webkit-user-select:none;opacity:0;transition:opacity .12s ease,color .12s ease}
620
620
  .copy-btn svg{display:block}
621
- .copy-btn:hover:not(:disabled){filter:none;color:var(--text-bright)}
621
+ .copy-btn:hover:not(:disabled){filter:none}@media(hover:hover){.copy-btn:hover:not(:disabled){color:var(--text-bright)}}
622
622
  .bubble:hover>.copy-btn,.code-wrap:hover>.copy-btn,.copy-btn:focus-visible,.copy-btn[data-copied]{opacity:1}
623
623
  .copy-btn[data-copied],.copy-btn[data-copied]:hover:not(:disabled){color:var(--ok-text)}
624
624
  .copy-btn[data-copied] svg{display:none}
@@ -847,8 +847,9 @@ body.stage-view .turn-stage{position:absolute;left:0;right:0;bottom:100%;margin-
847
847
  .ts-extras>.bubble.user>.copy-btn{display:none}
848
848
  .ts-extras>.bubble.user .user-message{display:-webkit-box;-webkit-box-orient:vertical;-webkit-line-clamp:2;line-clamp:2;overflow:hidden;white-space:normal}
849
849
  /* A line's markdown (a delegate report's) reads as a plain row in the clamped rows: its blocks run inline at the
850
- row's own size, a space between them, rules and copy buttons gone. The prompt line shown whole keeps it. */
851
- :is(.ts-extras>.bubble.user,.ts-prompt:not(.full)>.bubble.user)>.user-message.markdown :is(p,h1,h2,h3,h4,h5,h6,ul,ol,li,blockquote,pre,table,thead,tbody,tr,th,td,.code-wrap){display:inline;margin:0;padding:0;border:0;border-radius:0;background:transparent;font-size:inherit;font-weight:inherit;line-height:inherit;color:inherit;white-space:normal}
850
+ row's own size, font and baseline (a pre's monospace or a cell's middle alignment would make the line box taller), a
851
+ space between them, rules and copy buttons gone. The prompt line shown whole keeps it. */
852
+ :is(.ts-extras>.bubble.user,.ts-prompt:not(.full)>.bubble.user)>.user-message.markdown :is(p,h1,h2,h3,h4,h5,h6,ul,ol,li,blockquote,pre,table,thead,tbody,tr,th,td,.code-wrap){display:inline;margin:0;padding:0;border:0;border-radius:0;background:transparent;font-family:inherit;font-size:inherit;font-weight:inherit;line-height:inherit;color:inherit;white-space:normal;vertical-align:baseline}
852
853
  :is(.ts-extras>.bubble.user,.ts-prompt:not(.full)>.bubble.user)>.user-message.markdown :is(p,h1,h2,h3,h4,h5,h6,li,blockquote,pre,td,th)::after{content:" "}
853
854
  :is(.ts-extras>.bubble.user,.ts-prompt:not(.full)>.bubble.user)>.user-message.markdown :is(hr,img,.copy-btn){display:none}
854
855
  /* Inside the dock's own blur a second backdrop-filter sees nothing: the cards are opaque here (as #commandList is). */
@@ -200,7 +200,16 @@ export function createNotifications({
200
200
  button.hidden = state === "unsupported";
201
201
  button.dataset.state = state;
202
202
  button.title = (push ? PUSH_TITLES : NOTIFY_TITLES)[state] || "";
203
- button.setAttribute("aria-pressed", state === "on" ? "true" : "false");
203
+ // In a tab the bell is a toggle (aria-pressed); in the app it opens the
204
+ // notifications sheet, whose shell (chip_popover.js) keeps its
205
+ // aria-expanded, as every popover opener's: one state, never both.
206
+ if (push) {
207
+ button.removeAttribute("aria-pressed");
208
+ button.setAttribute("aria-haspopup", "dialog");
209
+ if (!button.hasAttribute("aria-expanded")) button.setAttribute("aria-expanded", "false");
210
+ } else {
211
+ button.setAttribute("aria-pressed", state === "on" ? "true" : "false");
212
+ }
204
213
  }
205
214
 
206
215
  async function toggleNotify() {
@@ -94,9 +94,10 @@ export function stubText({ version, label = "", lines = 0, top }) {
94
94
  return `v${version}${said}${size} — superseded by v${top}`;
95
95
  }
96
96
 
97
- // The version chip's words: "v3 · 2 earlier".
98
- export function versionChipText(count) {
99
- return `v${count} · ${count - 1} earlier`;
97
+ // The version chip's words for version +at+ (0-based) on view of +count+:
98
+ // the newest, "v3 · 2 earlier"; an earlier one the switcher shows, "v1 of 3".
99
+ export function versionChipText(count, at = count - 1) {
100
+ return at >= count - 1 ? `v${count} · ${count - 1} earlier` : `v${at + 1} of ${count}`;
100
101
  }
101
102
 
102
103
  // The composer's chip: "Revising: ‹label›", or the refusal (+unavailable+:
@@ -236,12 +237,12 @@ export function installReviseStacks({
236
237
  function drawRow(row, { id, answer, chain, openings }) {
237
238
  const off = unavailable();
238
239
  const isArmed = armed() === id;
240
+ const at = chain ? (shown.get(id) ?? chain.length - 1) : 0;
239
241
  const versions = chain
240
- ? html`<details class="stack-versions"><summary>${versionChipText(chain.length)}</summary><div class="stack-versions-list" role="list">${chain.map((vid, k) => html`<button type="button" role="listitem" class=${vid === id ? "stack-version current" : "stack-version"} data-stack-pick=${vid} ?disabled=${vid === id}><span class="stack-version-n">v${k + 1}</span> ${promptLabel(openings.get(vid))}</button>`)}</div></details>`
242
+ ? html`<details class="stack-versions"><summary>${versionChipText(chain.length, at)}</summary><div class="stack-versions-list" role="list">${chain.map((vid, k) => html`<button type="button" role="listitem" class=${vid === id ? "stack-version current" : "stack-version"} data-stack-pick=${vid} ?disabled=${vid === id}><span class="stack-version-n">v${k + 1}</span> ${promptLabel(openings.get(vid))}</button>`)}</div></details>`
241
243
  : nothing;
242
244
  const button = html`<button type="button" class=${isArmed ? "revise-btn armed" : "revise-btn"} data-revise=${id} ?disabled=${off} title=${off ? REVISE_UNAVAILABLE : "Rewrite this answer: your next message revises it, and the earlier version folds away"}>${isArmed ? "Revising…" : "Revise"}</button>`;
243
245
  row.classList.toggle("stacked", !!chain);
244
- const at = chain ? (shown.get(id) ?? chain.length - 1) : 0;
245
246
  render(html`${chain ? switcherTemplate(id, at, chain.length) : nothing}${versions}${button}`, row);
246
247
  rowAnswer.set(row, answer);
247
248
  }
@@ -16,6 +16,13 @@ export function mergeVersionState(current = {}, frame = {}) {
16
16
  if (has(frame, "available")) next.available = known(frame.available) ? frame.available : null;
17
17
  if (has(frame, "update_blocked")) next.updateBlocked = known(frame.update_blocked) ? frame.update_blocked : null;
18
18
  if (has(frame, "auto")) next.auto = frame.auto === "install" ? "install" : "off";
19
+ if (has(frame, "restart")) next.restart = frame.restart === "never" ? "never" : "idle";
20
+ // The last install's chi_update state (a snapshot's, or a frame's): the
21
+ // version an automatic one installs, while it hasn't failed.
22
+ const run = frame.chi_update;
23
+ if (run && run.operation === "install" && known(run.version)) {
24
+ next.autoInstall = run.auto === true && run.state !== "failed" ? run.version : null;
25
+ }
19
26
  return next;
20
27
  }
21
28
 
@@ -46,24 +53,30 @@ export function newerVersion(a, b) {
46
53
  // The toast for the versions as the page knows them, or null:
47
54
  // - loaded ≠ served: chi web restarted on another version; reload the page;
48
55
  // - installed newer than served: chi web runs an older chi than installed;
49
- // restart its workers or chi web;
56
+ // restart its workers or chi web; not for the version an automatic install
57
+ // (+autoInstall+) brought with update.restart: idle, whose run restarts
58
+ // them by itself (and pushes how it went);
50
59
  // - available newer than served: install it, unless the install is blocked;
51
60
  // with update.auto: install (and not blocked) chi web installs it by itself
52
61
  // once no turn runs: the notice says so, with no Update button.
53
62
  // Each has a key; one in +shown+ (a Set: what this tab already showed, as
54
63
  // every reconnect's snapshot says it again) is not shown twice.
55
64
  // @return {{key, text, reload: boolean} | null}
56
- export function versionNotice({ loaded, served, installed, available, updateBlocked, auto, shown = new Set() } = {}) {
65
+ export function versionNotice({ loaded, served, installed, available, updateBlocked, auto, restart, autoInstall,
66
+ shown = new Set() } = {}) {
57
67
  let notice = null;
58
68
  if (known(loaded) && known(served) && loaded !== served) {
59
69
  notice = { key: `reload:${served}`, text: `chi was updated to ${served}`, reload: true };
60
70
  } else if (newerVersion(installed, served)) {
61
- notice = {
62
- key: `installed:${installed}`,
63
- text: `chi ${installed} is installed; this chi web runs ${served}.`,
64
- actions: ["restartWorkers", "restartWeb"],
65
- reload: false,
66
- };
71
+ const restartsByItself = auto === "install" && restart !== "never" && autoInstall === installed;
72
+ if (!restartsByItself) {
73
+ notice = {
74
+ key: `installed:${installed}`,
75
+ text: `chi ${installed} is installed; this chi web runs ${served}.`,
76
+ actions: ["restartWorkers", "restartWeb"],
77
+ reload: false,
78
+ };
79
+ }
67
80
  } else if (newerVersion(available, served)) {
68
81
  const commands = {
69
82
  checkout: ["this chi is running from a checkout", "git pull"],
@@ -274,7 +274,7 @@ module Samagotchi
274
274
  model_name: session_access.show(id).model_name,
275
275
  default_model: ModelProfile.default_model_name,
276
276
  parent_strategy: strategy_json(session_access.parent_strategy(id)),
277
- **(@hub&.chi_state || { version: VERSION, installed: nil, available: nil, update_blocked: nil, auto: "off" }) })
277
+ **(@hub&.chi_state || { version: VERSION, installed: nil, available: nil, update_blocked: nil, auto: "off", restart: "idle" }) })
278
278
  rescue SessionManager::OwnedByTUI => e
279
279
  owned_by_tui(e)
280
280
  rescue SessionAccess::Refused => e
@@ -75,11 +75,14 @@ module Samagotchi
75
75
  # @param now [#call] a monotonic clock in seconds (specs drive it)
76
76
  # @param installed_versions [#newest] what is installed (InstalledVersions)
77
77
  # @param update_auto [String] update.auto, said in the chi state (the page's wording)
78
+ # @param update_restart [String] update.restart, said beside it (whether
79
+ # an automatic install's run restarts by itself: no Restart toast)
78
80
  def initialize(state_dir:, manager: SessionManager, now: nil, scan_interval: SCAN_INTERVAL,
79
81
  full_probe_interval: FULL_PROBE_INTERVAL, installed_versions: InstalledVersions.new,
80
- update_auto: Config.get("update.auto"))
82
+ update_auto: Config.get("update.auto"), update_restart: Config.get("update.restart"))
81
83
  @state_dir = state_dir
82
84
  @update_auto = update_auto
85
+ @update_restart = update_restart
83
86
  @installed_versions = installed_versions
84
87
  @installed = nil
85
88
  @available = nil
@@ -161,7 +164,7 @@ module Samagotchi
161
164
  def chi_state
162
165
  @monitor.synchronize do
163
166
  { version: VERSION, installed: @installed, available: @available, update_blocked: @update_blocked,
164
- auto: @update_auto }
167
+ auto: @update_auto, restart: @update_restart }
165
168
  end
166
169
  end
167
170
 
@@ -53,7 +53,11 @@ module Samagotchi
53
53
  @installed_versions = installed_versions
54
54
  @state = load_state
55
55
  @blocked = blocked_reason
56
+ # One check at a time (its fetch takes up to ~10 s); @state_lock
57
+ # only guards the state's reads and writes, so reading the state
58
+ # never waits for a fetch.
56
59
  @checking = Mutex.new
60
+ @state_lock = Mutex.new
57
61
  @wakeup = Queue.new
58
62
  @thread = nil
59
63
  @stopped = false
@@ -89,11 +93,13 @@ module Samagotchi
89
93
 
90
94
  releases = @fetcher.call
91
95
  available = available_release(releases, now)&.version
92
- notified = @state[:notified]
93
- notify = @notify && available && !notified.include?(available)
94
- next_state = @state.merge(checked_at: now.iso8601, available: available,
95
- notified: notify ? (notified + [available]) : notified)
96
- persist(next_state)
96
+ notify = @state_lock.synchronize do
97
+ notified = @state[:notified]
98
+ fresh = @notify && available && !notified.include?(available)
99
+ persist(@state.merge(checked_at: now.iso8601, available: available,
100
+ notified: fresh ? (notified + [available]) : notified))
101
+ fresh
102
+ end
97
103
  @hub.update_available(available: @notify ? available : nil, blocked: @blocked, notify: !!notify)
98
104
  @failed = false
99
105
  true
@@ -115,19 +121,19 @@ module Samagotchi
115
121
  # kept:, workers:, pushed:, detail:} (symbol keys), nil when none.
116
122
  # @return [Hash, nil]
117
123
  def auto_state
118
- @checking.synchronize { @state[:auto]&.dup }
124
+ @state_lock.synchronize { @state[:auto]&.dup }
119
125
  end
120
126
 
121
127
  # Whether AutoUpdate already tried +version+ and failed (one automatic
122
128
  # try per version).
123
129
  def auto_failed?(version)
124
- @checking.synchronize { @state[:auto_failed].include?(version) }
130
+ @state_lock.synchronize { @state[:auto_failed].include?(version) }
125
131
  end
126
132
 
127
133
  # Saves AutoUpdate's record (nil clears it); +failed+ adds a version to
128
134
  # the ones it gave up on.
129
135
  def save_auto(auto, failed: nil)
130
- @checking.synchronize do
136
+ @state_lock.synchronize do
131
137
  failed_list = failed && !@state[:auto_failed].include?(failed) ? @state[:auto_failed] + [failed] : @state[:auto_failed]
132
138
  persist(@state.merge(auto: auto, auto_failed: failed_list))
133
139
  end
@@ -36,6 +36,7 @@ require_relative "child_reports"
36
36
  require_relative "verify_fresh"
37
37
  require_relative "child_move"
38
38
  require_relative "context_absorber"
39
+ require_relative "context_candidates"
39
40
  require_relative "context_poller"
40
41
  require_relative "task_watch"
41
42
  require_relative "tools/task_runtime"
@@ -697,6 +698,10 @@ module Samagotchi
697
698
  return refuse_participant_turn(origin, reason)
698
699
  end
699
700
 
701
+ # A candidate the prompt mentions is attached and fetched first, so
702
+ # the absorb below brings its note into this turn.
703
+ ContextCandidates.before_turn(session: @session, prompt: prompt.text, author: origin.author, state_dir: @state_dir,
704
+ cancelled: -> { stopped_on_disk? })
700
705
  # A first turn sees the context attached before it, and any turn the
701
706
  # background tasks that finished since the last look.
702
707
  absorb_context(before_turn: true)
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.139.0
4
+ version: 0.140.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -298,17 +298,23 @@ files:
298
298
  - lib/samagotchi/config/restart_watch.rb
299
299
  - lib/samagotchi/config_text_edit.rb
300
300
  - lib/samagotchi/context_absorber.rb
301
+ - lib/samagotchi/context_candidate_choice.rb
302
+ - lib/samagotchi/context_candidates.rb
303
+ - lib/samagotchi/context_candidates_log.rb
301
304
  - lib/samagotchi/context_command.rb
302
305
  - lib/samagotchi/context_fetch.rb
306
+ - lib/samagotchi/context_judge.rb
303
307
  - lib/samagotchi/context_note.rb
304
308
  - lib/samagotchi/context_poller.rb
305
309
  - lib/samagotchi/context_providers.rb
306
310
  - lib/samagotchi/context_quote.rb
311
+ - lib/samagotchi/context_report_command.rb
307
312
  - lib/samagotchi/context_sources.rb
308
313
  - lib/samagotchi/context_status.rb
309
314
  - lib/samagotchi/context_usage.rb
310
315
  - lib/samagotchi/context_window.rb
311
316
  - lib/samagotchi/continue_offer.rb
317
+ - lib/samagotchi/cut.rb
312
318
  - lib/samagotchi/cut_policy.rb
313
319
  - lib/samagotchi/debug_log.rb
314
320
  - lib/samagotchi/delivery.rb
@@ -487,6 +493,7 @@ files:
487
493
  - lib/samagotchi/note_delivery.rb
488
494
  - lib/samagotchi/output_formatter.rb
489
495
  - lib/samagotchi/owner_lock.rb
496
+ - lib/samagotchi/owner_prompt.rb
490
497
  - lib/samagotchi/parent_answers_command.rb
491
498
  - lib/samagotchi/parent_report.rb
492
499
  - lib/samagotchi/participant_messages.rb
@@ -595,6 +602,7 @@ files:
595
602
  - lib/samagotchi/sessions_command.rb
596
603
  - lib/samagotchi/setting_explained.rb
597
604
  - lib/samagotchi/share_command.rb
605
+ - lib/samagotchi/side_judgments.rb
598
606
  - lib/samagotchi/side_usage.rb
599
607
  - lib/samagotchi/steer.rb
600
608
  - lib/samagotchi/steer_cut.rb