Files
Codewalkers/apps/server/db/repositories/log-chunk-repository.ts
Lukas May 6eb1f8fc2a feat: add agent_metrics write+read path to LogChunkRepository
Wrap insertChunk in a synchronous better-sqlite3 transaction that upserts
agent_metrics counters atomically on every chunk insert. Malformed JSON
skips the upsert but always preserves the chunk row.

Add findMetricsByAgentIds to the interface and Drizzle adapter for
efficient bulk metric reads.

Add 8-test suite covering all write/read paths and edge cases.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-06 21:31:41 +01:00

43 lines
1.3 KiB
TypeScript

/**
* Log Chunk Repository Port Interface
*
* Port for agent log chunk persistence operations.
* No FK to agents — chunks survive agent deletion.
*/
import type { AgentLogChunk } from '../schema.js';
export interface LogChunkRepository {
insertChunk(data: {
agentId: string;
agentName: string;
sessionNumber: number;
content: string;
}): Promise<void>;
findByAgentId(agentId: string): Promise<Pick<AgentLogChunk, 'content' | 'sessionNumber' | 'createdAt'>[]>;
/**
* Batch-fetch chunks for multiple agent IDs in a single query.
* Returns chunks ordered by createdAt ascending.
* agentId field is included so results can be grouped by agent.
*/
findByAgentIds(agentIds: string[]): Promise<{ agentId: string; content: string; sessionNumber: number; createdAt: Date }[]>;
deleteByAgentId(agentId: string): Promise<void>;
getSessionCount(agentId: string): Promise<number>;
/**
* Batch-fetch pre-computed metrics for multiple agent IDs.
* Returns one row per agent that has metrics. Agents with no
* matching row in agent_metrics are omitted (not returned as zeros).
*/
findMetricsByAgentIds(agentIds: string[]): Promise<{
agentId: string;
questionsCount: number;
subagentsCount: number;
compactionsCount: number;
}[]>;
}