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:
Fusion
2026-05-13 22:36:29 -07:00
committed by gsxdsm
parent 8539a3dec0
commit edbf093b52
2 changed files with 42 additions and 8 deletions

View File

@@ -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;
}
});