Repository navigation
Expand file tree
/
Copy pathtasks.ts
More file actions
366 lines (331 loc) · 13.9 KB
/
Copy pathtasks.ts
File metadata and controls
366 lines (331 loc) · 13.9 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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import { z } from "zod";
import { AnswerQuestionInput, type Callback, type Reply } from "./question.ts";
import {
buildEvidenceUrl,
MESSAGE_MAX_CHARS,
TRANSCRIPT_MAX_MESSAGES,
type Evidence,
type InvestigationResult,
type Transcript,
} from "./investigation.ts";
import type { RepoManifest } from "./manifest.ts";
import { RUNNER_EXIT, RunnerInput, type RunnerEvent, type Usage } from "./runner/protocol.ts";
import { SandboxMisconfiguredError, SnapshotUnavailableError, type OpenSandbox, type RunInvestigationResult, type Sandbox } from "./sandbox/index.ts";
import { getBuiltinModel } from "@earendil-works/pi-ai/providers/all";
import { callbackOrigins, isApprovedCallback, signCallback } from "./callback.ts";
import type { Log } from "./log.ts";
import type { Config } from "./workflow.ts";
export type RunnerErrorCode = Extract<RunnerEvent, { type: "error" }>["code"];
export type RunCapture = {
started?: Extract<RunnerEvent, { type: "started" }>;
result?: { value: InvestigationResult; at: string };
error?: { code: RunnerErrorCode; message: string };
usage?: Usage;
};
/** Folds the runner's event stream into what the workflow records. */
export function captureRun(events: RunnerEvent[]): RunCapture {
const capture: RunCapture = {};
for (const event of events) {
switch (event.type) {
case "started":
capture.started ??= event;
break;
case "usage":
capture.usage = addUsage(capture.usage, event.usage);
break;
case "result":
capture.result = { value: event.result, at: event.at };
break;
case "error":
capture.error ??= { code: event.code, message: event.message };
break;
}
}
return capture;
}
const TOKEN_KEYS = ["input_tokens", "output_tokens", "cache_read_tokens", "cache_write_tokens"] as const;
function addUsage(total: Usage | undefined, next: Usage): Usage {
const sum: Usage = { ...next };
for (const key of TOKEN_KEYS) sum[key] = (total?.[key] ?? 0) + next[key];
if (total?.cost_usd !== undefined || next.cost_usd !== undefined) {
sum.cost_usd = (total?.cost_usd ?? 0) + (next.cost_usd ?? 0);
}
return sum;
}
export type FailureCode = RunnerErrorCode | "retries_exhausted" | "misconfigured" | "no_snapshot" | "snapshot_not_ready" | "invalid_question";
const EXIT_TIMEOUT = 124; // coreutils `timeout`
const EXIT_SIGKILL = 137;
export type RunVerdict =
| { kind: "result"; value: InvestigationResult; at: string }
| { kind: "failed"; code: FailureCode; message: string };
/** Throws when the sandbox, not the agent, failed, so the workflow retries. */
export function classifyRun(
run: RunInvestigationResult,
capture: RunCapture,
{ elapsedMs, agentTimeoutMs }: { elapsedMs: number; agentTimeoutMs: number },
): RunVerdict {
if (capture.result) return { kind: "result", ...capture.result };
if (capture.error) return { kind: "failed", ...capture.error };
const exit = run.exitCode;
const timedOut =
run.aborted ||
exit === EXIT_TIMEOUT ||
exit === RUNNER_EXIT.timeout ||
(exit === EXIT_SIGKILL && elapsedMs >= agentTimeoutMs);
if (timedOut) return { kind: "failed", code: "timeout", message: `runner killed after ${agentTimeoutMs}ms` };
const stderr = run.stderrTail && `: ${run.stderrTail}`;
if (exit !== null && exit < 128) {
return { kind: "failed", code: "agent_error", message: `runner exited (${exit}) without a result${stderr}` };
}
const why =
exit === null ? "lost the runner's exec stream before it exited"
: exit === EXIT_SIGKILL ? `runner was killed after ${elapsedMs}ms (out of memory or killed by the sandbox)`
: `runner died from signal ${exit - 128}`;
throw new Error(why + stderr);
}
export function failureReason(code: FailureCode, agentTimeoutMs: number): string {
switch (code) {
case "timeout":
return `the investigation hit the ${Math.round(agentTimeoutMs / 60_000)}-minute limit`;
case "no_submission":
case "invalid_submission":
case "invalid_input":
case "agent_error":
return "the agent stopped without submitting an answer";
case "retries_exhausted":
return "the investigation kept failing on infrastructure errors";
case "misconfigured":
return "the investigation service is misconfigured (an operator needs to check the workflow's settings)";
case "snapshot_not_ready":
return "the repository snapshot isn't ready: a build is still running (about 5 minutes) or the last one failed; try again shortly";
case "no_snapshot":
return (
"no repository snapshot is available: the first build hasn't finished, or recent builds failed and the last " +
"snapshot expired. Try again in a few minutes; if this keeps happening, an operator should check the snapshot cron's logs"
);
case "invalid_question":
return `the question couldn't be read (a conversation holds at most ${TRANSCRIPT_MAX_MESSAGES} messages of up to ${MESSAGE_MAX_CHARS} characters each)`;
}
}
/** The parts of the input needed to deliver a reply, read on their own when the transcript is invalid. */
const DeliveryInput = AnswerQuestionInput.pick({ callback: true, metadata: true });
export function filterEvidence(
result: InvestigationResult,
manifest: RepoManifest,
): { result: InvestigationResult; dropped: Evidence[] } {
if (result.status !== "final_answer") return { result, dropped: [] };
const kept: Evidence[] = [];
const dropped: Evidence[] = [];
for (const e of result.evidence) {
const repo = manifest.repos.find(
(r) => r.repository.toLowerCase() === e.repository.toLowerCase() && r.commit.startsWith(e.commit),
);
if (!repo) {
dropped.push(e);
continue;
}
const pinned = { ...e, repository: repo.repository, commit: repo.commit };
kept.push({ ...pinned, url: buildEvidenceUrl(pinned, repo.browse, repo.url) });
}
return { result: { ...result, evidence: kept }, dropped };
}
const MAX_STDERR_LOG = 2_048;
export type InvestigateInput = { transcript: Transcript; trace_id: string };
export type InvestigateResult =
| { kind: "result"; result: InvestigationResult; model: string; prompt_version: string; usage?: Usage; manifest_created_at: string; repos: Array<{ repository: string; commit: string }> }
| { kind: "failed"; code: FailureCode };
export type DeliverInput = {
callback: Callback;
body: string;
};
export type AnswerQuestionDeps = {
config: Config;
log: Log;
runInvestigate: (input: InvestigateInput) => Promise<InvestigateResult>;
runDeliver: (input: DeliverInput) => Promise<void>;
};
export type AnswerQuestionResult = {
trace_id: string;
outcome: string;
reply: Reply;
};
export async function answerQuestion(
deps: AnswerQuestionDeps,
traceId: string,
rawInput: unknown,
): Promise<AnswerQuestionResult> {
const { config, log } = deps;
const started = Date.now();
const parsed = AnswerQuestionInput.safeParse(rawInput);
// Without a reply, a caller waiting on its callback would wait forever.
const input: z.infer<typeof DeliveryInput> = parsed.success ? parsed.data : DeliveryInput.safeParse(rawInput).data ?? {};
let investigateResult: InvestigateResult;
if (!parsed.success) {
log.warn("invalid question", { event: "invalid_question", trace_id: traceId, error: z.prettifyError(parsed.error) });
investigateResult = { kind: "failed", code: "invalid_question" };
} else {
try {
investigateResult = await deps.runInvestigate({ transcript: parsed.data.transcript, trace_id: traceId });
} catch (err) {
log.error("investigation failed", { trace_id: traceId, err });
investigateResult = { kind: "failed", code: "retries_exhausted" };
}
}
let reply: Reply;
if (investigateResult.kind === "result") {
const r = investigateResult.result;
reply = r.status === "needs_clarification"
? { kind: "clarification", traceId, result: r }
: {
kind: "answer",
traceId,
result: r,
model: investigateResult.model,
manifestCreatedAt: investigateResult.manifest_created_at,
};
} else {
reply = { kind: "failure", traceId, reason: failureReason(investigateResult.code, config.agentTimeoutMs) };
}
if (input.callback && !isApprovedCallback(input.callback.url, config.callbackOrigins)) {
log.warn("callback origin not approved", { event: "deliver_skipped", trace_id: traceId, origin: URL.parse(input.callback.url)?.origin });
} else if (input.callback) {
const body = JSON.stringify({
trace_id: traceId,
reply,
metadata: input.metadata,
});
try {
await deps.runDeliver({ callback: input.callback, body });
} catch (err) {
log.error("deliver failed", { event: "deliver_failed", trace_id: traceId, err });
}
}
const outcome = reply.kind;
const durationMs = Date.now() - started;
log.info("turn finished", {
event: "turn_finished",
trace_id: traceId,
outcome,
model: investigateResult.kind === "result" ? investigateResult.model : undefined,
prompt_version: investigateResult.kind === "result" ? investigateResult.prompt_version : undefined,
repos: investigateResult.kind === "result" ? investigateResult.repos : undefined,
usage: investigateResult.kind === "result" ? investigateResult.usage : undefined,
cost_usd: investigateResult.kind === "result" ? investigateResult.usage?.cost_usd : undefined,
duration_ms: durationMs,
});
return { trace_id: traceId, outcome, reply };
}
/** POST the callback body with a signed request. Throws on an unapproved origin, a redirect, non-2xx, or network error. */
export async function deliverCallback(input: DeliverInput, origins: readonly string[] = callbackOrigins()): Promise<void> {
if (!isApprovedCallback(input.callback.url, origins)) throw new Error("callback origin is not in CALLBACK_ALLOWED_ORIGINS");
const { signature, timestamp } = signCallback(input.callback.secret, input.body);
const controller = new AbortController();
const timer = setTimeout(() => controller.abort(), 10_000);
try {
const res = await fetch(input.callback.url, {
method: "POST",
headers: {
"Content-Type": "application/json",
"X-Researcher-Timestamp": String(timestamp),
"X-Researcher-Signature": signature,
},
body: input.body,
redirect: "error",
signal: controller.signal,
});
if (!res.ok) throw new Error(`deliver got ${res.status}`);
} finally {
clearTimeout(timer);
}
}
export type InvestigateDeps = {
config: Config;
log: Log;
configureSandboxes: (allowedDomain: string) => Promise<OpenSandbox>;
};
export async function investigateTask(
deps: InvestigateDeps,
input: InvestigateInput,
): Promise<InvestigateResult> {
const { config, log } = deps;
const record = getBuiltinModel(config.modelProvider as never, config.model as never);
if (!record) {
log.warn("unknown model", { event: "workflow_misconfigured", provider: config.modelProvider, model: config.model });
return { kind: "failed", code: "misconfigured" };
}
const allowedDomain = new URL(record.baseUrl).hostname;
let openSandbox: OpenSandbox;
try {
openSandbox = await deps.configureSandboxes(allowedDomain);
} catch (err) {
log.warn("sandbox setup failed", { event: "workflow_misconfigured", err });
return { kind: "failed", code: "misconfigured" };
}
let sandbox: Sandbox;
try {
sandbox = await openSandbox(config.snapshotName, { timeoutMs: config.sandboxProvisionTimeoutMs });
} catch (err) {
if (err instanceof SnapshotUnavailableError) return { kind: "failed", code: err.notReady ? "snapshot_not_ready" : "no_snapshot" };
if (err instanceof SandboxMisconfiguredError) {
log.warn("sandbox settings rejected", { event: "workflow_misconfigured", err });
return { kind: "failed", code: "misconfigured" };
}
throw err;
}
log.info("sandbox ready", { sandbox_id: sandbox.id, ...sandbox.info });
try {
const redact = (text: string) => (config.modelApiKey ? text.replaceAll(config.modelApiKey, "[redacted]") : text);
const runnerInput = RunnerInput.parse({
trace_id: input.trace_id,
transcript: input.transcript,
manifest: sandbox.manifest,
repos_root: sandbox.reposRoot,
scratch_dir: sandbox.scratchDir,
model_provider: config.modelProvider,
model: config.model,
thinking_level: config.thinkingLevel,
timeout_ms: config.agentTimeoutMs,
});
const startedAt = Date.now();
const run = await sandbox.run({
input: runnerInput,
modelApiKey: config.modelApiKey,
timeoutMs: config.agentTimeoutMs,
onStderr: (chunk: string) => {
const text = redact(chunk);
const truncated = Math.max(text.length - MAX_STDERR_LOG, 0);
log.info("runner stderr", { chunk: text.slice(0, MAX_STDERR_LOG), ...(truncated && { truncated }) });
},
});
const capture = captureRun(run.events);
const verdict = classifyRun(
{ ...run, stderrTail: redact(run.stderrTail).slice(-MAX_STDERR_LOG) },
capture,
{ elapsedMs: Date.now() - startedAt, agentTimeoutMs: config.agentTimeoutMs },
);
if (verdict.kind === "failed") {
log.warn("investigation failed", {
event: "investigation_failed",
trace_id: input.trace_id,
code: verdict.code,
message: redact(verdict.message),
});
return { kind: "failed", code: verdict.code };
}
const { result, dropped } = filterEvidence(verdict.value, runnerInput.manifest);
if (dropped.length > 0) log.warn("dropped evidence not in the sandbox manifest", { dropped });
return {
kind: "result",
result,
model: capture.started?.model ?? config.model,
prompt_version: capture.started?.prompt_version ?? "unknown",
usage: capture.usage,
manifest_created_at: sandbox.manifest.created_at,
repos: sandbox.manifest.repos.map((r) => ({ repository: r.repository, commit: r.commit })),
};
} finally {
await sandbox.close().catch((err: unknown) =>
log.warn("could not terminate sandbox", { sandbox_id: sandbox.id, err }),
);
}
}