feat: 补齐 HWLAB 基线 AgentRun 执行元语
This commit is contained in:
+21
-4
@@ -1,5 +1,6 @@
|
||||
import type { AgentRunStore } from "./store.js";
|
||||
import type { CommandRecord, JsonRecord, JsonValue, RunEvent, RunRecord, RunnerJobRecord, TerminalStatus } from "../common/types.js";
|
||||
import { outputBytesFromPayload, outputTruncatedFromPayload } from "../common/output.js";
|
||||
|
||||
export async function buildRunResult(store: AgentRunStore, runId: string, commandId?: string): Promise<JsonRecord> {
|
||||
const run = await store.getRun(runId);
|
||||
@@ -7,7 +8,9 @@ export async function buildRunResult(store: AgentRunStore, runId: string, comman
|
||||
const events = await store.listEvents(runId, 0, 500);
|
||||
const jobs = await store.listRunnerJobs(runId, command?.id);
|
||||
const latestJob = jobs.at(-1) ?? null;
|
||||
const terminal = terminalFromEvents(events) ?? run.terminalStatus;
|
||||
const terminalEventStatus = terminalFromEvents(events);
|
||||
const terminal = terminalEventStatus ?? run.terminalStatus;
|
||||
const terminalSource = terminalEventStatus ? "terminal_status-event" : run.terminalStatus ? "run-record" : "none";
|
||||
const failureKind = run.failureKind ?? failureKindFromEvents(events);
|
||||
const reply = assistantReply(events);
|
||||
const blocker = terminal === "blocked" || terminal === "failed" ? { failureKind, message: run.failureMessage ?? messageFromEvents(events) } : null;
|
||||
@@ -22,6 +25,8 @@ export async function buildRunResult(store: AgentRunStore, runId: string, comman
|
||||
runStatus: run.status,
|
||||
commandState: command?.state ?? null,
|
||||
terminalStatus: terminal,
|
||||
terminalSource,
|
||||
completed: terminal === "completed",
|
||||
reply,
|
||||
failureKind,
|
||||
failureMessage: run.failureMessage ?? messageFromEvents(events),
|
||||
@@ -90,18 +95,30 @@ function artifactSummary(events: RunEvent[]): JsonRecord {
|
||||
let diffEvents = 0;
|
||||
let toolCallEvents = 0;
|
||||
let outputChars = 0;
|
||||
let truncatedEvents = 0;
|
||||
let outputBytes = 0;
|
||||
let outputTruncatedEvents = 0;
|
||||
const streamSummary: Record<string, { events: number; outputBytes: number; outputTruncated: boolean }> = {
|
||||
stdout: { events: 0, outputBytes: 0, outputTruncated: false },
|
||||
stderr: { events: 0, outputBytes: 0, outputTruncated: false },
|
||||
};
|
||||
for (const event of events) {
|
||||
if (event.type === "command_output") {
|
||||
commandOutputEvents += 1;
|
||||
const text = textPayload(event.payload);
|
||||
outputChars += text.length;
|
||||
if (event.payload.truncated === true) truncatedEvents += 1;
|
||||
const bytes = outputBytesFromPayload(event.payload);
|
||||
outputBytes += bytes;
|
||||
const truncated = outputTruncatedFromPayload(event.payload);
|
||||
if (truncated) outputTruncatedEvents += 1;
|
||||
const stream = event.payload.stream === "stderr" ? "stderr" : "stdout";
|
||||
streamSummary[stream].events += 1;
|
||||
streamSummary[stream].outputBytes += bytes;
|
||||
streamSummary[stream].outputTruncated ||= truncated;
|
||||
}
|
||||
if (event.type === "diff") diffEvents += 1;
|
||||
if (event.type === "tool_call") toolCallEvents += 1;
|
||||
}
|
||||
return { commandOutputEvents, diffEvents, toolCallEvents, outputChars, truncatedEvents };
|
||||
return { commandOutputEvents, diffEvents, toolCallEvents, outputChars, outputBytes, truncatedEvents: outputTruncatedEvents, outputTruncatedEvents, stdoutSummary: streamSummary.stdout, stderrSummary: streamSummary.stderr };
|
||||
}
|
||||
|
||||
function attemptFromEvents(events: RunEvent[]): string | null {
|
||||
|
||||
Reference in New Issue
Block a user