judgelint 0.1.9 → 0.2.0
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 +33 -0
- data/README.md +9 -11
- data/exe/judgelint +9 -9
- metadata +3 -25
- data/lib/judgelint/cache.rb +0 -53
- data/lib/judgelint/changes.rb +0 -87
- data/lib/judgelint/cli.rb +0 -251
- data/lib/judgelint/client.rb +0 -186
- data/lib/judgelint/config.rb +0 -162
- data/lib/judgelint/errors.rb +0 -14
- data/lib/judgelint/glob.rb +0 -74
- data/lib/judgelint/paint.rb +0 -46
- data/lib/judgelint/pool.rb +0 -41
- data/lib/judgelint/protocol.rb +0 -122
- data/lib/judgelint/providers.rb +0 -78
- data/lib/judgelint/question.rb +0 -148
- data/lib/judgelint/reporter.rb +0 -188
- data/lib/judgelint/rule.rb +0 -167
- data/lib/judgelint/runner.rb +0 -400
- data/lib/judgelint/scaffold.rb +0 -66
- data/lib/judgelint/source.rb +0 -82
- data/lib/judgelint/spec.rb +0 -240
- data/lib/judgelint/usage.rb +0 -53
- data/lib/judgelint/verdict.rb +0 -65
- data/lib/judgelint/version.rb +0 -5
- data/lib/judgelint.rb +0 -61
data/lib/judgelint/client.rb
DELETED
|
@@ -1,186 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# Asks one model (an Endpoint) the questions over Net::HTTP, in whichever
|
|
5
|
-
# protocol its provider speaks. Plain Net::HTTP rather than a gem keeps the
|
|
6
|
-
# same code compiling with Spinel.
|
|
7
|
-
class Client
|
|
8
|
-
RETRY_STATUSES = %w[408 429 500 502 503 504 529].freeze
|
|
9
|
-
# The native binary links OpenSSL statically, so it looks for certificates
|
|
10
|
-
# where OpenSSL was built. Unless SSL_CERT_FILE says otherwise, it uses the
|
|
11
|
-
# system's bundle: macOS and Alpine, Debian and Ubuntu, then Fedora and RHEL.
|
|
12
|
-
CA_BUNDLES = %w[/etc/ssl/cert.pem /etc/ssl/certs/ca-certificates.crt /etc/pki/tls/certs/ca-bundle.crt].freeze
|
|
13
|
-
|
|
14
|
-
attr_reader :usage, :endpoint
|
|
15
|
-
|
|
16
|
-
def initialize(endpoint, usage: Usage.new, sleeper: ->(seconds) { sleep(seconds) })
|
|
17
|
-
if RUBY_ENGINE == "spinel" && ENV.fetch("SSL_CERT_FILE", "").empty?
|
|
18
|
-
bundle = CA_BUNDLES.find { |path| File.file?(path) }
|
|
19
|
-
ENV["SSL_CERT_FILE"] = bundle if bundle
|
|
20
|
-
end
|
|
21
|
-
@endpoint = endpoint
|
|
22
|
-
@protocol = Protocol::ALL.fetch(endpoint.protocol) do
|
|
23
|
-
raise ConfigError, "protocol is system_one or decisions, not #{endpoint.protocol}"
|
|
24
|
-
end
|
|
25
|
-
@usage = usage
|
|
26
|
-
@sleeper = sleeper
|
|
27
|
-
check_settings
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
# Returns the probability of "yes" for each question, keyed by its key,
|
|
31
|
-
# or nil for a question the model refused.
|
|
32
|
-
def judge(state, questions)
|
|
33
|
-
data = self.class.unwrap(post(JSON.generate(payload(state, questions))))
|
|
34
|
-
@usage.record(data, @endpoint.price_per_million)
|
|
35
|
-
@protocol.parse(data, questions)
|
|
36
|
-
end
|
|
37
|
-
|
|
38
|
-
def payload(state, questions)
|
|
39
|
-
@protocol.payload(@endpoint.model, state, questions).merge(@endpoint.provider_options)
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
# A conservative token count for code: three bytes a token.
|
|
43
|
-
def tokens(state, questions)
|
|
44
|
-
JSON.generate(payload(state, questions)).bytesize / 3
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# What one more question adds to a request, without serializing the file again.
|
|
48
|
-
def question_tokens(question)
|
|
49
|
-
(JSON.generate(@protocol.render(question)).bytesize + question.key.bytesize + 6) / 3
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
def url
|
|
53
|
-
base = Config.expand(@endpoint.api_base)
|
|
54
|
-
path = Config.expand(@endpoint.path).gsub("{model}", @endpoint.model.to_s)
|
|
55
|
-
"#{base.delete_suffix('/')}/#{path.delete_prefix('/')}"
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# Cloudflare wraps the answers as {result: {...}, success, errors}.
|
|
59
|
-
def self.unwrap(data)
|
|
60
|
-
raise ApiError, "Response is not a JSON object" unless data.is_a?(Hash)
|
|
61
|
-
|
|
62
|
-
result = data["result"]
|
|
63
|
-
!data.key?("answers") && result.is_a?(Hash) && result.key?("answers") ? result : data
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
# The error message, whichever of the shapes providers use: TypeSafe's
|
|
67
|
-
# detail (a string, {message}, or a validation list), OpenAI's
|
|
68
|
-
# {error: {message}}, Cloudflare's errors list, or a bare message.
|
|
69
|
-
def self.error_detail(body)
|
|
70
|
-
data = JSON.parse(body)
|
|
71
|
-
return body[0, 200] unless data.is_a?(Hash)
|
|
72
|
-
|
|
73
|
-
detail = data["detail"] || data["error"] || data["errors"] || data["message"]
|
|
74
|
-
message(detail) || body[0, 200]
|
|
75
|
-
rescue JSON::ParserError
|
|
76
|
-
body[0, 200]
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
def self.message(detail)
|
|
80
|
-
case detail
|
|
81
|
-
when String then detail.empty? ? nil : detail
|
|
82
|
-
when Hash then message(detail["message"] || detail["msg"])
|
|
83
|
-
when Array
|
|
84
|
-
messages = detail.map { |item| message(item) }.compact
|
|
85
|
-
messages.empty? ? nil : messages.join("; ")
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# Called before the first request, so a run the cache answers needs no key.
|
|
90
|
-
def check_key
|
|
91
|
-
raise ConfigError, "Set #{@endpoint.api_key_env} to your #{@endpoint.provider} API key" if key_missing?
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
private
|
|
95
|
-
|
|
96
|
-
def check_settings
|
|
97
|
-
provider = @endpoint.provider
|
|
98
|
-
if @endpoint.api_base.to_s.empty?
|
|
99
|
-
raise ConfigError, "Provider #{provider} needs #{@endpoint.needs || 'api_base'} in .judgelint.yml"
|
|
100
|
-
end
|
|
101
|
-
raise ConfigError, "Provider #{provider} needs a model in .judgelint.yml" if model_needed?
|
|
102
|
-
|
|
103
|
-
unless %w[bearer basic none].include?(@endpoint.auth) || @endpoint.auth.start_with?("header:")
|
|
104
|
-
raise ConfigError, "auth is bearer, basic, none, or header:<name>, not #{@endpoint.auth}"
|
|
105
|
-
end
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
# llama.cpp serves one model and doesn't need its name; nothing else can guess it.
|
|
109
|
-
def model_needed?
|
|
110
|
-
@endpoint.model.to_s.empty? && @endpoint.provider != "llamacpp"
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def key_missing?
|
|
114
|
-
@endpoint.auth != "none" && !@endpoint.api_key_env.to_s.empty? && @endpoint.api_key.to_s.empty?
|
|
115
|
-
end
|
|
116
|
-
|
|
117
|
-
def post(body)
|
|
118
|
-
attempt = 0
|
|
119
|
-
loop do
|
|
120
|
-
attempt += 1
|
|
121
|
-
response = request(body, attempt)
|
|
122
|
-
next if response.nil?
|
|
123
|
-
|
|
124
|
-
code = response.code
|
|
125
|
-
return parse_json(response.body.to_s) if code.start_with?("2")
|
|
126
|
-
|
|
127
|
-
detail = self.class.error_detail(response.body.to_s)
|
|
128
|
-
if RETRY_STATUSES.include?(code) && attempt <= Endpoint::RETRIES
|
|
129
|
-
@sleeper.call(retry_delay(response, attempt))
|
|
130
|
-
next
|
|
131
|
-
end
|
|
132
|
-
raise ApiError, "#{@endpoint.label} answered #{code}: #{detail}"
|
|
133
|
-
end
|
|
134
|
-
end
|
|
135
|
-
|
|
136
|
-
# Answers nil after a connection error that's worth another try.
|
|
137
|
-
def request(body, attempt)
|
|
138
|
-
uri = URI(url)
|
|
139
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
|
140
|
-
http.use_ssl = uri.scheme == "https"
|
|
141
|
-
http.open_timeout = Endpoint::TIMEOUT
|
|
142
|
-
http.read_timeout = Endpoint::TIMEOUT
|
|
143
|
-
|
|
144
|
-
request = Net::HTTP::Post.new(uri.request_uri)
|
|
145
|
-
request["Content-Type"] = "application/json"
|
|
146
|
-
request["Accept"] = "application/json"
|
|
147
|
-
authorize(request)
|
|
148
|
-
@endpoint.headers.each { |name, value| request[name.to_s] = Config.expand(value.to_s) }
|
|
149
|
-
request.body = body
|
|
150
|
-
http.request(request)
|
|
151
|
-
rescue Judgelint::Error
|
|
152
|
-
raise
|
|
153
|
-
rescue StandardError => e
|
|
154
|
-
# Nothing listening isn't going to change in a few seconds; a dropped or
|
|
155
|
-
# stalled connection might.
|
|
156
|
-
refused = e.is_a?(Errno::ECONNREFUSED) || e.message.include?("Connection refused")
|
|
157
|
-
raise ApiError, "Couldn't reach #{@endpoint.label} at #{url}: #{e.message}" if refused || attempt > Endpoint::RETRIES
|
|
158
|
-
|
|
159
|
-
@sleeper.call(2.0**attempt)
|
|
160
|
-
nil
|
|
161
|
-
end
|
|
162
|
-
|
|
163
|
-
def authorize(request)
|
|
164
|
-
key = @endpoint.api_key
|
|
165
|
-
return if key.to_s.empty?
|
|
166
|
-
|
|
167
|
-
case @endpoint.auth
|
|
168
|
-
when "bearer" then request["Authorization"] = "Bearer #{key}"
|
|
169
|
-
when "basic" then request["Authorization"] = "Basic #{["token:#{key}"].pack("m0")}"
|
|
170
|
-
when "none" then nil
|
|
171
|
-
else request[@endpoint.auth.delete_prefix("header:")] = key
|
|
172
|
-
end
|
|
173
|
-
end
|
|
174
|
-
|
|
175
|
-
def retry_delay(response, attempt)
|
|
176
|
-
after = Judgelint.number(response["Retry-After"])
|
|
177
|
-
after ? [after, 30.0].min : 0.5 * (2**(attempt - 1))
|
|
178
|
-
end
|
|
179
|
-
|
|
180
|
-
def parse_json(body)
|
|
181
|
-
JSON.parse(body)
|
|
182
|
-
rescue JSON::ParserError
|
|
183
|
-
raise ApiError, "#{@endpoint.label} answered with something other than JSON: #{body[0, 200]}"
|
|
184
|
-
end
|
|
185
|
-
end
|
|
186
|
-
end
|
data/lib/judgelint/config.rb
DELETED
|
@@ -1,162 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# .judgelint.yml, which is optional: which models to ask, the threshold,
|
|
5
|
-
# files to ignore, and a spending limit. Everything else has a fixed value.
|
|
6
|
-
#
|
|
7
|
-
# models: [typesafe, cloudflare:clef] # ask both and average
|
|
8
|
-
# threshold: 0.7
|
|
9
|
-
# ignore: ["vendor/**"]
|
|
10
|
-
# max_cost_usd: 1.00
|
|
11
|
-
class Config
|
|
12
|
-
FILE = ".judgelint.yml"
|
|
13
|
-
KEYS = %w[models threshold ignore max_cost_usd].freeze
|
|
14
|
-
RULES_DIR = ".judgelint/rules"
|
|
15
|
-
CACHE = ".judgelint/cache.json"
|
|
16
|
-
WINDOW_LINES = 40
|
|
17
|
-
THREADS = 8
|
|
18
|
-
# Answers this far below the threshold, or less, are uncertain: reported, never failing.
|
|
19
|
-
UNCERTAIN_BAND = 0.3
|
|
20
|
-
|
|
21
|
-
attr_reader :models, :threshold, :ignore, :max_cost_usd
|
|
22
|
-
|
|
23
|
-
# +overrides+ replaces the file's settings: --model replaces its models.
|
|
24
|
-
def self.load(path: nil, overrides: {})
|
|
25
|
-
new(Judgelint.read_yaml(path || FILE, required: !path.nil?).merge(overrides))
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
# ${VAR} reads the environment, so a URL can carry an account id. The
|
|
29
|
-
# client expands it when it sends, so commands that make no request don't
|
|
30
|
-
# need every variable set.
|
|
31
|
-
def self.expand(value)
|
|
32
|
-
value.gsub(/\$\{[A-Za-z_][A-Za-z0-9_]*\}/) do |reference|
|
|
33
|
-
name = reference[2..-2]
|
|
34
|
-
found = ENV.fetch(name, "")
|
|
35
|
-
raise ConfigError, "Set #{name} (used in #{value})" if found.empty?
|
|
36
|
-
|
|
37
|
-
found
|
|
38
|
-
end
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def initialize(data)
|
|
42
|
-
unknown = data.keys - KEYS
|
|
43
|
-
raise ConfigError, "#{FILE}: unknown keys #{unknown.join(', ')} (it takes #{KEYS.join(', ')})" unless unknown.empty?
|
|
44
|
-
|
|
45
|
-
entries = data.fetch("models", "typesafe")
|
|
46
|
-
entries = [entries] unless entries.is_a?(Array)
|
|
47
|
-
raise ConfigError, "models needs at least one model" if entries.empty?
|
|
48
|
-
|
|
49
|
-
@models = entries.map { |entry| Endpoint.from(entry) }
|
|
50
|
-
@threshold = Judgelint.number(data.key?("threshold") ? data["threshold"] : 0.7)
|
|
51
|
-
raise ConfigError, "threshold must be a number between 0 and 1" unless @threshold&.between?(0.01, 1)
|
|
52
|
-
|
|
53
|
-
@ignore = list(data["ignore"])
|
|
54
|
-
@max_cost_usd = Judgelint.number(data.key?("max_cost_usd") ? data["max_cost_usd"] : 1)
|
|
55
|
-
raise ConfigError, "max_cost_usd must be a number" if @max_cost_usd.nil?
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
def warn_at
|
|
59
|
-
[threshold - UNCERTAIN_BAND, 0.0].max
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def label
|
|
63
|
-
models.map(&:label).join(" + ")
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
private
|
|
67
|
-
|
|
68
|
-
def list(value)
|
|
69
|
-
return [] if value.nil?
|
|
70
|
-
|
|
71
|
-
value.is_a?(Array) ? value.map(&:to_s) : [value.to_s]
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
# One model to ask, and how to reach it: a provider preset and model id
|
|
76
|
-
# ("typesafe", "cloudflare:clef", "openrouter:typesafe/jev-1.13"), or a
|
|
77
|
-
# mapping for an endpoint judgelint has no preset for.
|
|
78
|
-
class Endpoint
|
|
79
|
-
KEYS = %w[
|
|
80
|
-
provider model api_base path protocol auth api_key_env headers provider_options
|
|
81
|
-
price_per_million batch_size max_state_tokens max_request_tokens
|
|
82
|
-
].freeze
|
|
83
|
-
TIMEOUT = 30
|
|
84
|
-
RETRIES = 3
|
|
85
|
-
|
|
86
|
-
attr_reader :provider, :model, :api_base, :path, :protocol, :auth, :api_key_env, :headers,
|
|
87
|
-
:provider_options, :price_per_million, :batch_size, :max_state_tokens, :max_request_tokens, :needs
|
|
88
|
-
|
|
89
|
-
def self.from(entry)
|
|
90
|
-
return new(entry) if entry.is_a?(Hash)
|
|
91
|
-
|
|
92
|
-
provider, model = entry.to_s.split(":", 2)
|
|
93
|
-
data = { "provider" => provider }
|
|
94
|
-
data["model"] = model if model && !model.empty?
|
|
95
|
-
new(data)
|
|
96
|
-
end
|
|
97
|
-
|
|
98
|
-
def initialize(data)
|
|
99
|
-
unknown = data.keys - KEYS
|
|
100
|
-
raise ConfigError, "models: unknown keys #{unknown.join(', ')}" unless unknown.empty?
|
|
101
|
-
|
|
102
|
-
@data = data
|
|
103
|
-
@provider = data.fetch("provider", "custom").to_s
|
|
104
|
-
@preset = PROVIDERS.fetch(@provider, {})
|
|
105
|
-
@model = setting("model")
|
|
106
|
-
@api_base = setting("api_base")
|
|
107
|
-
@path = setting("path") || "/v1/systemone"
|
|
108
|
-
@protocol = setting("protocol") || "system_one"
|
|
109
|
-
@auth = setting("auth") || "bearer"
|
|
110
|
-
@api_key_env = setting("api_key_env")
|
|
111
|
-
@headers = mapping("headers")
|
|
112
|
-
@provider_options = mapping("provider_options")
|
|
113
|
-
@price_per_million = price
|
|
114
|
-
@batch_size = count("batch_size", 32)
|
|
115
|
-
# A request's state plus its widest question, and a whole request: Jev's
|
|
116
|
-
# 32k and 64k token limits, with room for the estimate to be off.
|
|
117
|
-
@max_state_tokens = count("max_state_tokens", 30_000)
|
|
118
|
-
@max_request_tokens = count("max_request_tokens", 60_000)
|
|
119
|
-
@needs = @preset["needs"]
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
def label
|
|
123
|
-
"#{provider}:#{model}"
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def api_key
|
|
127
|
-
api_key_env.to_s.empty? ? nil : ENV.fetch(api_key_env, nil)
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
private
|
|
131
|
-
|
|
132
|
-
def setting(key)
|
|
133
|
-
value = @data.key?(key) ? @data[key] : @preset[key]
|
|
134
|
-
value.nil? ? nil : value.to_s
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
def mapping(key)
|
|
138
|
-
value = @data.fetch(key, {})
|
|
139
|
-
raise ConfigError, "models: #{key} must be a mapping" unless value.is_a?(Hash)
|
|
140
|
-
|
|
141
|
-
value
|
|
142
|
-
end
|
|
143
|
-
|
|
144
|
-
# A preset's prices are per model; another model has no price unless the
|
|
145
|
-
# entry gives one, and its cost shows as unknown.
|
|
146
|
-
def price
|
|
147
|
-
return Judgelint.number(@data["price_per_million"]) if @data.key?("price_per_million")
|
|
148
|
-
|
|
149
|
-
prices = @preset["prices"] || {}
|
|
150
|
-
prices.key?(@model.to_s) ? Judgelint.number(prices[@model.to_s]) : nil
|
|
151
|
-
end
|
|
152
|
-
|
|
153
|
-
def count(key, default)
|
|
154
|
-
# Not fetch(key, default): Spinel miscompiles a Float default on a YAML hash.
|
|
155
|
-
value = @data.key?(key) ? @data[key] : @preset[key]
|
|
156
|
-
number = Judgelint.number(value.nil? ? default : value)
|
|
157
|
-
raise ConfigError, "models: #{key} must be a whole number of at least 1" if number.nil? || number < 1
|
|
158
|
-
|
|
159
|
-
number.to_i
|
|
160
|
-
end
|
|
161
|
-
end
|
|
162
|
-
end
|
data/lib/judgelint/errors.rb
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
class Error < StandardError; end
|
|
5
|
-
|
|
6
|
-
# The command line doesn't parse.
|
|
7
|
-
class UsageError < Error; end
|
|
8
|
-
|
|
9
|
-
# A rule file or the config file can't be used as written.
|
|
10
|
-
class ConfigError < Error; end
|
|
11
|
-
|
|
12
|
-
# The decision model couldn't be reached or answered with something unusable.
|
|
13
|
-
class ApiError < Error; end
|
|
14
|
-
end
|
data/lib/judgelint/glob.rb
DELETED
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# Path matching shared by rule targeting and file discovery. `**/` matches
|
|
5
|
-
# zero or more directories and `{a,b}` alternates, as in Dir.glob, and a
|
|
6
|
-
# trailing `/**` matches everything under a directory.
|
|
7
|
-
module Glob
|
|
8
|
-
FLAGS = File::FNM_PATHNAME | File::FNM_EXTGLOB
|
|
9
|
-
|
|
10
|
-
def self.match_any?(patterns, path)
|
|
11
|
-
patterns.any? do |pattern|
|
|
12
|
-
File.fnmatch?(pattern, path, FLAGS) || (pattern.end_with?("/**") && File.fnmatch?("#{pattern}/*", path, FLAGS))
|
|
13
|
-
end
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
def self.expand(patterns)
|
|
17
|
-
patterns.flat_map { |pattern| glob(pattern) }.select { |path| File.file?(path) }.uniq.sort
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
# Dir.glob, except that a symlink to itself is skipped. Dir.glob raises
|
|
21
|
-
# ELOOP when a pattern looks inside one, so the pattern is then globbed a
|
|
22
|
-
# segment at a time, entering only directories that resolve.
|
|
23
|
-
def self.glob(pattern)
|
|
24
|
-
Dir.glob(pattern)
|
|
25
|
-
rescue Errno::ELOOP
|
|
26
|
-
parts = pattern.split("/")
|
|
27
|
-
found = [""]
|
|
28
|
-
until parts.empty?
|
|
29
|
-
part = parts.shift
|
|
30
|
-
part = "#{part}/#{parts.shift}" if part == "**" && !parts.empty?
|
|
31
|
-
found = found.flat_map do |dir|
|
|
32
|
-
next [] unless dir.empty? || File.directory?(dir)
|
|
33
|
-
|
|
34
|
-
Dir.glob(dir.empty? ? part : File.join(dir, part))
|
|
35
|
-
end
|
|
36
|
-
end
|
|
37
|
-
found
|
|
38
|
-
end
|
|
39
|
-
|
|
40
|
-
# The files to check: in a git repository, those git tracks or would add,
|
|
41
|
-
# so nothing it ignores (node_modules, build output) is ever read.
|
|
42
|
-
# Elsewhere, every file the patterns match.
|
|
43
|
-
def self.files(patterns)
|
|
44
|
-
listed = project_files
|
|
45
|
-
return expand(patterns) if listed.nil?
|
|
46
|
-
|
|
47
|
-
listed.select { |path| match_any?(patterns, path) && File.file?(path) }
|
|
48
|
-
end
|
|
49
|
-
|
|
50
|
-
# nil outside a git repository, or without git.
|
|
51
|
-
def self.project_files
|
|
52
|
-
out, _err, status = Open3.capture3("git", "ls-files", "-z", "--cached", "--others", "--exclude-standard")
|
|
53
|
-
status.success? ? out.split("\0").uniq.sort : nil
|
|
54
|
-
rescue SystemCallError
|
|
55
|
-
nil
|
|
56
|
-
end
|
|
57
|
-
|
|
58
|
-
# A NUL byte in the first 8 KB, as git decides.
|
|
59
|
-
def self.binary?(path)
|
|
60
|
-
File.open(path, "rb") { |file| file.read(8000).to_s.include?("\0") }
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
# Files named on the command line, with directories expanded.
|
|
64
|
-
def self.expand_arguments(arguments)
|
|
65
|
-
arguments.flat_map do |argument|
|
|
66
|
-
if File.directory?(argument)
|
|
67
|
-
files([File.join(argument, "**", "*").delete_prefix("./")])
|
|
68
|
-
else
|
|
69
|
-
[argument]
|
|
70
|
-
end
|
|
71
|
-
end.map { |path| path.delete_prefix("./") }.uniq
|
|
72
|
-
end
|
|
73
|
-
end
|
|
74
|
-
end
|
data/lib/judgelint/paint.rb
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# Colors for a terminal. They're on when the stream is a terminal, unless
|
|
5
|
-
# NO_COLOR is set or TERM is dumb. FORCE_COLOR turns them on anywhere, so
|
|
6
|
-
# piped output can be colored too. With colors off, text passes through
|
|
7
|
-
# unchanged.
|
|
8
|
-
class Paint
|
|
9
|
-
BOLD = "1"
|
|
10
|
-
DIM = "2"
|
|
11
|
-
RED = "31"
|
|
12
|
-
GREEN = "32"
|
|
13
|
-
YELLOW = "33"
|
|
14
|
-
CYAN = "36"
|
|
15
|
-
BOLD_RED = "1;31"
|
|
16
|
-
BOLD_GREEN = "1;32"
|
|
17
|
-
|
|
18
|
-
def self.for(io)
|
|
19
|
-
new(color?(io))
|
|
20
|
-
end
|
|
21
|
-
|
|
22
|
-
def self.color?(io)
|
|
23
|
-
return false if set?("NO_COLOR") || ENV.fetch("TERM", "") == "dumb"
|
|
24
|
-
return true if set?("FORCE_COLOR") && ENV.fetch("FORCE_COLOR", "") != "0"
|
|
25
|
-
|
|
26
|
-
io.tty?
|
|
27
|
-
end
|
|
28
|
-
|
|
29
|
-
def self.set?(name)
|
|
30
|
-
!ENV.fetch(name, "").empty?
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def initialize(on)
|
|
34
|
-
@on = on
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
def on?
|
|
38
|
-
@on
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
# +text+ in the SGR style +code+, such as Paint::RED.
|
|
42
|
-
def call(text, code)
|
|
43
|
-
@on ? "\e[#{code}m#{text}\e[0m" : text
|
|
44
|
-
end
|
|
45
|
-
end
|
|
46
|
-
end
|
data/lib/judgelint/pool.rb
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# Maps items over a few threads and returns results in the items' order, so
|
|
5
|
-
# output doesn't depend on which request came back first. The first error
|
|
6
|
-
# is raised once every thread has finished.
|
|
7
|
-
module Pool
|
|
8
|
-
def self.map(items, size)
|
|
9
|
-
results = Array.new(items.size)
|
|
10
|
-
queue = Queue.new
|
|
11
|
-
items.each_with_index { |item, index| queue << [item, index] }
|
|
12
|
-
errors = []
|
|
13
|
-
lock = Mutex.new
|
|
14
|
-
threads = Array.new([[size, items.size].min, 1].max) do
|
|
15
|
-
Thread.new do
|
|
16
|
-
Thread.current.report_on_exception = false
|
|
17
|
-
loop do
|
|
18
|
-
pair = next_item(queue)
|
|
19
|
-
break if pair.nil?
|
|
20
|
-
|
|
21
|
-
begin
|
|
22
|
-
results[pair[1]] = yield(pair[0])
|
|
23
|
-
rescue StandardError => e
|
|
24
|
-
lock.synchronize { errors << e }
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
threads.each(&:join)
|
|
30
|
-
raise errors.first unless errors.empty?
|
|
31
|
-
|
|
32
|
-
results
|
|
33
|
-
end
|
|
34
|
-
|
|
35
|
-
def self.next_item(queue)
|
|
36
|
-
queue.pop(true)
|
|
37
|
-
rescue ThreadError
|
|
38
|
-
nil
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
end
|
data/lib/judgelint/protocol.rb
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module Judgelint
|
|
4
|
-
# The two request shapes decision models are served with. Each renders a
|
|
5
|
-
# Question as a yes/no question or a choice, and reads back the probability
|
|
6
|
-
# of yes, or a choice's probabilities as {option => probability}. A nil
|
|
7
|
-
# answer is a refusal.
|
|
8
|
-
module Protocol
|
|
9
|
-
# TypeSafe's System One: questions keyed by name, yes/no is "noul", and a
|
|
10
|
-
# choice's options are its criteria.
|
|
11
|
-
module SystemOne
|
|
12
|
-
def self.payload(model, state, questions)
|
|
13
|
-
rendered = {}
|
|
14
|
-
questions.each { |question| rendered[question.key] = render(question) }
|
|
15
|
-
body = { "state" => state, "questions" => rendered }
|
|
16
|
-
body["model"] = model unless model.to_s.empty?
|
|
17
|
-
body
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
def self.render(question)
|
|
21
|
-
return { "type" => "choice", "instructions" => question.instructions, "criteria" => question.options } if question.choice?
|
|
22
|
-
|
|
23
|
-
rendered = { "type" => "noul", "instructions" => question.instructions }
|
|
24
|
-
criteria = {}
|
|
25
|
-
criteria["true"] = question.yes_means if question.yes_means
|
|
26
|
-
criteria["false"] = question.no_means if question.no_means
|
|
27
|
-
rendered["criteria"] = criteria unless criteria.empty?
|
|
28
|
-
rendered
|
|
29
|
-
end
|
|
30
|
-
|
|
31
|
-
def self.parse(data, questions)
|
|
32
|
-
answers = data["answers"]
|
|
33
|
-
raise ApiError, "Response has no answers: #{JSON.generate(data)[0, 200]}" unless answers.is_a?(Hash)
|
|
34
|
-
|
|
35
|
-
probabilities = {}
|
|
36
|
-
questions.each do |question|
|
|
37
|
-
answer = answers[question.key]
|
|
38
|
-
probabilities[question.key] =
|
|
39
|
-
question.choice? ? Protocol.distribution(answer, answer.is_a?(Hash) ? answer["probabilities"] : nil, question)
|
|
40
|
-
: Protocol.probability(answer, "noul", question)
|
|
41
|
-
end
|
|
42
|
-
probabilities
|
|
43
|
-
end
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
# OpenAI's Decisions API: questions are a list with names, yes/no is
|
|
47
|
-
# "predicate" with no criteria field, a choice lists its options as
|
|
48
|
-
# {value, description}, and answers come back as a list.
|
|
49
|
-
module Decisions
|
|
50
|
-
# Decisions' input is text, so a structured state goes as its JSON.
|
|
51
|
-
def self.payload(model, state, questions)
|
|
52
|
-
input = state.is_a?(String) ? state : JSON.generate(state)
|
|
53
|
-
{ "model" => model, "input" => input, "questions" => questions.map { |question| render(question) } }
|
|
54
|
-
end
|
|
55
|
-
|
|
56
|
-
# A yes/no question's criteria become lines of its instructions, as
|
|
57
|
-
# RubyLLM and LiteLLM do.
|
|
58
|
-
def self.render(question)
|
|
59
|
-
if question.choice?
|
|
60
|
-
choices = question.options.map do |value, description|
|
|
61
|
-
description ? { "value" => value, "description" => description } : { "value" => value }
|
|
62
|
-
end
|
|
63
|
-
return { "type" => "choice", "name" => question.key, "instructions" => question.instructions, "choices" => choices }
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
lines = [question.instructions]
|
|
67
|
-
lines << "Yes: #{question.yes_means}" if question.yes_means
|
|
68
|
-
lines << "No: #{question.no_means}" if question.no_means
|
|
69
|
-
{ "type" => "predicate", "name" => question.key, "instructions" => lines.join("\n") }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def self.parse(data, questions)
|
|
73
|
-
answers = data["answers"]
|
|
74
|
-
raise ApiError, "Response has no answers: #{JSON.generate(data)[0, 200]}" unless answers.is_a?(Array)
|
|
75
|
-
|
|
76
|
-
# Answers come back in question order; match by name when there is
|
|
77
|
-
# one, and give unnamed answers to the remaining questions in order.
|
|
78
|
-
by_name = {}
|
|
79
|
-
unnamed = []
|
|
80
|
-
answers.each do |answer|
|
|
81
|
-
name = answer.is_a?(Hash) ? answer["name"].to_s : ""
|
|
82
|
-
name.empty? ? unnamed << answer : by_name[name] = answer
|
|
83
|
-
end
|
|
84
|
-
probabilities = {}
|
|
85
|
-
questions.each do |question|
|
|
86
|
-
answer = by_name.key?(question.key) ? by_name[question.key] : unnamed.shift
|
|
87
|
-
probabilities[question.key] =
|
|
88
|
-
question.choice? ? Protocol.distribution(answer, listed(answer), question) : Protocol.probability(answer, "probability", question)
|
|
89
|
-
end
|
|
90
|
-
probabilities
|
|
91
|
-
end
|
|
92
|
-
|
|
93
|
-
# [{value, probability}] as {value => probability}.
|
|
94
|
-
def self.listed(answer)
|
|
95
|
-
list = answer.is_a?(Hash) ? answer["probabilities"] : nil
|
|
96
|
-
return nil unless list.is_a?(Array)
|
|
97
|
-
|
|
98
|
-
list.to_h { |entry| [entry["value"].to_s, entry["probability"]] }
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
ALL = { "system_one" => SystemOne, "decisions" => Decisions }.freeze
|
|
103
|
-
|
|
104
|
-
def self.probability(answer, field, question)
|
|
105
|
-
return nil if answer.is_a?(Hash) && answer["type"] == "refusal"
|
|
106
|
-
|
|
107
|
-
value = answer.is_a?(Hash) ? answer[field] : nil
|
|
108
|
-
raise ApiError, "No yes/no answer for #{question.key}: #{answer.inspect}" unless value.is_a?(Numeric)
|
|
109
|
-
|
|
110
|
-
value.to_f
|
|
111
|
-
end
|
|
112
|
-
|
|
113
|
-
def self.distribution(answer, probabilities, question)
|
|
114
|
-
return nil if answer.is_a?(Hash) && answer["type"] == "refusal"
|
|
115
|
-
raise ApiError, "No choice answer for #{question.key}: #{answer.inspect}" unless probabilities.is_a?(Hash)
|
|
116
|
-
|
|
117
|
-
found = {}
|
|
118
|
-
question.options.each_key { |option| found[option] = probabilities[option].to_f }
|
|
119
|
-
found
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
|
-
end
|