Class: Clacky::MessageHistory

Inherits:
Object
  • Object
show all
Defined in:
lib/clacky/message_history.rb

Overview

MessageHistory wraps the conversation message list and exposes business-meaningful operations instead of raw array manipulation.

Internal fields (task_id, created_at, system_injected, etc.) are kept in the internal store but stripped when calling #to_api.

Constant Summary collapse

INTERNAL_FIELDS =

Fields that are internal to the agent and must not be sent to the API.

%i[
  task_id created_at system_injected session_context memory_update
  subagent_instructions subagent_result token_usage
  compressed_summary chunk_path truncated transient
  chunk_index chunk_count
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(messages = []) ⇒ MessageHistory

Returns a new instance of MessageHistory.



18
19
20
# File 'lib/clacky/message_history.rb', line 18

def initialize(messages = [])
  @messages = messages.dup
end

Class Method Details

.pad_reasoning_content_if_needed(msgs, force: false) ⇒ Object

Public helper: pad assistant messages that lack a reasoning_content field with an empty string, either when forced or when the payload already shows evidence of thinking-mode (at least one assistant message with reasoning_content).

Exposed as a class method so Time Machine’s active_messages path can reuse the exact same logic without routing through #to_api.



320
321
322
323
324
325
326
327
328
329
330
# File 'lib/clacky/message_history.rb', line 320

def self.pad_reasoning_content_if_needed(msgs, force: false)
  should_pad = force || msgs.any? { |m| m[:role] == "assistant" && m[:reasoning_content] }
  return msgs unless should_pad

  msgs.map do |m|
    next m unless m[:role] == "assistant"
    next m if m.key?(:reasoning_content)

    m.merge(reasoning_content: "")
  end
end

Instance Method Details

#append(message) ⇒ Object

Append a single message hash to the history.

When appending a user message, automatically drop any trailing assistant message that has unanswered tool_calls (no tool_result follows it). This prevents API error 2013 (“tool call result does not follow tool call”) when a previous task ended before observe() could append tool results (e.g. subagent crash, interrupt, or error).



33
34
35
36
37
38
39
# File 'lib/clacky/message_history.rb', line 33

def append(message)
  if message[:role] == "user"
    drop_dangling_tool_calls!
  end
  @messages << deep_sanitize_utf8(message)
  self
end

#delete_where(&block) ⇒ Object

Remove all messages matching the block in-place. Generic history pruning utility — used by callers that need to strip transient/system-injected messages out of the persisted history (e.g. compaction, rollback on 400 errors).



69
70
71
72
# File 'lib/clacky/message_history.rb', line 69

def delete_where(&block)
  @messages.reject!(&block)
  self
end

#empty?Boolean

Returns:

  • (Boolean)


166
167
168
# File 'lib/clacky/message_history.rb', line 166

def empty?
  @messages.empty?
end

#estimate_tokensObject

Estimate total token count for all messages. Uses the ~4 chars/token heuristic (works well for English/code). Handles string content, array content blocks, and tool_calls.



173
174
175
# File 'lib/clacky/message_history.rb', line 173

def estimate_tokens
  @messages.sum { |m| estimate_message_tokens(m) }
end

#for_task(task_id) ⇒ Object

Return all messages where task_id <= given id (Time Machine support).



154
155
156
# File 'lib/clacky/message_history.rb', line 154

def for_task(task_id)
  @messages.select { |m| !m[:task_id] || m[:task_id] <= task_id }
end

#has_system_prompt?Boolean

True when a system prompt message is present in the history. Used by inject_session_context to avoid injecting context messages before the system prompt has been built (which would cause the guard in run() to skip building it altogether).

Returns:

  • (Boolean)


107
108
109
# File 'lib/clacky/message_history.rb', line 107

def has_system_prompt?
  @messages.any? { |m| m[:role] == "system" }
end

#last_injected_chunk_countObject

Return the chunk_count from the most recently injected chunk index message. Used by inject_chunk_index_if_needed to avoid re-injecting when nothing changed.



132
133
134
135
# File 'lib/clacky/message_history.rb', line 132

def last_injected_chunk_count
  msg = @messages.reverse.find { |m| m[:chunk_index] }
  msg&.dig(:chunk_count) || 0
end

#last_real_user_indexObject

Return the index of the last real (non-system-injected) user message. Used by restore_session to trim back to a clean state on error.



144
145
146
# File 'lib/clacky/message_history.rb', line 144

def last_real_user_index
  @messages.rindex { |m| m[:role] == "user" && !m[:system_injected] }
end

#last_session_context_dateObject

Return the session_date value from the most recent session_context message. Used by inject_session_context_if_needed to avoid re-injecting on the same date.



125
126
127
128
# File 'lib/clacky/message_history.rb', line 125

def last_session_context_date
  msg = @messages.reverse.find { |m| m[:session_context] }
  msg&.dig(:session_date)
end

#mutate_last_matching(predicate, &block) ⇒ Object

Mutate the last message matching the predicate lambda in-place. Used by execute_skill_with_subagent to update instruction messages.



76
77
78
79
80
# File 'lib/clacky/message_history.rb', line 76

def mutate_last_matching(predicate, &block)
  msg = @messages.reverse.find { |m| predicate.call(m) }
  block.call(msg) if msg
  self
end

#pending_tool_calls?Boolean

True when the last assistant message has tool_calls but no tool_result has been appended yet (would cause a 400 from the API).

Returns:

  • (Boolean)


113
114
115
116
117
118
119
120
121
# File 'lib/clacky/message_history.rb', line 113

def pending_tool_calls?
  return false if @messages.empty?

  last = @messages.last
  return false unless last[:role] == "assistant" && last[:tool_calls]&.any?

  last_assistant_idx = @messages.rindex { |m| m == last }
  @messages[(last_assistant_idx + 1)..].none? { |m| m[:role] == "tool" || m[:tool_results] }
end

#pop_lastObject

Remove and return the last message.



61
62
63
# File 'lib/clacky/message_history.rb', line 61

def pop_last
  @messages.pop
end

#real_user_messagesObject

Return only real (non-system-injected) user messages.



138
139
140
# File 'lib/clacky/message_history.rb', line 138

def real_user_messages
  @messages.select { |m| m[:role] == "user" && !m[:system_injected] }
end

#replace_all(new_messages) ⇒ Object

Replace the entire message list (used by compression rebuild).



55
56
57
58
# File 'lib/clacky/message_history.rb', line 55

def replace_all(new_messages)
  @messages = new_messages.map { |m| deep_sanitize_utf8(m) }
  self
end

#replace_system_prompt(content, **extra) ⇒ Object

Replace (or insert at head) the system prompt message. Used by session_serializer#refresh_system_prompt.



43
44
45
46
47
48
49
50
51
52
# File 'lib/clacky/message_history.rb', line 43

def replace_system_prompt(content, **extra)
  msg = { role: "system", content: content }.merge(extra)
  idx = @messages.index { |m| m[:role] == "system" }
  if idx
    @messages[idx] = msg
  else
    @messages.unshift(msg)
  end
  self
end

#rollback_before(message) ⇒ Object

Roll back the history to just before the given message object. Removes the message and anything appended after it. Used to undo a failed speculative append (e.g. compression message that errored).



91
92
93
94
95
96
97
# File 'lib/clacky/message_history.rb', line 91

def rollback_before(message)
  idx = @messages.index { |m| m.equal?(message) }
  return self unless idx

  @messages = @messages[0...idx]
  self
end

#sizeObject

─────────────────────────────────────────────Size helpers ─────────────────────────────────────────────



162
163
164
# File 'lib/clacky/message_history.rb', line 162

def size
  @messages.size
end

#subagent_instruction_messageObject

Return the message with :subagent_instructions set.



149
150
151
# File 'lib/clacky/message_history.rb', line 149

def subagent_instruction_message
  @messages.find { |m| m[:subagent_instructions] }
end

#to_aObject

Return a shallow copy of the message list, excluding transient messages. Transient messages (e.g. brand skill instructions) are valid during the current session but must not be persisted to session.json. For serialization, compression, and cloning.



203
204
205
# File 'lib/clacky/message_history.rb', line 203

def to_a
  @messages.reject { |m| m[:transient] }.dup
end

#to_api(force_reasoning_content_pad: false) ⇒ Object

Return a clean copy of messages suitable for sending to the LLM API:

  • strips internal-only fields

  • pads reasoning_content on synthetic assistant messages when the conversation is running against a thinking-mode provider

Parameters:

  • force_reasoning_content_pad (Boolean) (defaults to: false)

    When true, unconditionally pad every assistant message that lacks a reasoning_content field with an empty string. This is set by the LLM caller AFTER a 400 “reasoning_content must be passed back” error as a one-shot retry signal — the history-evidence heuristic below can’t fire when the previous turns came from a provider that keeps thinking inline (e.g. MiniMax: <think>…</think> in content), so this bypass lets us recover on the retry without a server restart.



194
195
196
197
# File 'lib/clacky/message_history.rb', line 194

def to_api(force_reasoning_content_pad: false)
  msgs = @messages.map { |m| strip_for_api(m) }
  ensure_reasoning_content_consistency(msgs, force: force_reasoning_content_pad)
end

#truncate_from(index) ⇒ Object

Remove all messages from index onward (used by restore_session on error).



83
84
85
86
# File 'lib/clacky/message_history.rb', line 83

def truncate_from(index)
  @messages = @messages[0...index]
  self
end