fix(FN-2116): restore agent run logs

This commit is contained in:
gsxdsm
2026-04-18 17:59:42 -07:00
parent 20e883c14f
commit 93b97cd786
4 changed files with 183 additions and 5 deletions

View File

@@ -14949,7 +14949,7 @@ describe("GET /api/agents/:id/runs/:runId/logs", () => {
expect(res.body[1]).toMatchObject({ taskId: "FN-001", type: "tool" });
});
it("returns empty array for run without contextSnapshot.taskId", async () => {
it("returns synthesized logs for run without contextSnapshot.taskId", async () => {
// Create a run without contextSnapshot
const { AgentStore } = await import("@fusion/core");
const agentStore = new AgentStore({ rootDir: fusionDir });
@@ -14957,6 +14957,7 @@ describe("GET /api/agents/:id/runs/:runId/logs", () => {
const run = await agentStore.startHeartbeatRun(agentId);
run.endedAt = new Date().toISOString();
run.status = "completed";
run.stdoutExcerpt = "Ambient heartbeat completed";
// No contextSnapshot
await agentStore.saveRun(run);
@@ -14971,7 +14972,13 @@ describe("GET /api/agents/:id/runs/:runId/logs", () => {
const res = await REQUEST(app, "GET", `/api/agents/${agentId}/runs/${run.id}/logs`);
expect(res.status).toBe(200);
expect(res.body).toEqual([]);
expect(res.body).toEqual([
expect.objectContaining({
taskId: "agent-run",
type: "text",
text: "Ambient heartbeat completed",
}),
]);
});
it("returns 404 for non-existent run", async () => {

View File

@@ -744,6 +744,41 @@ function logEntryToTimelineEntry(entry: import("@fusion/core").AgentLogEntry): T
};
}
function runExcerptToAgentLogs(run: import("@fusion/core").AgentHeartbeatRun): import("@fusion/core").AgentLogEntry[] {
const entries: import("@fusion/core").AgentLogEntry[] = [];
const taskId = typeof run.contextSnapshot?.taskId === "string" ? run.contextSnapshot.taskId : "agent-run";
if (run.stdoutExcerpt?.trim()) {
entries.push({
timestamp: run.endedAt ?? run.startedAt,
taskId,
type: "text",
text: run.stdoutExcerpt,
});
}
if (run.stderrExcerpt?.trim()) {
entries.push({
timestamp: run.endedAt ?? run.startedAt,
taskId,
type: "tool_error",
text: "stderr",
detail: run.stderrExcerpt,
});
}
if (run.resultJson && Object.keys(run.resultJson).length > 0 && entries.length === 0) {
entries.push({
timestamp: run.endedAt ?? run.startedAt,
taskId,
type: "text",
text: JSON.stringify(run.resultJson, null, 2),
});
}
return entries;
}
function trimTaskDetailActivityLog<T extends Task>(task: T): T {
if (!Array.isArray(task.log) || task.log.length <= TASK_DETAIL_ACTIVITY_LOG_LIMIT) {
return task;
@@ -13066,7 +13101,7 @@ export function createApiRoutes(store: TaskStore, options?: ServerOptions): Rout
// not the task active during a historical run.
const taskId = run.contextSnapshot?.taskId as string | undefined;
if (!taskId) {
res.json([]);
res.json(runExcerptToAgentLogs(run));
return;
}
@@ -13075,7 +13110,7 @@ export function createApiRoutes(store: TaskStore, options?: ServerOptions): Rout
run.startedAt,
run.endedAt,
);
res.json(logs);
res.json(logs.length > 0 ? logs : runExcerptToAgentLogs(run));
} catch (err: unknown) {
if (err instanceof ApiError) {
throw err;