-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathcompute-tasks.ts
More file actions
227 lines (200 loc) · 7.28 KB
/
compute-tasks.ts
File metadata and controls
227 lines (200 loc) · 7.28 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import fs from 'fs/promises';
import path from 'path';
type TaskTrial = {
job_name: string;
trial_name: string;
trajectory_id?: string;
agent: string;
model: string;
provider: string;
passed: boolean;
reward: number | null;
error: boolean;
latency_sec: number | null;
latency_breakdown: {
env_setup: number | null;
agent_setup: number | null;
agent_exec: number | null;
verifier: number | null;
};
tokens: {
input: number;
output: number;
cache: number;
};
stderr_text?: string | null;
stderr_line_count?: number;
verifier_text?: string | null;
verifier_line_count?: number;
};
type TaskRecord = {
instruction: string;
trials: TaskTrial[];
};
type ParsedResult = {
task_name?: string;
started_at?: string;
finished_at?: string;
environment_setup?: { started_at?: string; finished_at?: string };
agent_setup?: { started_at?: string; finished_at?: string };
agent_execution?: { started_at?: string; finished_at?: string };
verifier?: { started_at?: string; finished_at?: string };
verifier_result?: { rewards?: { reward?: number } };
exception_info?: unknown;
config?: { agent?: { model_name?: string; name?: string } };
agent_info?: { model_info?: { name?: string; provider?: string }; name?: string };
trial_name?: string;
agent_result?: {
n_input_tokens?: number;
n_output_tokens?: number;
n_cache_tokens?: number;
};
};
async function readTrajectoryId(jobsDir: string, jobName: string, trialName: string): Promise<string | null> {
const trajectoryIdPath = path.join(jobsDir, jobName, trialName, 'agent', 'pochi', 'trajectory-id.txt');
try {
const content = await fs.readFile(trajectoryIdPath, 'utf-8');
const id = content.trim();
return id.length > 0 ? id : null;
} catch (_e) {
return null;
}
}
async function getResultFiles(dir: string): Promise<string[]> {
const files: string[] = [];
try {
const jobs = await fs.readdir(dir, { withFileTypes: true });
for (const job of jobs) {
if (!job.isDirectory()) continue;
const jobDir = path.join(dir, job.name);
const trials = await fs.readdir(jobDir, { withFileTypes: true });
for (const trial of trials) {
if (!trial.isDirectory()) continue;
const resultPath = path.join(job.name, trial.name, 'result.json');
try {
await fs.access(path.join(dir, resultPath));
files.push(resultPath);
} catch {
// result.json doesn't exist in this trial dir
}
}
}
} catch (e) {
console.error(`Error reading ${dir}:`, e);
}
return files;
}
async function readTaskInstruction(repoRoot: string, taskName: string): Promise<string> {
const instructionPath = path.join(repoRoot, 'tasks', taskName, 'instruction.md');
try {
return await fs.readFile(instructionPath, 'utf-8');
} catch (_e) {
return '';
}
}
function countLines(text: string): number {
if (text.length === 0) {
return 0;
}
return text.split(/\r?\n/).length;
}
async function readOptionalTextFile(filePath: string): Promise<{ text: string | null; lineCount: number }> {
try {
const text = await fs.readFile(filePath, 'utf-8');
return {
text,
lineCount: countLines(text),
};
} catch (_e) {
return {
text: null,
lineCount: 0,
};
}
}
async function main() {
const siteRoot = process.cwd();
const repoRoot = path.join(siteRoot, '..');
const jobsDir = path.join(repoRoot, 'jobs');
const resultFiles = await getResultFiles(jobsDir);
const tasks: Record<string, TaskRecord> = {};
for (const file of resultFiles) {
const fullPath = path.join(jobsDir, file);
const content = await fs.readFile(fullPath, 'utf-8');
let data: ParsedResult;
try {
data = JSON.parse(content) as ParsedResult;
} catch (e) {
console.error(`Error parsing ${fullPath}:`, e);
continue;
}
const taskName = data.task_name;
if (!taskName) continue;
if (!tasks[taskName]) {
tasks[taskName] = {
instruction: await readTaskInstruction(repoRoot, taskName),
trials: [],
};
}
const startedAt = data.started_at ? new Date(data.started_at).getTime() : 0;
const finishedAt = data.finished_at ? new Date(data.finished_at).getTime() : 0;
const latencySec = startedAt && finishedAt ? (finishedAt - startedAt) / 1000 : null;
const getDuration = (start?: string, end?: string) => {
if (!start || !end) return null;
return (new Date(end).getTime() - new Date(start).getTime()) / 1000;
};
const envSetup = getDuration(data.environment_setup?.started_at, data.environment_setup?.finished_at);
const agentSetup = getDuration(data.agent_setup?.started_at, data.agent_setup?.finished_at);
const agentExec = getDuration(data.agent_execution?.started_at, data.agent_execution?.finished_at);
const verifierExec = getDuration(data.verifier?.started_at, data.verifier?.finished_at);
const reward = data.verifier_result?.rewards?.reward;
const passed = reward === 1.0;
const hasError = !!data.exception_info;
const model = data.config?.agent?.model_name || data.agent_info?.model_info?.name || 'unknown';
const provider = data.agent_info?.model_info?.provider || 'unknown';
const agentName = data.config?.agent?.name || data.agent_info?.name || 'unknown';
// Extract job directory name (e.g., 2026-03-08__16-54-33)
const jobName = file.split('/')[0] || file.split('\\')[0];
const trialName = data.trial_name || 'unknown-trial';
const trajectoryId = trialName ? await readTrajectoryId(jobsDir, jobName, trialName) : null;
const stderrPath = path.join(jobsDir, jobName, trialName, 'agent', agentName, 'stderr.txt');
const fallbackStderrPath = path.join(jobsDir, jobName, trialName, 'agent', 'pochi', 'stderr.txt');
const verifierLogPath = path.join(jobsDir, jobName, trialName, 'verifier', 'test-stdout.txt');
const stderrResult = await readOptionalTextFile(stderrPath);
const resolvedStderrResult = stderrResult.text === null && stderrPath !== fallbackStderrPath
? await readOptionalTextFile(fallbackStderrPath)
: stderrResult;
const verifierResult = await readOptionalTextFile(verifierLogPath);
tasks[taskName].trials.push({
job_name: jobName,
trial_name: trialName,
...(trajectoryId ? { trajectory_id: trajectoryId } : {}),
agent: agentName,
model: model,
provider: provider,
passed,
reward: reward !== undefined ? reward : null,
error: hasError,
latency_sec: latencySec,
latency_breakdown: {
env_setup: envSetup,
agent_setup: agentSetup,
agent_exec: agentExec,
verifier: verifierExec
},
tokens: {
input: data.agent_result?.n_input_tokens || 0,
output: data.agent_result?.n_output_tokens || 0,
cache: data.agent_result?.n_cache_tokens || 0,
},
stderr_text: resolvedStderrResult.text,
stderr_line_count: resolvedStderrResult.lineCount,
verifier_text: verifierResult.text,
verifier_line_count: verifierResult.lineCount,
});
}
const outputPath = path.join(siteRoot, '../.zealt/tasks.json');
await fs.writeFile(outputPath, JSON.stringify(tasks, null, 2));
console.log(`Computed ${Object.keys(tasks).length} tasks into ${outputPath}`);
}
main().catch(console.error);