@agentswf/lab 0.0.0-stage → 0.3.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/LICENSE +21 -0
- package/README.md +15 -2
- package/package.json +46 -4
- package/schema/case-check.schema.json +64 -0
- package/schema/case-config.schema.json +187 -0
- package/schema/case-final.schema.json +48 -0
- package/schema/case-good-record.schema.json +70 -0
- package/schema/case-grade.schema.json +49 -0
- package/schema/case-key.schema.json +117 -0
- package/schema/case-label.schema.json +32 -0
- package/schema/case-loop-report.schema.json +210 -0
- package/schema/case-loop.schema.json +141 -0
- package/schema/case-plan.schema.json +117 -0
- package/schema/case-previous.schema.json +70 -0
- package/schema/case-proposal.schema.json +52 -0
- package/schema/case-report.schema.json +892 -0
- package/schema/case-run.schema.json +253 -0
- package/schema/case-runs.schema.json +125 -0
- package/schema/case-score.schema.json +319 -0
- package/schema/case-spread.schema.json +110 -0
- package/schema/case-trial.schema.json +543 -0
- package/schema/case-try.schema.json +113 -0
- package/schema/local.json +155 -0
- package/src/cli.ts +9 -0
- package/src/env/capture.ts +48 -0
- package/src/env/compare.ts +201 -0
- package/src/env/facts.ts +51 -0
- package/src/env/fetch.ts +57 -0
- package/src/env/files.ts +228 -0
- package/src/env/grade-case.ts +80 -0
- package/src/env/grade-helper.ts +352 -0
- package/src/env/grader.ts +128 -0
- package/src/env/index.ts +727 -0
- package/src/env/mock.ts +374 -0
- package/src/env/routes.ts +90 -0
- package/src/env/sources.ts +19 -0
- package/src/env/state.ts +79 -0
- package/src/env/trial.ts +182 -0
- package/src/format/key.ts +144 -0
- package/src/format/loop.ts +178 -0
- package/src/format/output.ts +481 -0
- package/src/format/records.ts +440 -0
- package/src/format/schema-files.ts +64 -0
- package/src/format/settings.ts +552 -0
- package/src/format/trial-env.ts +64 -0
- package/src/format/trial-file.ts +134 -0
- package/src/help/awf-lab.txt +29 -0
- package/src/help/case.txt +38 -0
- package/src/help/config.txt +14 -0
- package/src/help/label.txt +12 -0
- package/src/help/loop.txt +44 -0
- package/src/help/report.txt +20 -0
- package/src/help/run.txt +60 -0
- package/src/help/runs.txt +10 -0
- package/src/help/score.txt +27 -0
- package/src/judge/criteria.workflow.ts +237 -0
- package/src/judge/version.ts +6 -0
- package/src/lab/answer.ts +47 -0
- package/src/lab/blame.ts +204 -0
- package/src/lab/box.ts +249 -0
- package/src/lab/capture-command.ts +298 -0
- package/src/lab/capture.ts +234 -0
- package/src/lab/check-command.ts +187 -0
- package/src/lab/cli.ts +324 -0
- package/src/lab/code-graders.ts +108 -0
- package/src/lab/columns.ts +138 -0
- package/src/lab/command-context.ts +436 -0
- package/src/lab/compare.ts +123 -0
- package/src/lab/config-command.ts +278 -0
- package/src/lab/criteria.ts +69 -0
- package/src/lab/dataset.ts +356 -0
- package/src/lab/eval-files.ts +185 -0
- package/src/lab/expect.ts +79 -0
- package/src/lab/facts.ts +59 -0
- package/src/lab/families.ts +32 -0
- package/src/lab/folders.ts +153 -0
- package/src/lab/git.ts +128 -0
- package/src/lab/good.ts +224 -0
- package/src/lab/grading-stub.txt +8 -0
- package/src/lab/grading.ts +332 -0
- package/src/lab/inspect.ts +226 -0
- package/src/lab/labels.ts +206 -0
- package/src/lab/loop-code.ts +255 -0
- package/src/lab/loop-command.ts +770 -0
- package/src/lab/loop-records.ts +144 -0
- package/src/lab/loop.ts +859 -0
- package/src/lab/marker.ts +23 -0
- package/src/lab/outcome.ts +196 -0
- package/src/lab/pooled.ts +106 -0
- package/src/lab/project-env.ts +39 -0
- package/src/lab/rates.ts +150 -0
- package/src/lab/reach.ts +111 -0
- package/src/lab/record-copies.ts +202 -0
- package/src/lab/refusal.ts +2 -0
- package/src/lab/report-command.ts +156 -0
- package/src/lab/report.ts +838 -0
- package/src/lab/reuse.ts +126 -0
- package/src/lab/run-command.ts +853 -0
- package/src/lab/run-logs.ts +89 -0
- package/src/lab/run-report.ts +259 -0
- package/src/lab/runs-command.ts +163 -0
- package/src/lab/runs.ts +172 -0
- package/src/lab/scaffold.ts +91 -0
- package/src/lab/score-command.ts +189 -0
- package/src/lab/score.ts +396 -0
- package/src/lab/store.ts +383 -0
- package/src/lab/text.ts +29 -0
- package/src/lab/trial-settings.ts +100 -0
- package/src/lab/trial-stub.txt +18 -0
- package/src/lab/trial.ts +443 -0
- package/src/lab/usage.ts +273 -0
- package/src/lab/verdict.ts +161 -0
- package/src/lab/workflow-graders.ts +161 -0
- package/src/lab/workspace.ts +489 -0
- package/src/loop/propose.workflow.ts +84 -0
- package/src/shared/allowance.ts +185 -0
- package/src/shared/compare/index.ts +3 -0
- package/src/shared/compare/paired.ts +397 -0
- package/src/shared/compare/stats.ts +139 -0
- package/src/shared/compare/types.ts +75 -0
- package/src/shared/config.ts +15 -0
- package/src/shared/runner.ts +321 -0
- package/src/shared/validate.ts +181 -0
- package/src/shared/version.ts +41 -0
- package/src/write-schemas.ts +10 -0
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { appendFileSync, existsSync, mkdirSync, readFileSync } from "node:fs";
|
|
2
|
+
import { dirname, join } from "node:path";
|
|
3
|
+
import { type LabelRecord, LabelRecordSchema } from "../format/records";
|
|
4
|
+
import { checkSchema, describeProblems } from "../shared/validate";
|
|
5
|
+
import { versionKey } from "../shared/version";
|
|
6
|
+
import { newestEntries, trialOutcome, votersOf } from "./outcome";
|
|
7
|
+
import { Refusal } from "./refusal";
|
|
8
|
+
import type { StoredTrial } from "./store";
|
|
9
|
+
|
|
10
|
+
// The person's labels: `{store}/labels/{dataset}.jsonl`, one line per label, newest last. They
|
|
11
|
+
// are on the trial, found by its workflow run's id, not on each criterion; a note says why.
|
|
12
|
+
|
|
13
|
+
export const labelsFileOf = (store: string, dataset: string) =>
|
|
14
|
+
join(store, "labels", `${dataset}.jsonl`);
|
|
15
|
+
|
|
16
|
+
export function appendLabel(store: string, dataset: string, label: LabelRecord): void {
|
|
17
|
+
const file = labelsFileOf(store, dataset);
|
|
18
|
+
const checked = checkSchema(LabelRecordSchema, label);
|
|
19
|
+
if (!checked.ok) throw new Error(describeProblems(file, checked.problems));
|
|
20
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
21
|
+
appendFileSync(file, `${JSON.stringify(checked.value)}\n`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readLines(file: string): LabelRecord[] {
|
|
25
|
+
if (!existsSync(file)) return [];
|
|
26
|
+
return readFileSync(file, "utf8")
|
|
27
|
+
.split("\n")
|
|
28
|
+
.filter((line) => line.trim() !== "")
|
|
29
|
+
.flatMap((line) => {
|
|
30
|
+
let value: unknown;
|
|
31
|
+
try {
|
|
32
|
+
value = JSON.parse(line);
|
|
33
|
+
} catch {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
// The person's labels never drop out unseen: one that doesn't read stops the command.
|
|
37
|
+
const checked = checkSchema(LabelRecordSchema, value);
|
|
38
|
+
if (!checked.ok) throw new Refusal(describeProblems(file, checked.problems));
|
|
39
|
+
return [checked.value];
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Each labeled trial's newest label, by its workflow run id; a cleared one is no label. */
|
|
44
|
+
export function readLabels(store: string, dataset: string): Map<string, LabelRecord> {
|
|
45
|
+
const labels = new Map<string, LabelRecord>();
|
|
46
|
+
const lines = readLines(labelsFileOf(store, dataset));
|
|
47
|
+
// A stable sort: two labels made in the same instant keep the order they were written in.
|
|
48
|
+
for (const line of [...lines].sort((x, y) => x.at.localeCompare(y.at))) {
|
|
49
|
+
if (line.mark === "cleared") labels.delete(line.runId);
|
|
50
|
+
else labels.set(line.runId, line);
|
|
51
|
+
}
|
|
52
|
+
return labels;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
type Tally = { labeled: number; agreed: number };
|
|
56
|
+
|
|
57
|
+
/** How a grader, or the lab as a whole, agrees with the person's labels. */
|
|
58
|
+
export type Agreement = {
|
|
59
|
+
/** Trials marked good, and how many the lab passed. */
|
|
60
|
+
good: Tally;
|
|
61
|
+
/** Trials marked needs-work, and how many the lab failed. */
|
|
62
|
+
needsWork: Tally;
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
export type PersonAgreement = Agreement & {
|
|
66
|
+
/** Labeled trials the lab neither passed nor failed: errored, undecided, by numbers alone. */
|
|
67
|
+
notCompared: number;
|
|
68
|
+
/**
|
|
69
|
+
* Each grader's own agreement, deciding or `manual`, one with an agent by it and its version;
|
|
70
|
+
* with what grading the run's trials cost them.
|
|
71
|
+
*/
|
|
72
|
+
graders: (Agreement & { grader: string; cost: number; deciding: boolean })[];
|
|
73
|
+
disagreements: {
|
|
74
|
+
case: string;
|
|
75
|
+
column: number;
|
|
76
|
+
trial: number;
|
|
77
|
+
mark: "good" | "needs-work";
|
|
78
|
+
note?: string;
|
|
79
|
+
outcome: string;
|
|
80
|
+
}[];
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
const tally = (): Agreement => ({
|
|
84
|
+
good: { labeled: 0, agreed: 0 },
|
|
85
|
+
needsWork: { labeled: 0, agreed: 0 },
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
function count(into: Agreement, mark: "good" | "needs-work", verdict: string | undefined) {
|
|
89
|
+
const side = mark === "good" ? into.good : into.needsWork;
|
|
90
|
+
side.labeled++;
|
|
91
|
+
if (verdict === (mark === "good" ? "passed" : "failed")) side.agreed++;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A grader's name in agreement lines: a judge by its agent, voters and major, minor and pre-release tag. */
|
|
95
|
+
function graderName(entry: ReturnType<typeof newestEntries>[number]): string {
|
|
96
|
+
if (!entry.agent) return entry.grader;
|
|
97
|
+
const minor = versionKey(entry.version);
|
|
98
|
+
return `${entry.grader} (${entry.agent.harness}/${entry.agent.model} ×${votersOf(entry.agent)}${minor ? ` ${minor}` : ""})`;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The lab's agreement with the person on a set of trials, undefined when none is labeled. */
|
|
102
|
+
export function agreementOf(
|
|
103
|
+
trials: readonly StoredTrial[],
|
|
104
|
+
labels: ReadonlyMap<string, LabelRecord>,
|
|
105
|
+
decidersOf: (name: string) => ReadonlySet<string> | undefined,
|
|
106
|
+
/** The `manual` graders `grading.ts` lists: graded beside the deciding ones, to compare. */
|
|
107
|
+
manual: ReadonlySet<string> = new Set(),
|
|
108
|
+
): PersonAgreement | undefined {
|
|
109
|
+
const all = tally();
|
|
110
|
+
const graders = new Map<string, Agreement & { cost: number; deciding: boolean }>();
|
|
111
|
+
const slot = (entry: ReturnType<typeof newestEntries>[number]) => {
|
|
112
|
+
const name = graderName(entry);
|
|
113
|
+
const into = graders.get(name) ?? { ...tally(), cost: 0, deciding: !manual.has(entry.grader) };
|
|
114
|
+
graders.set(name, into);
|
|
115
|
+
return into;
|
|
116
|
+
};
|
|
117
|
+
/** A grade counted: by a grader that decides the case, or a manual one. */
|
|
118
|
+
const counted = (entry: { grader: string }, deciders: ReadonlySet<string> | undefined) =>
|
|
119
|
+
manual.has(entry.grader) || !deciders || deciders.has(entry.grader);
|
|
120
|
+
// What each grader cost on the run's trials, labeled or not: the price of what it agrees on.
|
|
121
|
+
for (const { record, scores } of trials) {
|
|
122
|
+
const deciders = decidersOf(record.case);
|
|
123
|
+
for (const entry of newestEntries(scores)) {
|
|
124
|
+
if (!counted(entry, deciders)) continue;
|
|
125
|
+
if (entry.estimate !== undefined) slot(entry).cost += entry.estimate;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
const disagreements: PersonAgreement["disagreements"] = [];
|
|
129
|
+
let notCompared = 0;
|
|
130
|
+
for (const { record, scores } of trials) {
|
|
131
|
+
const label = record.runId ? labels.get(record.runId) : undefined;
|
|
132
|
+
if (!label || label.mark === "cleared") continue;
|
|
133
|
+
const deciders = decidersOf(record.case);
|
|
134
|
+
const { outcome } = trialOutcome(record, scores, deciders);
|
|
135
|
+
if (outcome !== "passed" && outcome !== "failed") {
|
|
136
|
+
notCompared++;
|
|
137
|
+
} else {
|
|
138
|
+
count(all, label.mark, outcome);
|
|
139
|
+
}
|
|
140
|
+
// Only a verdict can disagree: an errored or undecided trial is counted as not compared.
|
|
141
|
+
if (
|
|
142
|
+
(outcome === "passed" || outcome === "failed") &&
|
|
143
|
+
(label.mark === "good") !== (outcome === "passed")
|
|
144
|
+
) {
|
|
145
|
+
disagreements.push({
|
|
146
|
+
case: record.case,
|
|
147
|
+
column: record.column,
|
|
148
|
+
trial: record.trial,
|
|
149
|
+
mark: label.mark,
|
|
150
|
+
...(label.note ? { note: label.note } : {}),
|
|
151
|
+
outcome,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
for (const entry of newestEntries(scores)) {
|
|
155
|
+
if (!counted(entry, deciders)) continue;
|
|
156
|
+
if (entry.skipped || entry.errored || !entry.verdict) continue;
|
|
157
|
+
count(slot(entry), label.mark, entry.verdict);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (all.good.labeled + all.needsWork.labeled + notCompared === 0) return undefined;
|
|
161
|
+
return {
|
|
162
|
+
...all,
|
|
163
|
+
notCompared,
|
|
164
|
+
graders: [...graders]
|
|
165
|
+
.filter(([, a]) => a.good.labeled + a.needsWork.labeled > 0)
|
|
166
|
+
.map(([grader, a]) => ({ grader, ...a })),
|
|
167
|
+
disagreements,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** What clears the judge's "not checked against a person" mark. */
|
|
172
|
+
export const JUDGE_CHECK = { labeled: 10, needsWork: 3 } as const;
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* The labeled trials the current judge graded, across the dataset's runs: one per workflow run,
|
|
176
|
+
* counted when its newest grade by this `sentences` grader has this agent and major, minor and
|
|
177
|
+
* pre-release tag.
|
|
178
|
+
*/
|
|
179
|
+
export function judgeLabeled(
|
|
180
|
+
runs: readonly (readonly StoredTrial[])[],
|
|
181
|
+
labels: ReadonlyMap<string, LabelRecord>,
|
|
182
|
+
judge: { grader: string; harness: string; model: string; minor: string },
|
|
183
|
+
): { labeled: number; needsWork: number } {
|
|
184
|
+
const seen = new Map<string, LabelRecord>();
|
|
185
|
+
for (const trials of runs) {
|
|
186
|
+
for (const { record, scores } of trials) {
|
|
187
|
+
const label = record.runId ? labels.get(record.runId) : undefined;
|
|
188
|
+
if (!label || seen.has(record.runId!)) continue;
|
|
189
|
+
const entry = newestEntries(scores).find((e) => e.grader === judge.grader);
|
|
190
|
+
if (!entry || entry.skipped || entry.errored || !entry.verdict) continue;
|
|
191
|
+
const minor = versionKey(entry.version);
|
|
192
|
+
if (
|
|
193
|
+
entry.agent?.harness !== judge.harness ||
|
|
194
|
+
entry.agent?.model !== judge.model ||
|
|
195
|
+
minor !== judge.minor
|
|
196
|
+
)
|
|
197
|
+
continue;
|
|
198
|
+
seen.set(record.runId!, label);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
const marks = [...seen.values()];
|
|
202
|
+
return {
|
|
203
|
+
labeled: marks.length,
|
|
204
|
+
needsWork: marks.filter((l) => l.mark === "needs-work").length,
|
|
205
|
+
};
|
|
206
|
+
}
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import {
|
|
2
|
+
copyFileSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
lstatSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
readlinkSync,
|
|
8
|
+
realpathSync,
|
|
9
|
+
symlinkSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { dirname, join, relative } from "node:path";
|
|
13
|
+
import type { CaseInfo } from "./dataset";
|
|
14
|
+
import { git, ignoredPaths } from "./git";
|
|
15
|
+
import { type CaseWorkspace, ignored, inside, recordsFolders } from "./workspace";
|
|
16
|
+
|
|
17
|
+
// A loop's code: the start copied from your checkout, each try a clone of its parent's committed
|
|
18
|
+
// with the proposer's change, and the scope check read from that commit.
|
|
19
|
+
|
|
20
|
+
/** A run of this many characters of a tuning key's text in changed code is copied, not written. */
|
|
21
|
+
export const PASTE = 40;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The `node_modules` folders git ignores under `top`, linked into `code` at the same paths: a copy
|
|
25
|
+
* runs on your install, as an `--against` worktree runs on its own.
|
|
26
|
+
*/
|
|
27
|
+
function linkInstalls(top: string, code: string): void {
|
|
28
|
+
const listed = git(top, [
|
|
29
|
+
"ls-files",
|
|
30
|
+
"-z",
|
|
31
|
+
"--others",
|
|
32
|
+
"--ignored",
|
|
33
|
+
"--exclude-standard",
|
|
34
|
+
"--directory",
|
|
35
|
+
]);
|
|
36
|
+
for (const path of listed.split("\0").filter(Boolean)) {
|
|
37
|
+
const clean = path.replace(/\/$/, "");
|
|
38
|
+
if (clean.split("/").at(-1) !== "node_modules") continue;
|
|
39
|
+
const target = join(code, clean);
|
|
40
|
+
if (existsSync(target)) continue;
|
|
41
|
+
mkdirSync(dirname(target), { recursive: true });
|
|
42
|
+
symlinkSync(realpathSync(join(top, clean)), target);
|
|
43
|
+
}
|
|
44
|
+
writeFileSync(join(code, ".git", "info", "exclude"), "node_modules\n");
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* The start: what git would hold in your repository, less each dataset's cases, the records
|
|
49
|
+
* folders and `ignore`'s files, copied into `code` and committed there, its installs linked.
|
|
50
|
+
*/
|
|
51
|
+
export function copyStart(
|
|
52
|
+
top: string,
|
|
53
|
+
code: string,
|
|
54
|
+
workspace: CaseWorkspace,
|
|
55
|
+
// A checkout of the same repository to copy from, such as a worktree at the loop's start commit.
|
|
56
|
+
source = top,
|
|
57
|
+
): void {
|
|
58
|
+
const listed = git(source, ["ls-files", "-z", "--cached", "--others", "--exclude-standard"]);
|
|
59
|
+
const evals = relative(top, workspace.evals);
|
|
60
|
+
const project = relative(top, workspace.project);
|
|
61
|
+
const own = (dir: string) => (inside(top, dir) ? [relative(top, dir)] : []);
|
|
62
|
+
// Records and every data folder's cases are measuring, never code a try may change.
|
|
63
|
+
const leftOut = [
|
|
64
|
+
...recordsFolders(workspace).flatMap(own),
|
|
65
|
+
...workspace.datasets.flatMap((d) => own(join(d.data.value, "cases"))),
|
|
66
|
+
];
|
|
67
|
+
const under = (path: string, dir: string) => path === dir || path.startsWith(`${dir}/`);
|
|
68
|
+
// `ignore`'s files aren't code under test: a copy never has them.
|
|
69
|
+
const left = (path: string) =>
|
|
70
|
+
leftOut.some((dir) => under(path, dir)) ||
|
|
71
|
+
(project === ""
|
|
72
|
+
? ignored(workspace.ignore, path)
|
|
73
|
+
: under(path, project) && ignored(workspace.ignore, path.slice(project.length + 1))) ||
|
|
74
|
+
(path.startsWith(`${evals}/`) && path.slice(evals.length + 1).split("/")[1] === "cases");
|
|
75
|
+
mkdirSync(code, { recursive: true });
|
|
76
|
+
for (const path of new Set(listed.split("\0").filter(Boolean))) {
|
|
77
|
+
if (left(path)) continue;
|
|
78
|
+
const from = join(source, path);
|
|
79
|
+
let stat: ReturnType<typeof lstatSync>;
|
|
80
|
+
try {
|
|
81
|
+
stat = lstatSync(from);
|
|
82
|
+
} catch {
|
|
83
|
+
continue; // deleted in the working tree
|
|
84
|
+
}
|
|
85
|
+
const to = join(code, path);
|
|
86
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
87
|
+
if (stat.isSymbolicLink()) symlinkSync(readlinkSync(from), to);
|
|
88
|
+
else if (stat.isFile()) copyFileSync(from, to);
|
|
89
|
+
}
|
|
90
|
+
git(code, ["init", "-q"]);
|
|
91
|
+
linkInstalls(top, code);
|
|
92
|
+
git(code, ["add", "-A"]);
|
|
93
|
+
git(code, ["commit", "-q", "--allow-empty", "-m", "the start"]);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A try's code: a clone of its parent's, its installs linked as the start's. */
|
|
97
|
+
export function branchCode(parent: string, code: string, top: string): void {
|
|
98
|
+
mkdirSync(dirname(code), { recursive: true });
|
|
99
|
+
// Git's objects never change once written: the clone links them rather than copying.
|
|
100
|
+
git(dirname(code), ["clone", "-q", parent, code]);
|
|
101
|
+
git(code, ["remote", "remove", "origin"]);
|
|
102
|
+
linkInstalls(top, code);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* What git doesn't see in a try's copy, which the scope check can't read: anything but the
|
|
107
|
+
* installs linked as your checkout's. A try that writes code there, or replaces an install's link,
|
|
108
|
+
* is refused.
|
|
109
|
+
*/
|
|
110
|
+
export function outsideGit(code: string, top: string): string[] {
|
|
111
|
+
const listed = git(code, [
|
|
112
|
+
"ls-files",
|
|
113
|
+
"-z",
|
|
114
|
+
"--others",
|
|
115
|
+
"--ignored",
|
|
116
|
+
"--exclude-standard",
|
|
117
|
+
"--directory",
|
|
118
|
+
]);
|
|
119
|
+
const problems: string[] = [];
|
|
120
|
+
for (const path of listed.split("\0").filter(Boolean)) {
|
|
121
|
+
const clean = path.replace(/\/$/, "");
|
|
122
|
+
const at = join(code, clean);
|
|
123
|
+
const linked =
|
|
124
|
+
clean.split("/").at(-1) === "node_modules" &&
|
|
125
|
+
lstatSync(at).isSymbolicLink() &&
|
|
126
|
+
existsSync(join(top, clean)) &&
|
|
127
|
+
realpathSync(at) === realpathSync(join(top, clean));
|
|
128
|
+
if (!linked) problems.push(`${clean}: outside git, so the scope check can't read it`);
|
|
129
|
+
}
|
|
130
|
+
return problems;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** The paths a try changed, added, edited or deleted: its commit against its parent's. */
|
|
134
|
+
export function changedFiles(code: string): string[] {
|
|
135
|
+
const listed = git(code, ["diff", "--name-only", "--no-renames", "-z", "HEAD~1", "HEAD"]);
|
|
136
|
+
return [...new Set(listed.split("\0").filter(Boolean))].sort();
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** What a loop's tries may edit: its entry's globs, relative to the code folder. */
|
|
140
|
+
export type EditScope = { allow: readonly string[]; deny: readonly string[] };
|
|
141
|
+
|
|
142
|
+
/** What every loop denies, whatever its entry says: what measures a try, and what git can't see. */
|
|
143
|
+
export const ALWAYS_DENIED = [
|
|
144
|
+
"evals/",
|
|
145
|
+
"node_modules",
|
|
146
|
+
"files git ignores",
|
|
147
|
+
"a key's text copied into code",
|
|
148
|
+
] as const;
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* What a try may not have done, from its changed files: nothing when it is in scope. A file may be
|
|
152
|
+
* edited only when `allow` matches it and `deny` doesn't, and never one `ALWAYS_DENIED` names.
|
|
153
|
+
* `project` is the folder holding `evals/` in the copy, which the globs are relative to; `top`,
|
|
154
|
+
* the repository whose ignore rules apply.
|
|
155
|
+
*/
|
|
156
|
+
export function outOfScope(
|
|
157
|
+
code: string,
|
|
158
|
+
changed: readonly string[],
|
|
159
|
+
at: { evals: string; project: string; top: string; edit: EditScope },
|
|
160
|
+
tuning: readonly CaseInfo[],
|
|
161
|
+
): string[] {
|
|
162
|
+
if (changed.length === 0) return ["it changed nothing"];
|
|
163
|
+
const { evals, project, edit } = at;
|
|
164
|
+
const problems: string[] = [];
|
|
165
|
+
const gitIgnored = ignoredPaths(at.top, changed);
|
|
166
|
+
for (const path of changed) {
|
|
167
|
+
if (path === evals || path.startsWith(`${evals}/`)) {
|
|
168
|
+
problems.push(`${path}: under ${evals}/`);
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (path.split("/").includes("node_modules")) {
|
|
172
|
+
problems.push(`${path}: an install`);
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
if (gitIgnored.has(path)) {
|
|
176
|
+
problems.push(`${path}: a file git ignores`);
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
const inProject =
|
|
180
|
+
project === ""
|
|
181
|
+
? path
|
|
182
|
+
: path.startsWith(`${project}/`)
|
|
183
|
+
? path.slice(project.length + 1)
|
|
184
|
+
: undefined;
|
|
185
|
+
if (inProject === undefined) {
|
|
186
|
+
problems.push(`${path}: outside the project, which edit's globs are relative to`);
|
|
187
|
+
continue;
|
|
188
|
+
}
|
|
189
|
+
if (!ignored(edit.allow, inProject)) {
|
|
190
|
+
problems.push(`${path}: not in edit.allow`);
|
|
191
|
+
continue;
|
|
192
|
+
}
|
|
193
|
+
const denied = edit.deny.find((glob) => ignored([glob], inProject));
|
|
194
|
+
if (denied !== undefined) problems.push(`${path}: edit.deny has ${denied}`);
|
|
195
|
+
}
|
|
196
|
+
const texts = tuning.flatMap((c) => keyTexts(c));
|
|
197
|
+
const markers = tuning.map((c) => c.key.marker);
|
|
198
|
+
for (const path of changed) {
|
|
199
|
+
const file = join(code, path);
|
|
200
|
+
if (!existsSync(file) || !lstatSync(file).isFile()) continue;
|
|
201
|
+
const content = readFileSync(file, "utf8");
|
|
202
|
+
for (const marker of markers) {
|
|
203
|
+
if (content.includes(marker)) problems.push(`${path}: holds a tuning key's marker`);
|
|
204
|
+
}
|
|
205
|
+
const flat = content.replace(/\s+/g, " ").toLowerCase();
|
|
206
|
+
const copied = texts.find((text) => {
|
|
207
|
+
for (let at = 0; at + PASTE <= text.length; at += PASTE / 4) {
|
|
208
|
+
if (flat.includes(text.slice(at, at + PASTE))) return true;
|
|
209
|
+
}
|
|
210
|
+
return false;
|
|
211
|
+
});
|
|
212
|
+
if (copied) problems.push(`${path}: copies a tuning key's words`);
|
|
213
|
+
}
|
|
214
|
+
return problems;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/** A key's own words, flattened: its note, criteria, expected strings and its `key/` files. */
|
|
218
|
+
function keyTexts(info: CaseInfo): string[] {
|
|
219
|
+
const { key } = info;
|
|
220
|
+
const values = Object.values(key.expect ?? {}).filter((v): v is string => typeof v === "string");
|
|
221
|
+
return [info.note, ...(key.must ?? []), ...(key["must-not"] ?? []), ...values]
|
|
222
|
+
.concat(info.keyFiles.map((f) => Buffer.from(f.bytes).toString("utf8")))
|
|
223
|
+
.map((text) => text.replace(/\s+/g, " ").toLowerCase())
|
|
224
|
+
.filter((text) => text.length >= PASTE);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/** The proposer's change, committed as the try, whatever it is: the scope check reads it. */
|
|
228
|
+
export function commitTry(code: string, n: number): void {
|
|
229
|
+
git(code, ["add", "-A"]);
|
|
230
|
+
git(code, ["commit", "-q", "--allow-empty", "-m", `try ${n}`]);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** A try's own change, against its parent's code: what the records keep to build it again. */
|
|
234
|
+
export function changeOf(code: string): string {
|
|
235
|
+
return git(code, ["diff", "--no-color", "--binary", "HEAD~1", "HEAD"]);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** Builds a try's code again from its parent's and its change, as the records keep it. */
|
|
239
|
+
export function rebuildTry(parent: string, code: string, change: string, top: string, n: number) {
|
|
240
|
+
branchCode(parent, code, top);
|
|
241
|
+
if (change.trim() !== "") {
|
|
242
|
+
try {
|
|
243
|
+
git(code, ["apply", "--binary", "-"], change);
|
|
244
|
+
} catch (error) {
|
|
245
|
+
throw new Error(`try ${n}'s change doesn't apply: ${(error as Error).message}`);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
commitTry(code, n);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** The kept code against the start, as a patch. */
|
|
252
|
+
export function patchOf(code: string): string {
|
|
253
|
+
const root = git(code, ["rev-list", "--max-parents=0", "HEAD"]).trim().split("\n")[0]!;
|
|
254
|
+
return git(code, ["diff", "--no-color", root, "HEAD"]);
|
|
255
|
+
}
|