Class: Rubino::Context::MessageBoundary

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/context/message_boundary.rb

Overview

Splits messages into head (protected), middle (compressible), tail (protected).

Instance Method Summary collapse

Constructor Details

#initialize(messages:, config: nil) ⇒ MessageBoundary

Returns a new instance of MessageBoundary.



7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/rubino/context/message_boundary.rb', line 7

def initialize(messages:, config: nil)
  @messages = messages
  @config = config || Rubino.configuration
  @protect_first = @config.compression_protect_first_n
  # Anti-task-loss (#415c, Hermes _ensure_last_user_message_in_tail):
  # guarantee the most recent user message lands in the protected tail.
  # If it falls in the compressed middle, SUMMARY_PREFIX tells the next
  # model to ignore it (reference-only) and the user's latest request
  # silently vanishes — the agent stalls or re-does old work
  # (#10896 class). Grow the protected-last window backward to cover it.
  @protect_last = [@config.compression_protect_last_n, tail_to_last_user].max
end

Instance Method Details

#has_compressible_middle?Boolean

Returns true if there are enough messages to have a middle section

Returns:

  • (Boolean)


40
41
42
# File 'lib/rubino/context/message_boundary.rb', line 40

def has_compressible_middle?
  !middle.empty?
end

#headObject

Returns the protected head messages (system prompt + first N)



21
22
23
# File 'lib/rubino/context/message_boundary.rb', line 21

def head
  @messages.first(@protect_first)
end

#middleObject

Returns the compressible middle messages



26
27
28
29
30
# File 'lib/rubino/context/message_boundary.rb', line 26

def middle
  return [] if @messages.size <= (@protect_first + @protect_last)

  @messages[@protect_first...-@protect_last]
end

#tailObject

Returns the protected tail messages (recent context)



33
34
35
36
37
# File 'lib/rubino/context/message_boundary.rb', line 33

def tail
  return [] if @messages.size <= @protect_last

  @messages.last(@protect_last)
end