brute 6.1.1 → 6.1.3
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.
- checksums.yaml +4 -4
- data/lib/brute/message_transport/open_router.rb +1 -27
- data/lib/brute/messages.rb +0 -47
- data/lib/brute/middleware/006_loop.rb +36 -1
- data/lib/brute/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 6ecf8ae2bc514e6cb9cb6cefb1a06476ab68d13b459446ed0c398effcf334a86
|
|
4
|
+
data.tar.gz: 612dde127eee6f42a4aeb8f6c1fae039d74aaad8c9ed8481de22c0ec6b6cc2ce
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: fe3ee53f5b02b310397b959f307b5ddf8639ab4cedb6d747264a1b49756e65ef93bee312a609898fe03f896a27775b5adabbb30f9e278c71df9ec68a80547e81
|
|
7
|
+
data.tar.gz: d0cba57fc257001c634474596f82b1b55517ba9f9410106b34a142df1ab8ee6db29d580016dee6db354e181741dcbad0b6be507e048dc6240193ff1cdbedba4f
|
|
@@ -13,7 +13,6 @@ module Brute
|
|
|
13
13
|
#
|
|
14
14
|
# require "open_router"
|
|
15
15
|
class OpenRouter < MessageTransport
|
|
16
|
-
EmptyCompletion = Class.new(StandardError)
|
|
17
16
|
|
|
18
17
|
# What the provider reported about this call — the transport knows its
|
|
19
18
|
# own library's shape, so it knows which detector to ask.
|
|
@@ -94,15 +93,6 @@ module Brute
|
|
|
94
93
|
|
|
95
94
|
case hash
|
|
96
95
|
in { role: (:system | :user | :assistant | :tool) }
|
|
97
|
-
# A completion that answered with nothing is not an answer. The
|
|
98
|
-
# provider was paid for it and something came back -- reasoning,
|
|
99
|
-
# a refusal, a field this does not know -- and appending it as an
|
|
100
|
-
# empty assistant message loses the turn quietly: the loop stops
|
|
101
|
-
# because it is not a tool result, and there is no reply in it.
|
|
102
|
-
if hash[:role] == :assistant && hash[:content].to_s.strip.empty? && Array(hash[:tool_calls]).empty? && reasoning(hash).nil?
|
|
103
|
-
raise EmptyCompletion, "the provider answered with no content and no tool calls: #{message.inspect}"
|
|
104
|
-
end
|
|
105
|
-
|
|
106
96
|
# Slice away provider extras (refusal, model, ...) that
|
|
107
97
|
# Brute::Message doesn't know. Reasoning is not one of them: it is
|
|
108
98
|
# the model's own thinking and it goes back up with the message.
|
|
@@ -193,7 +183,7 @@ describe "brute/message_transport/open_router" do
|
|
|
193
183
|
out.first.content.should == "hi there"
|
|
194
184
|
end
|
|
195
185
|
|
|
196
|
-
it "keeps the model's reasoning
|
|
186
|
+
it "keeps the model's reasoning" do
|
|
197
187
|
# Reasoning is the model's own thinking, and it goes back up with the
|
|
198
188
|
# message: a reasoning model that called a tool has to see it on the next
|
|
199
189
|
# pass. Dropping it left an empty assistant message and a lost turn.
|
|
@@ -218,22 +208,6 @@ describe "brute/message_transport/open_router" do
|
|
|
218
208
|
{ type: "reasoning.text", text: "step by step", signature: "sig-1" },
|
|
219
209
|
]
|
|
220
210
|
|
|
221
|
-
# A tool call is an answer, even with nothing said alongside it.
|
|
222
|
-
calling = Struct.new(:choices).new([
|
|
223
|
-
{ "message" => {
|
|
224
|
-
"role" => "assistant", "content" => nil,
|
|
225
|
-
"tool_calls" => [{ "id" => "tc1", "type" => "function", "function" => { "name" => "shell", "arguments" => "{}" } }],
|
|
226
|
-
} },
|
|
227
|
-
])
|
|
228
|
-
|
|
229
|
-
Brute::MessageTransport::OpenRouter.new(calling).wrap_each.to_a.first.tool_call?.should.be.true
|
|
230
|
-
|
|
231
|
-
# Nothing said, nothing thought, nothing called: the provider was paid for
|
|
232
|
-
# an answer and there is none, which is an error rather than a message.
|
|
233
|
-
empty = Struct.new(:choices).new([{ "message" => { "role" => "assistant", "content" => "", "refusal" => nil } }])
|
|
234
|
-
|
|
235
|
-
lambda { Brute::MessageTransport::OpenRouter.new(empty).wrap_each.to_a }
|
|
236
|
-
.should.raise(Brute::MessageTransport::OpenRouter::EmptyCompletion)
|
|
237
211
|
end
|
|
238
212
|
|
|
239
213
|
it "round-trips every detail type under its own payload key" do
|
data/lib/brute/messages.rb
CHANGED
|
@@ -92,34 +92,12 @@ module Brute
|
|
|
92
92
|
# What the model thought, for anyone reading rather than replaying.
|
|
93
93
|
def text = blocks.filter_map(&:text).join("\n")
|
|
94
94
|
|
|
95
|
-
# Whether these blocks may be sent back to this provider as they are. A
|
|
96
|
-
# signature the provider did not issue is worse than no signature: it is
|
|
97
|
-
# a rejected request.
|
|
98
|
-
def signed_by?(format) = !format.nil? && blocks.any? && issued_by?(format) && blocks.all?(&:signed?)
|
|
99
|
-
|
|
100
95
|
# Whether this is the sequence the model emitted rather than one built
|
|
101
96
|
# around a plaintext string. Only the former may be replayed: a provider
|
|
102
97
|
# checks what comes back against what it produced, and prose lifted out of
|
|
103
98
|
# a `reasoning` field was never a sequence at all.
|
|
104
99
|
def detailed? = blocks.any? { |block| block.signed? || block.format || block.id }
|
|
105
100
|
|
|
106
|
-
# Whether every block came from this provider. The sequence goes back
|
|
107
|
-
# whole or not at all, so one foreign block disqualifies all of them.
|
|
108
|
-
#
|
|
109
|
-
# A block that names no provider is nobody's to vouch for: unsigned there
|
|
110
|
-
# is nothing to vouch for and it passes, but signed it is a signature that
|
|
111
|
-
# cannot be attributed, and offering one the provider did not issue is a
|
|
112
|
-
# rejected request rather than an unverified one.
|
|
113
|
-
def issued_by?(format)
|
|
114
|
-
blocks.all? do |block|
|
|
115
|
-
if block.format.nil?
|
|
116
|
-
!block.signed?
|
|
117
|
-
else
|
|
118
|
-
format.nil? || block.format == format
|
|
119
|
-
end
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
|
|
123
101
|
def to_h(...) = { blocks: blocks.map(&:to_h) }
|
|
124
102
|
end
|
|
125
103
|
|
|
@@ -256,31 +234,6 @@ describe "brute/messages" do
|
|
|
256
234
|
Brute::Message.new(**m.to_h).should == m
|
|
257
235
|
end
|
|
258
236
|
|
|
259
|
-
it "will not vouch for a sequence in which any block is unsigned" do
|
|
260
|
-
# A provider that verifies signatures rejects a thinking block without
|
|
261
|
-
# one, so one signed block among several does not make the sequence
|
|
262
|
-
# sendable: every block has to carry its own signature, or none goes.
|
|
263
|
-
mixed = Brute::Reasoning.build(blocks: [
|
|
264
|
-
{ type: :text, text: "step one", signature: "sig-1", format: "anthropic-claude-v1" },
|
|
265
|
-
{ type: :text, text: "step two", format: "anthropic-claude-v1" },
|
|
266
|
-
])
|
|
267
|
-
|
|
268
|
-
mixed.signed_by?("anthropic-claude-v1").should.be.false
|
|
269
|
-
end
|
|
270
|
-
|
|
271
|
-
it "will not vouch for a signature that names no provider" do
|
|
272
|
-
# A block with a signature and no format cannot be attributed, and a
|
|
273
|
-
# signature the provider did not issue is a rejected request rather than
|
|
274
|
-
# an unverified one. Unsigned and unstamped is nobody's claim to make, so
|
|
275
|
-
# it passes.
|
|
276
|
-
unattributed = Brute::Reasoning.build(text: "thought", signature: "sig-1")
|
|
277
|
-
|
|
278
|
-
unattributed.issued_by?("anthropic-claude-v1").should.be.false
|
|
279
|
-
unattributed.signed_by?("anthropic-claude-v1").should.be.false
|
|
280
|
-
|
|
281
|
-
Brute::Reasoning.build(text: "just prose").issued_by?("anthropic-claude-v1").should.be.true
|
|
282
|
-
end
|
|
283
|
-
|
|
284
237
|
it "knows a sequence the model produced from one built around a string" do
|
|
285
238
|
# Only what the model actually emitted may be sent back as a sequence, so
|
|
286
239
|
# reasoning coerced from plaintext is not one to be replayed.
|
|
@@ -50,6 +50,13 @@ module Brute
|
|
|
50
50
|
# stops when the LLM answers with text only or env[:should_exit] is set
|
|
51
51
|
# (e.g. by MaxIterations).
|
|
52
52
|
#
|
|
53
|
+
# It also loops on a completion that only thought: a reasoning model
|
|
54
|
+
# sometimes returns its thinking and stops — no text, no tool call —
|
|
55
|
+
# having decided what to do without doing it. That is not an answer, and
|
|
56
|
+
# the turn is not over; the thinking goes back with the conversation,
|
|
57
|
+
# which is what the provider asks for anyway, and the model does what it
|
|
58
|
+
# had already worked out.
|
|
59
|
+
#
|
|
53
60
|
# use Brute::Middleware::Loop::ToolResult
|
|
54
61
|
#
|
|
55
62
|
class ToolResult < Loop
|
|
@@ -58,7 +65,7 @@ module Brute
|
|
|
58
65
|
next false
|
|
59
66
|
end
|
|
60
67
|
|
|
61
|
-
unless env[:messages].last&.role == :tool
|
|
68
|
+
unless env[:messages].last&.role == :tool || ToolResult.thought_only?(env[:messages].last)
|
|
62
69
|
next false
|
|
63
70
|
end
|
|
64
71
|
|
|
@@ -67,6 +74,14 @@ module Brute
|
|
|
67
74
|
true
|
|
68
75
|
end
|
|
69
76
|
|
|
77
|
+
# Thinking, and nothing done with it.
|
|
78
|
+
def self.thought_only?(message)
|
|
79
|
+
message&.role == :assistant &&
|
|
80
|
+
message.reasoning &&
|
|
81
|
+
message.content.to_s.strip.empty? &&
|
|
82
|
+
!message.tool_call?
|
|
83
|
+
end
|
|
84
|
+
|
|
70
85
|
def initialize(app)
|
|
71
86
|
super(app, CONDITION)
|
|
72
87
|
end
|
|
@@ -136,6 +151,26 @@ describe "brute/middleware/006_loop" do
|
|
|
136
151
|
|
|
137
152
|
# --- Loop::BackgroundJobs ---
|
|
138
153
|
|
|
154
|
+
it "ToolResult reruns on a completion that only thought, so the turn is not lost" do
|
|
155
|
+
# A reasoning model sometimes returns the thinking and stops, having
|
|
156
|
+
# decided what to do without doing it. The turn is not over: the thinking
|
|
157
|
+
# goes back and the model does what it had worked out.
|
|
158
|
+
thought = Brute::Message.new(role: :assistant, reasoning: "I should list the events")
|
|
159
|
+
|
|
160
|
+
env = { messages: Brute.log.tap { |log| log << thought }, current_iteration: 1 }
|
|
161
|
+
Brute::Middleware::Loop::ToolResult::CONDITION.call(env).should.be.true
|
|
162
|
+
env[:current_iteration].should == 2
|
|
163
|
+
|
|
164
|
+
# An answer, or a tool call it did make, ends the turn as before.
|
|
165
|
+
answered = { messages: Brute.log.tap { |log| log.assistant("here they are") }, current_iteration: 1 }
|
|
166
|
+
Brute::Middleware::Loop::ToolResult::CONDITION.call(answered).should.be.false
|
|
167
|
+
|
|
168
|
+
calling = Brute::Message.new(role: :assistant, reasoning: "I should list them",
|
|
169
|
+
tool_calls: [{ id: "tc1", name: "list_events", arguments: {} }])
|
|
170
|
+
called = { messages: Brute.log.tap { |log| log << calling }, current_iteration: 1 }
|
|
171
|
+
Brute::Middleware::Loop::ToolResult::CONDITION.call(called).should.be.false
|
|
172
|
+
end
|
|
173
|
+
|
|
139
174
|
it "BackgroundJobs reruns while jobs are running" do
|
|
140
175
|
seen = []
|
|
141
176
|
inner = ->(env) { seen << env[:background_jobs]; env[:background_jobs] = seen.size < 3 }
|
data/lib/brute/version.rb
CHANGED