fix(api-server): SSE stream pulls incremental events via seq cursor

Replace per-poll 5000-row scan + JS filter with indexed WHERE seq > ?
query (uses idx_run_events_run). Batch up to 500 per call, drain loop
on reconnect. Poll interval raised 500ms -> 1000ms.
This commit is contained in:
ZacharyZhang-NY committed 2026-04-16 21:57:35 -04:00
1 parent c5c3919c78
commit c81d72fb9c
3 files changed
+33 -7

No files matched your search

+22
View File
@@ -340,6 +340,28 @@ export function buildRunMethods(
}));
},
listRunEventsAfter(runId: string, companyId: string, afterSeq: number, limit: number): Array<{
id: number; runId: string; agentId: string; seq: number; eventType: string;
stream: string | null; level: string | null; message: string | null;
payload: Record<string, unknown> | null; createdAt: string;
}> {
return readAll<SqliteRow>(
"SELECT id, run_id, agent_id, seq, event_type, stream, level, message, payload, created_at FROM run_events WHERE run_id = ? AND company_id = ? AND seq > ? ORDER BY seq ASC LIMIT ?",
runId, companyId, afterSeq, limit
).map((row: SqliteRow) => ({
id: row.id,
runId: row.run_id,
agentId: row.agent_id,
seq: row.seq,
eventType: row.event_type,
stream: row.stream,
level: row.level,
message: row.message,
payload: row.payload ? JSON.parse(row.payload) : null,
createdAt: row.created_at,
}));
},
updateRunActor(runId: string, actorType: string, actorId: string | null): void {
getDb().prepare(
"UPDATE runs SET actor_type = ?, actor_id = ?, updated_at = ? WHERE id = ?"