perf: replace O(N·chunks) listForRadar read path with O(N·agents) metrics lookup
listForRadar previously called findByAgentIds() and JSON-parsed every chunk to compute questionsCount, subagentsCount, and compactionsCount. Switch to findMetricsByAgentIds() which reads the pre-computed agent_metrics table, eliminating the chunk scan and per-row JSON.parse entirely. Add two new test cases: agent with no metrics row returns zero counts, and listForRadar response rows never carry chunk content. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -325,6 +325,47 @@ describe('agent.listForRadar', () => {
|
||||
expect(row!.subagentsCount).toBe(0);
|
||||
expect(row!.compactionsCount).toBe(0);
|
||||
});
|
||||
|
||||
it('returns zero counts for agent with no metrics row', async () => {
|
||||
const agents = new MockAgentManager();
|
||||
const ctx = makeCtx(agents);
|
||||
|
||||
const now = new Date();
|
||||
// Agent with no log chunks at all — no agent_metrics row will exist
|
||||
agents.addAgent({ id: 'agent-no-chunks', name: 'no-chunks-agent', status: 'running', createdAt: now });
|
||||
|
||||
const caller = createAgentCaller(ctx);
|
||||
const result = await caller.listForRadar({ timeRange: 'all' });
|
||||
|
||||
const row = result.find(r => r.id === 'agent-no-chunks');
|
||||
expect(row).toBeDefined();
|
||||
expect(row!.questionsCount).toBe(0);
|
||||
expect(row!.subagentsCount).toBe(0);
|
||||
expect(row!.compactionsCount).toBe(0);
|
||||
});
|
||||
|
||||
it('listForRadar response does not contain chunk content field', async () => {
|
||||
const agents = new MockAgentManager();
|
||||
const ctx = makeCtx(agents);
|
||||
const { logChunkRepo } = getRepos(ctx);
|
||||
|
||||
const now = new Date();
|
||||
agents.addAgent({ id: 'agent-content', name: 'content-agent', status: 'running', createdAt: now });
|
||||
|
||||
await logChunkRepo.insertChunk({
|
||||
agentId: 'agent-content',
|
||||
agentName: 'content-agent',
|
||||
sessionNumber: 1,
|
||||
content: JSON.stringify({ type: 'tool_use', name: 'Agent', input: { description: 'do stuff', prompt: 'some prompt' } }),
|
||||
});
|
||||
|
||||
const caller = createAgentCaller(ctx);
|
||||
const result = await caller.listForRadar({ timeRange: 'all' });
|
||||
|
||||
for (const row of result) {
|
||||
expect(row).not.toHaveProperty('content');
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// =============================================================================
|
||||
|
||||
Reference in New Issue
Block a user