@andrueandersoncs/lint-anything 0.3.0 → 0.5.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 +73 -70
- package/dist/cli.js +67 -64
- package/dist/lib.js +228 -146
- package/dist/types/decision-model.d.ts +162 -0
- package/dist/types/item.d.ts +1 -1
- package/dist/types/lib.d.ts +20 -8
- package/dist/types/lint.d.ts +13 -26
- package/dist/types/policy.d.ts +5 -18
- package/dist/types/score.d.ts +2 -2
- package/package.json +2 -2
- package/skills/lint-anything/SKILL.md +24 -41
- package/skills/lint-anything/scripts/score.js +45 -42
- 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}`
|
|
58
|
+
});
|
|
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
|
|
37
65
|
});
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
|
|
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
|
+
})))
|
|
74
|
+
};
|
|
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
|
+
};
|
|
41
94
|
};
|
|
42
|
-
var
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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",
|
|
@@ -287,33 +400,6 @@ var runSelector = Effect3.fn("runSelector")(function* (policy, cwd, runs) {
|
|
|
287
400
|
});
|
|
288
401
|
|
|
289
402
|
// src/lint.ts
|
|
290
|
-
var itemPlace = (finding, inFile) => finding.lines === undefined ? finding.item : `${inFile ? "" : `${finding.path}:`}${finding.lines[0]}-${finding.lines[1]}`;
|
|
291
|
-
var aggregate = (policy, limit, items) => {
|
|
292
|
-
const groups = new Map;
|
|
293
|
-
if (limit.per === "run")
|
|
294
|
-
groups.set(undefined, [...items]);
|
|
295
|
-
else
|
|
296
|
-
for (const item of items)
|
|
297
|
-
groups.set(item.path, [...groups.get(item.path) ?? [], item]);
|
|
298
|
-
return [...groups].map(([path, group]) => {
|
|
299
|
-
const violating = group.filter((item) => item.verdict === "violation");
|
|
300
|
-
const review = group.filter((item) => item.verdict === "review").length;
|
|
301
|
-
const unjudged = group.filter((item) => item.verdict === "inconclusive").length;
|
|
302
|
-
const verdict = violating.length > limit.max ? "violation" : violating.length + unjudged > limit.max ? "inconclusive" : "pass";
|
|
303
|
-
const places = violating.map((item) => itemPlace(item, path !== undefined)).join(", ");
|
|
304
|
-
return {
|
|
305
|
-
policy,
|
|
306
|
-
...path === undefined ? {} : { path },
|
|
307
|
-
verdict,
|
|
308
|
-
reason: [
|
|
309
|
-
`${violating.length} of ${group.length} items violate the rule (at most ${limit.max} allowed)${places === "" ? "" : `: ${places}`}`,
|
|
310
|
-
...review > 0 ? [`${review} more in review`] : [],
|
|
311
|
-
...unjudged > 0 ? [`${unjudged} not judged (request too large)`] : []
|
|
312
|
-
].join("; "),
|
|
313
|
-
violating: violating.map((item) => item.item)
|
|
314
|
-
};
|
|
315
|
-
});
|
|
316
|
-
};
|
|
317
403
|
var reviewFloor = 0.5;
|
|
318
404
|
var defaultThreshold = 0.65;
|
|
319
405
|
var verdictOf = (violates, floor, threshold) => violates <= floor ? "pass" : violates >= threshold ? "violation" : "review";
|
|
@@ -323,7 +409,7 @@ var toFinding = (violates, base, policy, threshold) => Result2.match(violates, {
|
|
|
323
409
|
const verdict = verdictOf(violates, reviewFloor, threshold);
|
|
324
410
|
return { ...base, verdict, violates, ...verdict === "pass" ? {} : { reason: policy.criteria.true } };
|
|
325
411
|
},
|
|
326
|
-
onFailure: (
|
|
412
|
+
onFailure: (error) => ({ ...base, verdict: "inconclusive", reason: error.message })
|
|
327
413
|
});
|
|
328
414
|
var itemLocation = (item) => ({
|
|
329
415
|
item: item.id,
|
|
@@ -332,23 +418,13 @@ var itemLocation = (item) => ({
|
|
|
332
418
|
});
|
|
333
419
|
var byLocation = (a, b) => (a.path ?? "").localeCompare(b.path ?? "") || (a.lines?.[0] ?? 0) - (b.lines?.[0] ?? 0) || a.id.localeCompare(b.id);
|
|
334
420
|
var select = Effect4.fn("select")(function* (policy, cwd, runs) {
|
|
335
|
-
|
|
336
|
-
const pathless = items.find((item) => item.path === undefined);
|
|
337
|
-
if (policy.limit?.per === "file" && pathless !== undefined) {
|
|
338
|
-
return yield* new SelectorError({
|
|
339
|
-
message: `${policy.name}: item ${pathless.id} has no path, and limit per file counts items by path; give every item a path or use per: run`
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
return items;
|
|
421
|
+
return (yield* runSelector(policy, cwd, runs)).toSorted(byLocation);
|
|
343
422
|
});
|
|
344
423
|
var judge = Effect4.fn("judge")(function* (policy, items) {
|
|
345
|
-
const
|
|
346
|
-
return yield*
|
|
424
|
+
const model = yield* DecisionModel;
|
|
425
|
+
return yield* model.ask(policy.name, policy.body, items.map((item) => itemQuestion(policy, item)));
|
|
347
426
|
});
|
|
348
|
-
var decide = (policy, items, answers, threshold) => {
|
|
349
|
-
const found = Arr.zipWith(items, answers, (item, violates) => toFinding(violates, { policy: policy.name, ...itemLocation(item) }, policy, threshold));
|
|
350
|
-
return policy.limit === undefined ? found : aggregate(policy.name, policy.limit, found);
|
|
351
|
-
};
|
|
427
|
+
var decide = (policy, items, answers, threshold) => Arr.zipWith(items, answers, (item, violates) => toFinding(violates, { policy: policy.name, ...itemLocation(item) }, policy, threshold));
|
|
352
428
|
var policyConcurrency = 8;
|
|
353
429
|
var lint = Effect4.fn("lint")(function* (policies, threshold) {
|
|
354
430
|
const runs = new Map;
|
|
@@ -371,10 +447,6 @@ var Frontmatter = Schema4.Struct({
|
|
|
371
447
|
criteria: Schema4.optional(Schema4.Struct({
|
|
372
448
|
true: Schema4.optional(Schema4.NonEmptyString),
|
|
373
449
|
false: Schema4.optional(Schema4.NonEmptyString)
|
|
374
|
-
})),
|
|
375
|
-
limit: Schema4.optional(Schema4.Struct({
|
|
376
|
-
max: Schema4.Int.check(Schema4.isGreaterThanOrEqualTo(0)),
|
|
377
|
-
per: Schema4.optional(Schema4.Literals(["file", "run"]))
|
|
378
450
|
}))
|
|
379
451
|
});
|
|
380
452
|
var frontmatterPattern = /^---\r?\n(.*?)\r?\n---(?:\r?\n|$)/s;
|
|
@@ -391,10 +463,10 @@ var parsePolicy = Effect5.fn("parsePolicy")(function* (name, source) {
|
|
|
391
463
|
const unknown = Object.keys(typeof parsed === "object" && parsed !== null ? parsed : {}).filter((key) => !(key in Frontmatter.fields));
|
|
392
464
|
if (unknown.length > 0) {
|
|
393
465
|
return yield* new PolicyError({
|
|
394
|
-
message: `${name}: unknown frontmatter ${unknown.join(", ")}; a policy has select
|
|
466
|
+
message: `${name}: unknown frontmatter ${unknown.join(", ")}; a policy has select and criteria, and its body is everything else the model reads`
|
|
395
467
|
});
|
|
396
468
|
}
|
|
397
|
-
const { select, criteria
|
|
469
|
+
const { select, criteria } = yield* Schema4.decodeUnknownEffect(Frontmatter)(parsed).pipe(Effect5.mapError((error) => new PolicyError({ message: `${name}: invalid frontmatter: ${error.message}` })));
|
|
398
470
|
if (select === undefined) {
|
|
399
471
|
return yield* new PolicyError({ message: `${name}: frontmatter needs a select command that picks what the rule judges` });
|
|
400
472
|
}
|
|
@@ -407,8 +479,7 @@ var parsePolicy = Effect5.fn("parsePolicy")(function* (name, source) {
|
|
|
407
479
|
name,
|
|
408
480
|
body,
|
|
409
481
|
select,
|
|
410
|
-
criteria: { true: criteria.true, false: criteria.false }
|
|
411
|
-
...limit === undefined ? {} : { limit: { max: limit.max, per: limit.per ?? "file" } }
|
|
482
|
+
criteria: { true: criteria.true, false: criteria.false }
|
|
412
483
|
};
|
|
413
484
|
});
|
|
414
485
|
var loadPolicies = Effect5.fn("loadPolicies")(function* (directory) {
|
|
@@ -648,25 +719,36 @@ var scorePolicy = Effect6.fn("scorePolicy")(function* (policy, labels, cwd) {
|
|
|
648
719
|
reach
|
|
649
720
|
};
|
|
650
721
|
});
|
|
722
|
+
|
|
723
|
+
// src/lib.ts
|
|
724
|
+
var Clef = DecisionModel;
|
|
725
|
+
var ClefError = DecisionModelError;
|
|
651
726
|
export {
|
|
652
727
|
Clef,
|
|
653
728
|
ClefError,
|
|
729
|
+
DecisionModel,
|
|
730
|
+
DecisionModelError,
|
|
731
|
+
Declined,
|
|
654
732
|
LabelError,
|
|
655
733
|
PolicyError,
|
|
656
734
|
RequestTooLarge,
|
|
657
735
|
SelectorError,
|
|
658
|
-
aggregate,
|
|
659
736
|
auroc,
|
|
660
737
|
bootstrapInterval,
|
|
661
738
|
brier,
|
|
662
739
|
cacheMaxAgeMs,
|
|
663
740
|
caught,
|
|
741
|
+
clef,
|
|
742
|
+
clefInputTokenPrice,
|
|
664
743
|
cliThresholds,
|
|
665
744
|
concordance,
|
|
745
|
+
configuredModel,
|
|
746
|
+
configuredTarget,
|
|
666
747
|
decide,
|
|
748
|
+
decodeReply,
|
|
667
749
|
defaultThreshold,
|
|
750
|
+
endpointTarget,
|
|
668
751
|
falseAlarms,
|
|
669
|
-
inputTokenPrice,
|
|
670
752
|
itemLabel,
|
|
671
753
|
itemQuestion,
|
|
672
754
|
itemsOf,
|
|
@@ -675,7 +757,6 @@ export {
|
|
|
675
757
|
lint,
|
|
676
758
|
loadLabels,
|
|
677
759
|
loadPolicies,
|
|
678
|
-
model,
|
|
679
760
|
noUsage,
|
|
680
761
|
pairedInterval,
|
|
681
762
|
parseItem,
|
|
@@ -695,5 +776,6 @@ export {
|
|
|
695
776
|
tuneThresholds,
|
|
696
777
|
verdict,
|
|
697
778
|
verdictF1,
|
|
698
|
-
verdictOf
|
|
779
|
+
verdictOf,
|
|
780
|
+
workersAiTarget
|
|
699
781
|
};
|