squishling 0.8.0 → 0.8.1
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/CHANGELOG.md +105 -0
- data/README.md +2 -2
- data/docs/adapters.md +95 -9
- data/docs/apprentice.md +6 -0
- data/docs/configuration.md +10 -8
- data/docs/failures.md +44 -7
- data/docs/harnesses.md +9 -5
- data/docs/images.md +2 -2
- data/docs/inputs.md +23 -10
- data/docs/measuring-tokens.md +3 -3
- data/docs/outer-loop-agents.md +56 -9
- data/docs/schemas.md +3 -1
- data/docs/testing.md +6 -3
- data/docs/verifying.md +11 -6
- data/lib/squishling/adapter/candidates.rb +144 -0
- data/lib/squishling/adapter/override.rb +109 -0
- data/lib/squishling/adapter/retry_after.rb +34 -0
- data/lib/squishling/adapter.rb +31 -15
- data/lib/squishling/adapters/openai_compatible.rb +10 -9
- data/lib/squishling/adapters/openai_options.rb +2 -1
- data/lib/squishling/adapters/ruby_llm.rb +14 -5
- data/lib/squishling/apprentice/shadow.rb +45 -4
- data/lib/squishling/attachment.rb +60 -28
- data/lib/squishling/configuration.rb +42 -12
- data/lib/squishling/definition.rb +8 -3
- data/lib/squishling/errors.rb +41 -8
- data/lib/squishling/image.rb +6 -3
- data/lib/squishling/judge.rb +19 -8
- data/lib/squishling/llm_client.rb +3 -2
- data/lib/squishling/outer_loop_agent.rb +33 -6
- data/lib/squishling/params.rb +3 -1
- data/lib/squishling/payload/normalizer.rb +24 -9
- data/lib/squishling/payload.rb +32 -8
- data/lib/squishling/result.rb +87 -34
- data/lib/squishling/run/rejections.rb +64 -6
- data/lib/squishling/run/reports.rb +23 -8
- data/lib/squishling/run.rb +36 -51
- data/lib/squishling/sanitizer.rb +5 -3
- data/lib/squishling/schema.rb +55 -4
- data/lib/squishling/squawk.rb +4 -3
- data/lib/squishling/testing/adapter.rb +1 -1
- data/lib/squishling/testing/outer_loop_agent.rb +11 -0
- data/lib/squishling/testing/reply.rb +23 -10
- data/lib/squishling/testing/script.rb +7 -7
- data/lib/squishling/testing/stubs.rb +8 -2
- data/lib/squishling/verifier.rb +41 -15
- data/lib/squishling/version.rb +1 -1
- data/lib/squishling.rb +3 -0
- metadata +4 -1
|
@@ -5,9 +5,13 @@ module Squishling
|
|
|
5
5
|
# declared workload), which squawk's after-the-fact report can't do. An outer-loop agent is any object responding to
|
|
6
6
|
# around(call), which yields once to make the request. See docs/outer-loop-agents.md.
|
|
7
7
|
#
|
|
8
|
-
#
|
|
9
|
-
# output or the method's inputs. One that also responds to invalid_output is told the messages of each
|
|
10
|
-
# response (Rejection), which can quote text the model chose, but never the response itself.
|
|
8
|
+
# around gives an outer-loop agent call metadata (Call) and, back from the yield, the request's token Usage: never the
|
|
9
|
+
# model's output or the method's inputs. One that also responds to invalid_output is told the messages of each
|
|
10
|
+
# rejected response (Rejection), which can quote text the model chose, but never the response itself.
|
|
11
|
+
#
|
|
12
|
+
# The one way an outer-loop agent receives output is a report(output:, metadata:, error:) method it defines on
|
|
13
|
+
# purpose: it is sent the same reports squawk is (see Squawk and .reporter), so an agent that doesn't define it
|
|
14
|
+
# never sees output, however it is configured.
|
|
11
15
|
module OuterLoopAgent
|
|
12
16
|
# What an outer-loop agent is told about one provider request. handed_off is true when squish! or squish_rescue sent
|
|
13
17
|
# the call to the LLM (typically from a rescue) and false when squish_when or a missing implementation did. role is
|
|
@@ -42,6 +46,8 @@ module Squishling
|
|
|
42
46
|
:apprentice)
|
|
43
47
|
|
|
44
48
|
BUILT_IN = %i[coolhand].freeze
|
|
49
|
+
# The keywords report is called with (the squawk hook's).
|
|
50
|
+
REPORT_KEYWORDS = Squawk::KEYWORDS
|
|
45
51
|
|
|
46
52
|
module_function
|
|
47
53
|
|
|
@@ -56,11 +62,22 @@ module Squishling
|
|
|
56
62
|
raise ConfigurationError, "#{where} outer-loop agent must be one of #{BUILT_IN.map(&:inspect).join(', ')}, " \
|
|
57
63
|
"an object that responds to around, or false, got #{Appendices.describe(agent)}"
|
|
58
64
|
end
|
|
59
|
-
|
|
65
|
+
unless agent.respond_to?(:around)
|
|
66
|
+
raise ConfigurationError, "#{where} outer-loop agent must respond to around(call) { ... } (or be false), " \
|
|
67
|
+
"got #{Appendices.describe(agent)}"
|
|
68
|
+
end
|
|
69
|
+
validate_report(agent, where)
|
|
70
|
+
agent
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# An agent's optional report is called with keywords only, so a mismatch fails at setup, not inside a call.
|
|
74
|
+
def validate_report(agent, where)
|
|
75
|
+
return unless agent.respond_to?(:report)
|
|
60
76
|
|
|
61
|
-
|
|
62
|
-
|
|
77
|
+
KeywordHook.check_parameters(agent.method(:report).parameters, "#{where} outer-loop agent's report",
|
|
78
|
+
REPORT_KEYWORDS)
|
|
63
79
|
end
|
|
80
|
+
private_class_method :validate_report
|
|
64
81
|
|
|
65
82
|
# The outer-loop agent that wraps a request: the configured one, the Coolhand outer-loop agent when nothing is
|
|
66
83
|
# configured and Coolhand is loaded and set up, or nil.
|
|
@@ -117,6 +134,16 @@ module Squishling
|
|
|
117
134
|
agent.apprentice(review) if agent.respond_to?(:apprentice)
|
|
118
135
|
end
|
|
119
136
|
|
|
137
|
+
# The resolved outer-loop agent's report(output:, metadata:, error:) as a callable, or nil when the agent doesn't
|
|
138
|
+
# define report: the one way output reaches an outer-loop agent, and only by its own choice. A Run sends it the
|
|
139
|
+
# same reports, under the same squawk_on: gate, as the squawk hook (and none when squawk is false for the method),
|
|
140
|
+
# calling it like the hook, with the keywords it declares (all of them when it takes **). It is called on the
|
|
141
|
+
# thread that makes the report, and the agent's exceptions are its own and propagate.
|
|
142
|
+
def reporter
|
|
143
|
+
agent = resolve(Squishling.config.outer_loop_agent)
|
|
144
|
+
agent.method(:report) if agent.respond_to?(:report)
|
|
145
|
+
end
|
|
146
|
+
|
|
120
147
|
# Tells the resolved outer-loop agent about a response that failed the output check (see Rejection), when it takes
|
|
121
148
|
# them (responds to invalid_output). Called on the thread that checked the response, before the failure is retried
|
|
122
149
|
# or raised. Its exceptions are its own and propagate.
|
data/lib/squishling/params.rb
CHANGED
|
@@ -7,10 +7,12 @@ module Squishling
|
|
|
7
7
|
|
|
8
8
|
# Request keys Squishling or the adapter own. Provider params are merged into the request alongside what the
|
|
9
9
|
# adapter builds, so these would silently replace the model, the conversation, or the strict output format.
|
|
10
|
+
# preset is Perplexity's: a preset replaces the model and its instructions (ruby_llm's Perplexity agent merges it
|
|
11
|
+
# before the provider options, so a param would win).
|
|
10
12
|
RESERVED_KEYS = %i[
|
|
11
13
|
model messages input inputs instructions contents system system_instruction systemInstruction cachedContent
|
|
12
14
|
cached_content stream stream_options store include response_format text output_config outputConfig tools
|
|
13
|
-
tool_choice toolConfig tool_config schema functions function_call
|
|
15
|
+
tool_choice toolConfig tool_config schema functions function_call preset
|
|
14
16
|
].freeze
|
|
15
17
|
|
|
16
18
|
# Some protocols carry the structured-output format (or tool selection) inside a container that is
|
|
@@ -9,6 +9,8 @@ module Squishling
|
|
|
9
9
|
# its elements, not as Ruby's inspect text. A container that contains itself is a ConfigurationError rather than a
|
|
10
10
|
# SystemStackError. Files are read as they are found, so max_files is checked as the count grows: the call is
|
|
11
11
|
# refused once the count passes the limit, though the file (or collection) that crosses it has been read by then.
|
|
12
|
+
# max_total_file_bytes is checked as the bytes grow, and before each read where the size is known (Attachment.read's
|
|
13
|
+
# room), so the file that would cross it is refused unread; a stream of unknown size is read to one byte past it.
|
|
12
14
|
class Normalizer
|
|
13
15
|
# JSON's own limit on nesting (JSON.generate raises past it), so a deeper input is refused here, before a walk
|
|
14
16
|
# that could overflow the stack, instead of there.
|
|
@@ -19,12 +21,15 @@ module Squishling
|
|
|
19
21
|
def initialize(label)
|
|
20
22
|
@label = label
|
|
21
23
|
@reads = 0
|
|
24
|
+
@bytes = 0
|
|
22
25
|
end
|
|
23
26
|
|
|
24
|
-
# Starts the count of files read against max_files again, for the
|
|
25
|
-
# the
|
|
27
|
+
# Starts the count of files, and of their bytes, read against max_files and max_total_file_bytes again, for the
|
|
28
|
+
# files a sanitizer returns: each pass is held to the limits on its own (the serializer checks the files the call
|
|
29
|
+
# sends in the end).
|
|
26
30
|
def restart_count
|
|
27
31
|
@reads = 0
|
|
32
|
+
@bytes = 0
|
|
28
33
|
end
|
|
29
34
|
|
|
30
35
|
# containers are the ones being walked, to catch one that contains itself. path is where value is, for an error.
|
|
@@ -79,8 +84,8 @@ module Squishling
|
|
|
79
84
|
|
|
80
85
|
# An Exception is sent as its class and message, the message as any String is.
|
|
81
86
|
def error(value, path)
|
|
82
|
-
{
|
|
83
|
-
|
|
87
|
+
{ Payload.key_name(:class) => exception_class_name(value.class),
|
|
88
|
+
Payload.key_name(:message) => text(value.message.to_s, Payload.join(path, :message)) }
|
|
84
89
|
end
|
|
85
90
|
|
|
86
91
|
def utf8(value, path)
|
|
@@ -102,7 +107,7 @@ module Squishling
|
|
|
102
107
|
def other(value, containers, path)
|
|
103
108
|
return members(value, containers, path) if open_struct?(value)
|
|
104
109
|
|
|
105
|
-
node = located(path) { Attachment.read(value, @label) }
|
|
110
|
+
node = located(path) { Attachment.read(value, @label, room:) }
|
|
106
111
|
return plain(value, containers, path) if Attachment::NOT_A_FILE.equal?(node)
|
|
107
112
|
|
|
108
113
|
count(node, path)
|
|
@@ -153,10 +158,20 @@ module Squishling
|
|
|
153
158
|
|
|
154
159
|
def count(node, path)
|
|
155
160
|
@reads += node.is_a?(Array) ? node.size : 1 unless node.nil?
|
|
161
|
+
@bytes += Attachment.bytes_of(node)
|
|
156
162
|
max = Squishling.config.max_files
|
|
157
|
-
|
|
163
|
+
if max && @reads > max
|
|
164
|
+
raise InputRejectedError.single("#{@reads} files is over max_files (#{max})", label: @label, path:)
|
|
165
|
+
end
|
|
166
|
+
return unless room&.negative?
|
|
167
|
+
|
|
168
|
+
raise InputRejectedError.single(Attachment.total_message("the call's files are"), label: @label, path:)
|
|
169
|
+
end
|
|
158
170
|
|
|
159
|
-
|
|
171
|
+
# What the files read so far leave of max_total_file_bytes (nil when it is lifted); negative once they are over.
|
|
172
|
+
def room
|
|
173
|
+
max = Squishling.config.max_total_file_bytes
|
|
174
|
+
max && (max - @bytes)
|
|
160
175
|
end
|
|
161
176
|
|
|
162
177
|
def elements(value, containers, path)
|
|
@@ -174,10 +189,10 @@ module Squishling
|
|
|
174
189
|
|
|
175
190
|
within(value, containers) do |inner|
|
|
176
191
|
pairs.each_with_object({}) do |(key, item), members|
|
|
177
|
-
name = key
|
|
192
|
+
name = Payload.key_name(key)
|
|
178
193
|
raise duplicate_key(path) if members.key?(name)
|
|
179
194
|
|
|
180
|
-
members[name] = call(item, inner, Payload.join(path,
|
|
195
|
+
members[name] = call(item, inner, Payload.join(path, name))
|
|
181
196
|
end
|
|
182
197
|
end
|
|
183
198
|
end
|
data/lib/squishling/payload.rb
CHANGED
|
@@ -7,9 +7,15 @@ module Squishling
|
|
|
7
7
|
# public API.
|
|
8
8
|
module Payload
|
|
9
9
|
SENDABLE = (Image::MEDIA_TYPES.keys + Document::MEDIA_TYPES.keys).freeze
|
|
10
|
-
# A Hash key
|
|
10
|
+
# A Hash key shaped like an identifier, which an error's path may quote as it is (see .join for which keys).
|
|
11
11
|
SAFE_KEY = /\A[A-Za-z_][A-Za-z0-9_-]{0,63}\z/
|
|
12
12
|
|
|
13
|
+
# A Hash key that began as a Symbol. Hash keys are Strings once normalized (JSON keys are), so this String marks the
|
|
14
|
+
# ones the developer named in code, which an error's path may quote (see .join). It is equal to the String of the
|
|
15
|
+
# same text, so a Hash behaves as it would with a plain String key. (A sanitizer sees these keys as this String
|
|
16
|
+
# subclass; what is sent is plain JSON.)
|
|
17
|
+
class ChosenKey < String; end
|
|
18
|
+
|
|
13
19
|
module_function
|
|
14
20
|
|
|
15
21
|
# The JSON body for tree (the call's { arguments:, context: }) and the files it sends, in order: each goes into
|
|
@@ -49,15 +55,23 @@ module Squishling
|
|
|
49
55
|
def serialize(node, label, files)
|
|
50
56
|
problems = []
|
|
51
57
|
body = serialized(node, files, problems, nil)
|
|
52
|
-
|
|
53
|
-
if max && files.size > max
|
|
54
|
-
problems << InputRejectedError::Rejection.new(nil, "#{files.size} files is over max_files (#{max})")
|
|
55
|
-
end
|
|
58
|
+
problems.concat(count_problems(files))
|
|
56
59
|
raise InputRejectedError.new(problems, label:) if problems.any?
|
|
57
60
|
|
|
58
61
|
body
|
|
59
62
|
end
|
|
60
63
|
|
|
64
|
+
# The files the call sends (each once) against max_files and max_total_file_bytes.
|
|
65
|
+
def count_problems(files)
|
|
66
|
+
messages = []
|
|
67
|
+
max = Squishling.config.max_files
|
|
68
|
+
messages << "#{files.size} files is over max_files (#{max})" if max && files.size > max
|
|
69
|
+
total = Squishling.config.max_total_file_bytes
|
|
70
|
+
bytes = files.sum { |file| file.bytesize.to_i }
|
|
71
|
+
messages << Attachment.total_message("the call's files total #{bytes} bytes, which is") if total && bytes > total
|
|
72
|
+
messages.map { |message| InputRejectedError::Rejection.new(nil, message) }
|
|
73
|
+
end
|
|
74
|
+
|
|
61
75
|
def serialized(node, files, problems, path)
|
|
62
76
|
case node
|
|
63
77
|
when Image, Document then { placeholder(node) => attach(node, files, problems, path) }
|
|
@@ -99,10 +113,20 @@ module Squishling
|
|
|
99
113
|
"#{file.bytesize} bytes is over max_file_bytes (#{max})" if max && file.bytesize && file.bytesize > max
|
|
100
114
|
end
|
|
101
115
|
|
|
102
|
-
#
|
|
103
|
-
#
|
|
116
|
+
# The String a Hash key becomes once normalized: a Symbol's name stays marked as a ChosenKey, anything else is
|
|
117
|
+
# its to_s, unmarked.
|
|
118
|
+
def key_name(key)
|
|
119
|
+
return key if key.is_a?(ChosenKey)
|
|
120
|
+
|
|
121
|
+
key.is_a?(Symbol) ? ChosenKey.new(key.name).freeze : key.to_s
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
# path extended by a Hash key, for an error message. Only a Symbol key (or a ChosenKey: one that began as a Symbol)
|
|
125
|
+
# shaped like an identifier is quoted. Every other key is masked as [key], since a Hash keyed by data (an MRN, a
|
|
126
|
+
# name, even a plain "jane_doe") would put that data in the message, and error trackers collect messages.
|
|
104
127
|
def join(path, key)
|
|
105
|
-
|
|
128
|
+
quotable = (key.is_a?(Symbol) || key.is_a?(ChosenKey)) && SAFE_KEY.match?(key)
|
|
129
|
+
return path ? "#{path}.#{key}" : key.to_s if quotable
|
|
106
130
|
|
|
107
131
|
"#{path}[key]"
|
|
108
132
|
end
|
data/lib/squishling/result.rb
CHANGED
|
@@ -45,8 +45,8 @@ module Squishling
|
|
|
45
45
|
end
|
|
46
46
|
|
|
47
47
|
class << self
|
|
48
|
-
def build(
|
|
49
|
-
Builder.new
|
|
48
|
+
def build(validator)
|
|
49
|
+
Builder.new.class_for(validator)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
def deep_to_h(value)
|
|
@@ -58,31 +58,48 @@ module Squishling
|
|
|
58
58
|
end
|
|
59
59
|
end
|
|
60
60
|
|
|
61
|
-
#
|
|
61
|
+
# Builds (memoized, so recursive schemas work) one Data class per object schema and one converter per array schema,
|
|
62
|
+
# from the validator's parsed schema, where every $ref is already resolved and none only leads back to itself (see
|
|
63
|
+
# Schema#refuse_endless!).
|
|
62
64
|
class Builder
|
|
63
65
|
IDENTITY = ->(value, _squished) { value.is_a?(Hash) ? value.transform_keys(&:to_sym) : value }
|
|
64
66
|
|
|
65
|
-
def initialize
|
|
66
|
-
@root = root
|
|
67
|
+
def initialize
|
|
67
68
|
@classes = {}.compare_by_identity
|
|
69
|
+
@array_converters = {}.compare_by_identity
|
|
70
|
+
@canonical = {}.compare_by_identity
|
|
71
|
+
@by_place = {}
|
|
68
72
|
end
|
|
69
73
|
|
|
70
|
-
# Returns a Data class, or nil when the schema isn't an object with properties
|
|
74
|
+
# Returns a Data class, or nil when the schema isn't an object with properties: that of the first object a chain
|
|
75
|
+
# of $refs leads to.
|
|
71
76
|
def class_for(schema)
|
|
72
|
-
|
|
73
|
-
|
|
77
|
+
target = resolve(schema)
|
|
78
|
+
object_class(target) || (class_for(target) unless target.equal?(schema))
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
private
|
|
74
82
|
|
|
83
|
+
def object_class(schema)
|
|
84
|
+
properties = schema.parsed["properties"]
|
|
85
|
+
return nil unless properties&.value.is_a?(Hash)
|
|
86
|
+
|
|
87
|
+
schema = canonical(schema)
|
|
75
88
|
return @classes[schema] if @classes.key?(schema)
|
|
76
89
|
|
|
77
|
-
require_object_type!(schema)
|
|
78
|
-
klass = @classes[schema] = define_class(schema)
|
|
90
|
+
require_object_type!(schema.value)
|
|
91
|
+
klass = @classes[schema] = define_class(schema.value)
|
|
79
92
|
# Memoized first so recursive schemas stop; typing the fields builds nested classes now, so a bad
|
|
80
93
|
# property name in any object the result types (nested, array items, resolved $refs) raises at schema build.
|
|
81
|
-
klass.instance_variable_set(:@squishling_fields, fields_for(
|
|
94
|
+
klass.instance_variable_set(:@squishling_fields, fields_for(properties.parsed))
|
|
82
95
|
klass
|
|
83
96
|
end
|
|
84
97
|
|
|
85
|
-
|
|
98
|
+
# json_schemer parses a schema again for each way a pointer reaches it through a keyword it doesn't know (such as
|
|
99
|
+
# `components`). Those copies are told by where they are, and by value in case two resources declare the same $id.
|
|
100
|
+
def canonical(schema)
|
|
101
|
+
@canonical[schema] ||= (@by_place[[schema.absolute_keyword_location, schema.value]] ||= schema)
|
|
102
|
+
end
|
|
86
103
|
|
|
87
104
|
# `properties` alone constrains objects only, so a schema without `type: "object"` accepts a String or Array
|
|
88
105
|
# that would then sit in the typed result where a nested result is expected. A raw Hash schema must say so.
|
|
@@ -96,8 +113,8 @@ module Squishling
|
|
|
96
113
|
"(a union such as [\"object\", \"null\"] is also fine)"
|
|
97
114
|
end
|
|
98
115
|
|
|
99
|
-
def fields_for(
|
|
100
|
-
|
|
116
|
+
def fields_for(properties)
|
|
117
|
+
properties.to_h { |key, property| [key.to_sym, converter_for(property)] }
|
|
101
118
|
end
|
|
102
119
|
|
|
103
120
|
def define_class(schema)
|
|
@@ -105,6 +122,10 @@ module Squishling
|
|
|
105
122
|
klass.include(Instance)
|
|
106
123
|
klass.extend(ClassMethods)
|
|
107
124
|
klass
|
|
125
|
+
rescue ArgumentError => e
|
|
126
|
+
# Data.define refuses a name that can't be a member ("a="), with its own message and no hint at the cause.
|
|
127
|
+
raise ConfigurationError,
|
|
128
|
+
"output_schema has a property name that can't be a result member (#{e.message}). Rename the field", cause: nil
|
|
108
129
|
end
|
|
109
130
|
|
|
110
131
|
def property_names(schema)
|
|
@@ -117,39 +138,71 @@ module Squishling
|
|
|
117
138
|
"Rename the field(s) (e.g. `class` to `ticket_class`)"
|
|
118
139
|
end
|
|
119
140
|
|
|
120
|
-
def converter_for(property)
|
|
121
|
-
property = resolve(property)
|
|
122
|
-
return IDENTITY unless property.is_a?(Hash)
|
|
141
|
+
def converter_for(property) = typed_converter(property, {}.compare_by_identity) || IDENTITY
|
|
123
142
|
|
|
143
|
+
# The converter of the first schema along a property's chain of $refs that types anything, or nil. `properties`
|
|
144
|
+
# next to a $ref count only when a longer chain leads to them: where the property has, or directly references,
|
|
145
|
+
# such a schema, what that $ref leads to is tried instead.
|
|
146
|
+
#
|
|
147
|
+
# chain holds the ones visited, none of which types anything if the walk gets back to it: two unions can share a
|
|
148
|
+
# branch, and one can lead back to itself when a discriminator picks its branch.
|
|
149
|
+
def typed_converter(property, chain)
|
|
150
|
+
return if chain.key?(property)
|
|
151
|
+
|
|
152
|
+
chain[property] = true
|
|
153
|
+
schema = resolve(property)
|
|
154
|
+
own = own_converter(schema, chain)
|
|
155
|
+
own || schema.equal?(property) ? own : typed_converter(schema, chain)
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def own_converter(schema, chain)
|
|
124
159
|
# `optional` (anyOf [schema, null]) is typed as its schema; nil passes through every converter.
|
|
125
|
-
if (branch = nullable_branch(
|
|
126
|
-
|
|
127
|
-
elsif (klass =
|
|
160
|
+
if (branch = nullable_branch(schema))
|
|
161
|
+
typed_converter(branch, chain)
|
|
162
|
+
elsif (klass = object_class(resolve(schema)))
|
|
128
163
|
->(value, squished) { value.is_a?(Hash) ? klass.from_h(value, squished:) : value }
|
|
129
|
-
elsif
|
|
130
|
-
|
|
131
|
-
->(value, squished) { value.is_a?(Array) ? value.map { |v| item.call(v, squished) } : value }
|
|
132
|
-
else
|
|
133
|
-
IDENTITY
|
|
164
|
+
elsif schema.parsed["items"]&.value
|
|
165
|
+
array_converter(schema)
|
|
134
166
|
end
|
|
135
167
|
end
|
|
136
168
|
|
|
169
|
+
# Memoized before its items are typed, so an array whose items lead back to it stops too.
|
|
170
|
+
def array_converter(schema)
|
|
171
|
+
schema = canonical(schema)
|
|
172
|
+
return @array_converters[schema] if @array_converters.key?(schema)
|
|
173
|
+
|
|
174
|
+
item = IDENTITY
|
|
175
|
+
converter = ->(value, squished) { value.is_a?(Array) ? value.map { |v| item.call(v, squished) } : value }
|
|
176
|
+
@array_converters[schema] = converter
|
|
177
|
+
items = schema.parsed["items"].parsed
|
|
178
|
+
# An older draft's tuple is an Array of schemas, left untyped.
|
|
179
|
+
item = converter_for(items) if items.is_a?(JSONSchemer::Schema)
|
|
180
|
+
converter
|
|
181
|
+
end
|
|
182
|
+
|
|
137
183
|
# The single non-null branch of an anyOf/oneOf. Unions with several non-null branches are
|
|
138
184
|
# ambiguous, so their values are left untyped.
|
|
139
|
-
def nullable_branch(
|
|
140
|
-
union =
|
|
141
|
-
return unless union
|
|
185
|
+
def nullable_branch(schema)
|
|
186
|
+
union = schema.parsed["anyOf"] || schema.parsed["oneOf"]
|
|
187
|
+
return unless union
|
|
142
188
|
|
|
143
|
-
branches = union.reject { |branch|
|
|
189
|
+
branches = union.parsed.reject { |branch| null?(branch) }
|
|
144
190
|
branches.first if branches.size == 1
|
|
145
191
|
end
|
|
146
192
|
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
193
|
+
# Whether a schema, or the one its $refs lead to when it has no type of its own, is `type: "null"`.
|
|
194
|
+
def null?(schema)
|
|
195
|
+
target = resolve(schema)
|
|
196
|
+
type = target.parsed["type"]
|
|
197
|
+
return type.value == "null" if type
|
|
198
|
+
|
|
199
|
+
!target.equal?(schema) && null?(target)
|
|
200
|
+
end
|
|
150
201
|
|
|
151
|
-
|
|
152
|
-
|
|
202
|
+
# The schema a local $ref points at (one hop), or the schema itself.
|
|
203
|
+
def resolve(schema)
|
|
204
|
+
ref = schema.parsed["$ref"]
|
|
205
|
+
ref&.value&.start_with?("#/") ? ref.ref_schema : schema
|
|
153
206
|
end
|
|
154
207
|
end
|
|
155
208
|
end
|
|
@@ -2,15 +2,73 @@
|
|
|
2
2
|
|
|
3
3
|
module Squishling
|
|
4
4
|
class Run
|
|
5
|
-
# What a Run does with a reply it rejects
|
|
6
|
-
#
|
|
7
|
-
#
|
|
5
|
+
# What a Run does with a reply it rejects, or a sample's request that failed (a sample round checks every sample
|
|
6
|
+
# first, then reports them): the attempt that follows, the error it raises, the reason it logs, and the token budget
|
|
7
|
+
# that can end the call before another request. A model that sends back the reply it was just told was invalid, in
|
|
8
|
+
# the same chat, would do it again, so a repeated reply gives up on the rest of its step's attempts, and so does a
|
|
9
|
+
# rate limit, which the same model would hit again.
|
|
8
10
|
module Rejections
|
|
9
11
|
private
|
|
10
12
|
|
|
11
|
-
#
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
# Checks one sample's outcome without calling a hook (so the round can tell whether it ends the call before it
|
|
14
|
+
# reports any sample): runs the output check, and sets up the retry of a sample that has an attempt left (a
|
|
15
|
+
# setup #settle discards when another sample's terminal outcome ends the call).
|
|
16
|
+
def check(sample, (response, failure), attempt)
|
|
17
|
+
stage = sample.stage
|
|
18
|
+
last = attempt == stage.size
|
|
19
|
+
if failure
|
|
20
|
+
sample.chat = nil
|
|
21
|
+
sample.retry_reason = failure.message
|
|
22
|
+
# A rate limit ends a sample at once: a sample is one model, and the same model would hit it again.
|
|
23
|
+
return Check.new(sample, nil, failure, false, last || failure.is_a?(RateLimitError))
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
result, errors = @output_check.call(response.content, @prompt)
|
|
27
|
+
if errors.empty?
|
|
28
|
+
# A pair, since a valid result can be false or nil (a boolean or nullable schema).
|
|
29
|
+
sample.accepted = [result, @client.origin_of(response)]
|
|
30
|
+
return Check.new(sample, response, nil, false, false)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A sample is one step (#samples refuses a stage that mixes them), so a repeat leaves nothing to move on to.
|
|
34
|
+
repeated = sample.reask && response.content == sample.rejected.first
|
|
35
|
+
invalid = invalid_output(response, errors, attempt, stage.first(attempt).map(&:model))
|
|
36
|
+
unless last || repeated
|
|
37
|
+
sample.rejected = [response.content, invalid.errors]
|
|
38
|
+
sample.retry_reason = rejection_reason(invalid.errors, response)
|
|
39
|
+
end
|
|
40
|
+
Check.new(sample, response, invalid, repeated, last || repeated)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
# Reports a checked sample to squawk and the outer-loop agent. A rejected or failed sample is final when it is
|
|
44
|
+
# terminal, or when another sample's terminal outcome ends the call (ending): no attempt of its own will follow.
|
|
45
|
+
# An accepted one is done, and final only if it was its last attempt.
|
|
46
|
+
def report(checked, attempt, ending)
|
|
47
|
+
sample = checked.sample
|
|
48
|
+
stage = sample.stage
|
|
49
|
+
error = checked.error
|
|
50
|
+
final = error ? checked.terminal || ending : attempt == stage.size
|
|
51
|
+
squawk(@prompt, stage, attempt, checked.response, error, sample:, reask: sample.reask,
|
|
52
|
+
repeated: checked.repeated, final:)
|
|
53
|
+
return unless error && checked.response
|
|
54
|
+
|
|
55
|
+
report_rejection(checked.response, error, stage, attempt, role: :sample, sample: sample.name)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Ends a round whose samples are all checked and reported: raises the first sample's terminal error, or logs
|
|
59
|
+
# the retry of each sample that needs one (so a call that is ending logs no retry it won't make).
|
|
60
|
+
def settle(pending, errors, attempt)
|
|
61
|
+
raise errors.first unless errors.empty?
|
|
62
|
+
|
|
63
|
+
pending.reject(&:accepted).each do |sample|
|
|
64
|
+
log_failure("sample #{sample.name}", attempt, sample.stage, sample.retry_reason)
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
# The number of the attempt after attempt in path, or nil when none is left. skip_step: leave the rest of the
|
|
69
|
+
# step's attempts out (a repeated reply, or a rate limit), so the next one is on another step.
|
|
70
|
+
def next_attempt(path, attempt, skip_step)
|
|
71
|
+
return (attempt + 1 if attempt < path.size) unless skip_step
|
|
14
72
|
|
|
15
73
|
skipped = path.drop(attempt).index { |step| step != path[attempt - 1] }
|
|
16
74
|
attempt + skipped + 1 if skipped
|
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
module Squishling
|
|
4
4
|
class Run
|
|
5
|
-
# What a Run tells the squawk hook (see Squawk)
|
|
6
|
-
# a token budget or a
|
|
5
|
+
# What a Run tells the squawk hook (see Squawk) and an outer-loop agent that defines report (see
|
|
6
|
+
# OuterLoopAgent.reporter): the report of each attempt, and the one final report of a call that a token budget or a
|
|
7
|
+
# disagreement stopped. Both get the same report, under the same squawk_on: gate.
|
|
7
8
|
module Reports
|
|
8
9
|
# Set on a stop error once it has been reported (see #report_stop).
|
|
9
10
|
REPORTED = :@squawk_reported
|
|
@@ -15,11 +16,13 @@ module Squishling
|
|
|
15
16
|
# output, and the provider and params are unknown (the stop comes from the harness, not from a step).
|
|
16
17
|
# Internal: Invoker calls it once, whatever the harness, so a custom harness has nothing to do.
|
|
17
18
|
def report_stop(error)
|
|
18
|
-
hook = @definition.squawk
|
|
19
19
|
attempts = error.attempts.to_i
|
|
20
|
-
return if error.instance_variable_get(REPORTED)
|
|
20
|
+
return if error.instance_variable_get(REPORTED)
|
|
21
21
|
return unless Squawk.report?(@definition.squawk_on, attempts, error, true)
|
|
22
22
|
|
|
23
|
+
sinks = report_sinks
|
|
24
|
+
return if sinks.empty?
|
|
25
|
+
|
|
23
26
|
# Marked first, so an error that comes back through an outer squished call (or a second report_stop) is not
|
|
24
27
|
# reported again, and a hook that raises doesn't get the same error again either.
|
|
25
28
|
error.instance_variable_set(REPORTED, true)
|
|
@@ -27,11 +30,21 @@ module Squishling
|
|
|
27
30
|
attempt: attempts, attempts:, final: true, model: error.models&.last, input: @prompt.input,
|
|
28
31
|
stopped: error.is_a?(BudgetExhaustedError) ? :budget : :disagreement
|
|
29
32
|
)
|
|
30
|
-
|
|
33
|
+
deliver(sinks, output: nil, metadata:, error:)
|
|
31
34
|
end
|
|
32
35
|
|
|
33
36
|
private
|
|
34
37
|
|
|
38
|
+
# Where a report goes: the squawk hook, then the outer-loop agent's report when it defines one and squawk isn't
|
|
39
|
+
# silenced for this method. Both are callables taking the report's keywords.
|
|
40
|
+
def report_sinks
|
|
41
|
+
[@definition.squawk, (OuterLoopAgent.reporter unless @definition.squawk_silenced?)].compact
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def deliver(sinks, **report)
|
|
45
|
+
sinks.each { |sink| Squawk.call(sink, **report) }
|
|
46
|
+
end
|
|
47
|
+
|
|
35
48
|
# Runs the observability hook, if any, with this attempt's raw output (nil when the call itself failed),
|
|
36
49
|
# the error that ended it (nil when it was accepted), and what was asked. role is nil for escalation
|
|
37
50
|
# attempts, :sample (with the sample's name) or :judge. reask is whether the attempt retried in the same chat,
|
|
@@ -39,8 +52,10 @@ module Squishling
|
|
|
39
52
|
# follows it.
|
|
40
53
|
def squawk(prompt, path, attempt, response, error, sample: nil, reask: false, repeated: false,
|
|
41
54
|
final: attempt == path.size)
|
|
42
|
-
|
|
43
|
-
|
|
55
|
+
return unless Squawk.report?(@definition.squawk_on, attempt, error, final)
|
|
56
|
+
|
|
57
|
+
sinks = report_sinks
|
|
58
|
+
return if sinks.empty?
|
|
44
59
|
|
|
45
60
|
step = path[attempt - 1]
|
|
46
61
|
metadata = squawk_metadata(
|
|
@@ -48,7 +63,7 @@ module Squishling
|
|
|
48
63
|
provider: step.provider, params: step.request_params(reask), input: prompt.input, response:,
|
|
49
64
|
role: sample ? :sample : prompt.role, sample: sample&.name
|
|
50
65
|
)
|
|
51
|
-
|
|
66
|
+
deliver(sinks, output: response&.content, metadata:, error:)
|
|
52
67
|
end
|
|
53
68
|
|
|
54
69
|
def squawk_metadata(attempt:, attempts:, final:, model:, input:, repeated: false, provider: nil, params: nil,
|