samagotchi 0.181.0 → 0.182.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 (67) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +26 -1
  3. data/bin/chi +1 -0
  4. data/docs/architecture.md +1 -1
  5. data/docs/cli.md +30 -4
  6. data/docs/configuration.md +1 -0
  7. data/docs/internals/block-renderers.md +114 -0
  8. data/docs/internals/bridge.md +10 -0
  9. data/docs/internals/engine.md +11 -0
  10. data/docs/internals/inline-widgets.md +3 -1
  11. data/docs/internals/prompt-caching.md +9 -1
  12. data/docs/internals/remote-sessions.md +14 -2
  13. data/docs/internals/web-frontend.md +33 -3
  14. data/docs/sessions.md +7 -4
  15. data/lib/samagotchi/answer_display.rb +3 -2
  16. data/lib/samagotchi/block_fix/stamp.rb +49 -0
  17. data/lib/samagotchi/block_fix.rb +220 -0
  18. data/lib/samagotchi/bridge/block_fix_route.rb +30 -0
  19. data/lib/samagotchi/bridge/turn_drawings.rb +19 -0
  20. data/lib/samagotchi/bridge.rb +11 -2
  21. data/lib/samagotchi/bridge_client.rb +9 -0
  22. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  23. data/lib/samagotchi/config.rb +3 -0
  24. data/lib/samagotchi/continue_intent.rb +8 -0
  25. data/lib/samagotchi/engine/block_fix.rb +332 -0
  26. data/lib/samagotchi/engine/side_requests.rb +7 -5
  27. data/lib/samagotchi/engine.rb +4 -0
  28. data/lib/samagotchi/events.rb +1 -0
  29. data/lib/samagotchi/idle_client.rb +10 -6
  30. data/lib/samagotchi/inline_widgets.rb +12 -0
  31. data/lib/samagotchi/kernel_loop.rb +7 -5
  32. data/lib/samagotchi/llm/chat_loop.rb +2 -0
  33. data/lib/samagotchi/markdown_parser.rb +28 -0
  34. data/lib/samagotchi/session_access/contract.rb +3 -0
  35. data/lib/samagotchi/session_access/delete_plan.rb +77 -0
  36. data/lib/samagotchi/session_access/local.rb +13 -0
  37. data/lib/samagotchi/session_access/remote.rb +19 -1
  38. data/lib/samagotchi/session_access.rb +1 -0
  39. data/lib/samagotchi/session_chain/delete.rb +39 -81
  40. data/lib/samagotchi/session_chain/delete_preview.rb +55 -19
  41. data/lib/samagotchi/session_delete_command.rb +129 -3
  42. data/lib/samagotchi/session_manager/delegate_rule.rb +86 -0
  43. data/lib/samagotchi/session_manager/listing.rb +4 -4
  44. data/lib/samagotchi/session_manager/removal.rb +38 -12
  45. data/lib/samagotchi/session_manager/running_tasks.rb +50 -23
  46. data/lib/samagotchi/session_manager.rb +1 -0
  47. data/lib/samagotchi/version.rb +1 -1
  48. data/lib/samagotchi/web/app.rb +13 -1
  49. data/lib/samagotchi/web/block_fix_routes.rb +39 -0
  50. data/lib/samagotchi/web/markdown_renderer.rb +24 -9
  51. data/lib/samagotchi/web/message_display.rb +6 -1
  52. data/lib/samagotchi/web/participant_view.rb +1 -1
  53. data/lib/samagotchi/web/public/app.js +5 -5
  54. data/lib/samagotchi/web/public/block_fix.js +270 -0
  55. data/lib/samagotchi/web/public/block_fix_note.js +104 -0
  56. data/lib/samagotchi/web/public/block_frame.js +6 -3
  57. data/lib/samagotchi/web/public/block_host.js +1 -1
  58. data/lib/samagotchi/web/public/block_renderers.css +12 -0
  59. data/lib/samagotchi/web/public/block_watch.js +16 -2
  60. data/lib/samagotchi/web/public/inline_widgets.js +1 -1
  61. data/lib/samagotchi/web/public/stream_handlers.js +5 -1
  62. data/lib/samagotchi/web/public/turn_block.js +5 -2
  63. data/lib/samagotchi/web/server.rb +6 -4
  64. data/lib/samagotchi/web/session_control.rb +53 -21
  65. data/lib/samagotchi/worker/block_fixes.rb +22 -0
  66. data/lib/samagotchi/worker.rb +10 -3
  67. metadata +12 -1
@@ -500,12 +500,16 @@ module Samagotchi
500
500
  # lists, keep it for good. A live idle worker is stopped first; a turn
501
501
  # running in it or a delegate, a one-turn chi run's session and a scratch one
502
502
  # are refused (409). Background tasks it or a delegate started that
503
- # still run: 409 tasks_running, unless the body says {stop_tasks: true}
504
- # (then stopped first; the 200 names them, #task_fields).
503
+ # still run: 409 tasks_running (with +sessions+, how many the archive
504
+ # hides), unless the body says {stop_tasks: true} (then stopped first;
505
+ # the 200 names them, #task_fields). With {tasks: [ids]} too only
506
+ # those are stopped, and a running one not named refuses (the sheet's
507
+ # tap: #running_tasks_policy).
505
508
  def handle_archive(req, id)
506
509
  body = parse_json(request_body(req, 4096))
510
+ body = {} unless body.is_a?(Hash)
507
511
  result = @manager.archive_session(id, state_dir: @state_dir, wait: STOP_WAIT_SECONDS,
508
- running_tasks: running_tasks_policy(body.is_a?(Hash) && body["stop_tasks"]))
512
+ running_tasks: running_tasks_policy(body["stop_tasks"], body_tasks(body)))
509
513
  (result.archived + result.discarded).each { |sid| @hub&.touch(sid) }
510
514
  json_response(200, { status: "archived", session_id: result.id, archived: result.archived,
511
515
  stopped: result.stopped, discarded: result.discarded,
@@ -529,12 +533,14 @@ module Samagotchi
529
533
  # worker_running, as chi sessions delete without --force. A one-turn chi run's
530
534
  # session is always refused. Background tasks the delete's sessions
531
535
  # started that still run: 409 tasks_running, unless ?stop_tasks=true
532
- # (then stopped first; the 200 names them, #task_fields).
536
+ # (then stopped first; the 200 names them, #task_fields); with
537
+ # &tasks=<id,id> only those (#running_tasks_policy). Every session's
538
+ # stopped delegates go with it (SessionManager::DELEGATES).
533
539
  def handle_delete(req, id)
534
540
  result = @manager.delete_session(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
535
541
  wait: STOP_WAIT_SECONDS, revive_previous: true, refuse_continuing: true,
536
542
  expect: req.GET["expect"],
537
- running_tasks: running_tasks_policy(req.GET["stop_tasks"]))
543
+ running_tasks: running_tasks_policy(req.GET["stop_tasks"], query_tasks(req)))
538
544
  touch_deleted(result[:id], result)
539
545
  json_response(200, { status: "deleted", session_id: result[:id], stopped: result[:stopped],
540
546
  unarchived: Array(result[:unarchived]), moved_back: Array(result[:moved_back]),
@@ -546,12 +552,13 @@ module Samagotchi
546
552
  delete_refused(e)
547
553
  end
548
554
 
549
- # DELETE /api/sessions/:id/chain?force=&stop_tasks=: every link of the
550
- # chain with their delegates (SessionManager.delete_chain). Query-only.
555
+ # DELETE /api/sessions/:id/chain?force=&stop_tasks=&tasks=: every link
556
+ # of the chain with their delegates (SessionManager.delete_chain).
557
+ # Query-only.
551
558
  def handle_delete_chain(req, id)
552
559
  result = @manager.delete_chain(id, state_dir: @state_dir, stop: truthy_param?(req.GET["force"]),
553
560
  wait: STOP_WAIT_SECONDS, expect: req.GET["expect"],
554
- running_tasks: running_tasks_policy(req.GET["stop_tasks"]))
561
+ running_tasks: running_tasks_policy(req.GET["stop_tasks"], query_tasks(req)))
555
562
  touch_deleted(id, result)
556
563
  json_response(200, { status: "deleted", session_id: id, deleted: result[:deleted], left: result[:left],
557
564
  left_running: result[:left_running], unarchived: result[:unarchived],
@@ -563,14 +570,19 @@ module Samagotchi
563
570
  delete_refused(e)
564
571
  end
565
572
 
566
- # GET /api/sessions/:id/delete_plan: what the delete sheet shows for a
567
- # chained session (SessionChain::Delete::Preview: the plan and the
568
- # refusals the delete itself runs), {chained: false} for one in no
569
- # chain. One session listing walk per call; the page asks only for a
570
- # chained session, as the sheet opens. No query or body read; no-store
571
- # (json_response's).
573
+ # GET /api/sessions/:id/delete_plan: what the delete sheet shows for
574
+ # any session (SessionChain::Delete::Preview: the plan and the
575
+ # refusals the delete itself runs, each choice's delegates, running
576
+ # tasks and workers); {chained: false, link} for one in no chain (an
577
+ # older page asked only for a chained one). A folder whose session
578
+ # file is gone answers too, as the delete still takes it. One shared
579
+ # session-row snapshot serves the link and chain choices; Links lists
580
+ # on its own. Each call read-only checks the session's continue lock.
581
+ # No query or body read; no-store (json_response's).
572
582
  def handle_delete_plan(_req, id)
573
- return unknown_session(id) unless Session.exist?(id, state_dir: @state_dir || Session.default_state_dir)
583
+ sd = @state_dir || Session.default_state_dir
584
+ on_disk = File.exist?(Session.session_file(id, state_dir: sd)) || Dir.exist?(Session.session_dir(id, state_dir: sd))
585
+ return unknown_session(id) unless on_disk
574
586
 
575
587
  require_relative "../session_chain/delete_preview"
576
588
  json_response(200, SessionChain::Delete::Preview.call(id, state_dir: @state_dir))
@@ -586,11 +598,28 @@ module Samagotchi
586
598
  end
587
599
 
588
600
  # A user's delete or archive: refused on running tasks (REFUSE), or,
589
- # asked to (+stop_tasks+: the page's second confirm, --stop-tasks),
590
- # stopped by "user".
591
- def running_tasks_policy(stop_tasks)
601
+ # asked to (+stop_tasks+: the page's sheet, --stop-tasks), stopped by
602
+ # "user". +named+ (task ids, the ones the sheet showed) makes the stop
603
+ # take only those and refuse on another that runs (Policy.stop's
604
+ # only); nil stops whatever runs. Without +stop_tasks+ it is ignored.
605
+ def running_tasks_policy(stop_tasks, named = nil)
592
606
  policies = SessionManager::RunningTasks::Policy
593
- truthy_param?(stop_tasks) ? policies.stop(by: "user") : policies::REFUSE
607
+ truthy_param?(stop_tasks) ? policies.stop(by: "user", only: named) : policies::REFUSE
608
+ end
609
+
610
+ # The body's tasks as ids, nil when not given; anything but a list of
611
+ # strings names none (so the stop refuses on any task that runs).
612
+ def body_tasks(body)
613
+ return nil unless body.key?("tasks")
614
+
615
+ body["tasks"].is_a?(Array) ? body["tasks"].grep(String) : []
616
+ end
617
+
618
+ # ?tasks=<id,id> as ids ([] for an empty one), nil when not given.
619
+ def query_tasks(req)
620
+ return nil unless req.GET.key?("tasks")
621
+
622
+ req.GET["tasks"].to_s.split(",").map(&:strip).reject(&:empty?)
594
623
  end
595
624
 
596
625
  # The 200's tasks_stopped, tasks_not_stopped and tasks_ended (each
@@ -604,8 +633,10 @@ module Samagotchi
604
633
  # was stopped, a chain's links (the words name them).
605
634
  def tasks_running_fields(error, op)
606
635
  links = error.respond_to?(:links) ? error.links : []
607
- { op: op, tasks: error.tasks.to_wire, worker_stopped: error.worker_stopped, stopped_worker: error.stopped_worker,
608
- links: (links unless links.empty?) }
636
+ fields = { op: op, tasks: error.tasks.to_wire, worker_stopped: error.worker_stopped, stopped_worker: error.stopped_worker,
637
+ links: (links unless links.empty?) }
638
+ fields[:stopped_workers] = error.stopped_workers if error.respond_to?(:stopped_workers) && !error.stopped_workers.empty?
639
+ fields
609
640
  end
610
641
 
611
642
  # 409 with the refusal's reason as its code, and what a remote client
@@ -654,6 +685,7 @@ module Samagotchi
654
685
  def archive_refused(error)
655
686
  if error.reason == :tasks_running
656
687
  return refusal_response(409, "tasks_running", error, session_id: error.session_id, reason: "tasks_running",
688
+ sessions: error.sessions,
657
689
  **tasks_running_fields(error, "archive"))
658
690
  end
659
691
 
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../degrade"
4
+
5
+ module Samagotchi
6
+ class Worker
7
+ # A failed block's fix (Engine::BlockFixPart) lands on the loop, between
8
+ # turns: in each pass that runs the queued commands and absorbs the
9
+ # notes, after the commands and before the notes and the next prompt, so
10
+ # a fix ready as a turn ends is in the conversation the next queued turn
11
+ # sends; one that isn't lands when that turn ends. Reads @engine.
12
+ module BlockFixesPart
13
+ private
14
+
15
+ # The fixes waiting, written into the conversation and saved.
16
+ def apply_block_fixes
17
+ applied = Degrade.call(:worker, "block_fix_apply_failed", fallback: 0) { @engine.apply_block_fixes }
18
+ save_or_log(:block_fix) { save_session } if applied.positive?
19
+ end
20
+ end
21
+ end
22
+ end
@@ -48,6 +48,7 @@ require_relative "worker/signal_grace"
48
48
  require_relative "worker/scratch"
49
49
  require_relative "worker/child_side"
50
50
  require_relative "worker/command_queue"
51
+ require_relative "worker/block_fixes"
51
52
 
52
53
  module Samagotchi
53
54
  # The loop of a background session worker, once it owns the session (see
@@ -82,13 +83,14 @@ module Samagotchi
82
83
  # the loop's thread, as Ruby's default would (the teardown still runs).
83
84
  #
84
85
  # This file keeps the loop, the wiring, the prompt turns and the steering
85
- # drain. Seven concerns are parts, lib/samagotchi/worker/<concern>.rb
86
+ # drain. Eight concerns are parts, lib/samagotchi/worker/<concern>.rb
86
87
  # holding Worker::<Concern>Part, included here (they share Worker's ivars):
87
88
  # LeavePart (exit and leave), WakeTurnsPart (the turns nobody typed),
88
89
  # StartupPart (the start-up settle), SignalGracePart (a signal's grace),
89
90
  # ScratchPart (a scratch worker whose terminal left), ChildSidePart (a
90
- # delegate child's rings, Verify fresh verdicts) and CommandQueuePart
91
- # (session commands between turns).
91
+ # delegate child's rings, Verify fresh verdicts), CommandQueuePart
92
+ # (session commands between turns) and BlockFixesPart (a failed block's
93
+ # fix, between turns).
92
94
  class Worker
93
95
  include LeavePart
94
96
  include WakeTurnsPart
@@ -97,6 +99,7 @@ module Samagotchi
97
99
  include ScratchPart
98
100
  include ChildSidePart
99
101
  include CommandQueuePart
102
+ include BlockFixesPart
100
103
 
101
104
  FALLBACK_TICK_SECONDS = 5
102
105
  # A worker just started runs no wake turn for delegate reports this
@@ -482,6 +485,7 @@ module Samagotchi
482
485
  # before a new prompt still answers the offer).
483
486
  return if run_queued_commands
484
487
 
488
+ apply_block_fixes
485
489
  # Between turns, so the next turn (the first one too) sees them.
486
490
  # A change that asks to wake may start a turn here (C4), but not
487
491
  # ahead of an exit or restart a client asked for: it is a note then.
@@ -543,6 +547,7 @@ module Samagotchi
543
547
  # Commands that came before this file (D1: a queued /model X sent
544
548
  # before this prompt, a command an earlier file carried) run first.
545
549
  run_queued_commands
550
+ apply_block_fixes
546
551
  @inbound.absorb_notes
547
552
  if held ? SessionInbox.participant_input?(input_file) : held_for_offer?(input_file)
548
553
  held = true
@@ -623,6 +628,8 @@ module Samagotchi
623
628
  engine.interface = :worker
624
629
  # A pair turn stops mid-turn at its participant's cap.
625
630
  engine.participant_stop = ->(author) { @participant_gate.turn_stop(author) }
631
+ # A block fix's result wakes the loop, which applies it between turns.
632
+ engine.block_fix_ready = -> { waker.wake }
626
633
  engine.guardrail_state_dir = @state_dir if @state_dir
627
634
  engine.session_state_dir = @state_dir if @state_dir
628
635
  engine
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.181.0
4
+ version: 0.182.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -191,12 +191,15 @@ files:
191
191
  - lib/samagotchi/answer_text.rb
192
192
  - lib/samagotchi/archive_store.rb
193
193
  - lib/samagotchi/atomic_file.rb
194
+ - lib/samagotchi/block_fix.rb
195
+ - lib/samagotchi/block_fix/stamp.rb
194
196
  - lib/samagotchi/block_renderers.rb
195
197
  - lib/samagotchi/bootstrap/bundles.rb
196
198
  - lib/samagotchi/bootstrap/config_writer.rb
197
199
  - lib/samagotchi/bootstrap/probe.rb
198
200
  - lib/samagotchi/bootstrap_command.rb
199
201
  - lib/samagotchi/bridge.rb
202
+ - lib/samagotchi/bridge/block_fix_route.rb
200
203
  - lib/samagotchi/bridge/bounded_queue.rb
201
204
  - lib/samagotchi/bridge/card_places.rb
202
205
  - lib/samagotchi/bridge/card_store.rb
@@ -374,6 +377,7 @@ files:
374
377
  - lib/samagotchi/empty_answer_error.rb
375
378
  - lib/samagotchi/empty_answer_retry.rb
376
379
  - lib/samagotchi/engine.rb
380
+ - lib/samagotchi/engine/block_fix.rb
377
381
  - lib/samagotchi/engine/cards.rb
378
382
  - lib/samagotchi/engine/context_judge.rb
379
383
  - lib/samagotchi/engine/events.rb
@@ -521,6 +525,7 @@ files:
521
525
  - lib/samagotchi/log_line.rb
522
526
  - lib/samagotchi/log_path.rb
523
527
  - lib/samagotchi/log_subscriber.rb
528
+ - lib/samagotchi/markdown_parser.rb
524
529
  - lib/samagotchi/memory_bundle.rb
525
530
  - lib/samagotchi/memory_bundle/asset_installer.rb
526
531
  - lib/samagotchi/memory_bundle/asset_state.rb
@@ -678,6 +683,7 @@ files:
678
683
  - lib/samagotchi/session.rb
679
684
  - lib/samagotchi/session_access.rb
680
685
  - lib/samagotchi/session_access/contract.rb
686
+ - lib/samagotchi/session_access/delete_plan.rb
681
687
  - lib/samagotchi/session_access/dispatch.rb
682
688
  - lib/samagotchi/session_access/http_transport.rb
683
689
  - lib/samagotchi/session_access/local.rb
@@ -701,6 +707,7 @@ files:
701
707
  - lib/samagotchi/session_lifecycle/canonical.rb
702
708
  - lib/samagotchi/session_lifecycle/reading.rb
703
709
  - lib/samagotchi/session_manager.rb
710
+ - lib/samagotchi/session_manager/delegate_rule.rb
704
711
  - lib/samagotchi/session_manager/delivery.rb
705
712
  - lib/samagotchi/session_manager/emptiness.rb
706
713
  - lib/samagotchi/session_manager/lifecycle.rb
@@ -917,6 +924,7 @@ files:
917
924
  - lib/samagotchi/web/app.rb
918
925
  - lib/samagotchi/web/auto_update.rb
919
926
  - lib/samagotchi/web/auto_update_record.rb
927
+ - lib/samagotchi/web/block_fix_routes.rb
920
928
  - lib/samagotchi/web/block_renderer_routes.rb
921
929
  - lib/samagotchi/web/capabilities.rb
922
930
  - lib/samagotchi/web/editor.rb
@@ -948,6 +956,8 @@ files:
948
956
  - lib/samagotchi/web/public/apple-touch-icon.png
949
957
  - lib/samagotchi/web/public/auth_lost.js
950
958
  - lib/samagotchi/web/public/bar_fold.js
959
+ - lib/samagotchi/web/public/block_fix.js
960
+ - lib/samagotchi/web/public/block_fix_note.js
951
961
  - lib/samagotchi/web/public/block_frame.js
952
962
  - lib/samagotchi/web/public/block_host.js
953
963
  - lib/samagotchi/web/public/block_open.js
@@ -1202,6 +1212,7 @@ files:
1202
1212
  - lib/samagotchi/web/viewer.rb
1203
1213
  - lib/samagotchi/web/working_set_routes.rb
1204
1214
  - lib/samagotchi/worker.rb
1215
+ - lib/samagotchi/worker/block_fixes.rb
1205
1216
  - lib/samagotchi/worker/child_side.rb
1206
1217
  - lib/samagotchi/worker/command_queue.rb
1207
1218
  - lib/samagotchi/worker/leave.rb