feat(pipeline): F4.1 — rotas GET /patterns e GET /stats (contrato C4-análise) #2415

- routes/sessions.ts: createSessionsAnalysisRouter com GET /patterns (filtros
  week/severity/key/days, paginado, Zod) e GET /stats (agregados: sessões
  totais e por dia, by_source, by_outcome, tool_top, skill_top,
  patterns_by_type, gate_blocks, avg_duration_sec, last_indexed_at,
  latest_pattern_week); montado em /api/sessions (C4) e /api (alias F4.1)
- services/sessions/db.ts: listPatterns/countPatterns/getStats +
  rowToPattern/buildPatternsWhere; gate_blocks devolve null enquanto a
  tabela gate_events não existir (F1 arco C3-gates)
- tests/sessions-analysis-route.test.ts: 15 testes (filtros, paginação,
  validação, gate_blocks condicional, não-colisão com /:id)
- inclui camada session_events pendente de v2.7.3 (db.ts/indexer.ts —
  storeSessionEvents no indexAll; rota /:id lê da BD) já em execução, para
  a árvore commitada compilar coerente

Validado contra BD real (11459 sessões, 160 padrões): curl 200 com JSON
não-vazio em /api/patterns, /api/stats, /api/sessions/patterns?week=2026-W26,
/api/sessions/stats?days=7.
This commit is contained in:
2026-07-03 06:01:22 +01:00
parent 9b2dfdc412
commit 810401e4e7
5 changed files with 511 additions and 36 deletions
+5 -2
View File
@@ -48,8 +48,9 @@ export function findAllJsonl(root: string = PROJECTS_ROOT): string[] {
* Indexa um único ficheiro (parse + upsert). Uso individual — útil para o watcher (Task 8).
*/
export async function indexFile(db: SessionsDb, path: string): Promise<void> {
const { meta } = await parseSessionFile(path)
const { meta, events } = await parseSessionFile(path)
db.upsertSession(meta)
db.storeSessionEvents(meta.session_id, events)
}
export interface IndexAllOptions {
@@ -78,7 +79,7 @@ export async function indexAll(
try {
for (let i = 0; i < files.length; i++) {
try {
const { meta } = await parseSessionFile(files[i])
const { meta, events } = await parseSessionFile(files[i])
meta.source = 'claude'
meta.model = meta.model ?? null
meta.ended_at = meta.ended_at ?? null
@@ -91,6 +92,8 @@ export async function indexAll(
meta.project_path = meta.project_path ?? null
meta.project_slug = meta.project_slug ?? null
meta.jsonl_path = meta.jsonl_path ?? null
// Store events per-session (delete+insert)
db.storeSessionEvents(meta.session_id, events)
batch.push(meta)
if (batch.length >= BATCH) {
db.upsertMany(batch)