samagotchi 0.173.0 → 0.174.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +31 -1
  3. data/docs/architecture.md +2 -2
  4. data/docs/cli.md +9 -2
  5. data/docs/internals/background-tasks.md +0 -3
  6. data/docs/internals/participants.md +3 -1
  7. data/docs/internals/remote-sessions.md +8 -0
  8. data/docs/internals/web-frontend.md +24 -3
  9. data/docs/plugins.md +1 -3
  10. data/docs/scheduling.md +9 -3
  11. data/docs/sessions.md +3 -1
  12. data/docs/testing.md +6 -0
  13. data/lib/samagotchi/bridge.rb +1 -1
  14. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  15. data/lib/samagotchi/child_move.rb +11 -4
  16. data/lib/samagotchi/continued_from.rb +51 -0
  17. data/lib/samagotchi/engine.rb +1 -1
  18. data/lib/samagotchi/note_move.rb +11 -4
  19. data/lib/samagotchi/schedules.rb +13 -16
  20. data/lib/samagotchi/session_access/contract.rb +1 -0
  21. data/lib/samagotchi/session_access/local.rb +9 -1
  22. data/lib/samagotchi/session_access/remote.rb +26 -3
  23. data/lib/samagotchi/session_chain/delete.rb +405 -0
  24. data/lib/samagotchi/session_chain/delete_preview.rb +96 -0
  25. data/lib/samagotchi/session_chain/links.rb +38 -0
  26. data/lib/samagotchi/session_command_catalog.rb +2 -2
  27. data/lib/samagotchi/session_commands.rb +3 -13
  28. data/lib/samagotchi/session_continue.rb +51 -13
  29. data/lib/samagotchi/session_delete_command.rb +82 -17
  30. data/lib/samagotchi/session_manager/removal.rb +137 -31
  31. data/lib/samagotchi/session_stores.rb +2 -0
  32. data/lib/samagotchi/terminal_ui/attached_loop/exit_commands.rb +2 -2
  33. data/lib/samagotchi/terminal_ui/attached_loop.rb +1 -1
  34. data/lib/samagotchi/terminal_ui/exit_lines.rb +16 -0
  35. data/lib/samagotchi/terminal_ui/local_link.rb +7 -1
  36. data/lib/samagotchi/terminal_ui.rb +3 -3
  37. data/lib/samagotchi/testing/boundary.rb +0 -5
  38. data/lib/samagotchi/testing/bundle.rb +1 -1
  39. data/lib/samagotchi/tools/task_runtime.rb +0 -5
  40. data/lib/samagotchi/version.rb +1 -1
  41. data/lib/samagotchi/web/app.rb +7 -1
  42. data/lib/samagotchi/web/public/app.js +13 -3
  43. data/lib/samagotchi/web/public/chains.js +3 -3
  44. data/lib/samagotchi/web/public/data.js +12 -0
  45. data/lib/samagotchi/web/public/delete_sheet.js +344 -0
  46. data/lib/samagotchi/web/public/format.js +1 -1
  47. data/lib/samagotchi/web/public/index.html +27 -0
  48. data/lib/samagotchi/web/public/info_bar.js +3 -2
  49. data/lib/samagotchi/web/public/lists_view.js +2 -2
  50. data/lib/samagotchi/web/public/more_sheet.js +42 -5
  51. data/lib/samagotchi/web/public/session_actions.js +50 -6
  52. data/lib/samagotchi/web/server.rb +13 -13
  53. data/lib/samagotchi/web/session_control.rb +58 -5
  54. metadata +5 -1
@@ -2,6 +2,7 @@ import { continueRefusal, continuedText, openChildrenTemplate, openerText } from
2
2
  import { cantText, deleteConfirmText, goneSessionNotice } from "./format.js";
3
3
  import { sessionHash } from "./route.js";
4
4
  import { inTree } from "./sessions_list.js";
5
+ import { isChained, listAfterDelete, deleteToast, runDeleteFlow } from "./delete_sheet.js";
5
6
 
6
7
  // What the page does to a whole session (used by app.js): Continue, Archive
7
8
  // and Unarchive, Delete and Stop, from the info bar's buttons, a card's or a
@@ -13,7 +14,8 @@ import { inTree } from "./sessions_list.js";
13
14
  // +setSessions(list)+ the page's list (the store's: a new list redraws the
14
15
  // lists and the bar);
15
16
  // +api+: {continueSession, archiveSession, unarchiveSession,
16
- // deleteSession, stopSession, sessionHolds, stopSessionHolds} (data.js);
17
+ // deleteSession, deleteChain, deletePlan, stopSession, sessionHolds,
18
+ // stopSessionHolds} (data.js);
17
19
  // +eventsLive()+ the hub's stream is open (else
18
20
  // +refresh()+ fetches the list);
19
21
  // +select(id)+, +clearSelection()+, +showNotice(text)+,
@@ -31,20 +33,62 @@ export function createSessionActions({
31
33
  // true; nothing to stop (or no sheet wired) is false, and Stop is the
32
34
  // confirm.
33
35
  stopSheet = { open: () => false },
36
+ // A card's delete sheet (delete_sheet.js createDeleteSheet): +host(anchor)+
37
+ // draws a chained session's choices at it.
38
+ deleteSheet = null,
34
39
  }) {
35
40
  // The session this tab is stopping: its stream may close before the stop
36
41
  // request returns (workerGone reads it).
37
42
  let stoppingId = null;
38
43
 
39
- // Delete a session (the info bar's button, an all-sessions card's ✕) after
40
- // a confirm; the list drops it without a reload. The server stops a live
41
- // worker first and refuses a session a terminal chi has open.
42
- async function confirmDelete(id) {
44
+ // Delete a session (the ⋯ More panel's Delete, an all-sessions card's ✕).
45
+ // A chained session gets the delete sheet (delete_sheet.js): in the More
46
+ // panel's place (deleteIn), at the card's ✕ (+anchor+), else at ⋯. Any
47
+ // other keeps today's confirm. The list drops what went without a reload.
48
+ // The server stops a live worker first and refuses a session a terminal
49
+ // chi has open.
50
+ async function confirmDelete(id, { anchor = null } = {}) {
43
51
  const sess = sessions().find((s) => s.id === id) || { id };
44
52
  if (sess.owner === "tui") {
45
53
  alert(`Session ${id.slice(0, 8)} is open in a terminal chi; close it there first.`);
46
54
  return;
47
55
  }
56
+ if (deleteSheet && api.deletePlan && isChained(sess, sessions())) {
57
+ const at = anchor || globalThis.document?.querySelector("#infoMoreBtn");
58
+ if (at) { startDeleteFlow(id, sess, deleteSheet.host(at, id)); return; }
59
+ }
60
+ await plainDelete(id, sess);
61
+ }
62
+
63
+ // The More panel's Delete: true when it took +host+ (a chained session).
64
+ function deleteIn(id, host) {
65
+ const sess = sessions().find((s) => s.id === id);
66
+ if (!sess || sess.owner === "tui" || !api.deletePlan || !isChained(sess, sessions())) return false;
67
+ startDeleteFlow(id, sess, host);
68
+ return true;
69
+ }
70
+
71
+ function startDeleteFlow(id, sess, host) {
72
+ const flow = runDeleteFlow({
73
+ id, session: sess, host, api,
74
+ done: (which, result, plan) => afterDelete(id, which, result, plan),
75
+ plain: () => plainDelete(id, sess),
76
+ failed: (error) => alert(`Could not delete session ${id.slice(0, 8)}: ${error.message}`),
77
+ });
78
+ host.wire(flow);
79
+ }
80
+
81
+ function afterDelete(id, which, result, plan) {
82
+ setSessions(listAfterDelete(sessions(), result));
83
+ const gone = new Set(result.deleted || [id]);
84
+ if (gone.has(selected())) clearSelection();
85
+ if (!eventsLive()) refresh();
86
+ const { text, back } = deleteToast(which, result, plan, id);
87
+ toast(text, back ? { label: `Open ${back.slice(0, 8)}`, run: () => select(back) } : null);
88
+ }
89
+
90
+ // Today's confirm: a session in no chain.
91
+ async function plainDelete(id, sess) {
48
92
  if (!confirm(deleteConfirmText(sess))) return;
49
93
  try {
50
94
  await api.deleteSession(id);
@@ -245,7 +289,7 @@ export function createSessionActions({
245
289
  }
246
290
 
247
291
  return {
248
- confirmDelete, leaveGoneSession, stopSelected, continueSelected, archiveOne, setArchived, toggleArchiveSelected,
292
+ confirmDelete, deleteIn, leaveGoneSession, stopSelected, continueSelected, archiveOne, setArchived, toggleArchiveSelected,
249
293
  // The sessions whose Continue is under way (the info bar's button).
250
294
  continuing: continuingIds,
251
295
  // The session this tab is stopping, or null.
@@ -91,24 +91,24 @@ module Samagotchi
91
91
  LINGER_BYTES = 4 * 1024 * 1024
92
92
  LINGER_SECONDS = 2.0
93
93
 
94
- # A refusal (status 400 or more) to a request that declared a body
95
- # (Content-Length > 0, or Transfer-Encoding), and any answer with
96
- # Connection: close (App's 413 too_large), end the connection: WEBrick
97
- # would otherwise read and drop the rest of the body
98
- # (HTTPRequest#fixup, up to its Content-Length) to keep the connection
99
- # alive, before it sends the answer; rackup never tells WEBrick about
100
- # the header (WEBrick reads it only as it sends). One rule here, not
101
- # per route: the gate's 401, a participant's 403/404, the image
102
- # path's own 413 and 429 close as App's 413 does. A 2xx/3xx stays
103
- # alive (WEBrick drains a body the app left, as before); a hijacked
104
- # stream is left to its own.
94
+ # A refusal (status 400 or more) to a request with a body, a successful
95
+ # answer to a large body the app may have left unread (over the small
96
+ # body limit or any Transfer-Encoding), and any answer with
97
+ # Connection: close end the connection: otherwise WEBrick drains the
98
+ # unread body before answering, holding a thread for an unbounded
99
+ # upload. One rule here, not per route. A small body on a 2xx/3xx stays
100
+ # alive; a hijacked stream is left to its own.
105
101
  module CloseOnConnectionClose
106
102
  def service(req, res)
107
103
  super
108
104
  return if res.body.respond_to?(:call)
109
105
 
110
- body = req["content-length"].to_i.positive? || !req["transfer-encoding"].nil?
111
- return unless res["connection"].to_s.casecmp?("close") || (res.status >= 400 && body)
106
+ content_length = req["content-length"].to_i
107
+ transfer_encoding = !req["transfer-encoding"].nil?
108
+ body = content_length.positive? || transfer_encoding
109
+ large_body = content_length > RequestBody::SMALL_BODY_LIMIT || transfer_encoding
110
+ close_for_body = body && (res.status >= 400 || large_body)
111
+ return unless res["connection"].to_s.casecmp?("close") || close_for_body
112
112
 
113
113
  res.keep_alive = false
114
114
  res.linger = body
@@ -524,14 +524,67 @@ module Samagotchi
524
524
  # session is always refused.
525
525
  def handle_delete(req, id)
526
526
  result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
527
- wait: STOP_WAIT_SECONDS)
528
- @hub&.touch(result[:id])
529
- json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped] })
527
+ wait: STOP_WAIT_SECONDS, revive_previous: true, refuse_continuing: true)
528
+ touch_deleted(result[:id], result)
529
+ json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped],
530
+ unarchived: Array(result[:unarchived]), moved_back: Array(result[:moved_back]),
531
+ deleted: Array(result[:deleted] || [result[:id]]), left_running: Array(result[:left_running]) })
530
532
  rescue SessionManager::OwnedByTUI => e
531
533
  owned_by_one_shot(id, e)
532
534
  rescue SessionManager::DeleteRefused => e
533
- detail = e.reason == :still_stopping ? "#{e.message}; try again in a moment" : e.message
534
- refusal_response(409, e.reason.to_s, e, detail: detail, session_id: e.session_id, reason: e.reason.to_s)
535
+ delete_refused(e)
536
+ end
537
+
538
+ # DELETE /api/sessions/:id/chain?force=: every link of the chain with
539
+ # their delegates (SessionManager.delete_chain). Query-only.
540
+ def handle_delete_chain(req, id)
541
+ result = @manager.delete_chain(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
542
+ wait: STOP_WAIT_SECONDS)
543
+ touch_deleted(id, result)
544
+ json_response(200, { status: "deleted", session_id: id, deleted: result[:deleted], left: result[:left],
545
+ left_running: result[:left_running], unarchived: result[:unarchived],
546
+ stopped: result[:stopped], cut_off: result[:cut_off], refusal: result[:refusal] })
547
+ rescue SessionManager::OwnedByTUI => e
548
+ owned_by_one_shot(e.session_id, e)
549
+ rescue SessionManager::DeleteRefused => e
550
+ delete_refused(e)
551
+ end
552
+
553
+ # GET /api/sessions/:id/delete_plan: what the delete sheet shows for a
554
+ # chained session (SessionChain::Delete::Preview: the plan and the
555
+ # refusals the delete itself runs), {chained: false} for one in no
556
+ # chain. One session listing walk per call; the page asks only for a
557
+ # chained session, as the sheet opens. No query or body read; no-store
558
+ # (json_response's).
559
+ def handle_delete_plan(_req, id)
560
+ return unknown_session(id) unless Session.exist?(id, state_dir: @state_dir || Session.default_state_dir)
561
+
562
+ require_relative "../session_chain/delete_preview"
563
+ json_response(200, SessionChain::Delete::Preview.call(id, state_dir: @state_dir))
564
+ end
565
+
566
+ # The hub hears every session a delete changed: deleted, back in the
567
+ # lists, moved back to the previous link, left running without a parent.
568
+ def touch_deleted(id, result)
569
+ return unless @hub
570
+
571
+ ids = [id, *result[:deleted], *result[:unarchived], *result[:moved_back], *result[:left_running]]
572
+ ids.compact.uniq.each { |sid| @hub.touch(sid) }
573
+ end
574
+
575
+ # 409 with the refusal's reason as its code, and what a remote client
576
+ # raises it again from (ids, the schedule).
577
+ def delete_refused(error)
578
+ detail = error.reason == :still_stopping ? "#{error.message}; try again in a moment" : error.message
579
+ # A moved delegate that can't go back: whether the page can stop it
580
+ # (its "Stop its worker, then delete"), as the refusal saw it.
581
+ if error.reason == :delegate_unmovable
582
+ require_relative "../session_chain/delete"
583
+ stoppable = SessionChain::Delete.stoppable?(error.ids, @state_dir || Session.default_state_dir)
584
+ end
585
+ refusal_response(409, error.reason.to_s, error, detail: detail, session_id: error.session_id,
586
+ reason: error.reason.to_s, ids: error.ids.empty? ? nil : error.ids,
587
+ schedule_id: error.schedule_id, stoppable: stoppable)
535
588
  end
536
589
 
537
590
  # 409 owned_by_tui in the words a refused archive, delete or continue
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.173.0
4
+ version: 0.174.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -343,6 +343,7 @@ files:
343
343
  - lib/samagotchi/context_window.rb
344
344
  - lib/samagotchi/continue_intent.rb
345
345
  - lib/samagotchi/continue_offer.rb
346
+ - lib/samagotchi/continued_from.rb
346
347
  - lib/samagotchi/cut.rb
347
348
  - lib/samagotchi/cut_policy.rb
348
349
  - lib/samagotchi/debug_log.rb
@@ -674,6 +675,8 @@ files:
674
675
  - lib/samagotchi/session_access/web_bridge_client.rb
675
676
  - lib/samagotchi/session_archive_command.rb
676
677
  - lib/samagotchi/session_chain.rb
678
+ - lib/samagotchi/session_chain/delete.rb
679
+ - lib/samagotchi/session_chain/delete_preview.rb
677
680
  - lib/samagotchi/session_chain/links.rb
678
681
  - lib/samagotchi/session_command_catalog.rb
679
682
  - lib/samagotchi/session_commands.rb
@@ -962,6 +965,7 @@ files:
962
965
  - lib/samagotchi/web/public/copy.js
963
966
  - lib/samagotchi/web/public/ctx.js
964
967
  - lib/samagotchi/web/public/data.js
968
+ - lib/samagotchi/web/public/delete_sheet.js
965
969
  - lib/samagotchi/web/public/delivery_line.js
966
970
  - lib/samagotchi/web/public/details_motion.js
967
971
  - lib/samagotchi/web/public/diff_view.js