Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
## 2024-10-24 - Efficient Next Sequence Calculation for Job Events
**Learning:** The codebase previously fetched all `JobEvent` rows into memory (`list_for_job(limit=1000)[-1]`) just to determine the latest `sequence_no` when inserting a new event. This acts like an N+1 inefficiency during bulk event insertions or long-running jobs.
**Action:** Use an optimized SQL aggregation method `get_max_sequence_no` (e.g. using `select(func.coalesce(func.max(Model.sequence_no), 0))`) in the repository and query for only the scalar max instead of loading list instances.
4 changes: 2 additions & 2 deletions apps/api/src/cortex_api/services/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ async def cancel_job(uow: CortexUnitOfWork, job_id: str) -> JobStatusDetail:
if updated is None: # pragma: no cover - defensive
raise NotFoundError(f"Job `{job_id}` was not found.")

existing_events = await uow.job_events.list_for_job(job_id, limit=1000)
next_sequence = existing_events[-1].sequence_no + 1 if existing_events else 1
max_seq = await uow.job_events.get_max_sequence_no(job_id)
next_sequence = max_seq + 1
await uow.job_events.add(
JobEventRecord(
job_id=job_id,
Expand Down
11 changes: 10 additions & 1 deletion packages/db/src/cortex_db/repositories.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
SynthesisType,
TenantRecord,
)
from sqlalchemy import desc, or_, select, update
from sqlalchemy import desc, func, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession

from .models import (
Expand Down Expand Up @@ -1651,6 +1651,15 @@ async def list_for_job(
)
return [_job_event_from_model(model) for model in result.scalars().all()]

async def get_max_sequence_no(self, job_id: str) -> int:
# Optimized: fetching max sequence directly via SQL to prevent loading 1000 rows into memory
result = await self._session.execute(
select(func.coalesce(func.max(JobEventModel.sequence_no), 0)).where(
JobEventModel.job_id == job_id
)
)
return result.scalar_one()


class ParseRunRepository:
def __init__(self, session: AsyncSession) -> None:
Expand Down
4 changes: 2 additions & 2 deletions packages/evaluation/src/cortex_evaluation/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ async def _append_event(
message: str,
details: dict[str, Any] | None = None,
) -> None:
existing_events = await uow.job_events.list_for_job(job.job_id, limit=1000)
next_sequence = existing_events[-1].sequence_no + 1 if existing_events else 1
max_seq = await uow.job_events.get_max_sequence_no(job.job_id)
next_sequence = max_seq + 1
await uow.job_events.add(
JobEventRecord(
job_id=job.job_id,
Expand Down
4 changes: 2 additions & 2 deletions packages/knowledge/src/cortex_knowledge/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ async def _append_event(
message: str,
details: dict[str, Any] | None = None,
) -> None:
existing_events = await uow.job_events.list_for_job(job.job_id, limit=1000)
next_sequence = existing_events[-1].sequence_no + 1 if existing_events else 1
max_seq = await uow.job_events.get_max_sequence_no(job.job_id)
next_sequence = max_seq + 1
await uow.job_events.add(
JobEventRecord(
job_id=job.job_id,
Expand Down
4 changes: 2 additions & 2 deletions packages/parse/src/cortex_parse/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,8 +412,8 @@ async def _append_event(
message: str,
details: dict[str, Any] | None = None,
) -> None:
existing_events = await uow.job_events.list_for_job(job.job_id, limit=1000)
next_sequence = existing_events[-1].sequence_no + 1 if existing_events else 1
max_seq = await uow.job_events.get_max_sequence_no(job.job_id)
next_sequence = max_seq + 1
await uow.job_events.add(
JobEventRecord(
job_id=job.job_id,
Expand Down
4 changes: 2 additions & 2 deletions packages/synthesis/src/cortex_synthesis/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ async def _append_event(
message: str,
details: dict[str, Any] | None = None,
) -> None:
existing_events = await uow.job_events.list_for_job(job.job_id, limit=1000)
next_sequence = existing_events[-1].sequence_no + 1 if existing_events else 1
max_seq = await uow.job_events.get_max_sequence_no(job.job_id)
next_sequence = max_seq + 1
await uow.job_events.add(
JobEventRecord(
job_id=job.job_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from cortex_evaluation import (
EvaluationStorageCaller as WorkerStorageCaller,
)
from cortex_evaluation import (
persist_evaluation_report,
)

Expand Down
Loading