fix(FN-4388): tighten cache-stats ephemeral filtering and tests
Fusion-Task-Id: FN-4388 Fusion-Task-Lineage: 7497a0a1-5edb-4778-b339-118d1fd668f7
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { collectCacheStats } from "../cache-stats.mjs";
|
||||
import { collectCacheStats, main } from "../cache-stats.mjs";
|
||||
|
||||
test("collectCacheStats groups role and permanent-agent totals", async () => {
|
||||
const taskStore = {
|
||||
@@ -21,7 +21,11 @@ test("collectCacheStats groups role and permanent-agent totals", async () => {
|
||||
},
|
||||
};
|
||||
|
||||
const result = await collectCacheStats({ taskStore, agentStore });
|
||||
const result = await collectCacheStats({
|
||||
taskStore,
|
||||
agentStore,
|
||||
isEphemeralAgent: (agent) => agent.metadata?.type === "spawned" || agent.metadata?.agentKind === "task-worker",
|
||||
});
|
||||
|
||||
assert.equal(result.byRole.find((r) => r.role === "executor")?.total_cached, 50);
|
||||
assert.equal(result.byRole.find((r) => r.role === "reviewer")?.total_input, 200);
|
||||
@@ -30,3 +34,33 @@ test("collectCacheStats groups role and permanent-agent totals", async () => {
|
||||
assert.equal(result.byAgent[0].id, "a1");
|
||||
assert.equal(result.byAgent[0].hit_ratio, 50 / 150);
|
||||
});
|
||||
|
||||
test("collectCacheStats handles empty and zero-token datasets", async () => {
|
||||
const taskStore = { async listTasks() { return [{ assignedAgentId: "a1" }, { assignedAgentId: "a1", tokenUsage: { inputTokens: 0, cachedTokens: 0, cacheWriteTokens: 0, outputTokens: 0 } }]; } };
|
||||
const agentStore = { async listAgents() { return [{ id: "a1", role: "executor", metadata: { type: "permanent" } }]; } };
|
||||
|
||||
const result = await collectCacheStats({ taskStore, agentStore, isEphemeralAgent: () => false });
|
||||
assert.equal(result.byRole.length, 1);
|
||||
assert.equal(result.byRole[0].hit_ratio, 0);
|
||||
assert.equal(result.byAgent[0].n_tasks, 1);
|
||||
});
|
||||
|
||||
test("main --json prints machine-readable output", async () => {
|
||||
const logs = [];
|
||||
const originalLog = console.log;
|
||||
console.log = (value) => logs.push(String(value));
|
||||
try {
|
||||
const code = await main(["--json"], {
|
||||
stores: {
|
||||
taskStore: { async listTasks() { return []; } },
|
||||
agentStore: { async listAgents() { return []; } },
|
||||
isEphemeralAgent: () => false,
|
||||
},
|
||||
});
|
||||
assert.equal(code, 0);
|
||||
const parsed = JSON.parse(logs[0]);
|
||||
assert.deepEqual(parsed, { byRole: [], byAgent: [] });
|
||||
} finally {
|
||||
console.log = originalLog;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -22,7 +22,7 @@ function printTable(title, rows) {
|
||||
console.table(rows);
|
||||
}
|
||||
|
||||
export async function collectCacheStats({ taskStore, agentStore }) {
|
||||
export async function collectCacheStats({ taskStore, agentStore, isEphemeralAgent = () => false }) {
|
||||
const tasks = await taskStore.listTasks({ includeArchived: true, slim: true });
|
||||
const agents = await agentStore.listAgents({ includeEphemeral: true });
|
||||
const agentById = new Map(agents.map((agent) => [agent.id, agent]));
|
||||
@@ -39,7 +39,7 @@ export async function collectCacheStats({ taskStore, agentStore }) {
|
||||
if (!roleSummaries.has(role)) roleSummaries.set(role, createSummary());
|
||||
applyUsage(roleSummaries.get(role), task.tokenUsage);
|
||||
|
||||
if (owner && owner.metadata?.type !== "spawned") {
|
||||
if (owner && !isEphemeralAgent(owner)) {
|
||||
if (!agentSummaries.has(owner.id)) agentSummaries.set(owner.id, { id: owner.id, role: owner.role, ...createSummary() });
|
||||
applyUsage(agentSummaries.get(owner.id), task.tokenUsage);
|
||||
}
|
||||
@@ -54,16 +54,16 @@ export async function main(argv = process.argv.slice(2), deps = {}) {
|
||||
const asJson = argv.includes("--json");
|
||||
const projectDir = process.cwd();
|
||||
|
||||
const { taskStore, agentStore } = deps.stores ?? (await (async () => {
|
||||
const { TaskStore, AgentStore } = await import("../packages/core/dist/index.js");
|
||||
const { taskStore, agentStore, isEphemeralAgent } = deps.stores ?? (await (async () => {
|
||||
const { TaskStore, AgentStore, isEphemeralAgent } = await import("../packages/core/dist/index.js");
|
||||
const store = new TaskStore(projectDir);
|
||||
await store.init();
|
||||
const aStore = new AgentStore({ rootDir: store.getFusionDir() });
|
||||
await aStore.init();
|
||||
return { taskStore: store, agentStore: aStore };
|
||||
return { taskStore: store, agentStore: aStore, isEphemeralAgent };
|
||||
})());
|
||||
|
||||
const result = await collectCacheStats({ taskStore, agentStore });
|
||||
const result = await collectCacheStats({ taskStore, agentStore, isEphemeralAgent });
|
||||
if (asJson) {
|
||||
console.log(JSON.stringify(result, null, 2));
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user