samagotchi 0.40.0 → 0.41.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 (65) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +54 -1
  3. data/README.md +1 -1
  4. data/bin/chi +19 -2
  5. data/docs/cli.md +25 -2
  6. data/docs/configuration.md +34 -3
  7. data/docs/guardrails.md +15 -3
  8. data/docs/internals/prompt-caching.md +2 -1
  9. data/docs/memory.md +90 -1
  10. data/docs/sessions.md +1 -1
  11. data/lib/samagotchi/bridge/card_store.rb +6 -7
  12. data/lib/samagotchi/bundles/guardrails/guardrails/rules.yml +23 -0
  13. data/lib/samagotchi/bundles/guardrails/manifest.yml +5 -5
  14. data/lib/samagotchi/bundles/system/config_modification_protocol.md +1 -1
  15. data/lib/samagotchi/bundles/system/manifest.yml +4 -4
  16. data/lib/samagotchi/bundles/system/memory_guide.md +13 -0
  17. data/lib/samagotchi/bundles/system/self_map.md +4 -2
  18. data/lib/samagotchi/config.rb +3 -0
  19. data/lib/samagotchi/engine.rb +91 -16
  20. data/lib/samagotchi/events.rb +8 -1
  21. data/lib/samagotchi/guardrails/model_size.rb +3 -4
  22. data/lib/samagotchi/guardrails/rules.rb +27 -17
  23. data/lib/samagotchi/guardrails/targets.rb +40 -1
  24. data/lib/samagotchi/guardrails/verdict.rb +6 -3
  25. data/lib/samagotchi/llm/errors.rb +33 -1
  26. data/lib/samagotchi/llm_context_command.rb +151 -0
  27. data/lib/samagotchi/llm_context_override.rb +180 -0
  28. data/lib/samagotchi/llm_context_strategy.rb +63 -13
  29. data/lib/samagotchi/memory_bundle/index_size.rb +103 -0
  30. data/lib/samagotchi/memory_bundle/index_sync.rb +3 -2
  31. data/lib/samagotchi/model_match.rb +54 -0
  32. data/lib/samagotchi/model_notes.rb +202 -0
  33. data/lib/samagotchi/parent_report.rb +12 -2
  34. data/lib/samagotchi/plugin/context.rb +3 -2
  35. data/lib/samagotchi/plugin/sessions.rb +3 -1
  36. data/lib/samagotchi/reply_wait.rb +3 -17
  37. data/lib/samagotchi/self_report.rb +15 -0
  38. data/lib/samagotchi/send_command.rb +22 -2
  39. data/lib/samagotchi/session.rb +29 -4
  40. data/lib/samagotchi/session_commands.rb +21 -1
  41. data/lib/samagotchi/session_manager.rb +5 -2
  42. data/lib/samagotchi/session_metrics.rb +26 -3
  43. data/lib/samagotchi/system_prompt.rb +66 -11
  44. data/lib/samagotchi/terminal_ui/attach_launcher.rb +13 -5
  45. data/lib/samagotchi/terminal_ui/attached_loop.rb +20 -12
  46. data/lib/samagotchi/terminal_ui/event_renderer.rb +2 -0
  47. data/lib/samagotchi/terminal_ui/formatting.rb +36 -0
  48. data/lib/samagotchi/terminal_ui.rb +16 -6
  49. data/lib/samagotchi/tool_runner.rb +26 -1
  50. data/lib/samagotchi/tools/memory.rb +70 -3
  51. data/lib/samagotchi/turn_flow.rb +36 -8
  52. data/lib/samagotchi/turn_note.rb +40 -1
  53. data/lib/samagotchi/version.rb +1 -1
  54. data/lib/samagotchi/web/app.rb +20 -1
  55. data/lib/samagotchi/web/public/app.js +40 -6
  56. data/lib/samagotchi/web/public/ctx.js +28 -5
  57. data/lib/samagotchi/web/public/format.js +5 -2
  58. data/lib/samagotchi/web/public/index.html +12 -0
  59. data/lib/samagotchi/web/public/live_turn.js +1 -0
  60. data/lib/samagotchi/web/public/llm_context_chip.js +194 -0
  61. data/lib/samagotchi/web/public/session_state.js +3 -0
  62. data/lib/samagotchi/web/public/timing.js +5 -1
  63. data/lib/samagotchi/web/session_summary.rb +2 -0
  64. data/lib/samagotchi/worker.rb +24 -18
  65. metadata +7 -1
@@ -413,10 +413,9 @@ module Samagotchi
413
413
  run_engine_turn(nil, continue: true, origin: context_wake.origin,
414
414
  id: context_wake.turn_id, max_iterations: IterationLimit.for) do |result, error|
415
415
  if error
416
- note = TurnNote.failed(error.respond_to?(:summary) ? error.summary : error.message,
417
- wake: "the change in attached context #{name}")
418
- @turn_flow.prompt_turn_failed(note: note)
419
- @inbound.unmark_wake(context_wake)
416
+ kept = @turn_flow.prompt_turn_failed(error: error, wake: "the change in attached context #{name}") == :kept
417
+ # A turn that got somewhere stays, its wake mark with it.
418
+ @inbound.unmark_wake(context_wake) unless kept
420
419
  @wakes.pause!
421
420
  else
422
421
  @continue_offer.after_turn(result)
@@ -510,12 +509,13 @@ module Samagotchi
510
509
  @turn_flow.before_prompt_turn
511
510
  run_engine_turn(nil, continue: true, origin: reports.first.origin, max_iterations: IterationLimit.for) do |result, error|
512
511
  if error
513
- # The rings stay (settle_child_reports): the user's next turn takes them.
514
- note = TurnNote.failed(error.respond_to?(:summary) ? error.summary : error.message,
515
- wake: reports.size == 1 ? "a delegate's report" : "#{reports.size} delegate reports",
516
- kept: "chi keeps #{reports.size == 1 ? "the report and brings it" : "the reports and brings them"} " \
517
- "again with the user's next message.")
518
- @turn_flow.prompt_turn_failed(note: note)
512
+ # Rolled back, the rings stay (settle_child_reports): the user's
513
+ # next turn takes them. A turn that got somewhere stays, and read them.
514
+ @turn_flow.prompt_turn_failed(
515
+ error: error, wake: reports.size == 1 ? "a delegate's report" : "#{reports.size} delegate reports",
516
+ wake_kept: "chi keeps #{reports.size == 1 ? "the report and brings it" : "the reports and brings them"} " \
517
+ "again with the user's next message."
518
+ )
519
519
  @wakes.pause!
520
520
  else
521
521
  @continue_offer.after_turn(result)
@@ -635,6 +635,8 @@ module Samagotchi
635
635
  model_name: @engine.effective_model_name }
636
636
  event[:anytime] = true if anytime
637
637
  event[:card] = true if command[:card]
638
+ # /llm-context or /model: what the next turn runs under now (the web's chip).
639
+ event[:llm_context] = @engine.llm_context_summary if changed.intersect?(%i[llm_context model])
638
640
  event[:output_truncated] = true if text.length > COMMAND_OUTPUT_LIMIT
639
641
  @engine.announce(event)
640
642
  end
@@ -680,15 +682,17 @@ module Samagotchi
680
682
  SessionInbox.write_output(@session_dir, response)
681
683
  end
682
684
  save_or_log(:turn) { save_session }
683
- settle_child_reports(error)
685
+ # A failed turn whose work stayed (TurnFlow#prompt_turn_failed) read
686
+ # the reports it merged, as a turn that ended does.
687
+ settle_child_reports(rolled_back: error && !LLM::FailedTurn.kept_steps(error))
684
688
  ring_parent_after_turn
685
689
  end
686
690
 
687
691
  # The delegate reports this turn took: a kept turn moves the cursors on
688
- # and deletes their rings; a failed one (rolled back) leaves the rings
689
- # for the next turn.
690
- def settle_child_reports(error)
691
- error ? @child_reports.release : @child_reports.commit
692
+ # and deletes their rings; a rolled-back one leaves the rings for the
693
+ # next turn.
694
+ def settle_child_reports(rolled_back:)
695
+ rolled_back ? @child_reports.release : @child_reports.commit
692
696
  end
693
697
 
694
698
  # A delegate child's turn that was its parent's (#reports_to_parent?)
@@ -735,16 +739,18 @@ module Samagotchi
735
739
  # prompt it took (its own and any merged into it) goes back to its
736
740
  # sender, who can send it again. The rollback and the announcements are
737
741
  # one step of the event log: a snapshot shows the failed turn's messages
738
- # or the restored ones, never the one without the other.
742
+ # or the restored ones, never the one without the other. A turn that
743
+ # got somewhere stays instead (TurnFlow#prompt_turn_failed), with the
744
+ # prompts it took: nothing goes back.
739
745
  # @param prompts [Array<Array(String, Hash|nil, Array|nil)>] [prompt,
740
746
  # origin, images] (a web client gets its image chips back)
741
747
  # @param error [Exception, nil] what failed: its one line stays in the
742
748
  # conversation as a turn note
743
749
  # Not saved here: run_engine_turn saves after its block.
744
750
  def restore_failed_turn(prompts, error: nil)
745
- note = error && TurnNote.failed(error.respond_to?(:summary) ? error.summary : error.message, restored: true)
746
751
  @engine.synchronize_events do
747
- @turn_flow.prompt_turn_failed(note: note)
752
+ next if @turn_flow.prompt_turn_failed(error: error) == :kept
753
+
748
754
  prompts.each do |prompt, origin, images|
749
755
  restored = { type: :prompt_restored, prompt: prompt, origin: origin }
750
756
  restored[:images] = images unless Array(images).empty?
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.40.0
4
+ version: 0.41.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dmitry Dedov
@@ -312,8 +312,10 @@ files:
312
312
  - lib/samagotchi/llm/usage.rb
313
313
  - lib/samagotchi/llm/utf8_scrub.rb
314
314
  - lib/samagotchi/llm_context_apply.rb
315
+ - lib/samagotchi/llm_context_command.rb
315
316
  - lib/samagotchi/llm_context_edit.rb
316
317
  - lib/samagotchi/llm_context_forget.rb
318
+ - lib/samagotchi/llm_context_override.rb
317
319
  - lib/samagotchi/llm_context_stale.rb
318
320
  - lib/samagotchi/llm_context_strategy.rb
319
321
  - lib/samagotchi/llm_context_view.rb
@@ -323,6 +325,7 @@ files:
323
325
  - lib/samagotchi/log_subscriber.rb
324
326
  - lib/samagotchi/memory_bundle.rb
325
327
  - lib/samagotchi/memory_bundle/builder.rb
328
+ - lib/samagotchi/memory_bundle/index_size.rb
326
329
  - lib/samagotchi/memory_bundle/index_sync.rb
327
330
  - lib/samagotchi/memory_bundle/index_updater.rb
328
331
  - lib/samagotchi/memory_bundle/installer.rb
@@ -341,6 +344,8 @@ files:
341
344
  - lib/samagotchi/memory_paths.rb
342
345
  - lib/samagotchi/model_catalog.rb
343
346
  - lib/samagotchi/model_list_store.rb
347
+ - lib/samagotchi/model_match.rb
348
+ - lib/samagotchi/model_notes.rb
344
349
  - lib/samagotchi/model_overlay.rb
345
350
  - lib/samagotchi/model_profile.rb
346
351
  - lib/samagotchi/model_ref.rb
@@ -507,6 +512,7 @@ files:
507
512
  - lib/samagotchi/web/public/init_row.js
508
513
  - lib/samagotchi/web/public/live_timing.js
509
514
  - lib/samagotchi/web/public/live_turn.js
515
+ - lib/samagotchi/web/public/llm_context_chip.js
510
516
  - lib/samagotchi/web/public/model_pick.js
511
517
  - lib/samagotchi/web/public/model_picker.js
512
518
  - lib/samagotchi/web/public/notifications.js