samagotchi 0.148.0 → 0.149.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 +15 -1
  3. data/docs/architecture.md +1 -1
  4. data/docs/bundles.md +38 -1
  5. data/docs/cli.md +17 -0
  6. data/docs/configuration.md +4 -2
  7. data/docs/guardrails.md +2 -1
  8. data/docs/hooks.md +2 -1
  9. data/docs/internals/objectives.md +264 -0
  10. data/docs/internals/web-frontend.md +4 -1
  11. data/docs/plugins.md +4 -2
  12. data/docs/releasing.md +1 -1
  13. data/docs/sessions.md +42 -0
  14. data/docs/system-one.md +17 -1
  15. data/lib/samagotchi/bundles/coordinator/manifest.yml +6 -4
  16. data/lib/samagotchi/bundles/coordinator/plugin.rb +3 -2
  17. data/lib/samagotchi/bundles/coordinator/skill_coordinator.md +9 -0
  18. data/lib/samagotchi/bundles/decision-card/manifest.yml +13 -0
  19. data/lib/samagotchi/bundles/decision-card/plugin.rb +280 -0
  20. data/lib/samagotchi/bundles/dev/manifest.yml +3 -2
  21. data/lib/samagotchi/bundles/system/manifest.yml +1 -1
  22. data/lib/samagotchi/config.rb +7 -1
  23. data/lib/samagotchi/context_note.rb +17 -5
  24. data/lib/samagotchi/engine.rb +113 -6
  25. data/lib/samagotchi/engine_peers.rb +11 -0
  26. data/lib/samagotchi/guardrails/pair_boundary.rb +3 -2
  27. data/lib/samagotchi/llm/chat_loop.rb +1 -1
  28. data/lib/samagotchi/note_command.rb +1 -1
  29. data/lib/samagotchi/note_delivery.rb +6 -2
  30. data/lib/samagotchi/objectives.rb +395 -0
  31. data/lib/samagotchi/objectives_audit.rb +115 -0
  32. data/lib/samagotchi/objectives_check.rb +335 -0
  33. data/lib/samagotchi/objectives_command.rb +192 -0
  34. data/lib/samagotchi/plugin/api.rb +2 -1
  35. data/lib/samagotchi/plugin/context.rb +21 -1
  36. data/lib/samagotchi/session_commands.rb +15 -0
  37. data/lib/samagotchi/session_continue.rb +3 -0
  38. data/lib/samagotchi/session_inbox.rb +14 -6
  39. data/lib/samagotchi/session_manager.rb +1 -1
  40. data/lib/samagotchi/steer.rb +24 -4
  41. data/lib/samagotchi/testing/boundary.rb +1 -0
  42. data/lib/samagotchi/testing/host.rb +1 -0
  43. data/lib/samagotchi/tool_activity.rb +17 -0
  44. data/lib/samagotchi/tool_declarations.rb +34 -0
  45. data/lib/samagotchi/tools/builtin_calls.rb +2 -1
  46. data/lib/samagotchi/tools/builtins.rb +4 -1
  47. data/lib/samagotchi/tools/objectives_tool.rb +75 -0
  48. data/lib/samagotchi/tools/peers.rb +3 -0
  49. data/lib/samagotchi/version.rb +1 -1
  50. data/lib/samagotchi/web/message_display.rb +9 -0
  51. data/lib/samagotchi/web/participant_view.rb +1 -1
  52. data/lib/samagotchi/web/public/app.js +10 -0
  53. data/lib/samagotchi/web/public/cards_place.js +20 -4
  54. data/lib/samagotchi/web/public/commands.js +4 -1
  55. data/lib/samagotchi/web/public/index.html +2 -0
  56. data/lib/samagotchi/web/public/objectives.css +80 -0
  57. data/lib/samagotchi/web/public/objectives_entry.js +22 -0
  58. data/lib/samagotchi/web/public/objectives_model.js +100 -0
  59. data/lib/samagotchi/web/public/objectives_moves.js +145 -0
  60. data/lib/samagotchi/web/public/objectives_strip.js +242 -0
  61. data/lib/samagotchi/web/public/objectives_wiring.js +62 -0
  62. data/lib/samagotchi/web/public/stream_handlers.js +6 -1
  63. data/lib/samagotchi/web/public/turn_html.js +4 -1
  64. data/lib/samagotchi/web/session_show.rb +12 -1
  65. data/lib/samagotchi/worker.rb +3 -0
  66. data/lib/samagotchi/worker_inbound.rb +16 -8
  67. metadata +15 -1
@@ -51,6 +51,7 @@ require_relative "context_candidates"
51
51
  require_relative "context_candidates_log"
52
52
  require_relative "context_candidate_choice"
53
53
  require_relative "context_judge"
54
+ require_relative "objectives_audit"
54
55
  require_relative "owner_prompt"
55
56
  require_relative "side_judgments"
56
57
  require_relative "toolkit_suggest"
@@ -156,6 +157,8 @@ module Samagotchi
156
157
  # steers) and the idle layer's activity clock. Built first: a plugin
157
158
  # may steer or ask whether a turn runs while it loads.
158
159
  @turn_state = TurnState.new(clock: -> { monotonic_now })
160
+ @objective_proposals = []
161
+ @objective_proposals_mutex = Mutex.new
159
162
  # A message for the running turn cutting its generation.
160
163
  @steer_cut = SteerCut.new(clock: -> { monotonic_now }, controller: -> { active_cancel_controller },
161
164
  cut_after: -> { steer_cut_after })
@@ -767,6 +770,8 @@ module Samagotchi
767
770
  # command's ask_model between turns).
768
771
  # @return [String, nil]
769
772
  def side_author(kind)
773
+ # Check this is only ever the owner's, whichever turn runs as it ends.
774
+ return nil if kind.to_s == ObjectivesCheck::SOURCE
770
775
  return nil unless turn_running? || kind.to_s == RECAP_SIDE_KIND
771
776
 
772
777
  @guardrail_wiring.turn_author
@@ -956,7 +961,7 @@ module Samagotchi
956
961
  ANNOUNCEABLE_EVENTS = %i[turn_enqueued turn_dequeued input_merged prompt_restored continue_offered continue_resolved
957
962
  command_queued command_ran context_added card hook_notice guardrail_warning
958
963
  plugin_init_started plugin_init_finished working_set_updated participants_changed
959
- verdict_recorded].freeze
964
+ verdict_recorded objectives_updated].freeze
960
965
 
961
966
  # Put a transport-level event into the ordered event log. Unlike turn
962
967
  # events it reaches only persistent observers (no turn sink, no memory
@@ -987,6 +992,63 @@ module Samagotchi
987
992
  synchronize_events { emit_working_set(nil, session, session.messages) }
988
993
  end
989
994
 
995
+ # The session's objectives as the page draws them (Objectives.view), when
996
+ # that differs from the last announced: after an edit (its file written,
997
+ # pending), once a list note is in, at a turn's end (its moves) and after
998
+ # a rollback. A session with no list announces nothing. It only informs:
999
+ # a failure is logged, never the turn's.
1000
+ def announce_objectives(session = @session)
1001
+ return unless session
1002
+
1003
+ synchronize_events do
1004
+ view = Objectives.view(session.id, session.messages, state_dir: session_state_dir, running: live_conversation&.dup)
1005
+ next if view.nil? || view == @objectives_announced
1006
+
1007
+ @objectives_announced = view
1008
+ announce({ type: :objectives_updated, session_id: session.id, **view })
1009
+ end
1010
+ rescue StandardError => e
1011
+ Log.warn(:turn, "objectives_announce_failed", error: e.class.name, msg: e.message)
1012
+ end
1013
+
1014
+ # Check this (ObjectivesCheck)'s one side request. Its usage is in the
1015
+ # session's side line. Raises as ctx.ask_model's request does.
1016
+ # @param request [Array<Hash>] chat messages
1017
+ # @param model [String, nil] a configured model; nil: the session's
1018
+ # @return [String] the answer
1019
+ def ask_objective_check(request, model:)
1020
+ ask_side_model(request, timeout: ObjectivesCheck::TIMEOUT_SECONDS, max_tokens: ObjectivesCheck::MAX_TOKENS,
1021
+ cancel_controller: nil, model: model, source: ObjectivesCheck::SOURCE)
1022
+ end
1023
+
1024
+ # A change the model proposes (the objectives tool), kept until the turn
1025
+ # is over, as #context_choice's cards are: a card shown then is a quiet one, between turns, with its Apply kept
1026
+ # over a restart (CardStore), not a running turn's that says it waits on the
1027
+ # user. The same change twice in a turn is one card.
1028
+ # @return [Boolean] false past Objectives::PROPOSALS_PER_TURN
1029
+ def propose_objective(proposal)
1030
+ @objective_proposals_mutex.synchronize do
1031
+ next true if @objective_proposals.any? { |p| p.line == proposal.line }
1032
+ next false if @objective_proposals.size >= Objectives::PROPOSALS_PER_TURN
1033
+
1034
+ @objective_proposals << proposal
1035
+ true
1036
+ end
1037
+ end
1038
+
1039
+ def show_objective_proposals
1040
+ proposals = @objective_proposals_mutex.synchronize { @objective_proposals.slice!(0..) }
1041
+ proposals.each do |proposal|
1042
+ id = "objectives-#{SecureRandom.hex(4)}"
1043
+ show_card(source: Objectives::SOURCE, id: id, title: proposal.title, body: proposal.body,
1044
+ actions: [{ label: "Apply", command: proposal.action_line(id) },
1045
+ { label: "Dismiss", command: "/objectives --card #{id} dismiss" }])
1046
+ end
1047
+ rescue StandardError => e
1048
+ Log.warn(:turn, "objectives_proposal_failed", error: e.class.name, msg: e.message)
1049
+ end
1050
+ private :show_objective_proposals
1051
+
990
1052
  # The strategy the working set is built under now, as LLMContextView
991
1053
  # takes it: the running turn's, else the next turn's (#emit_working_set).
992
1054
  # /working-set's ops act under it.
@@ -1018,11 +1080,14 @@ module Samagotchi
1018
1080
  next nil if Array(session.messages).any? { |m| m[:note_id] == note[:note_id] }
1019
1081
 
1020
1082
  message = ContextNote.message(**note)
1083
+ entry = note[:objectives] && Objectives.entry(note[:objectives], before: Objectives.build(session.messages))
1021
1084
  replace_session_messages(session, Array(session.messages) + [message])
1022
1085
  announce({ type: :context_added, session_id: session.id, note_id: note[:note_id], source: note[:source],
1023
1086
  label: ContextNote.label_of(message), from_session: note[:from_session],
1024
1087
  from_cwd: note[:from_cwd], text: note[:text], context_source: note[:context_source],
1025
- created_at: note[:created_at] }.compact)
1088
+ created_at: note[:created_at], objectives: entry }.compact)
1089
+ announce_objectives(session) if entry
1090
+ objectives_audit.note(session, note)
1026
1091
  message
1027
1092
  end
1028
1093
  end
@@ -1677,6 +1742,7 @@ module Samagotchi
1677
1742
  event[:verdict] = verdict if verdict
1678
1743
  event[:revises] = revises.to_h if revises
1679
1744
  emit_working_set(turn.on_event, turn.session, turn.session.messages)
1745
+ announce_objectives(turn.session)
1680
1746
  turn.session.status = Session::STATUS_IDLE
1681
1747
  record_last_turn(turn.session, outcome, seconds, turn.origin, event, limit: exhausted ? turn.limit : nil)
1682
1748
  emit_event(turn.on_event, turn.tag(event))
@@ -2366,6 +2432,7 @@ module Samagotchi
2366
2432
  ensure
2367
2433
  release_turn(probe_cancel_before)
2368
2434
  show_context_cards_after_turn
2435
+ show_objective_proposals
2369
2436
  end
2370
2437
  end
2371
2438
 
@@ -2469,6 +2536,7 @@ module Samagotchi
2469
2536
  # The candidate judge: its asks run beside the turn; on mode's
2470
2537
  # attaches reach the model at the next between-turn delivery.
2471
2538
  start_context_judge(turn)
2539
+ audit_objectives_incoming(turn)
2472
2540
  apply_toolkit!(session, turn.on_event)
2473
2541
  refresh_participants!(session)
2474
2542
  # A Stop during the waits above: the turn ends here,
@@ -2740,14 +2808,18 @@ module Samagotchi
2740
2808
 
2741
2809
  # Fire :after_turn hook (an answer or a Stop; an Interrupt or a failure
2742
2810
  # ends the turn in #run_turn without it), with a read-only
2743
- # copy of the conversation the turn stored, event[:present] for
2811
+ # copy of the conversation the turn stored, event[:answer] (the answer's
2812
+ # text: the native loop's stored message still holds its thought),
2813
+ # event[:present] for
2744
2814
  # a display version of the answer (AnswerDisplay) and event[:refs]
2745
2815
  # for refs on it (AnswerRefs).
2746
2816
  session = turn.session
2817
+ audit_objectives_outgoing(turn, result) unless canceled || result.exhausted?
2747
2818
  display = AnswerDisplay.new(session.messages)
2748
2819
  refs = AnswerRefs.new(session.messages, cwd: session.working_directory)
2749
2820
  after_turn = { type: :after_turn, status: canceled ? "canceled" : "completed",
2750
- messages: hook_messages(session.messages) }
2821
+ messages: hook_messages(session.messages),
2822
+ answer: (result.text unless canceled || result.exhausted?) }
2751
2823
  after_turn[:present] = display.presenter(after_turn)
2752
2824
  after_turn[:refs] = refs.adder(after_turn)
2753
2825
  @hooks.fire(:after_turn, after_turn)
@@ -2855,7 +2927,8 @@ module Samagotchi
2855
2927
  # The running turn's conversation as its loop builds it (both loops
2856
2928
  # register it on the kernel's llm_context_turn): the same array the loop
2857
2929
  # appends to, not a copy, so read it on the turn's thread (the pending
2858
- # input drain, at an iteration boundary: TurnCheckpoint). nil when no
2930
+ # input drain, at an iteration boundary: TurnCheckpoint), or from another
2931
+ # only as a dup, read only (/objectives). nil when no
2859
2932
  # turn runs (llm_context_turn is never cleared, and still holds the
2860
2933
  # last turn's then).
2861
2934
  # @return [Array<Hash>, nil]
@@ -2885,7 +2958,10 @@ module Samagotchi
2885
2958
  raise ArgumentError, "no current session" unless @session
2886
2959
 
2887
2960
  messages = replace_session_messages(@session, clone_messages(checkpoint))
2888
- announce_working_set unless turn_running?
2961
+ unless turn_running?
2962
+ announce_working_set
2963
+ announce_objectives
2964
+ end
2889
2965
  messages
2890
2966
  end
2891
2967
 
@@ -3296,6 +3372,36 @@ module Samagotchi
3296
3372
  end
3297
3373
  private :start_context_judge
3298
3374
 
3375
+ # System 1 on the objectives, in audit (ObjectivesAudit): the owner's turns
3376
+ # only, and for a message a person's: a reminder's or a delegate's prompt isn't one.
3377
+ def audit_objectives_incoming(turn)
3378
+ return unless turn.prompt.is_a?(String) && turn.origin.human? && OwnerPrompt.turn?(session: turn.session, author: turn.origin.author)
3379
+
3380
+ objectives_audit.incoming(turn.session, turn.prompt)
3381
+ end
3382
+ private :audit_objectives_incoming
3383
+
3384
+ def audit_objectives_outgoing(turn, result)
3385
+ return unless OwnerPrompt.turn?(session: turn.session, author: turn.origin.author)
3386
+
3387
+ objectives_audit.outgoing(turn.session, result.text)
3388
+ end
3389
+ private :audit_objectives_outgoing
3390
+
3391
+ # With saving off nothing would come of an ask, so none is made.
3392
+ def objectives_audit
3393
+ @objectives_audit ||= ObjectivesAudit.new(
3394
+ side: @side_judgments, state_dir: -> { session_state_dir },
3395
+ client_for: lambda do |judgment, session|
3396
+ next nil unless system_one_runs.on?
3397
+
3398
+ system_one_recording(SystemOne::Client.from_config(host_registry: @host_registry, mode: ObjectivesAudit::MODE, tag: :system_one),
3399
+ judgment, session)
3400
+ end
3401
+ )
3402
+ end
3403
+ private :objectives_audit
3404
+
3299
3405
  # A late proposal no owner prompt mentions (Plugin::Host
3300
3406
  # #candidate_proposed: github-pr's init task runs beside the first
3301
3407
  # turn): judged on the owner's prompts so far, the running turn's too.
@@ -3446,6 +3552,7 @@ module Samagotchi
3446
3552
  cwd: -> { @session&.working_directory },
3447
3553
  messages: -> { plugin_messages },
3448
3554
  messages_partial: -> { turn_running? && @running_turn_messages.nil? },
3555
+ turn_running: -> { turn_running? },
3449
3556
  notify: ->(text, level, label, fallback_for: nil) { hook_notify(text, level, label, fallback_for: fallback_for) },
3450
3557
  ask_user: ->(question) { hook_ask_user(question) },
3451
3558
  # An init task's is its own (a Ctrl-C ends a turn, not the task).
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative "objectives"
4
+
3
5
  module Samagotchi
4
6
  # The views of an Engine its peers get: the kernel's tools
5
7
  # (Engine#initialize sets @kernel.peers) and a delegate's approval relay
@@ -28,6 +30,15 @@ module Samagotchi
28
30
  # The peer's name when +name+ is a source a peer bundle declared
29
31
  # (PeerSources), for context_read's header.
30
32
  def peer_label(name) = engine.peer_sources&.label_for(name)
33
+
34
+ # The session's objectives now (the objectives tool), nil outside one.
35
+ def objectives
36
+ session = engine.session
37
+ session && Objectives.current(session.id, session.messages, state_dir: engine.peer_state_dir)
38
+ end
39
+
40
+ # A proposed change: its card shows at the turn's end (Engine#propose_objective).
41
+ def propose_objective(proposal) = engine.propose_objective(proposal)
31
42
  end
32
43
 
33
44
  # What the approval relay needs from the parent's Engine (Peers#relay):
@@ -34,7 +34,8 @@ module Samagotchi
34
34
  # - owner-voiced tools (OWNER_VOICED, and any delegate_* tool): what
35
35
  # speaks or acts in the owner's name beyond this turn (a delegate, a
36
36
  # reminder, a note to another session, a background task, a
37
- # forgotten output, the session's loaded tools); the tools that read
37
+ # forgotten output, the session's loaded tools, a move on the owner's
38
+ # objectives); the tools that read
38
39
  # the owner's background tasks (OWNER_TASK_READERS); and memory_write:
39
40
  # a memory reaches the owner's later sessions;
40
41
  # - ask_user_question: there is no one on the participant's side to
@@ -51,7 +52,7 @@ module Samagotchi
51
52
  RULE = "pair-boundary"
52
53
  SOURCE = "core"
53
54
  OWNER_VOICED = %w[delegate register_reminder cancel_reminder send_note forget_outputs task_create task_stop
54
- find_tools].freeze
55
+ find_tools objectives].freeze
55
56
  OWNER_VOICED_PREFIX = "delegate_"
56
57
  # The background task tools that read the owner's tasks (a pair turn
57
58
  # starts none: task_create is owner-voiced, and its execute is never
@@ -259,7 +259,7 @@ module Samagotchi
259
259
  # Claude model's cache breakpoint there (PromptCache) and drops it
260
260
  # for every other request.
261
261
  def system_message(entry, index)
262
- text = entry[:content].to_s
262
+ text = Steer.wire_text(entry, entry[:content].to_s)
263
263
  split = index.zero? && @stable_length&.call(text)
264
264
  split ? { role: "system", content: text, cache_split: split } : { role: "system", content: text }
265
265
  end
@@ -123,7 +123,7 @@ module Samagotchi
123
123
 
124
124
  def outcome(result)
125
125
  case result.status
126
- when :refused then "refused: it is open in a chi REPL; notes need attached mode"
126
+ when :refused then "refused: #{NoteDelivery::REPL_REFUSAL}"
127
127
  when :queued then "queued: its worker adds it within a few seconds"
128
128
  else "waits for the session's next start (#{result.queued} #{result.queued == 1 ? "note" : "notes"} queued)"
129
129
  end
@@ -14,6 +14,9 @@ module Samagotchi
14
14
  # note in its notes/ folder, which a live worker adds within seconds and
15
15
  # a session with no worker at its next start. Callers word the outcome.
16
16
  module NoteDelivery
17
+ # Why a chi REPL's session takes no note.
18
+ REPL_REFUSAL = "it is open in a chi REPL; notes need attached mode"
19
+
17
20
  # The outcome for one session.
18
21
  # @!attribute status [Symbol] :queued (a worker runs it), :waits (no
19
22
  # worker: it waits for the next start) or :refused (a chi REPL owns it)
@@ -26,14 +29,15 @@ module Samagotchi
26
29
  module_function
27
30
 
28
31
  # @param id [String] a full session id (resolved by the caller)
32
+ # @param objectives [Hash, nil] an objectives note's payload
29
33
  # @return [Result]
30
34
  # @raise [SessionInbox::NoteRejected] an empty note or one over 16 KiB
31
- def deliver(id, text:, source:, state_dir:, from_session: nil, from_cwd: nil)
35
+ def deliver(id, text:, source:, state_dir:, from_session: nil, from_cwd: nil, objectives: nil)
32
36
  owner = SessionManager.session_owner(id, state_dir: state_dir)
33
37
  return Result.new(id: id, status: :refused, queued: 0) if owner&.tui?
34
38
 
35
39
  path = SessionInbox.write_note(id, text: text, source: source, from_session: from_session, from_cwd: from_cwd,
36
- state_dir: state_dir)
40
+ objectives: objectives, state_dir: state_dir)
37
41
  return Result.new(id: id, status: :queued, queued: 0) if owner
38
42
 
39
43
  Result.new(id: id, status: :waits, queued: SessionInbox.find_new_note_files(File.dirname(path, 2)).size)