@stigmer/plugin-package 0.1.0-dev.20261017020314.g644422724c27
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/LICENSE +194 -0
- package/README.md +72 -0
- package/client/archive.d.ts +27 -0
- package/client/archive.d.ts.map +1 -0
- package/client/archive.js +38 -0
- package/client/archive.js.map +1 -0
- package/client/builtin.d.ts +37 -0
- package/client/builtin.d.ts.map +1 -0
- package/client/builtin.js +49 -0
- package/client/builtin.js.map +1 -0
- package/client/ignore/defaults.d.ts +2 -0
- package/client/ignore/defaults.d.ts.map +1 -0
- package/client/ignore/defaults.js +140 -0
- package/client/ignore/defaults.js.map +1 -0
- package/client/ignore/match.d.ts +3 -0
- package/client/ignore/match.d.ts.map +1 -0
- package/client/ignore/match.js +166 -0
- package/client/ignore/match.js.map +1 -0
- package/client/ignore/matcher.d.ts +50 -0
- package/client/ignore/matcher.d.ts.map +1 -0
- package/client/ignore/matcher.js +116 -0
- package/client/ignore/matcher.js.map +1 -0
- package/client/ignore/pattern.d.ts +13 -0
- package/client/ignore/pattern.d.ts.map +1 -0
- package/client/ignore/pattern.js +119 -0
- package/client/ignore/pattern.js.map +1 -0
- package/client/prepare-eval.d.ts +62 -0
- package/client/prepare-eval.d.ts.map +1 -0
- package/client/prepare-eval.js +60 -0
- package/client/prepare-eval.js.map +1 -0
- package/client/prepare.d.ts +99 -0
- package/client/prepare.d.ts.map +1 -0
- package/client/prepare.js +89 -0
- package/client/prepare.js.map +1 -0
- package/client/presentation.d.ts +19 -0
- package/client/presentation.d.ts.map +1 -0
- package/client/presentation.js +44 -0
- package/client/presentation.js.map +1 -0
- package/client/refs.d.ts +76 -0
- package/client/refs.d.ts.map +1 -0
- package/client/refs.js +123 -0
- package/client/refs.js.map +1 -0
- package/client/release.d.ts +20 -0
- package/client/release.d.ts.map +1 -0
- package/client/release.js +28 -0
- package/client/release.js.map +1 -0
- package/client/reroot.d.ts +21 -0
- package/client/reroot.d.ts.map +1 -0
- package/client/reroot.js +37 -0
- package/client/reroot.js.map +1 -0
- package/client/select.d.ts +74 -0
- package/client/select.d.ts.map +1 -0
- package/client/select.js +121 -0
- package/client/select.js.map +1 -0
- package/client/vocabulary.d.ts +13 -0
- package/client/vocabulary.d.ts.map +1 -0
- package/client/vocabulary.js +17 -0
- package/client/vocabulary.js.map +1 -0
- package/client.d.ts +51 -0
- package/client.d.ts.map +1 -0
- package/client.js +51 -0
- package/client.js.map +1 -0
- package/detect.d.ts +83 -0
- package/detect.d.ts.map +1 -0
- package/detect.js +210 -0
- package/detect.js.map +1 -0
- package/dialects/claude.d.ts +34 -0
- package/dialects/claude.d.ts.map +1 -0
- package/dialects/claude.js +84 -0
- package/dialects/claude.js.map +1 -0
- package/dialects/codex.d.ts +20 -0
- package/dialects/codex.d.ts.map +1 -0
- package/dialects/codex.js +21 -0
- package/dialects/codex.js.map +1 -0
- package/dialects/cursor.d.ts +25 -0
- package/dialects/cursor.d.ts.map +1 -0
- package/dialects/cursor.js +66 -0
- package/dialects/cursor.js.map +1 -0
- package/dialects/manifest.d.ts +164 -0
- package/dialects/manifest.d.ts.map +1 -0
- package/dialects/manifest.js +256 -0
- package/dialects/manifest.js.map +1 -0
- package/dialects/open.d.ts +19 -0
- package/dialects/open.d.ts.map +1 -0
- package/dialects/open.js +49 -0
- package/dialects/open.js.map +1 -0
- package/documents.d.ts +58 -0
- package/documents.d.ts.map +1 -0
- package/documents.js +121 -0
- package/documents.js.map +1 -0
- package/evals/eval-dir.d.ts +59 -0
- package/evals/eval-dir.d.ts.map +1 -0
- package/evals/eval-dir.js +84 -0
- package/evals/eval-dir.js.map +1 -0
- package/evals/fields.d.ts +78 -0
- package/evals/fields.d.ts.map +1 -0
- package/evals/fields.js +156 -0
- package/evals/fields.js.map +1 -0
- package/evals/glob.d.ts +40 -0
- package/evals/glob.d.ts.map +1 -0
- package/evals/glob.js +146 -0
- package/evals/glob.js.map +1 -0
- package/evals/graders.d.ts +52 -0
- package/evals/graders.d.ts.map +1 -0
- package/evals/graders.js +279 -0
- package/evals/graders.js.map +1 -0
- package/evals/markdown.d.ts +24 -0
- package/evals/markdown.d.ts.map +1 -0
- package/evals/markdown.js +48 -0
- package/evals/markdown.js.map +1 -0
- package/evals/mocks.d.ts +50 -0
- package/evals/mocks.d.ts.map +1 -0
- package/evals/mocks.js +182 -0
- package/evals/mocks.js.map +1 -0
- package/evals/read-eval-suite.d.ts +110 -0
- package/evals/read-eval-suite.d.ts.map +1 -0
- package/evals/read-eval-suite.js +603 -0
- package/evals/read-eval-suite.js.map +1 -0
- package/evals/summary.d.ts +61 -0
- package/evals/summary.d.ts.map +1 -0
- package/evals/summary.js +69 -0
- package/evals/summary.js.map +1 -0
- package/evals/types.d.ts +203 -0
- package/evals/types.d.ts.map +1 -0
- package/evals/types.js +21 -0
- package/evals/types.js.map +1 -0
- package/files.d.ts +135 -0
- package/files.d.ts.map +1 -0
- package/files.js +209 -0
- package/files.js.map +1 -0
- package/frontmatter.d.ts +51 -0
- package/frontmatter.d.ts.map +1 -0
- package/frontmatter.js +61 -0
- package/frontmatter.js.map +1 -0
- package/index.d.ts +37 -0
- package/index.d.ts.map +1 -0
- package/index.js +33 -0
- package/index.js.map +1 -0
- package/marketplace/messages.d.ts +20 -0
- package/marketplace/messages.d.ts.map +1 -0
- package/marketplace/messages.js +49 -0
- package/marketplace/messages.js.map +1 -0
- package/marketplace/outcome.d.ts +70 -0
- package/marketplace/outcome.d.ts.map +1 -0
- package/marketplace/outcome.js +29 -0
- package/marketplace/outcome.js.map +1 -0
- package/marketplace/read-marketplace.d.ts +52 -0
- package/marketplace/read-marketplace.d.ts.map +1 -0
- package/marketplace/read-marketplace.js +351 -0
- package/marketplace/read-marketplace.js.map +1 -0
- package/messages.d.ts +55 -0
- package/messages.d.ts.map +1 -0
- package/messages.js +167 -0
- package/messages.js.map +1 -0
- package/normalise/hooks.d.ts +91 -0
- package/normalise/hooks.d.ts.map +1 -0
- package/normalise/hooks.js +437 -0
- package/normalise/hooks.js.map +1 -0
- package/normalise/ignored.d.ts +34 -0
- package/normalise/ignored.d.ts.map +1 -0
- package/normalise/ignored.js +78 -0
- package/normalise/ignored.js.map +1 -0
- package/normalise/mcp-servers.d.ts +50 -0
- package/normalise/mcp-servers.d.ts.map +1 -0
- package/normalise/mcp-servers.js +398 -0
- package/normalise/mcp-servers.js.map +1 -0
- package/normalise/settings.d.ts +32 -0
- package/normalise/settings.d.ts.map +1 -0
- package/normalise/settings.js +72 -0
- package/normalise/settings.js.map +1 -0
- package/normalise/skills.d.ts +39 -0
- package/normalise/skills.d.ts.map +1 -0
- package/normalise/skills.js +135 -0
- package/normalise/skills.js.map +1 -0
- package/normalise/stigmer-folder.d.ts +17 -0
- package/normalise/stigmer-folder.d.ts.map +1 -0
- package/normalise/stigmer-folder.js +19 -0
- package/normalise/stigmer-folder.js.map +1 -0
- package/normalise/sub-agents.d.ts +54 -0
- package/normalise/sub-agents.d.ts.map +1 -0
- package/normalise/sub-agents.js +219 -0
- package/normalise/sub-agents.js.map +1 -0
- package/normalise/variables.d.ts +31 -0
- package/normalise/variables.d.ts.map +1 -0
- package/normalise/variables.js +170 -0
- package/normalise/variables.js.map +1 -0
- package/outcome.d.ts +59 -0
- package/outcome.d.ts.map +1 -0
- package/outcome.js +32 -0
- package/outcome.js.map +1 -0
- package/package.json +47 -0
- package/placeholders.d.ts +65 -0
- package/placeholders.d.ts.map +1 -0
- package/placeholders.js +99 -0
- package/placeholders.js.map +1 -0
- package/presentation.d.ts +53 -0
- package/presentation.d.ts.map +1 -0
- package/presentation.js +156 -0
- package/presentation.js.map +1 -0
- package/read-plugin-package.d.ts +20 -0
- package/read-plugin-package.d.ts.map +1 -0
- package/read-plugin-package.js +73 -0
- package/read-plugin-package.js.map +1 -0
- package/src/client/archive.ts +44 -0
- package/src/client/builtin.ts +60 -0
- package/src/client/ignore/defaults.ts +155 -0
- package/src/client/ignore/match.ts +158 -0
- package/src/client/ignore/matcher.ts +156 -0
- package/src/client/ignore/pattern.ts +132 -0
- package/src/client/prepare-eval.ts +92 -0
- package/src/client/prepare.ts +160 -0
- package/src/client/presentation.ts +47 -0
- package/src/client/refs.ts +170 -0
- package/src/client/release.ts +29 -0
- package/src/client/reroot.ts +35 -0
- package/src/client/select.ts +161 -0
- package/src/client/vocabulary.ts +19 -0
- package/src/client.ts +102 -0
- package/src/detect.ts +245 -0
- package/src/dialects/claude.ts +102 -0
- package/src/dialects/codex.ts +26 -0
- package/src/dialects/cursor.ts +77 -0
- package/src/dialects/manifest.ts +316 -0
- package/src/dialects/open.ts +50 -0
- package/src/documents.ts +158 -0
- package/src/evals/eval-dir.ts +101 -0
- package/src/evals/fields.ts +184 -0
- package/src/evals/glob.ts +141 -0
- package/src/evals/graders.ts +293 -0
- package/src/evals/markdown.ts +56 -0
- package/src/evals/mocks.ts +202 -0
- package/src/evals/read-eval-suite.ts +713 -0
- package/src/evals/summary.ts +100 -0
- package/src/evals/types.ts +217 -0
- package/src/files.ts +235 -0
- package/src/frontmatter.ts +70 -0
- package/src/index.ts +129 -0
- package/src/marketplace/messages.ts +61 -0
- package/src/marketplace/outcome.ts +91 -0
- package/src/marketplace/read-marketplace.ts +388 -0
- package/src/messages.ts +248 -0
- package/src/normalise/hooks.ts +469 -0
- package/src/normalise/ignored.ts +79 -0
- package/src/normalise/mcp-servers.ts +423 -0
- package/src/normalise/settings.ts +81 -0
- package/src/normalise/skills.ts +145 -0
- package/src/normalise/stigmer-folder.ts +23 -0
- package/src/normalise/sub-agents.ts +231 -0
- package/src/normalise/variables.ts +181 -0
- package/src/outcome.ts +158 -0
- package/src/placeholders.ts +110 -0
- package/src/presentation.ts +170 -0
- package/src/read-plugin-package.ts +80 -0
- package/src/testing.ts +347 -0
- package/src/tool-names.ts +30 -0
- package/src/trim.ts +10 -0
- package/src/types.ts +214 -0
- package/testing.d.ts +127 -0
- package/testing.d.ts.map +1 -0
- package/testing.js +266 -0
- package/testing.js.map +1 -0
- package/tool-names.d.ts +21 -0
- package/tool-names.d.ts.map +1 -0
- package/tool-names.js +28 -0
- package/tool-names.js.map +1 -0
- package/trim.d.ts +7 -0
- package/trim.d.ts.map +1 -0
- package/trim.js +12 -0
- package/trim.js.map +1 -0
- package/types.d.ts +179 -0
- package/types.d.ts.map +1 -0
- package/types.js +19 -0
- package/types.js.map +1 -0
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The field readers every eval document shares: a collector for the
|
|
3
|
+
* sentences a case's files produce, the typed reads over a parsed YAML
|
|
4
|
+
* mapping, and the regex compile check.
|
|
5
|
+
*
|
|
6
|
+
* Every finding is one sentence that starts with the plugin-relative file
|
|
7
|
+
* and names the field, the shape Claude Code's loader prints
|
|
8
|
+
* (`prompt.md: unknown frontmatter key 'foo'`), so the CLI and the plugin
|
|
9
|
+
* page show an author the same words. A read that fails records its finding
|
|
10
|
+
* and returns `undefined`; the caller keeps reading so one pass names every
|
|
11
|
+
* problem in the file, and the case loads only when its collector stayed
|
|
12
|
+
* empty.
|
|
13
|
+
*
|
|
14
|
+
* Regexes are compiled here and never run: compiling a JavaScript pattern
|
|
15
|
+
* is linear in its length, and running author patterns is the grader's
|
|
16
|
+
* job, behind its own time limit.
|
|
17
|
+
*
|
|
18
|
+
* A finding quotes at most {@link QUOTED_TEXT_LIMIT} characters of an
|
|
19
|
+
* author's value, then an ellipsis: the findings are stored on the plugin,
|
|
20
|
+
* and one huge key, pattern or version string in a compressible archive
|
|
21
|
+
* would otherwise bloat that row past what one API message carries. The
|
|
22
|
+
* first characters are enough to find the value in the file.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
import { isJsonObject, isStringArray, type JsonObject } from "../documents.js";
|
|
26
|
+
import type { EvalSuiteFinding } from "./types.js";
|
|
27
|
+
|
|
28
|
+
export type EvalFindingKind = EvalSuiteFinding["kind"];
|
|
29
|
+
|
|
30
|
+
/** The most characters of an author's value one finding quotes. */
|
|
31
|
+
export const QUOTED_TEXT_LIMIT = 200;
|
|
32
|
+
|
|
33
|
+
/** `text`, cut to {@link QUOTED_TEXT_LIMIT} characters and an ellipsis when longer, never inside a surrogate pair. */
|
|
34
|
+
export function excerpt(text: string): string {
|
|
35
|
+
return cutText(text, QUOTED_TEXT_LIMIT);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** `text`, cut to `limit` UTF-16 code units and an ellipsis when longer, never inside a surrogate pair. */
|
|
39
|
+
export function cutText(text: string, limit: number): string {
|
|
40
|
+
if (text.length <= limit) return text;
|
|
41
|
+
const cut = text.slice(0, limit);
|
|
42
|
+
return `${/[\uD800-\uDBFF]$/.test(cut) ? cut.slice(0, -1) : cut}…`;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** An author's text in single quotes, as a finding names it. */
|
|
46
|
+
export function quote(text: string): string {
|
|
47
|
+
return `'${excerpt(text)}'`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** A value read from YAML, as JSON, as a finding names a value of the wrong kind. */
|
|
51
|
+
export function quoteJson(value: unknown): string {
|
|
52
|
+
return excerpt(JSON.stringify(value));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** The findings one case's files produced. */
|
|
56
|
+
export class CaseFindings {
|
|
57
|
+
readonly list: EvalSuiteFinding[] = [];
|
|
58
|
+
|
|
59
|
+
add(kind: EvalFindingKind, path: string, problem: string): void {
|
|
60
|
+
this.list.push({ kind, path, message: `${path}: ${problem}` });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
get empty(): boolean {
|
|
64
|
+
return this.list.length === 0;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Where a field is being read: the file, how its fields are named, and the kind a wrong value is. */
|
|
69
|
+
export interface FieldScope {
|
|
70
|
+
readonly path: string;
|
|
71
|
+
/** Prepended to each field name in a sentence, e.g. "execution." or "grader 'x': ". */
|
|
72
|
+
readonly label: string;
|
|
73
|
+
readonly invalid: Extract<EvalFindingKind, "eval-case-invalid" | "eval-grader-invalid" | "eval-mock-invalid">;
|
|
74
|
+
readonly findings: CaseFindings;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export function wrong(scope: FieldScope, field: string, expected: string): undefined {
|
|
78
|
+
scope.findings.add(scope.invalid, scope.path, `${scope.label}${field} must be ${expected}`);
|
|
79
|
+
return undefined;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** Report every key of `object` not in `known` as `kind`, in document order. */
|
|
83
|
+
export function unknownKeys(
|
|
84
|
+
object: JsonObject,
|
|
85
|
+
known: ReadonlySet<string>,
|
|
86
|
+
scope: FieldScope,
|
|
87
|
+
kind: EvalFindingKind,
|
|
88
|
+
noun: string,
|
|
89
|
+
): void {
|
|
90
|
+
for (const key of Object.keys(object)) {
|
|
91
|
+
if (!known.has(key)) scope.findings.add(kind, scope.path, `unknown ${noun} ${quote(`${scope.label}${key}`)}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export function readString(object: JsonObject, field: string, scope: FieldScope): string | undefined {
|
|
96
|
+
const value = object[field];
|
|
97
|
+
if (value === undefined) return undefined;
|
|
98
|
+
if (typeof value !== "string") return wrong(scope, field, "a string");
|
|
99
|
+
return value;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function readNonEmptyString(object: JsonObject, field: string, scope: FieldScope): string | undefined {
|
|
103
|
+
const value = object[field];
|
|
104
|
+
if (value === undefined) return undefined;
|
|
105
|
+
if (typeof value !== "string" || value.trim() === "") return wrong(scope, field, "a non-empty string");
|
|
106
|
+
return value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function readStringList(object: JsonObject, field: string, scope: FieldScope): readonly string[] | undefined {
|
|
110
|
+
const value = object[field];
|
|
111
|
+
if (value === undefined) return undefined;
|
|
112
|
+
if (!isStringArray(value)) return wrong(scope, field, "a list of strings");
|
|
113
|
+
return value;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/** A whole number from `min` to `max`, inclusive. */
|
|
117
|
+
export function readInteger(object: JsonObject, field: string, min: number, max: number, scope: FieldScope): number | undefined {
|
|
118
|
+
const value = object[field];
|
|
119
|
+
if (value === undefined) return undefined;
|
|
120
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < min || value > max) {
|
|
121
|
+
return wrong(scope, field, `a whole number from ${min} to ${max}`);
|
|
122
|
+
}
|
|
123
|
+
return value;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Keys an `env` map may carry, the format's rule. */
|
|
127
|
+
export const EVAL_ENV_KEY_PATTERN = /^EVAL_[A-Z0-9_]*$/;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* An `env` map. A scalar value is kept as its string form, since YAML reads
|
|
131
|
+
* `EVAL_LEVEL: 2` as a number and the child process sees text either way.
|
|
132
|
+
*/
|
|
133
|
+
export function readEnv(object: JsonObject, field: string, scope: FieldScope): Readonly<Record<string, string>> | undefined {
|
|
134
|
+
const value = object[field];
|
|
135
|
+
if (value === undefined) return undefined;
|
|
136
|
+
if (!isJsonObject(value)) return wrong(scope, field, "a map of EVAL_* names to values");
|
|
137
|
+
const env: Record<string, string> = {};
|
|
138
|
+
let ok = true;
|
|
139
|
+
for (const [key, item] of Object.entries(value)) {
|
|
140
|
+
if (!EVAL_ENV_KEY_PATTERN.test(key)) {
|
|
141
|
+
scope.findings.add(scope.invalid, scope.path, `${scope.label}${field} key ${quote(key)} must match EVAL_[A-Z0-9_]*`);
|
|
142
|
+
ok = false;
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (typeof item !== "string" && typeof item !== "number" && typeof item !== "boolean") {
|
|
146
|
+
wrong(scope, `${field}.${excerpt(key)}`, "a string, number or boolean");
|
|
147
|
+
ok = false;
|
|
148
|
+
continue;
|
|
149
|
+
}
|
|
150
|
+
env[key] = String(item);
|
|
151
|
+
}
|
|
152
|
+
return ok ? env : undefined;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** The case format version this reader reads. */
|
|
156
|
+
export const EVAL_SCHEMA_VERSION = "1.1";
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* `schema_version`: `"1.1"`, or the number YAML reads from an unquoted
|
|
160
|
+
* `1.1`. `true` when it is that version, `false` after a finding.
|
|
161
|
+
*/
|
|
162
|
+
export function checkSchemaVersion(value: unknown, scope: FieldScope): boolean {
|
|
163
|
+
if (value === EVAL_SCHEMA_VERSION || value === 1.1) return true;
|
|
164
|
+
scope.findings.add(
|
|
165
|
+
"eval-schema-version-unsupported",
|
|
166
|
+
scope.path,
|
|
167
|
+
`schema_version ${quoteJson(value)} is not supported; this reader reads "${EVAL_SCHEMA_VERSION}"`,
|
|
168
|
+
);
|
|
169
|
+
return false;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* The compile error of a JavaScript regex, or `undefined` when it compiles.
|
|
174
|
+
* The engine's message repeats the whole pattern; it is quoted as an excerpt.
|
|
175
|
+
*/
|
|
176
|
+
export function regexError(pattern: string, flags: string): string | undefined {
|
|
177
|
+
try {
|
|
178
|
+
new RegExp(pattern, flags);
|
|
179
|
+
return undefined;
|
|
180
|
+
} catch (error) {
|
|
181
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
182
|
+
return message.replace(`/${pattern}/`, () => `/${excerpt(pattern)}/`);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a glob is well formed, in the grammar Stigmer's eval graders
|
|
3
|
+
* match with: `*`, `**`, `?`, `[...]` classes with ranges and `!` or `^`
|
|
4
|
+
* negation (a `]` right after the opening or the negation is a member),
|
|
5
|
+
* and `{a,b}` alternatives that may nest, expanding to at most
|
|
6
|
+
* GLOB_MAX_ALTERNATIVES and nesting at most GLOB_MAX_BRACE_DEPTH deep;
|
|
7
|
+
* everything else is literal, a `}` with no `{` included.
|
|
8
|
+
*
|
|
9
|
+
* A glob is also bounded in size, because a match costs its alternatives
|
|
10
|
+
* times their tokens times the path's length: at most GLOB_MAX_LENGTH
|
|
11
|
+
* characters, and at most GLOB_MAX_TOKENS tokens summed over every
|
|
12
|
+
* alternative it expands to. A token is a literal character, a `?`, a
|
|
13
|
+
* class, or a run of stars; a `/` right after `**` counts as its own token,
|
|
14
|
+
* so the count does not depend on whether the glob matches paths or names,
|
|
15
|
+
* and a class counts once per range or character it holds, since the
|
|
16
|
+
* matcher tests a character against each of them. A class holds at most
|
|
17
|
+
* GLOB_MAX_CLASS_ITEMS ranges or characters.
|
|
18
|
+
*
|
|
19
|
+
* Only validity is checked here: the suite reader refuses a `file_exists`
|
|
20
|
+
* grader whose `path` is malformed (too long, a class or a brace never
|
|
21
|
+
* closed, a reversed range, a class too large, too many alternatives or
|
|
22
|
+
* tokens, braces nested too deep), so the author hears it at push rather than as a try left not
|
|
23
|
+
* graded. The server
|
|
24
|
+
* compiles and matches globs with its own matcher over the same grammar,
|
|
25
|
+
* and its tests pin the two readers to one table, error sentences
|
|
26
|
+
* included.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
/** The most alternatives a glob's braces may expand to. */
|
|
30
|
+
export const GLOB_MAX_ALTERNATIVES = 64;
|
|
31
|
+
|
|
32
|
+
/** The deepest braces may nest. */
|
|
33
|
+
export const GLOB_MAX_BRACE_DEPTH = 16;
|
|
34
|
+
|
|
35
|
+
/** The most characters (code points) a glob may hold. */
|
|
36
|
+
export const GLOB_MAX_LENGTH = 1024;
|
|
37
|
+
|
|
38
|
+
/** The most tokens a glob may expand to, summed over its alternatives. */
|
|
39
|
+
export const GLOB_MAX_TOKENS = 4096;
|
|
40
|
+
|
|
41
|
+
/** The most ranges or characters one `[...]` class may hold. */
|
|
42
|
+
export const GLOB_MAX_CLASS_ITEMS = 32;
|
|
43
|
+
|
|
44
|
+
/** A sequence's alternatives and their tokens in all, each capped one past its limit. */
|
|
45
|
+
interface Size {
|
|
46
|
+
readonly count: number;
|
|
47
|
+
readonly tokens: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Whether `glob` holds more than GLOB_MAX_LENGTH code points, counting no further. */
|
|
51
|
+
function tooLong(glob: string): boolean {
|
|
52
|
+
if (glob.length <= GLOB_MAX_LENGTH) return false;
|
|
53
|
+
let points = 0;
|
|
54
|
+
for (const _ of glob) {
|
|
55
|
+
if (++points > GLOB_MAX_LENGTH) return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** Why `glob` is malformed, or `undefined` when it is a glob. */
|
|
61
|
+
export function globError(glob: string): string | undefined {
|
|
62
|
+
if (tooLong(glob)) return `it is longer than ${GLOB_MAX_LENGTH} characters`;
|
|
63
|
+
const chars = Array.from(glob);
|
|
64
|
+
let pos = 0;
|
|
65
|
+
const cap = GLOB_MAX_ALTERNATIVES + 1;
|
|
66
|
+
const tokenCap = GLOB_MAX_TOKENS + 1;
|
|
67
|
+
|
|
68
|
+
/** Skips a class from its `[`: its ranges and characters, or the error when it never closes, holds a reversed range or holds too many. */
|
|
69
|
+
const skipClass = (): number | string => {
|
|
70
|
+
const open = pos;
|
|
71
|
+
let i = open + 1;
|
|
72
|
+
if (chars[i] === "!" || chars[i] === "^") i++;
|
|
73
|
+
let first = true;
|
|
74
|
+
let items = 0;
|
|
75
|
+
for (;;) {
|
|
76
|
+
const char = chars[i];
|
|
77
|
+
if (char === undefined) return `'[' at ${open} is never closed`;
|
|
78
|
+
if (char === "]" && !first) break;
|
|
79
|
+
first = false;
|
|
80
|
+
const end = chars[i + 2];
|
|
81
|
+
if (chars[i + 1] === "-" && end !== undefined && end !== "]") {
|
|
82
|
+
if ((char.codePointAt(0) ?? 0) > (end.codePointAt(0) ?? 0)) return `range '${char}-${end}' is reversed`;
|
|
83
|
+
i += 3;
|
|
84
|
+
} else {
|
|
85
|
+
i++;
|
|
86
|
+
}
|
|
87
|
+
if (++items > GLOB_MAX_CLASS_ITEMS) return `'[' at ${open} holds more than ${GLOB_MAX_CLASS_ITEMS} ranges or characters`;
|
|
88
|
+
}
|
|
89
|
+
pos = i + 1;
|
|
90
|
+
return items;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
/** The size of a sequence, up to the end or, inside braces, its `,` or `}`. */
|
|
94
|
+
const sequence = (depth: number): Size | string => {
|
|
95
|
+
let count = 1;
|
|
96
|
+
let tokens = 0;
|
|
97
|
+
while (pos < chars.length) {
|
|
98
|
+
const char = chars[pos];
|
|
99
|
+
if (depth > 0 && (char === "," || char === "}")) return { count, tokens };
|
|
100
|
+
if (char === "{") {
|
|
101
|
+
const open = pos;
|
|
102
|
+
if (depth + 1 > GLOB_MAX_BRACE_DEPTH) return `braces are nested more than ${GLOB_MAX_BRACE_DEPTH} deep`;
|
|
103
|
+
pos++;
|
|
104
|
+
let sum = 0;
|
|
105
|
+
let sumTokens = 0;
|
|
106
|
+
for (;;) {
|
|
107
|
+
const inner = sequence(depth + 1);
|
|
108
|
+
if (typeof inner === "string") return inner;
|
|
109
|
+
sum = Math.min(sum + inner.count, cap);
|
|
110
|
+
sumTokens = Math.min(sumTokens + inner.tokens, tokenCap);
|
|
111
|
+
const close = chars[pos];
|
|
112
|
+
if (close === undefined) return `'{' at ${open} is never closed`;
|
|
113
|
+
pos++;
|
|
114
|
+
if (close === "}") break;
|
|
115
|
+
}
|
|
116
|
+
// Each head alternative is followed by every tail: n heads of t tokens
|
|
117
|
+
// in all, m tails of s, make n*m alternatives of t*m + s*n tokens.
|
|
118
|
+
tokens = Math.min(tokens * sum + sumTokens * count, tokenCap);
|
|
119
|
+
count = Math.min(count * sum, cap);
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
let weight = 1;
|
|
123
|
+
if (char === "[") {
|
|
124
|
+
const items = skipClass();
|
|
125
|
+
if (typeof items === "string") return items;
|
|
126
|
+
weight = items;
|
|
127
|
+
} else if (char === "*") {
|
|
128
|
+
while (chars[pos] === "*") pos++;
|
|
129
|
+
} else {
|
|
130
|
+
pos++;
|
|
131
|
+
}
|
|
132
|
+
tokens = Math.min(tokens + count * weight, tokenCap);
|
|
133
|
+
}
|
|
134
|
+
return { count, tokens };
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
const result = sequence(0);
|
|
138
|
+
if (typeof result === "string") return result;
|
|
139
|
+
if (result.count > GLOB_MAX_ALTERNATIVES) return `it expands to more than ${GLOB_MAX_ALTERNATIVES} alternatives`;
|
|
140
|
+
return result.tokens > GLOB_MAX_TOKENS ? `it expands to more than ${GLOB_MAX_TOKENS} tokens` : undefined;
|
|
141
|
+
}
|
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One grader, from a `graders/<name>.md` file's frontmatter and body or a
|
|
3
|
+
* `case.yaml` `graders:` entry, into the normalised `EvalGrader`.
|
|
4
|
+
*
|
|
5
|
+
* The keys are the format's: `type`, `weight` and `arm` for every grader,
|
|
6
|
+
* plus the options of its type, and `name` on a `case.yaml` entry. A key
|
|
7
|
+
* outside that set, or a value of the wrong type, is `eval-grader-invalid`
|
|
8
|
+
* and the case does not load: a grader read leniently is a different check
|
|
9
|
+
* from the one the author wrote, and a score built on it is wrong.
|
|
10
|
+
*
|
|
11
|
+
* In the `.md` layout the body carries the text a grader needs most: the
|
|
12
|
+
* rubric of an `llm` or `baseline` grader, or a `regex` grader's pattern.
|
|
13
|
+
* A frontmatter key of the same name wins when both are present, so a
|
|
14
|
+
* file that sets `criteria:` and also keeps notes in its body grades on
|
|
15
|
+
* the key. The body of a criteria is trimmed (a judge reads it as prose);
|
|
16
|
+
* a pattern from the body is trimmed too, since its trailing newline would
|
|
17
|
+
* otherwise be part of the expression.
|
|
18
|
+
*
|
|
19
|
+
* A weight is at most {@link EVAL_MAX_GRADER_WEIGHT}, so a weighted mean
|
|
20
|
+
* of passing checks never overflows to a score that is not a number.
|
|
21
|
+
*
|
|
22
|
+
* Defaults the format documents are applied here: weight 1, regex flags
|
|
23
|
+
* empty and `match: contains`, `target`/`focus` `last_message`,
|
|
24
|
+
* `tool_used` min 1 and max unlimited, `file_exists` `exists: true`. A
|
|
25
|
+
* `file_exists` `path` must be a well-formed glob (glob.ts).
|
|
26
|
+
*/
|
|
27
|
+
|
|
28
|
+
import { isJsonObject, type JsonObject } from "../documents.js";
|
|
29
|
+
import {
|
|
30
|
+
type FieldScope,
|
|
31
|
+
quote,
|
|
32
|
+
readNonEmptyString,
|
|
33
|
+
readString,
|
|
34
|
+
regexError,
|
|
35
|
+
unknownKeys,
|
|
36
|
+
wrong,
|
|
37
|
+
} from "./fields.js";
|
|
38
|
+
import { globError } from "./glob.js";
|
|
39
|
+
import type { EvalFocus, EvalGrader, EvalGraderArm, EvalGraderCheck, EvalGraderType, EvalToolRef } from "./types.js";
|
|
40
|
+
|
|
41
|
+
const COMMON_KEYS = ["type", "weight", "arm"] as const;
|
|
42
|
+
|
|
43
|
+
/** Each type's own options, the format's "Grader types" table. */
|
|
44
|
+
const TYPE_KEYS: Readonly<Record<EvalGraderType, readonly string[]>> = {
|
|
45
|
+
regex: ["pattern", "flags", "match", "target"],
|
|
46
|
+
tool_used: ["tool", "input_match", "min", "max"],
|
|
47
|
+
tool_order: ["before", "after"],
|
|
48
|
+
file_exists: ["path", "exists"],
|
|
49
|
+
llm: ["criteria", "focus"],
|
|
50
|
+
baseline: ["baseline_file", "criteria"],
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
const GRADER_TYPES = Object.keys(TYPE_KEYS) as readonly EvalGraderType[];
|
|
54
|
+
|
|
55
|
+
function isGraderType(value: unknown): value is EvalGraderType {
|
|
56
|
+
return typeof value === "string" && (GRADER_TYPES as readonly string[]).includes(value);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** What one grader is read from. */
|
|
60
|
+
export interface GraderSource {
|
|
61
|
+
/** The grader's name: the file stem, or the entry's `name`. */
|
|
62
|
+
readonly name: string;
|
|
63
|
+
/** The file that declared it, plugin-relative. */
|
|
64
|
+
readonly path: string;
|
|
65
|
+
/** The frontmatter or the entry, `name` removed. */
|
|
66
|
+
readonly fields: JsonObject;
|
|
67
|
+
/** A `.md` file's body; `undefined` for a `case.yaml` entry. */
|
|
68
|
+
readonly body?: string;
|
|
69
|
+
/** The case's files, to check a `baseline_file` is one of them. */
|
|
70
|
+
readonly caseDir: string;
|
|
71
|
+
readonly caseFiles: ReadonlySet<string>;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/** The grader, or `undefined` after at least one finding in `scope`. */
|
|
75
|
+
export function readGrader(source: GraderSource, scope: FieldScope, noun: string): EvalGrader | undefined {
|
|
76
|
+
const { fields } = source;
|
|
77
|
+
const before = scope.findings.list.length;
|
|
78
|
+
const type = fields["type"];
|
|
79
|
+
if (type === undefined) {
|
|
80
|
+
scope.findings.add(scope.invalid, scope.path, `${scope.label}type is required (one of ${GRADER_TYPES.join(", ")})`);
|
|
81
|
+
return undefined;
|
|
82
|
+
}
|
|
83
|
+
if (!isGraderType(type)) {
|
|
84
|
+
return wrong(scope, "type", `one of ${GRADER_TYPES.join(", ")}`);
|
|
85
|
+
}
|
|
86
|
+
unknownKeys(fields, new Set<string>([...COMMON_KEYS, ...TYPE_KEYS[type]]), scope, scope.invalid, noun);
|
|
87
|
+
|
|
88
|
+
const weight = readWeight(fields, scope);
|
|
89
|
+
const arm = readArm(fields, scope);
|
|
90
|
+
const check = readCheck(type, source, scope);
|
|
91
|
+
if (scope.findings.list.length > before || check === undefined) return undefined;
|
|
92
|
+
return {
|
|
93
|
+
name: source.name,
|
|
94
|
+
path: source.path,
|
|
95
|
+
weight: weight ?? 1,
|
|
96
|
+
...(arm !== undefined && { arm }),
|
|
97
|
+
check,
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The heaviest weight a grader may carry. A try's score is a weighted mean
|
|
103
|
+
* over a case's at most 32 graders, so this keeps every sum of weights and
|
|
104
|
+
* of weighted scores finite.
|
|
105
|
+
*/
|
|
106
|
+
export const EVAL_MAX_GRADER_WEIGHT = 1000;
|
|
107
|
+
|
|
108
|
+
function readWeight(fields: JsonObject, scope: FieldScope): number | undefined {
|
|
109
|
+
const value = fields["weight"];
|
|
110
|
+
if (value === undefined) return undefined;
|
|
111
|
+
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0 || value > EVAL_MAX_GRADER_WEIGHT) {
|
|
112
|
+
return wrong(scope, "weight", `a positive number no larger than ${EVAL_MAX_GRADER_WEIGHT}`);
|
|
113
|
+
}
|
|
114
|
+
return value;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function readArm(fields: JsonObject, scope: FieldScope): EvalGraderArm | undefined {
|
|
118
|
+
const value = fields["arm"];
|
|
119
|
+
if (value === undefined) return undefined;
|
|
120
|
+
if (value !== "with-only" && value !== "both") return wrong(scope, "arm", "'with-only' or 'both'");
|
|
121
|
+
return value;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function readCheck(type: EvalGraderType, source: GraderSource, scope: FieldScope): EvalGraderCheck | undefined {
|
|
125
|
+
const { fields } = source;
|
|
126
|
+
switch (type) {
|
|
127
|
+
case "regex":
|
|
128
|
+
return readRegex(source, scope);
|
|
129
|
+
case "tool_used": {
|
|
130
|
+
const tool = readNonEmptyString(fields, "tool", scope);
|
|
131
|
+
const inputMatch = readPattern(fields, "input_match", scope);
|
|
132
|
+
const writtenMin = readCount(fields, "min", scope);
|
|
133
|
+
const max = readCount(fields, "max", scope);
|
|
134
|
+
if (tool === undefined) return required(scope, "tool", fields);
|
|
135
|
+
// A max alone lowers the default min of 1 to it, so `max: 0` reads as
|
|
136
|
+
// "never called" rather than a range nothing can satisfy.
|
|
137
|
+
const min = writtenMin ?? (max === undefined ? 1 : Math.min(1, max));
|
|
138
|
+
if (max !== undefined && min > max) return wrong(scope, "min", `at most max (${max})`);
|
|
139
|
+
return {
|
|
140
|
+
type,
|
|
141
|
+
tool,
|
|
142
|
+
...(inputMatch !== undefined && { inputMatch }),
|
|
143
|
+
min,
|
|
144
|
+
...(max !== undefined && { max }),
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
case "tool_order": {
|
|
148
|
+
const before = readToolRef(fields, "before", scope);
|
|
149
|
+
const after = readToolRef(fields, "after", scope);
|
|
150
|
+
if (before === undefined) return required(scope, "before", fields);
|
|
151
|
+
if (after === undefined) return required(scope, "after", fields);
|
|
152
|
+
return { type, before, after };
|
|
153
|
+
}
|
|
154
|
+
case "file_exists": {
|
|
155
|
+
const path = readNonEmptyString(fields, "path", scope);
|
|
156
|
+
const exists = fields["exists"];
|
|
157
|
+
if (exists !== undefined && typeof exists !== "boolean") return wrong(scope, "exists", "true or false");
|
|
158
|
+
if (path === undefined) return required(scope, "path", fields);
|
|
159
|
+
const malformed = globError(path);
|
|
160
|
+
if (malformed !== undefined) return wrong(scope, "path", `a glob (${malformed})`);
|
|
161
|
+
return { type, path, exists: exists ?? true };
|
|
162
|
+
}
|
|
163
|
+
case "llm": {
|
|
164
|
+
const criteria = readCriteria(source, scope);
|
|
165
|
+
const focus = readFocus(fields["focus"], "focus", scope);
|
|
166
|
+
if (criteria === undefined || focus === undefined) return undefined;
|
|
167
|
+
return { type, criteria, focus };
|
|
168
|
+
}
|
|
169
|
+
case "baseline": {
|
|
170
|
+
const criteria = readCriteria(source, scope);
|
|
171
|
+
const baselineFile = readNonEmptyString(fields, "baseline_file", scope);
|
|
172
|
+
if (baselineFile === undefined) return required(scope, "baseline_file", fields);
|
|
173
|
+
if (!source.caseFiles.has(`${source.caseDir}/${baselineFile.replace(/^\.\//, "")}`)) {
|
|
174
|
+
return wrong(scope, "baseline_file", `a file in the case directory (${quote(baselineFile)} is not)`);
|
|
175
|
+
}
|
|
176
|
+
if (criteria === undefined) return undefined;
|
|
177
|
+
return { type, baselineFile, criteria };
|
|
178
|
+
}
|
|
179
|
+
/* v8 ignore start -- @preserve: the never arm; readGrader admits only GRADER_TYPES, so no type reaches it */
|
|
180
|
+
default: {
|
|
181
|
+
const exhaustive: never = type;
|
|
182
|
+
throw new Error(`unknown grader type ${String(exhaustive)}`);
|
|
183
|
+
}
|
|
184
|
+
/* v8 ignore stop */
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** A required option that is absent; a present one of the wrong type already reported itself. */
|
|
189
|
+
function required(scope: FieldScope, field: string, fields: JsonObject): undefined {
|
|
190
|
+
if (fields[field] === undefined) scope.findings.add(scope.invalid, scope.path, `${scope.label}${field} is required`);
|
|
191
|
+
return undefined;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function readRegex(source: GraderSource, scope: FieldScope): EvalGraderCheck | undefined {
|
|
195
|
+
const { fields } = source;
|
|
196
|
+
const flags = readString(fields, "flags", scope) ?? "";
|
|
197
|
+
const fromBody = source.body?.trim();
|
|
198
|
+
const pattern =
|
|
199
|
+
fields["pattern"] !== undefined ? readNonEmptyString(fields, "pattern", scope) : fromBody === "" ? undefined : fromBody;
|
|
200
|
+
const match = readMatch(fields["match"], scope);
|
|
201
|
+
const target = readFocus(fields["target"], "target", scope);
|
|
202
|
+
if (pattern === undefined) {
|
|
203
|
+
if (fields["pattern"] === undefined) {
|
|
204
|
+
scope.findings.add(scope.invalid, scope.path, `${scope.label}pattern is required`);
|
|
205
|
+
}
|
|
206
|
+
return undefined;
|
|
207
|
+
}
|
|
208
|
+
if (regexError("", flags) !== undefined) return wrong(scope, "flags", "JavaScript regular expression flags, such as 'i'");
|
|
209
|
+
const error = regexError(pattern, flags);
|
|
210
|
+
if (error !== undefined) return wrong(scope, "pattern", `a JavaScript regular expression (${error})`);
|
|
211
|
+
if (match === undefined || target === undefined) return undefined;
|
|
212
|
+
return { type: "regex", pattern, flags, match, target };
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
type RegexMatch = Extract<EvalGraderCheck, { type: "regex" }>["match"];
|
|
216
|
+
|
|
217
|
+
const COUNT_MATCH = /^count:(\d+)$/;
|
|
218
|
+
|
|
219
|
+
function readMatch(value: unknown, scope: FieldScope): RegexMatch | undefined {
|
|
220
|
+
if (value === undefined || value === "contains") return { kind: "contains" };
|
|
221
|
+
if (value === "not_contains") return { kind: "not_contains" };
|
|
222
|
+
const count = typeof value === "string" ? COUNT_MATCH.exec(value) : null;
|
|
223
|
+
if (count?.[1] !== undefined) return { kind: "count", count: Number(count[1]) };
|
|
224
|
+
return wrong(scope, "match", "'contains', 'not_contains' or \"count:N\"");
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const NAMED_FOCUS: Readonly<Record<string, EvalFocus>> = {
|
|
228
|
+
last_message: { kind: "last_message" },
|
|
229
|
+
trace: { kind: "trace" },
|
|
230
|
+
files: { kind: "files" },
|
|
231
|
+
mock_calls: { kind: "mock_calls" },
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
/** A `target` or `focus`; absent is `last_message`. */
|
|
235
|
+
function readFocus(value: unknown, field: string, scope: FieldScope): EvalFocus | undefined {
|
|
236
|
+
if (value === undefined) return { kind: "last_message" };
|
|
237
|
+
const expected = "last_message, trace, files, mock_calls, or { source: file, path: <path> }";
|
|
238
|
+
// Own keys only: an author's `toString` or `constructor` is not a focus.
|
|
239
|
+
if (typeof value === "string") return Object.hasOwn(NAMED_FOCUS, value) ? NAMED_FOCUS[value] : wrong(scope, field, expected);
|
|
240
|
+
if (!isJsonObject(value)) return wrong(scope, field, expected);
|
|
241
|
+
const keys = Object.keys(value);
|
|
242
|
+
const path = value["path"];
|
|
243
|
+
if (value["source"] !== "file" || typeof path !== "string" || path.trim() === "" || keys.some((k) => k !== "source" && k !== "path")) {
|
|
244
|
+
return wrong(scope, field, expected);
|
|
245
|
+
}
|
|
246
|
+
return { kind: "file", path };
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** A `tool_order` side: a tool name, or `{ tool, input_match }`. */
|
|
250
|
+
function readToolRef(fields: JsonObject, field: string, scope: FieldScope): EvalToolRef | undefined {
|
|
251
|
+
const value = fields[field];
|
|
252
|
+
if (value === undefined) return undefined;
|
|
253
|
+
if (typeof value === "string" && value.trim() !== "") return { tool: value };
|
|
254
|
+
const expected = "a tool name or { tool, input_match }";
|
|
255
|
+
if (!isJsonObject(value)) return wrong(scope, field, expected);
|
|
256
|
+
const tool = value["tool"];
|
|
257
|
+
if (typeof tool !== "string" || tool.trim() === "" || Object.keys(value).some((k) => k !== "tool" && k !== "input_match")) {
|
|
258
|
+
return wrong(scope, field, expected);
|
|
259
|
+
}
|
|
260
|
+
const inputMatch = readPattern(value, "input_match", { ...scope, label: `${scope.label}${field}.` });
|
|
261
|
+
if (value["input_match"] !== undefined && inputMatch === undefined) return undefined;
|
|
262
|
+
return { tool, ...(inputMatch !== undefined && { inputMatch }) };
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/** An `input_match` regex, checked to compile. */
|
|
266
|
+
function readPattern(fields: JsonObject, field: string, scope: FieldScope): string | undefined {
|
|
267
|
+
const pattern = readString(fields, field, scope);
|
|
268
|
+
if (pattern === undefined) return undefined;
|
|
269
|
+
const error = regexError(pattern, "");
|
|
270
|
+
if (error !== undefined) return wrong(scope, field, `a JavaScript regular expression (${error})`);
|
|
271
|
+
return pattern;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function readCount(fields: JsonObject, field: string, scope: FieldScope): number | undefined {
|
|
275
|
+
const value = fields[field];
|
|
276
|
+
if (value === undefined) return undefined;
|
|
277
|
+
if (typeof value !== "number" || !Number.isInteger(value) || value < 0) return wrong(scope, field, "a whole number, 0 or more");
|
|
278
|
+
return value;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** The rubric: the `criteria` key, else a `.md` file's body. */
|
|
282
|
+
function readCriteria(source: GraderSource, scope: FieldScope): string | undefined {
|
|
283
|
+
const { fields } = source;
|
|
284
|
+
if (fields["criteria"] !== undefined) {
|
|
285
|
+
const criteria = readNonEmptyString(fields, "criteria", scope);
|
|
286
|
+
return criteria?.trim();
|
|
287
|
+
}
|
|
288
|
+
const body = source.body?.trim() ?? "";
|
|
289
|
+
if (body !== "") return body;
|
|
290
|
+
const where = source.body === undefined ? "" : " (the file's body, or a criteria key)";
|
|
291
|
+
scope.findings.add(scope.invalid, scope.path, `${scope.label}criteria is required${where}`);
|
|
292
|
+
return undefined;
|
|
293
|
+
}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The suite's documents as the reader opens them: a file's text through the
|
|
3
|
+
* `evalDocument` cap, a YAML mapping, and a Markdown file with optional
|
|
4
|
+
* frontmatter. Each records a finding and returns `undefined` when the file
|
|
5
|
+
* cannot be read, so the caller keeps reading the rest. Shared by the case
|
|
6
|
+
* reader (`read-eval-suite.ts`) and the mock reader (`mocks.ts`).
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { type JsonObject, readCapped } from "../documents.js";
|
|
10
|
+
import { decodeUtf8, type PluginFileIndex } from "../files.js";
|
|
11
|
+
import { extractFrontmatter, parseFrontmatter } from "../frontmatter.js";
|
|
12
|
+
import type { CaseFindings, EvalFindingKind, FieldScope } from "./fields.js";
|
|
13
|
+
|
|
14
|
+
/** One document's text through the `evalDocument` cap, or `undefined` after a finding of `kind`. */
|
|
15
|
+
export function readDocument(index: PluginFileIndex, path: string, kind: EvalFindingKind, findings: CaseFindings): string | undefined {
|
|
16
|
+
const read = readCapped(index, path, "evalDocument");
|
|
17
|
+
if (!read.ok) {
|
|
18
|
+
findings.add(kind, path, `the file is ${read.size} bytes, over the ${read.limit}-byte limit`);
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
return decodeUtf8(read.bytes);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** A YAML mapping, or `undefined` after a finding. */
|
|
25
|
+
export function parseMapping(yaml: string, scope: FieldScope, what: string): JsonObject | undefined {
|
|
26
|
+
const parsed = parseFrontmatter(yaml);
|
|
27
|
+
if (!parsed.ok) {
|
|
28
|
+
scope.findings.add(scope.invalid, scope.path, `${what} is not valid YAML: ${parsed.detail}`);
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
return parsed.fields;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface MarkdownDocument {
|
|
35
|
+
readonly fields: JsonObject;
|
|
36
|
+
readonly body: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A Markdown file with optional frontmatter: no opening `---` is a body
|
|
41
|
+
* with no fields, an unclosed one is a finding.
|
|
42
|
+
*/
|
|
43
|
+
export function readMarkdown(index: PluginFileIndex, scope: FieldScope): MarkdownDocument | undefined {
|
|
44
|
+
const text = readDocument(index, scope.path, scope.invalid, scope.findings);
|
|
45
|
+
if (text === undefined) return undefined;
|
|
46
|
+
const extracted = extractFrontmatter(text);
|
|
47
|
+
if (!extracted.ok) {
|
|
48
|
+
if (extracted.reason === "unclosed") {
|
|
49
|
+
scope.findings.add(scope.invalid, scope.path, "the frontmatter is not closed (missing the closing '---')");
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
return { fields: {}, body: text };
|
|
53
|
+
}
|
|
54
|
+
const fields = parseMapping(extracted.yaml, scope, "the frontmatter");
|
|
55
|
+
return fields === undefined ? undefined : { fields, body: extracted.body };
|
|
56
|
+
}
|