-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathassertions.ts
More file actions
188 lines (177 loc) · 5.45 KB
/
assertions.ts
File metadata and controls
188 lines (177 loc) · 5.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { promisify } from "node:util";
import type { AgentAssertion, AssertionResult } from "./types.js";
const execFileAsync = promisify(execFile);
const SCRIPT_TIMEOUT_MS = 30_000;
export async function evaluateAssertions(
finalAnswer: string,
assertions: AgentAssertion[],
workspaceDir: string,
): Promise<AssertionResult[]> {
const results: AssertionResult[] = [];
for (const assertion of assertions) {
switch (assertion.type) {
case "contains":
results.push(evaluateContains(assertion, finalAnswer));
break;
case "not_contains":
results.push(evaluateNotContains(assertion, finalAnswer));
break;
case "matches":
results.push(evaluateMatches(assertion, finalAnswer));
break;
case "file_contains":
results.push(await evaluateFileContains(assertion, workspaceDir));
break;
case "file_matches":
results.push(await evaluateFileMatches(assertion, workspaceDir));
break;
case "script":
if (assertion.when_env && !process.env[assertion.when_env]) {
results.push({
assertion,
passed: true,
message: `Script "${assertion.name}" skipped (${assertion.when_env} not set)`,
});
} else {
results.push(await evaluateScript(assertion, workspaceDir));
}
break;
}
}
return results;
}
function evaluateContains(
assertion: AgentAssertion & { type: "contains" },
text: string,
): AssertionResult {
const passed = text.includes(assertion.value);
return {
assertion,
passed,
message: passed
? `Output contains "${assertion.value}"`
: `Output does not contain "${assertion.value}"`,
};
}
function evaluateNotContains(
assertion: AgentAssertion & { type: "not_contains" },
text: string,
): AssertionResult {
const passed = !text.includes(assertion.value);
return {
assertion,
passed,
message: passed
? `Output does not contain "${assertion.value}"`
: `Output unexpectedly contains "${assertion.value}"`,
};
}
function evaluateMatches(
assertion: AgentAssertion & { type: "matches" },
text: string,
): AssertionResult {
const re = new RegExp(assertion.pattern, assertion.flags);
const passed = re.test(text);
return {
assertion,
passed,
message: passed
? `Output matches /${assertion.pattern}/${assertion.flags ?? ""}`
: `Output does not match /${assertion.pattern}/${assertion.flags ?? ""}`,
};
}
async function evaluateFileContains(
assertion: AgentAssertion & { type: "file_contains" },
workspaceDir: string,
): Promise<AssertionResult> {
const fullPath = join(workspaceDir, assertion.path);
try {
const content = await readFile(fullPath, "utf-8");
const passed = content.includes(assertion.value);
return {
assertion,
passed,
message: passed
? `File "${assertion.path}" contains "${assertion.value}"`
: `File "${assertion.path}" does not contain "${assertion.value}"`,
};
} catch (err) {
const code = (err as globalThis.NodeJS.ErrnoException).code;
return {
assertion,
passed: false,
message:
code === "ENOENT"
? `File "${assertion.path}" not found in ${workspaceDir}`
: `File "${assertion.path}" unreadable: ${code ?? err}`,
};
}
}
async function evaluateFileMatches(
assertion: AgentAssertion & { type: "file_matches" },
workspaceDir: string,
): Promise<AssertionResult> {
const fullPath = join(workspaceDir, assertion.path);
try {
const content = await readFile(fullPath, "utf-8");
const re = new RegExp(assertion.pattern, assertion.flags);
const passed = re.test(content);
return {
assertion,
passed,
message: passed
? `File "${assertion.path}" matches /${assertion.pattern}/${assertion.flags ?? ""}`
: `File "${assertion.path}" does not match /${assertion.pattern}/${assertion.flags ?? ""}`,
};
} catch (err) {
const code = (err as globalThis.NodeJS.ErrnoException).code;
return {
assertion,
passed: false,
message:
code === "ENOENT"
? `File "${assertion.path}" not found in ${workspaceDir}`
: `File "${assertion.path}" unreadable: ${code ?? err}`,
};
}
}
async function evaluateScript(
assertion: AgentAssertion & { type: "script" },
workspaceDir: string,
): Promise<AssertionResult> {
try {
const { stdout, stderr } = await execFileAsync("sh", ["-c", assertion.command], {
cwd: workspaceDir,
timeout: SCRIPT_TIMEOUT_MS,
env: process.env as Record<string, string>,
maxBuffer: 10 * 1024 * 1024,
});
return {
assertion,
passed: true,
message: `Script "${assertion.name}" passed${stdout.trim() ? `: ${stdout.trim().slice(0, 200)}` : ""}${stderr.trim() ? ` (stderr: ${stderr.trim().slice(0, 200)})` : ""}`,
};
} catch (err: unknown) {
const error = err as {
code?: number;
killed?: boolean;
stdout?: string;
stderr?: string;
message?: string;
};
const detail = error.killed
? "timed out"
: error.stderr?.trim()?.slice(0, 500) ||
error.stdout?.trim()?.slice(0, 500) ||
error.message ||
"unknown error";
return {
assertion,
passed: false,
message: `Script "${assertion.name}" failed: ${detail}`,
};
}
}