diff --git a/packages/dashboard/src/__tests__/routes-agent-prompt-sizes-integration.test.ts b/packages/dashboard/src/__tests__/routes-agent-prompt-sizes-integration.test.ts new file mode 100644 index 000000000..f3876bfd2 --- /dev/null +++ b/packages/dashboard/src/__tests__/routes-agent-prompt-sizes-integration.test.ts @@ -0,0 +1,58 @@ +import { afterEach, beforeEach, describe, expect, it } from "vitest"; +import { mkdtempSync, rmSync } from "node:fs"; +import { join } from "node:path"; +import { tmpdir } from "node:os"; +import { AgentStore, TaskStore } from "@fusion/core"; +import { createServer } from "../server.js"; +import { get } from "../test-request.js"; + +describe("GET /api/agents/:id/prompt-sizes integration", () => { + let rootDir: string; + let store: TaskStore; + let agentId: string; + + beforeEach(async () => { + rootDir = mkdtempSync(join(tmpdir(), "fn-4928-prompt-sizes-")); + store = new TaskStore(rootDir, join(rootDir, ".fusion-global-settings")); + await store.init(); + + const agentStore = new AgentStore({ rootDir: store.getFusionDir() }); + await agentStore.init(); + + const agent = await agentStore.createAgent({ + name: "Prompt Sizes Agent", + role: "executor", + metadata: {}, + }); + agentId = agent.id; + + await agentStore.saveRun({ + id: "run-prompt-size-1", + agentId, + startedAt: "2026-05-17T12:00:00.000Z", + endedAt: "2026-05-17T12:00:01.000Z", + status: "completed", + systemPrompt: "sys prompt", + executionPrompt: "execute now", + }); + }); + + afterEach(() => { + rmSync(rootDir, { recursive: true, force: true }); + }); + + it("returns prompt-size rows derived from startedAt and run JSON", async () => { + const app = createServer(store); + const okRes = await get(app, `/api/agents/${agentId}/prompt-sizes`); + expect(okRes.status).toBe(200); + expect(okRes.body).toEqual([ + { + runId: "run-prompt-size-1", + createdAt: "2026-05-17T12:00:00.000Z", + systemChars: "sys prompt".length, + execChars: "execute now".length, + totalChars: "sys prompt".length + "execute now".length, + }, + ]); + }); +}); diff --git a/packages/dashboard/src/routes/register-agent-runtime-routes.ts b/packages/dashboard/src/routes/register-agent-runtime-routes.ts index f8df0d1ce..bc2a3f62f 100644 --- a/packages/dashboard/src/routes/register-agent-runtime-routes.ts +++ b/packages/dashboard/src/routes/register-agent-runtime-routes.ts @@ -659,14 +659,14 @@ export function registerAgentRuntimeRoutes(ctx: ApiRoutesContext, deps: AgentRun const rows = scopedStore.getDatabase().prepare(` SELECT id AS runId, - createdAt, + startedAt AS createdAt, COALESCE(length(json_extract(data, '$.systemPrompt')), 0) AS systemChars, COALESCE(length(json_extract(data, '$.executionPrompt')), 0) AS execChars, COALESCE(length(json_extract(data, '$.systemPrompt')), 0) + COALESCE(length(json_extract(data, '$.executionPrompt')), 0) AS totalChars FROM agentRuns WHERE json_extract(data, '$.agentId') = ? - ORDER BY createdAt DESC + ORDER BY startedAt DESC LIMIT ? `).all(req.params.id, limit) as Array<{ runId: string;