Class: Mneme::Tools::AttachMessagesToGoals

Inherits:
Tools::Base
  • Object
show all
Defined in:
lib/mneme/tools/attach_messages_to_goals.rb

Overview

Pins critical messages to active Goals so they survive viewport eviction. Mneme calls this when it sees important messages (user instructions, key decisions, critical corrections) approaching the eviction zone.

Messages are pinned via a many-to-many join: one message can be attached to multiple Goals. When all referencing Goals complete, the pin is automatically released (reference-counted cleanup in Goal#release_orphaned_pins!).

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tools::Base

schema

Constructor Details

#initialize(main_session:) ⇒ AttachMessagesToGoals

Returns a new instance of AttachMessagesToGoals.

Parameters:

  • main_session (Session)

    the session being observed



35
36
37
# File 'lib/mneme/tools/attach_messages_to_goals.rb', line 35

def initialize(main_session:, **)
  @session = main_session
end

Class Method Details

.descriptionObject



15
# File 'lib/mneme/tools/attach_messages_to_goals.rb', line 15

def self.description = "Pin critical messages to goals so they survive viewport eviction."

.input_schemaObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mneme/tools/attach_messages_to_goals.rb', line 17

def self.input_schema
  {
    type: "object",
    properties: {
      message_ids: {
        type: "array",
        items: {type: "integer"}
      },
      goal_ids: {
        type: "array",
        items: {type: "integer"}
      }
    },
    required: %w[message_ids goal_ids]
  }
end

.tool_nameObject



13
# File 'lib/mneme/tools/attach_messages_to_goals.rb', line 13

def self.tool_name = "attach_messages_to_goals"

Instance Method Details

#execute(input) ⇒ String

Returns confirmation with link count, or error description.

Parameters:

  • input (Hash<String, Object>)

    with “message_ids” and “goal_ids”

Returns:

  • (String)

    confirmation with link count, or error description



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mneme/tools/attach_messages_to_goals.rb', line 41

def execute(input)
  message_ids = Array(input["message_ids"]).map(&:to_i).uniq
  goal_ids = Array(input["goal_ids"]).map(&:to_i).uniq

  return "Error: message_ids cannot be empty" if message_ids.empty?
  return "Error: goal_ids cannot be empty" if goal_ids.empty?

  messages = @session.messages.where(id: message_ids)
  all_goals = @session.goals
  goals = all_goals.active.where(id: goal_ids)

  missing_messages = message_ids - messages.pluck(:id)
  inactive_goal_ids = goal_ids - goals.pluck(:id)

  errors = []
  errors << "Messages not found: #{missing_messages.join(", ")}" if missing_messages.any?

  if inactive_goal_ids.any?
    completed_ids = all_goals.completed.where(id: inactive_goal_ids).pluck(:id)
    not_found_ids = inactive_goal_ids - completed_ids
    errors << "Goals already completed: #{completed_ids.join(", ")}" if completed_ids.any?
    errors << "Goals not found: #{not_found_ids.join(", ")}" if not_found_ids.any?
  end

  return "Error: #{errors.join("; ")}" if errors.any?

  attached = attach(messages, goals)
  "Pinned #{attached} message-goal links"
end