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 @@
## 2025-05-18 - Optimize Maximum Sequence Number Lookup
**Learning:** Using array indexing on repository list methods (e.g., `list_for_job(limit=1000)[-1]`) to determine maximum sequence numbers is an anti-pattern. It loads unnecessary records into memory causing N+1 query-like inefficiencies.
**Action:** Use direct SQLAlchemy aggregation queries (e.g., `select(func.coalesce(func.max(Model.field), 0))`) in the repository methods for $O(1)$ database efficiency.
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_sequence = await uow.job_events.get_max_sequence_no(job_id)
next_sequence = max_sequence + 1
await uow.job_events.add(
JobEventRecord(
job_id=job_id,
Expand Down
10 changes: 9 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 @@ -1637,6 +1637,14 @@ async def add(self, record: JobEventRecord) -> JobEventRecord:
await self._session.refresh(model)
return _job_event_from_model(model)


async def get_max_sequence_no(self, job_id: str) -> int:
result = await self._session.execute(
select(func.coalesce(func.max(JobEventModel.sequence_no), 0))
.where(JobEventModel.job_id == job_id)
)
return result.scalar_one()

async def list_for_job(
self,
job_id: str,
Expand Down
4 changes: 1 addition & 3 deletions packages/parse/src/cortex_parse/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ async def submit(
idempotency_key: str | None = None,
request_id: str | None = None,
) -> JobAccepted:
normalized_key = (
normalize_idempotency_key(idempotency_key) if idempotency_key else None
)
normalized_key = normalize_idempotency_key(idempotency_key) if idempotency_key else None
if normalized_key is not None:
existing = await uow.jobs.get_by_idempotency(
caller.tenant_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