feat(P4-003): @mosaic/log — log ingest, parsing, tiered storage
Implement AgentLogsRepo with structured log ingest (single + batch), flexible query builder (filter by session, level, category, tier, date range), and tiered storage management (hot→warm→cold→purge). Add getLogsForSummarization() for the summarization pipeline. Wire LogModule into gateway with REST endpoints at /api/logs. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
{
|
||||
"name": "@mosaic/log",
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"exports": {
|
||||
@@ -15,6 +16,10 @@
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "vitest run --passWithNoTests"
|
||||
},
|
||||
"dependencies": {
|
||||
"@mosaic/db": "workspace:*",
|
||||
"drizzle-orm": "^0.45.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"typescript": "^5.8.0",
|
||||
"vitest": "^2.0.0"
|
||||
|
||||
117
packages/log/src/agent-logs.ts
Normal file
117
packages/log/src/agent-logs.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import { eq, and, desc, lt, sql, type Db, agentLogs } from '@mosaic/db';
|
||||
|
||||
export type AgentLog = typeof agentLogs.$inferSelect;
|
||||
export type NewAgentLog = typeof agentLogs.$inferInsert;
|
||||
|
||||
export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
|
||||
export type LogCategory = 'decision' | 'tool_use' | 'learning' | 'error' | 'general';
|
||||
export type LogTier = 'hot' | 'warm' | 'cold';
|
||||
|
||||
export interface LogQuery {
|
||||
userId?: string;
|
||||
sessionId?: string;
|
||||
level?: LogLevel;
|
||||
category?: LogCategory;
|
||||
tier?: LogTier;
|
||||
since?: Date;
|
||||
until?: Date;
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
export function createAgentLogsRepo(db: Db) {
|
||||
return {
|
||||
async ingest(entry: NewAgentLog): Promise<AgentLog> {
|
||||
const rows = await db.insert(agentLogs).values(entry).returning();
|
||||
return rows[0]!;
|
||||
},
|
||||
|
||||
async ingestBatch(entries: NewAgentLog[]): Promise<AgentLog[]> {
|
||||
if (entries.length === 0) return [];
|
||||
return db.insert(agentLogs).values(entries).returning();
|
||||
},
|
||||
|
||||
async query(params: LogQuery): Promise<AgentLog[]> {
|
||||
const conditions = [];
|
||||
|
||||
if (params.userId) conditions.push(eq(agentLogs.userId, params.userId));
|
||||
if (params.sessionId) conditions.push(eq(agentLogs.sessionId, params.sessionId));
|
||||
if (params.level) conditions.push(eq(agentLogs.level, params.level));
|
||||
if (params.category) conditions.push(eq(agentLogs.category, params.category));
|
||||
if (params.tier) conditions.push(eq(agentLogs.tier, params.tier));
|
||||
if (params.since) conditions.push(sql`${agentLogs.createdAt} >= ${params.since}`);
|
||||
if (params.until) conditions.push(sql`${agentLogs.createdAt} <= ${params.until}`);
|
||||
|
||||
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
||||
|
||||
return db
|
||||
.select()
|
||||
.from(agentLogs)
|
||||
.where(where)
|
||||
.orderBy(desc(agentLogs.createdAt))
|
||||
.limit(params.limit ?? 100)
|
||||
.offset(params.offset ?? 0);
|
||||
},
|
||||
|
||||
async findById(id: string): Promise<AgentLog | undefined> {
|
||||
const rows = await db.select().from(agentLogs).where(eq(agentLogs.id, id));
|
||||
return rows[0];
|
||||
},
|
||||
|
||||
/**
|
||||
* Transition hot logs older than the cutoff to warm tier.
|
||||
* Returns the number of logs transitioned.
|
||||
*/
|
||||
async promoteToWarm(olderThan: Date): Promise<number> {
|
||||
const result = await db
|
||||
.update(agentLogs)
|
||||
.set({ tier: 'warm', summarizedAt: new Date() })
|
||||
.where(and(eq(agentLogs.tier, 'hot'), lt(agentLogs.createdAt, olderThan)))
|
||||
.returning();
|
||||
return result.length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Transition warm logs older than the cutoff to cold tier.
|
||||
*/
|
||||
async promoteToCold(olderThan: Date): Promise<number> {
|
||||
const result = await db
|
||||
.update(agentLogs)
|
||||
.set({ tier: 'cold', archivedAt: new Date() })
|
||||
.where(and(eq(agentLogs.tier, 'warm'), lt(agentLogs.createdAt, olderThan)))
|
||||
.returning();
|
||||
return result.length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Delete cold logs older than the retention period.
|
||||
*/
|
||||
async purge(olderThan: Date): Promise<number> {
|
||||
const result = await db
|
||||
.delete(agentLogs)
|
||||
.where(and(eq(agentLogs.tier, 'cold'), lt(agentLogs.createdAt, olderThan)))
|
||||
.returning();
|
||||
return result.length;
|
||||
},
|
||||
|
||||
/**
|
||||
* Get hot logs ready for summarization (decisions + learnings).
|
||||
*/
|
||||
async getLogsForSummarization(olderThan: Date, limit = 100): Promise<AgentLog[]> {
|
||||
return db
|
||||
.select()
|
||||
.from(agentLogs)
|
||||
.where(
|
||||
and(
|
||||
eq(agentLogs.tier, 'hot'),
|
||||
lt(agentLogs.createdAt, olderThan),
|
||||
sql`${agentLogs.category} IN ('decision', 'learning', 'tool_use')`,
|
||||
),
|
||||
)
|
||||
.orderBy(agentLogs.createdAt)
|
||||
.limit(limit);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export type AgentLogsRepo = ReturnType<typeof createAgentLogsRepo>;
|
||||
@@ -1 +1,11 @@
|
||||
export const VERSION = '0.0.0';
|
||||
export { createLogService, type LogService } from './log-service.js';
|
||||
export {
|
||||
createAgentLogsRepo,
|
||||
type AgentLogsRepo,
|
||||
type AgentLog,
|
||||
type NewAgentLog,
|
||||
type LogLevel,
|
||||
type LogCategory,
|
||||
type LogTier,
|
||||
type LogQuery,
|
||||
} from './agent-logs.js';
|
||||
|
||||
12
packages/log/src/log-service.ts
Normal file
12
packages/log/src/log-service.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { Db } from '@mosaic/db';
|
||||
import { createAgentLogsRepo, type AgentLogsRepo } from './agent-logs.js';
|
||||
|
||||
export interface LogService {
|
||||
logs: AgentLogsRepo;
|
||||
}
|
||||
|
||||
export function createLogService(db: Db): LogService {
|
||||
return {
|
||||
logs: createAgentLogsRepo(db),
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user