@andrueandersoncs/lint-anything 0.2.0 → 0.4.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.
- package/README.md +68 -31
- package/dist/cli.js +83 -74
- package/dist/lib.js +236 -102
- package/dist/types/decision-model.d.ts +162 -0
- package/dist/types/item.d.ts +1 -1
- package/dist/types/lib.d.ts +18 -6
- package/dist/types/lint.d.ts +10 -7
- package/dist/types/policy.d.ts +4 -4
- package/dist/types/score.d.ts +2 -2
- package/package.json +6 -6
- package/skills/lint-anything/SKILL.md +19 -18
- package/skills/lint-anything/scripts/score.js +45 -41
- package/dist/types/clef.d.ts +0 -82
package/dist/lib.js
CHANGED
|
@@ -1,85 +1,183 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
// src/
|
|
2
|
+
// src/decision-model.ts
|
|
3
3
|
import { Database } from "bun:sqlite";
|
|
4
4
|
import { mkdirSync } from "fs";
|
|
5
5
|
import { join } from "path";
|
|
6
|
-
import { Cause, Config, Context, Effect, flow, Layer, Ref, Result, Runtime, Schedule, Schema, Semaphore } from "effect";
|
|
7
|
-
import { FetchHttpClient, HttpClient, HttpClientError, HttpClientRequest
|
|
8
|
-
var model = "clef";
|
|
6
|
+
import { Cause, Config, Context, Effect, flow, Layer, Option, Redacted, Ref, Result, Runtime, Schedule, Schema, Semaphore } from "effect";
|
|
7
|
+
import { FetchHttpClient, HttpClient, HttpClientError, HttpClientRequest } from "effect/http";
|
|
9
8
|
|
|
10
|
-
class
|
|
9
|
+
class DecisionModelError extends Schema.TaggedError()("DecisionModelError", {
|
|
11
10
|
message: Schema.String
|
|
12
11
|
}) {
|
|
13
12
|
[Runtime.errorExitCode] = 2;
|
|
14
13
|
}
|
|
15
14
|
|
|
16
15
|
class RequestTooLarge extends Schema.TaggedError()("RequestTooLarge", {
|
|
17
|
-
bytes: Schema.Number
|
|
16
|
+
bytes: Schema.Number,
|
|
17
|
+
message: Schema.String
|
|
18
|
+
}) {
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
class Declined extends Schema.TaggedError()("Declined", {
|
|
22
|
+
message: Schema.String
|
|
18
23
|
}) {
|
|
19
24
|
}
|
|
20
25
|
var maxRequestBytes = 64000;
|
|
21
26
|
var maxQuestions = 64;
|
|
22
|
-
var
|
|
27
|
+
var clefInputTokenPrice = 0.00000024;
|
|
28
|
+
var defaultCloudflareBaseUrl = "https://api.cloudflare.com/client/v4";
|
|
23
29
|
var maxConcurrentRequests = 8;
|
|
24
30
|
var attemptTimeout = "60 seconds";
|
|
25
31
|
var attemptRetries = 3;
|
|
26
32
|
var cacheMaxAgeMs = 30 * 24 * 60 * 60 * 1000;
|
|
27
33
|
var cacheFile = "answers.sqlite";
|
|
28
34
|
var cacheBusyTimeoutMs = 1e4;
|
|
29
|
-
var
|
|
30
|
-
|
|
35
|
+
var Probability = Schema.Number.check(Schema.isBetween({ minimum: 0, maximum: 1 }));
|
|
36
|
+
var ReportedUsage = Schema.optional(Schema.Struct({ input_tokens: Schema.Number }));
|
|
37
|
+
var noul = (question) => ({ type: "noul", instructions: question.instructions, criteria: question.criteria });
|
|
38
|
+
var SystemOneResponse = Schema.Struct({
|
|
39
|
+
answers: Schema.Record(Schema.String, Schema.Struct({ noul: Probability })),
|
|
40
|
+
usage: ReportedUsage
|
|
31
41
|
});
|
|
32
|
-
var
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
})
|
|
42
|
+
var systemOne = {
|
|
43
|
+
head: (model, state) => `${JSON.stringify({ model, state }).slice(0, -1)},"questions":{`,
|
|
44
|
+
tail: "}}",
|
|
45
|
+
entry: (id, question) => `${JSON.stringify(id)}:${JSON.stringify(noul(question))}`,
|
|
46
|
+
decode: (body) => Schema.decodeUnknownEffect(SystemOneResponse)(typeof body === "object" && body !== null && "result" in body ? body.result : body).pipe(Effect.map(({ answers, usage }) => ({
|
|
47
|
+
answers: new Map(Object.entries(answers).map(([id, answer]) => [id, answer.noul])),
|
|
48
|
+
inputTokens: usage?.input_tokens
|
|
49
|
+
})))
|
|
50
|
+
};
|
|
51
|
+
var predicate = (id, question) => ({
|
|
52
|
+
type: "predicate",
|
|
53
|
+
name: id,
|
|
54
|
+
instructions: `${question.instructions}
|
|
55
|
+
|
|
56
|
+
true: ${question.criteria.true}
|
|
57
|
+
false: ${question.criteria.false}`
|
|
37
58
|
});
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
|
|
59
|
+
var DecisionsResponse = Schema.Struct({
|
|
60
|
+
answers: Schema.Array(Schema.Union([
|
|
61
|
+
Schema.Struct({ type: Schema.Literal("predicate"), name: Schema.String, probability: Probability }),
|
|
62
|
+
Schema.Struct({ type: Schema.Literal("refusal"), name: Schema.String })
|
|
63
|
+
])),
|
|
64
|
+
usage: ReportedUsage
|
|
65
|
+
});
|
|
66
|
+
var decisions = {
|
|
67
|
+
head: (model, state) => `${JSON.stringify({ model, input: state }).slice(0, -1)},"questions":[`,
|
|
68
|
+
tail: "]}",
|
|
69
|
+
entry: (id, question) => JSON.stringify(predicate(id, question)),
|
|
70
|
+
decode: (body) => Schema.decodeUnknownEffect(DecisionsResponse)(body).pipe(Effect.map(({ answers, usage }) => ({
|
|
71
|
+
answers: new Map(answers.map((answer) => [answer.name, answer.type === "refusal" ? "declined" : answer.probability])),
|
|
72
|
+
inputTokens: usage?.input_tokens
|
|
73
|
+
})))
|
|
41
74
|
};
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
75
|
+
var wires = { "system-one": systemOne, decisions };
|
|
76
|
+
var decodeReply = (format, body) => wires[format].decode(body);
|
|
77
|
+
var clef = workersAiModel("clef");
|
|
78
|
+
function workersAiModel(model) {
|
|
79
|
+
return {
|
|
80
|
+
format: "system-one",
|
|
81
|
+
model,
|
|
82
|
+
name: model === "clef" ? "Clef" : model,
|
|
83
|
+
cacheKey: model,
|
|
84
|
+
inputTokenPrice: model === "clef" ? clefInputTokenPrice : undefined
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
var workersAiTarget = (options) => {
|
|
88
|
+
const model = options.model ?? "clef";
|
|
89
|
+
return {
|
|
90
|
+
...workersAiModel(model),
|
|
91
|
+
url: `${options.baseUrl ?? defaultCloudflareBaseUrl}/accounts/${options.accountId}/ai/run/@cf/cloudflare/${model}`,
|
|
92
|
+
token: options.token
|
|
93
|
+
};
|
|
94
|
+
};
|
|
95
|
+
var formatOf = (url) => new URL(url).pathname.replace(/\/+$/, "").endsWith("/decisions") ? "decisions" : "system-one";
|
|
96
|
+
var endpointModel = (url, model) => ({
|
|
97
|
+
format: formatOf(url),
|
|
98
|
+
model,
|
|
99
|
+
name: model,
|
|
100
|
+
cacheKey: `${url} ${model}`,
|
|
101
|
+
inputTokenPrice: undefined
|
|
102
|
+
});
|
|
103
|
+
var endpointTarget = (options) => ({ ...endpointModel(options.url, options.model), url: options.url, token: options.token });
|
|
104
|
+
var configError = (message) => new DecisionModelError({ message });
|
|
105
|
+
var setting = (name) => Config.option(Config.String(name)).pipe(Effect.map(Option.filter((value) => value !== "")), Effect.mapError((error) => configError(`${name}: ${error.message}`)));
|
|
106
|
+
var isHttpUrl = (value) => URL.canParse(value) && ["http:", "https:"].includes(new URL(value).protocol);
|
|
107
|
+
var choice = Effect.gen(function* () {
|
|
108
|
+
const endpoint = yield* setting("LINT_ANYTHING_ENDPOINT");
|
|
109
|
+
const model = yield* setting("LINT_ANYTHING_MODEL");
|
|
110
|
+
const apiKey = yield* setting("LINT_ANYTHING_API_KEY");
|
|
111
|
+
if (Option.isSome(endpoint)) {
|
|
112
|
+
if (!isHttpUrl(endpoint.value)) {
|
|
113
|
+
return yield* configError(`LINT_ANYTHING_ENDPOINT must be an http or https URL, such as http://localhost:4000/v1/systemone`);
|
|
114
|
+
}
|
|
115
|
+
if (Option.isNone(model)) {
|
|
116
|
+
return yield* configError(`LINT_ANYTHING_MODEL is not set; LINT_ANYTHING_ENDPOINT needs the name of the model to ask`);
|
|
117
|
+
}
|
|
118
|
+
return { kind: "endpoint", url: endpoint.value, model: model.value, token: Option.getOrUndefined(Option.map(apiKey, Redacted.make)) };
|
|
119
|
+
}
|
|
120
|
+
if (Option.isSome(apiKey)) {
|
|
121
|
+
return yield* configError("LINT_ANYTHING_API_KEY is set but LINT_ANYTHING_ENDPOINT is not; set the endpoint the key is for (Clef on Workers AI uses CLOUDFLARE_AUTH_TOKEN)");
|
|
122
|
+
}
|
|
123
|
+
const name = Option.getOrElse(model, () => "clef");
|
|
124
|
+
if (!/^[\w.-]+$/.test(name)) {
|
|
125
|
+
return yield* configError(`LINT_ANYTHING_MODEL ${name} is not a Cloudflare model; to ask it, set LINT_ANYTHING_ENDPOINT to the decision endpoint that serves it`);
|
|
126
|
+
}
|
|
127
|
+
return { kind: "workers-ai", model: name };
|
|
128
|
+
});
|
|
129
|
+
var configuredModel = Effect.map(choice, (chosen) => chosen.kind === "endpoint" ? endpointModel(chosen.url, chosen.model) : workersAiModel(chosen.model));
|
|
130
|
+
var configuredTarget = Effect.gen(function* () {
|
|
131
|
+
const chosen = yield* choice;
|
|
132
|
+
if (chosen.kind === "endpoint")
|
|
133
|
+
return endpointTarget(chosen);
|
|
134
|
+
const required = (name, config) => config.pipe(Effect.mapError(() => configError(`${name} is not set; set it in the environment or in .env`)));
|
|
135
|
+
const accountId = yield* required("CLOUDFLARE_ACCOUNT_ID", Config.String("CLOUDFLARE_ACCOUNT_ID"));
|
|
136
|
+
const token = yield* required("CLOUDFLARE_AUTH_TOKEN", Config.Redacted("CLOUDFLARE_AUTH_TOKEN"));
|
|
137
|
+
const baseUrl = yield* Config.String("CLOUDFLARE_API_BASE_URL").pipe(Config.withDefault(defaultCloudflareBaseUrl), Effect.mapError((error) => configError(`CLOUDFLARE_API_BASE_URL: ${error.message}`)));
|
|
138
|
+
return workersAiTarget({ accountId, token, model: chosen.model, baseUrl });
|
|
139
|
+
});
|
|
140
|
+
var pack = (model, state, values, question) => {
|
|
141
|
+
const wire = wires[model.format];
|
|
142
|
+
const head = wire.head(model.model, state);
|
|
143
|
+
const fixedBytes = Buffer.byteLength(head) + Buffer.byteLength(wire.tail);
|
|
144
|
+
const entry = (value, index) => {
|
|
145
|
+
const id = `i${index}`;
|
|
146
|
+
const text = wire.entry(id, question(value));
|
|
147
|
+
return { id, text, bytes: Buffer.byteLength(text) };
|
|
148
|
+
};
|
|
47
149
|
const requests = [];
|
|
48
150
|
const oversized = [];
|
|
49
151
|
let entries = [];
|
|
50
152
|
let questions = [];
|
|
51
|
-
let taken = new Set;
|
|
52
153
|
let bytes = fixedBytes;
|
|
53
154
|
const flush = () => {
|
|
54
155
|
if (questions.length > 0)
|
|
55
|
-
requests.push({ body: `${head}${entries.join(",")}${tail}`, questions });
|
|
156
|
+
requests.push({ body: `${head}${entries.join(",")}${wire.tail}`, questions });
|
|
56
157
|
entries = [];
|
|
57
158
|
questions = [];
|
|
58
|
-
taken = new Set;
|
|
59
159
|
bytes = fixedBytes;
|
|
60
160
|
};
|
|
61
161
|
for (const value of values) {
|
|
62
|
-
const alone = entry(value,
|
|
63
|
-
if (fixedBytes + alone.bytes >
|
|
162
|
+
const alone = entry(value, 0);
|
|
163
|
+
if (fixedBytes + alone.bytes > maxRequestBytes) {
|
|
64
164
|
oversized.push({ value, bytes: fixedBytes + alone.bytes });
|
|
65
165
|
continue;
|
|
66
166
|
}
|
|
67
|
-
let next = entry(value,
|
|
68
|
-
if (questions.length === maxQuestions || bytes + ",".length + next.bytes >
|
|
167
|
+
let next = entry(value, questions.length);
|
|
168
|
+
if (questions.length === maxQuestions || bytes + ",".length + next.bytes > maxRequestBytes) {
|
|
69
169
|
flush();
|
|
70
170
|
next = alone;
|
|
71
171
|
}
|
|
72
172
|
bytes += (questions.length === 0 ? 0 : ",".length) + next.bytes;
|
|
73
173
|
entries.push(next.text);
|
|
74
174
|
questions.push({ id: next.id, value });
|
|
75
|
-
taken.add(next.id);
|
|
76
175
|
}
|
|
77
176
|
flush();
|
|
78
177
|
return { requests, oversized };
|
|
79
178
|
};
|
|
80
|
-
var
|
|
81
|
-
var
|
|
82
|
-
var openAnswerCache = (directory, now) => {
|
|
179
|
+
var planRequests = (state, questions, model = clef) => pack(model, state, questions.map((_, index) => index), (index) => questions[index]);
|
|
180
|
+
var openAnswerCache = (directory, now, model) => {
|
|
83
181
|
mkdirSync(directory, { recursive: true });
|
|
84
182
|
const db = new Database(join(directory, cacheFile));
|
|
85
183
|
db.run(`PRAGMA busy_timeout = ${cacheBusyTimeoutMs}`);
|
|
@@ -93,13 +191,13 @@ var openAnswerCache = (directory, now) => {
|
|
|
93
191
|
db.run("DELETE FROM answers WHERE answered <= ?", [now - cacheMaxAgeMs]);
|
|
94
192
|
const select = db.query("SELECT noul FROM answers WHERE key = ?");
|
|
95
193
|
const upsert = db.query("INSERT OR REPLACE INTO answers (key, noul, answered) VALUES (?, ?, ?)");
|
|
96
|
-
const key = (state, question) => new Bun.CryptoHasher("sha256").update(`${model}\x00${state}\x00${JSON.stringify(question)}`).digest();
|
|
194
|
+
const key = (state, question) => new Bun.CryptoHasher("sha256").update(`${model.cacheKey}\x00${state}\x00${JSON.stringify(noul(question))}`).digest();
|
|
97
195
|
const insert = db.transaction((state, answers) => {
|
|
98
196
|
const answered = Date.now();
|
|
99
197
|
for (const { question, noul } of answers)
|
|
100
198
|
upsert.run(key(state, question), noul, answered);
|
|
101
199
|
});
|
|
102
|
-
const failed = (error) => new
|
|
200
|
+
const failed = (error) => new DecisionModelError({ message: `answer cache ${join(directory, cacheFile)}: ${error instanceof Error ? error.message : String(error)}` });
|
|
103
201
|
return {
|
|
104
202
|
get: (state, questions) => Effect.try({ try: () => questions.map((question) => select.get(key(state, question))?.noul), catch: failed }),
|
|
105
203
|
set: (state, answers) => Effect.try({ try: () => insert(state, answers), catch: failed }),
|
|
@@ -119,74 +217,89 @@ var sumUsage = (usages) => {
|
|
|
119
217
|
}
|
|
120
218
|
return sum;
|
|
121
219
|
};
|
|
122
|
-
var failureMessage = (error) => {
|
|
220
|
+
var failureMessage = (name, error) => {
|
|
123
221
|
if (Cause.isTimeoutError(error)) {
|
|
124
|
-
return Effect.succeed(
|
|
222
|
+
return Effect.succeed(`${name} did not respond within ${attemptTimeout}, after ${attemptRetries} retries`);
|
|
125
223
|
}
|
|
126
224
|
if (HttpClientError.isHttpClientError(error)) {
|
|
127
225
|
const { reason } = error;
|
|
128
226
|
if (reason._tag !== "StatusCodeError") {
|
|
129
227
|
const cause = "cause" in reason && reason.cause instanceof Error ? `: ${reason.cause.message}` : "";
|
|
130
|
-
return Effect.succeed(
|
|
228
|
+
return Effect.succeed(`${name} request failed: ${error.message}${cause}`);
|
|
131
229
|
}
|
|
132
|
-
return reason.response.text.pipe(Effect.timeout("5 seconds"), Effect.map((text) => text.trim().slice(0, 500)), Effect.orElseSucceed(() => ""), Effect.map((body) =>
|
|
230
|
+
return reason.response.text.pipe(Effect.timeout("5 seconds"), Effect.map((text) => text.trim().slice(0, 500)), Effect.orElseSucceed(() => ""), Effect.map((body) => `${name} responded with HTTP ${reason.response.status}${body === "" ? "" : `: ${body}`}`));
|
|
133
231
|
}
|
|
134
|
-
|
|
232
|
+
const detail = (error instanceof Error ? error.message : String(error)).replace(/\s*\n\s*/g, " ");
|
|
233
|
+
return Effect.succeed(`${name} sent an unexpected response: ${detail}`);
|
|
135
234
|
};
|
|
136
235
|
|
|
137
|
-
class
|
|
138
|
-
static layer = (cacheDirectory) => Layer.effect(
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
236
|
+
class DecisionModel extends Context.Service()("lint-anything/DecisionModel") {
|
|
237
|
+
static layer = (cacheDirectory) => Layer.effect(DecisionModel, Effect.flatMap(configuredTarget, (target) => make(target, cacheDirectory))).pipe(Layer.provide(FetchHttpClient.layer));
|
|
238
|
+
static layerFor = (target, cacheDirectory) => Layer.effect(DecisionModel, make(target, cacheDirectory)).pipe(Layer.provide(FetchHttpClient.layer));
|
|
239
|
+
}
|
|
240
|
+
var make = Effect.fnUntraced(function* (target, cacheDirectory) {
|
|
241
|
+
const wire = wires[target.format];
|
|
242
|
+
const client = (yield* HttpClient.HttpClient).pipe(HttpClient.mapRequest(flow(target.token === undefined ? (request) => request : HttpClientRequest.bearerToken(target.token), HttpClientRequest.acceptJson)), HttpClient.filterStatusOk, HttpClient.transformResponse(flow(Effect.tap((response) => response.text), Effect.timeout(attemptTimeout))), HttpClient.retryTransient({ schedule: Schedule.exponential(500), times: attemptRetries }));
|
|
243
|
+
const cache = cacheDirectory === undefined ? undefined : yield* Effect.acquireRelease(Effect.try({
|
|
244
|
+
try: () => openAnswerCache(cacheDirectory, Date.now(), target),
|
|
245
|
+
catch: (error) => new DecisionModelError({
|
|
246
|
+
message: `cannot open the answer cache in ${cacheDirectory}: ${error instanceof Error ? error.message : String(error)}`
|
|
247
|
+
})
|
|
248
|
+
}), (cache) => Effect.sync(cache.close));
|
|
249
|
+
const permits = yield* Semaphore.make(maxConcurrentRequests);
|
|
250
|
+
const usage = yield* Ref.make(new Map);
|
|
251
|
+
const record = (source, added) => Ref.update(usage, (bySource) => new Map(bySource).set(source, sumUsage([bySource.get(source) ?? noUsage, { ...noUsage, ...added }])));
|
|
252
|
+
const tooLarge = (bytes) => new RequestTooLarge({
|
|
253
|
+
bytes,
|
|
254
|
+
message: target.name === "Clef" ? `request is ${bytes} bytes; Clef would truncate it` : `request is ${bytes} bytes, over the ${maxRequestBytes}-byte request limit`
|
|
255
|
+
});
|
|
256
|
+
const declined = new Declined({ message: `${target.name} declined to answer` });
|
|
257
|
+
const send = Effect.fn("DecisionModel.send")(function* (source, request) {
|
|
258
|
+
const reply = yield* record(source, { requests: 1 }).pipe(Effect.andThen(HttpClientRequest.post(target.url).pipe(HttpClientRequest.bodyText(request.body, "application/json"), client.execute)), Effect.flatMap((response) => response.json), Effect.flatMap(wire.decode), permits.withPermit, Effect.catch((error) => Effect.flatMap(failureMessage(target.name, error), (message) => Effect.fail(new DecisionModelError({ message })))));
|
|
259
|
+
yield* record(source, reply.inputTokens === undefined ? { unmetered: 1 } : { inputTokens: reply.inputTokens });
|
|
260
|
+
const answers = [];
|
|
261
|
+
for (const { id, value } of request.questions) {
|
|
262
|
+
const answer = reply.answers.get(id);
|
|
263
|
+
if (answer === undefined) {
|
|
264
|
+
return yield* new DecisionModelError({ message: `${target.name} returned no answer for question ${id}` });
|
|
161
265
|
}
|
|
162
|
-
|
|
266
|
+
answers.push({ value, answer });
|
|
267
|
+
}
|
|
268
|
+
return answers;
|
|
269
|
+
});
|
|
270
|
+
const ask = Effect.fn("DecisionModel.ask")(function* (source, state, questions) {
|
|
271
|
+
const results = [];
|
|
272
|
+
const misses = [];
|
|
273
|
+
const cached = cache === undefined ? [] : yield* cache.get(state, questions);
|
|
274
|
+
questions.forEach((_, index) => {
|
|
275
|
+
const answer = cached[index];
|
|
276
|
+
if (answer === undefined)
|
|
277
|
+
misses.push(index);
|
|
278
|
+
else
|
|
279
|
+
results[index] = Result.succeed(answer);
|
|
163
280
|
});
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
281
|
+
if (misses.length < questions.length)
|
|
282
|
+
yield* record(source, { cached: questions.length - misses.length });
|
|
283
|
+
const { requests, oversized } = planRequests(state, misses.map((index) => questions[index]), target);
|
|
284
|
+
for (const { value, bytes } of oversized)
|
|
285
|
+
results[misses[value]] = Result.fail(tooLarge(bytes));
|
|
286
|
+
yield* Effect.forEach(requests, (request) => send(source, request).pipe(Effect.tap((received) => {
|
|
287
|
+
const answered = [];
|
|
288
|
+
for (const { value, answer } of received) {
|
|
289
|
+
const index = misses[value];
|
|
290
|
+
if (answer === "declined") {
|
|
291
|
+
results[index] = Result.fail(declined);
|
|
292
|
+
} else {
|
|
173
293
|
results[index] = Result.succeed(answer);
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return cache === undefined ? Effect.void : cache.set(state, received.map(({ value, noul: answer }) => ({ question: noul(questions[misses[value]]), noul: answer })));
|
|
184
|
-
})), { concurrency: "unbounded", discard: true });
|
|
185
|
-
return results;
|
|
186
|
-
});
|
|
187
|
-
return Clef.of({ ask, stats: Ref.get(usage) });
|
|
188
|
-
})).pipe(Layer.provide(FetchHttpClient.layer));
|
|
189
|
-
}
|
|
294
|
+
answered.push({ question: questions[index], noul: answer });
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
return cache === undefined ? Effect.void : cache.set(state, answered);
|
|
298
|
+
})), { concurrency: "unbounded", discard: true });
|
|
299
|
+
return results;
|
|
300
|
+
});
|
|
301
|
+
return DecisionModel.of({ target, ask, stats: Ref.get(usage) });
|
|
302
|
+
});
|
|
190
303
|
// src/item.ts
|
|
191
304
|
import { Effect as Effect2, Schema as Schema2, SchemaIssue } from "effect";
|
|
192
305
|
var ItemFields = Schema2.Struct({
|
|
@@ -202,7 +315,7 @@ var parseItem = Effect2.fn("parseItem")(function* (line) {
|
|
|
202
315
|
return yield* Effect2.fail("not an object");
|
|
203
316
|
const unknown = Object.keys(value).filter((key) => !(key in ItemFields.fields));
|
|
204
317
|
if (unknown.length > 0) {
|
|
205
|
-
return yield* Effect2.fail(`unknown field ${unknown.join(", ")}; put what
|
|
318
|
+
return yield* Effect2.fail(`unknown field ${unknown.join(", ")}; put what the model should read in text`);
|
|
206
319
|
}
|
|
207
320
|
return yield* Schema2.decodeUnknownEffect(ItemFields)(value).pipe(Effect2.mapError((error) => formatIssues(error.issue).issues.map(({ path, message }) => `${path?.length ? `${path.join(".")}: ` : ""}${message}`).join("; ")));
|
|
208
321
|
});
|
|
@@ -235,7 +348,7 @@ process.on("exit", killRunningSelectors);
|
|
|
235
348
|
var start = async (select, cwd) => {
|
|
236
349
|
const child = Bun.spawn(["sh", "-c", select], {
|
|
237
350
|
cwd,
|
|
238
|
-
env: Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("CLOUDFLARE_"))),
|
|
351
|
+
env: Object.fromEntries(Object.entries(process.env).filter(([name]) => !name.startsWith("CLOUDFLARE_") && !name.startsWith("LINT_ANYTHING_"))),
|
|
239
352
|
stdin: "ignore",
|
|
240
353
|
stdout: "pipe",
|
|
241
354
|
stderr: "pipe",
|
|
@@ -288,6 +401,7 @@ var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
|
|
|
288
401
|
|
|
289
402
|
// src/lint.ts
|
|
290
403
|
var itemPlace = (finding, inFile) => finding.lines === undefined ? finding.item : `${inFile ? "" : `${finding.path}:`}${finding.lines[0]}-${finding.lines[1]}`;
|
|
404
|
+
var unjudgedCauses = ["request too large", "declined"];
|
|
291
405
|
var aggregate = (policy, limit, items) => {
|
|
292
406
|
const groups = new Map;
|
|
293
407
|
if (limit.per === "run")
|
|
@@ -298,8 +412,8 @@ var aggregate = (policy, limit, items) => {
|
|
|
298
412
|
return [...groups].map(([path, group]) => {
|
|
299
413
|
const violating = group.filter((item) => item.verdict === "violation");
|
|
300
414
|
const review = group.filter((item) => item.verdict === "review").length;
|
|
301
|
-
const unjudged = group.filter((item) => item.verdict === "inconclusive")
|
|
302
|
-
const verdict = violating.length > limit.max ? "violation" : violating.length + unjudged > limit.max ? "inconclusive" : "pass";
|
|
415
|
+
const unjudged = group.filter((item) => item.verdict === "inconclusive");
|
|
416
|
+
const verdict = violating.length > limit.max ? "violation" : violating.length + unjudged.length > limit.max ? "inconclusive" : "pass";
|
|
303
417
|
const places = violating.map((item) => itemPlace(item, path !== undefined)).join(", ");
|
|
304
418
|
return {
|
|
305
419
|
policy,
|
|
@@ -308,7 +422,10 @@ var aggregate = (policy, limit, items) => {
|
|
|
308
422
|
reason: [
|
|
309
423
|
`${violating.length} of ${group.length} items violate the rule (at most ${limit.max} allowed)${places === "" ? "" : `: ${places}`}`,
|
|
310
424
|
...review > 0 ? [`${review} more in review`] : [],
|
|
311
|
-
...
|
|
425
|
+
...unjudgedCauses.flatMap((cause) => {
|
|
426
|
+
const count = unjudged.filter((item) => (item.unjudged ?? "request too large") === cause).length;
|
|
427
|
+
return count > 0 ? [`${count} not judged (${cause})`] : [];
|
|
428
|
+
})
|
|
312
429
|
].join("; "),
|
|
313
430
|
violating: violating.map((item) => item.item)
|
|
314
431
|
};
|
|
@@ -323,7 +440,12 @@ var toFinding = (violates, base, policy, threshold) => Result2.match(violates, {
|
|
|
323
440
|
const verdict = verdictOf(violates, reviewFloor, threshold);
|
|
324
441
|
return { ...base, verdict, violates, ...verdict === "pass" ? {} : { reason: policy.criteria.true } };
|
|
325
442
|
},
|
|
326
|
-
onFailure: (
|
|
443
|
+
onFailure: (error) => ({
|
|
444
|
+
...base,
|
|
445
|
+
verdict: "inconclusive",
|
|
446
|
+
reason: error.message,
|
|
447
|
+
unjudged: error._tag === "Declined" ? "declined" : "request too large"
|
|
448
|
+
})
|
|
327
449
|
});
|
|
328
450
|
var itemLocation = (item) => ({
|
|
329
451
|
item: item.id,
|
|
@@ -342,8 +464,8 @@ var select = Effect4.fn("select")(function* (policy, cwd, runs) {
|
|
|
342
464
|
return items;
|
|
343
465
|
});
|
|
344
466
|
var judge = Effect4.fn("judge")(function* (policy, items) {
|
|
345
|
-
const
|
|
346
|
-
return yield*
|
|
467
|
+
const model = yield* DecisionModel;
|
|
468
|
+
return yield* model.ask(policy.name, policy.body, items.map((item) => itemQuestion(policy, item)));
|
|
347
469
|
});
|
|
348
470
|
var decide = (policy, items, answers, threshold) => {
|
|
349
471
|
const found = Arr.zipWith(items, answers, (item, violates) => toFinding(violates, { policy: policy.name, ...itemLocation(item) }, policy, threshold));
|
|
@@ -391,7 +513,7 @@ var parsePolicy = Effect5.fn("parsePolicy")(function* (name, source) {
|
|
|
391
513
|
const unknown = Object.keys(typeof parsed === "object" && parsed !== null ? parsed : {}).filter((key) => !(key in Frontmatter.fields));
|
|
392
514
|
if (unknown.length > 0) {
|
|
393
515
|
return yield* new PolicyError({
|
|
394
|
-
message: `${name}: unknown frontmatter ${unknown.join(", ")}; a policy has select, criteria, and limit, and its body is everything else
|
|
516
|
+
message: `${name}: unknown frontmatter ${unknown.join(", ")}; a policy has select, criteria, and limit, and its body is everything else the model reads`
|
|
395
517
|
});
|
|
396
518
|
}
|
|
397
519
|
const { select, criteria, limit } = yield* Schema4.decodeUnknownEffect(Frontmatter)(parsed).pipe(Effect5.mapError((error) => new PolicyError({ message: `${name}: invalid frontmatter: ${error.message}` })));
|
|
@@ -648,9 +770,16 @@ var scorePolicy = Effect6.fn("scorePolicy")(function* (policy, labels, cwd) {
|
|
|
648
770
|
reach
|
|
649
771
|
};
|
|
650
772
|
});
|
|
773
|
+
|
|
774
|
+
// src/lib.ts
|
|
775
|
+
var Clef = DecisionModel;
|
|
776
|
+
var ClefError = DecisionModelError;
|
|
651
777
|
export {
|
|
652
778
|
Clef,
|
|
653
779
|
ClefError,
|
|
780
|
+
DecisionModel,
|
|
781
|
+
DecisionModelError,
|
|
782
|
+
Declined,
|
|
654
783
|
LabelError,
|
|
655
784
|
PolicyError,
|
|
656
785
|
RequestTooLarge,
|
|
@@ -661,12 +790,17 @@ export {
|
|
|
661
790
|
brier,
|
|
662
791
|
cacheMaxAgeMs,
|
|
663
792
|
caught,
|
|
793
|
+
clef,
|
|
794
|
+
clefInputTokenPrice,
|
|
664
795
|
cliThresholds,
|
|
665
796
|
concordance,
|
|
797
|
+
configuredModel,
|
|
798
|
+
configuredTarget,
|
|
666
799
|
decide,
|
|
800
|
+
decodeReply,
|
|
667
801
|
defaultThreshold,
|
|
802
|
+
endpointTarget,
|
|
668
803
|
falseAlarms,
|
|
669
|
-
inputTokenPrice,
|
|
670
804
|
itemLabel,
|
|
671
805
|
itemQuestion,
|
|
672
806
|
itemsOf,
|
|
@@ -675,7 +809,6 @@ export {
|
|
|
675
809
|
lint,
|
|
676
810
|
loadLabels,
|
|
677
811
|
loadPolicies,
|
|
678
|
-
model,
|
|
679
812
|
noUsage,
|
|
680
813
|
pairedInterval,
|
|
681
814
|
parseItem,
|
|
@@ -695,5 +828,6 @@ export {
|
|
|
695
828
|
tuneThresholds,
|
|
696
829
|
verdict,
|
|
697
830
|
verdictF1,
|
|
698
|
-
verdictOf
|
|
831
|
+
verdictOf,
|
|
832
|
+
workersAiTarget
|
|
699
833
|
};
|