diff --git a/apps/api/src/cortex_api/routers/jobs.py b/apps/api/src/cortex_api/routers/jobs.py index ec54a2d..edf1902 100644 --- a/apps/api/src/cortex_api/routers/jobs.py +++ b/apps/api/src/cortex_api/routers/jobs.py @@ -52,8 +52,7 @@ async def _authorize_job( operation_id="getJob", summary="Get job status", description=( - "Return the current state, timestamps, and telemetry " - "context for one long-running job." + "Return the current state, timestamps, and telemetry context for one long-running job." ), ) async def get_job( @@ -132,8 +131,7 @@ async def get_job_events( operation_id="cancelJob", summary="Cancel a queued or running job", description=( - "Request cancellation of a queued or running job and " - "return the updated status snapshot." + "Request cancellation of a queued or running job and return the updated status snapshot." ), ) async def post_job_cancel( diff --git a/apps/api/src/cortex_api/routers/knowledge.py b/apps/api/src/cortex_api/routers/knowledge.py index 9561dbc..e9d595a 100644 --- a/apps/api/src/cortex_api/routers/knowledge.py +++ b/apps/api/src/cortex_api/routers/knowledge.py @@ -229,8 +229,7 @@ async def get_knowledge_dataset( operation_id="createAddJob", summary="Submit a Cognee Add job", description=( - "Queue an Add job to ingest objects, documents, text, " - "or URIs into a knowledge dataset." + "Queue an Add job to ingest objects, documents, text, or URIs into a knowledge dataset." ), ) async def create_add_job( diff --git a/apps/api/src/cortex_api/routers/parse.py b/apps/api/src/cortex_api/routers/parse.py index 00866a9..69570d3 100644 --- a/apps/api/src/cortex_api/routers/parse.py +++ b/apps/api/src/cortex_api/routers/parse.py @@ -93,9 +93,7 @@ async def list_parse_engines( caller: Annotated[CallerContext, Depends(get_current_caller)], auth_service: Annotated[AuthorizationService, Depends(get_auth_service)], parse_service: Annotated[ParseService, Depends(get_parse_service)], - parse_request_compiler: Annotated[ - ParseRequestCompiler, Depends(get_parse_request_compiler) - ], + parse_request_compiler: Annotated[ParseRequestCompiler, Depends(get_parse_request_compiler)], uow: Annotated[CortexUnitOfWork, Depends(get_uow)], ) -> ParseEngineList: await auth_service.authorize( @@ -159,9 +157,7 @@ async def parse_content_sync( caller: Annotated[CallerContext, Depends(get_current_caller)], auth_service: Annotated[AuthorizationService, Depends(get_auth_service)], parse_service: Annotated[ParseService, Depends(get_parse_service)], - parse_request_compiler: Annotated[ - ParseRequestCompiler, Depends(get_parse_request_compiler) - ], + parse_request_compiler: Annotated[ParseRequestCompiler, Depends(get_parse_request_compiler)], storage_service: Annotated[StorageService, Depends(get_storage_service)], uow: Annotated[CortexUnitOfWork, Depends(get_uow)], ) -> ParseBatchResult: @@ -218,9 +214,7 @@ async def create_parse_job( caller: Annotated[CallerContext, Depends(get_current_caller)], auth_service: Annotated[AuthorizationService, Depends(get_auth_service)], parse_job_service: Annotated[ParseJobControlService, Depends(get_parse_job_service)], - parse_request_compiler: Annotated[ - ParseRequestCompiler, Depends(get_parse_request_compiler) - ], + parse_request_compiler: Annotated[ParseRequestCompiler, Depends(get_parse_request_compiler)], storage_service: Annotated[StorageService, Depends(get_storage_service)], uow: Annotated[CortexUnitOfWork, Depends(get_uow)], idempotency_key: Annotated[ diff --git a/apps/api/src/cortex_api/routers/storage.py b/apps/api/src/cortex_api/routers/storage.py index b2f77dc..56bc4b5 100644 --- a/apps/api/src/cortex_api/routers/storage.py +++ b/apps/api/src/cortex_api/routers/storage.py @@ -189,8 +189,7 @@ async def upload_small_file( str | None, Form( description=( - "Optional comma-separated tags or JSON array string. " - "Best default: empty." + "Optional comma-separated tags or JSON array string. Best default: empty." ), examples=["docs,product"], ), @@ -286,8 +285,7 @@ async def complete_upload_session( operation_id="getStorageObject", summary="Get object metadata", description=( - "Return the stored metadata, current version reference, " - "and access policy for one object." + "Return the stored metadata, current version reference, and access policy for one object." ), ) async def get_storage_object( diff --git a/apps/api/src/cortex_api/services/health.py b/apps/api/src/cortex_api/services/health.py index 2650c2f..054241e 100644 --- a/apps/api/src/cortex_api/services/health.py +++ b/apps/api/src/cortex_api/services/health.py @@ -76,9 +76,7 @@ async def build_readiness( name="queue-config", status="ok" if settings.queue.url else "degraded", detail=( - "Queue endpoint configured." - if settings.queue.url - else "Queue endpoint missing." + "Queue endpoint configured." if settings.queue.url else "Queue endpoint missing." ), ) ) diff --git a/apps/api/src/cortex_api/services/jobs.py b/apps/api/src/cortex_api/services/jobs.py index f99216e..5ebdecc 100644 --- a/apps/api/src/cortex_api/services/jobs.py +++ b/apps/api/src/cortex_api/services/jobs.py @@ -110,8 +110,10 @@ 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 + # Bolt Performance Optimization: + # Prevents N+1 query memory bloat by fetching max sequence directly in O(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, diff --git a/examples/tensorzero-cortex/src/cli.py b/examples/tensorzero-cortex/src/cli.py index a1b5267..00d985d 100644 --- a/examples/tensorzero-cortex/src/cli.py +++ b/examples/tensorzero-cortex/src/cli.py @@ -50,9 +50,7 @@ def main() -> None: "--cortex-eval-mode", choices=("sync", "async"), default=None, - help=( - "Use Cortex /v1/eval/sync or /v1/eval/jobs when Cortex Eval submission is enabled." - ), + help=("Use Cortex /v1/eval/sync or /v1/eval/jobs when Cortex Eval submission is enabled."), ) run_parser.add_argument("--skip-knowledge-jobs", action="store_true") run_parser.add_argument( @@ -145,8 +143,7 @@ def main() -> None: else settings.submit_cortex_eval, mode=args.cortex_eval_mode or settings.cortex_eval_mode, eval_types=_csv_arg(args.cortex_eval_types) or list(settings.cortex_eval_types), - metric_profile=args.cortex_eval_metric_profile - or settings.cortex_eval_metric_profile, + metric_profile=args.cortex_eval_metric_profile or settings.cortex_eval_metric_profile, ) if ( args.submit_cortex_eval is not None diff --git a/examples/tensorzero-cortex/src/knowledge_graph_visualization.py b/examples/tensorzero-cortex/src/knowledge_graph_visualization.py index de1f005..9041b6a 100644 --- a/examples/tensorzero-cortex/src/knowledge_graph_visualization.py +++ b/examples/tensorzero-cortex/src/knowledge_graph_visualization.py @@ -16,9 +16,7 @@ def latest_run_id() -> str | None: if not ARTIFACTS_DIR.exists(): return None candidates = [ - path - for path in ARTIFACTS_DIR.iterdir() - if path.is_dir() and path.name.startswith("tzcx_") + path for path in ARTIFACTS_DIR.iterdir() if path.is_dir() and path.name.startswith("tzcx_") ] if not candidates: return None diff --git a/examples/tensorzero-cortex/src/main.py b/examples/tensorzero-cortex/src/main.py index a2b6ac3..51b6cf4 100644 --- a/examples/tensorzero-cortex/src/main.py +++ b/examples/tensorzero-cortex/src/main.py @@ -220,7 +220,7 @@ def cortex_status() -> dict[str, object]: def run_experiment( request: Annotated[ ExperimentRequest, - Body(openapi_examples=EXPERIMENT_RUN_EXAMPLES), # type: ignore + Body(openapi_examples=EXPERIMENT_RUN_EXAMPLES), # type: ignore ], ) -> ExperimentReport: settings = load_settings() diff --git a/examples/tensorzero-cortex/src/pipeline.py b/examples/tensorzero-cortex/src/pipeline.py index 149c7f8..35b7854 100644 --- a/examples/tensorzero-cortex/src/pipeline.py +++ b/examples/tensorzero-cortex/src/pipeline.py @@ -181,9 +181,7 @@ def run(self, request: ExperimentRequest) -> ExperimentReport: ) eval_dataset_path = run_dir / "tensorzero_eval_dataset.jsonl" eval_dataset_path.write_text( - "\n".join( - json.dumps(case.model_dump(), ensure_ascii=False) for case in eval_cases - ) + "\n".join(json.dumps(case.model_dump(), ensure_ascii=False) for case in eval_cases) + ("\n" if eval_cases else ""), encoding="utf-8", ) @@ -275,7 +273,7 @@ def _parse_and_store( artifacts: list[ParseArtifact] = [] for source in urls: url = str(source["url"]) - expected_keywords = [str(item) for item in source.get("expected_keywords", [])] # type: ignore + expected_keywords = [str(item) for item in source.get("expected_keywords", [])] # type: ignore for engine_id in engines: engine_mode = _mode_for_engine( engine_id, @@ -357,8 +355,7 @@ def _build_knowledge( dataset_key=dataset_key, display_name=f"TensorZero Cortex {dataset_key}", description=( - "Parsed financial and macroeconomic Markdown for TensorZero " - "adaptive A/B testing." + "Parsed financial and macroeconomic Markdown for TensorZero adaptive A/B testing." ), ) documents = [ @@ -491,9 +488,7 @@ def _run_tensorzero_matrix( answer_text = _answer_text(output) answer_score = score_answer(output, expected_keywords) e2e_pass = ( - parse_score >= 0.45 - and group.context_score >= 0.35 - and answer_score >= 0.45 + parse_score >= 0.45 and group.context_score >= 0.35 and answer_score >= 0.45 ) record.inference_id = result.get("inference_id") record.episode_id = result.get("episode_id") @@ -639,8 +634,7 @@ def _context_groups( normalized = grouping.strip().lower() if normalized not in {"combined", "by_parse_engine", "knowledge_or_parse"}: raise ValueError( - "tensorzero.context_grouping must be combined, by_parse_engine, " - "or knowledge_or_parse." + "tensorzero.context_grouping must be combined, by_parse_engine, or knowledge_or_parse." ) groups: list[ContextGroup] = [] @@ -869,9 +863,7 @@ def _resolve_evaluation_options( ) update: dict[str, Any] = { "enabled": ( - request.evaluation.enabled - if request.evaluation.enabled is not None - else enabled + request.evaluation.enabled if request.evaluation.enabled is not None else enabled ), "mode": request.evaluation.mode or request.cortex_eval_mode or settings.cortex_eval_mode, } @@ -972,10 +964,7 @@ def _metrics_for_eval_type( options: EvaluationOptions, ) -> list[dict[str, Any]]: if options.metrics_by_type and eval_type in options.metrics_by_type: - return [ - _metric_to_payload(metric) - for metric in options.metrics_by_type[eval_type] - ] + return [_metric_to_payload(metric) for metric in options.metrics_by_type[eval_type]] profile = options.metric_profile.strip().lower() if profile == "deepeval_local_smoke": @@ -1031,7 +1020,7 @@ def _metric_to_payload(metric: MetricConfig) -> dict[str, Any]: def _expected_keywords(urls: list[dict[str, object]]) -> list[str]: keywords: list[str] = [] for source in urls: - keywords.extend(str(item) for item in source.get("expected_keywords", [])) # type: ignore + keywords.extend(str(item) for item in source.get("expected_keywords", [])) # type: ignore deduped = list(dict.fromkeys(keyword.lower() for keyword in keywords)) return deduped[:30] @@ -1156,7 +1145,7 @@ def _render_markdown_report(report: ExperimentReport) -> str: ## Artifacts - Eval dataset JSONL: `{report.eval_dataset_jsonl_path}` -- Knowledge graph HTML: `{report.knowledge_graph_html_path or 'not generated'}` +- Knowledge graph HTML: `{report.knowledge_graph_html_path or "not generated"}` - JSON report: `{report.report_json_path}` """ diff --git a/examples/tensorzero-cortex/src/scoring.py b/examples/tensorzero-cortex/src/scoring.py index 4b8fd2a..27d88af 100644 --- a/examples/tensorzero-cortex/src/scoring.py +++ b/examples/tensorzero-cortex/src/scoring.py @@ -10,16 +10,19 @@ def score_markdown(markdown: str, expected_keywords: Iterable[str]) -> float: if not markdown.strip(): return 0.0 length_score = min(len(markdown) / 8000, 1.0) * 0.35 - structure_score = min( - ( - markdown.count("#") - + markdown.count("|") - + markdown.lower().count("table") - + markdown.lower().count("figure") + structure_score = ( + min( + ( + markdown.count("#") + + markdown.count("|") + + markdown.lower().count("table") + + markdown.lower().count("figure") + ) + / 30, + 1.0, ) - / 30, - 1.0, - ) * 0.25 + * 0.25 + ) keyword_score = _keyword_ratio(markdown, expected_keywords) * 0.4 return round(length_score + structure_score + keyword_score, 4) diff --git a/packages/contracts/src/cortex_contracts/knowledge.py b/packages/contracts/src/cortex_contracts/knowledge.py index 5155267..acac140 100644 --- a/packages/contracts/src/cortex_contracts/knowledge.py +++ b/packages/contracts/src/cortex_contracts/knowledge.py @@ -120,8 +120,7 @@ class KnowledgeInput(BaseModel): metadata: dict[str, Any] = Field( default_factory=dict, description=( - "Optional input metadata copied into downstream " - "processing. Best default: empty object." + "Optional input metadata copied into downstream processing. Best default: empty object." ), examples=[{"source": "parse"}], ) @@ -203,8 +202,7 @@ class CognifyJobRequest(BaseModel): incremental_loading: bool = Field( default=True, description=( - "Only process newly added or changed content when " - "possible. Best default: `true`." + "Only process newly added or changed content when possible. Best default: `true`." ), ) graph_prompt_profile: GraphPromptProfile = Field( @@ -255,8 +253,7 @@ class MemifyJobRequest(BaseModel): session_ids: list[str] = Field( default_factory=list, description=( - "Optional session filters for session-aware " - "pipelines. Best default: empty list." + "Optional session filters for session-aware pipelines. Best default: empty list." ), examples=[["session_demo_001"]], ) diff --git a/packages/contracts/src/cortex_contracts/openapi_examples.py b/packages/contracts/src/cortex_contracts/openapi_examples.py index aba7e9b..f1b72c9 100644 --- a/packages/contracts/src/cortex_contracts/openapi_examples.py +++ b/packages/contracts/src/cortex_contracts/openapi_examples.py @@ -140,9 +140,7 @@ "be parsed by the cloud document parser." ), "value": { - "sources": [ - "cortex-local/tenant_demo/obj_a3da967e3ca446cab3631bb7/bofa_note.pdf" - ], + "sources": ["cortex-local/tenant_demo/obj_a3da967e3ca446cab3631bb7/bofa_note.pdf"], "engine_id": "llama_parse", "scene": "document_fidelity", "priority": 5, @@ -378,9 +376,7 @@ "metadata": { "source": "storage", "bucket": "cortex-local", - "object_key": ( - "tenant_demo/obj_a3da967e3ca446cab3631bb7/README.md" - ), + "object_key": ("tenant_demo/obj_a3da967e3ca446cab3631bb7/README.md"), }, } ], @@ -873,9 +869,7 @@ "config": { "sample_count": 5, "anonymize_pii": True, - "quality_gates": [ - {"metric_key": "quality.row_count_match", "threshold": 1.0} - ], + "quality_gates": [{"metric_key": "quality.row_count_match", "threshold": 1.0}], }, "output": {"output_format": "json", "include_preview": True}, }, diff --git a/packages/contracts/src/cortex_contracts/parse.py b/packages/contracts/src/cortex_contracts/parse.py index 0c0fc4e..b912410 100644 --- a/packages/contracts/src/cortex_contracts/parse.py +++ b/packages/contracts/src/cortex_contracts/parse.py @@ -146,8 +146,10 @@ def _validate_locator(self) -> "ParseSourceInput": raise ValueError("`source.kind=object` requires `source.object_id`.") if self.kind in {ParseInputKind.URL, ParseInputKind.URI} and not self.uri: raise ValueError("`source.kind=url|uri` requires `source.uri`.") - if self.kind is ParseInputKind.URL and self.uri and not self.uri.startswith( - ("http://", "https://") + if ( + self.kind is ParseInputKind.URL + and self.uri + and not self.uri.startswith(("http://", "https://")) ): raise ValueError("`source.kind=url` requires an HTTP(S) `source.uri`.") return self @@ -171,8 +173,7 @@ class FallbackPolicy(BaseModel): ge=1, le=10, description=( - "Maximum number of engine attempts before the " - "parse run fails. Best default: 3." + "Maximum number of engine attempts before the parse run fails. Best default: 3." ), examples=[3], ) @@ -243,8 +244,7 @@ class BrowserProfile(BaseModel): timezone_id: str | None = Field( default=None, description=( - "Optional browser timezone. Best default: omit " - "unless rendering depends on timezone." + "Optional browser timezone. Best default: omit unless rendering depends on timezone." ), examples=["Asia/Shanghai"], ) @@ -342,8 +342,7 @@ class CaptureOptions(BaseModel): flatten_shadow_dom: bool = Field( default=False, description=( - "Flatten shadow DOM content before extraction " - "when supported. Best default: `false`." + "Flatten shadow DOM content before extraction when supported. Best default: `false`." ), ) @@ -413,15 +412,13 @@ class CrawlOptions(BaseModel): remove_overlay_elements: bool = Field( default=True, description=( - "Remove cookie banners and similar overlays when " - "supported. Best default: `true`." + "Remove cookie banners and similar overlays when supported. Best default: `true`." ), ) cache_mode: str = Field( default="bypass", description=( - "Adapter cache mode. Best default: `bypass` for " - "fresh fetches in tests and demos." + "Adapter cache mode. Best default: `bypass` for fresh fetches in tests and demos." ), examples=["bypass"], ) @@ -450,8 +447,7 @@ class ParseNormalizationOptions(BaseModel): normalize_markdown: bool = Field( default=True, description=( - "Normalize headings, lists, whitespace, and block " - "structure. Best default: `true`." + "Normalize headings, lists, whitespace, and block structure. Best default: `true`." ), ) normalize_metadata: bool = Field( @@ -469,8 +465,7 @@ class ParseNormalizationOptions(BaseModel): infer_title: bool = Field( default=True, description=( - "Infer a title when the source does not provide " - "one cleanly. Best default: `true`." + "Infer a title when the source does not provide one cleanly. Best default: `true`." ), ) infer_language: bool = Field( @@ -498,8 +493,7 @@ class ChunkingOptions(BaseModel): enabled: bool = Field( default=True, description=( - "Whether Cortex should emit chunking hints or " - "stored chunks. Best default: `true`." + "Whether Cortex should emit chunking hints or stored chunks. Best default: `true`." ), ) strategy: ChunkingStrategy = Field( @@ -572,8 +566,7 @@ class ParsePersistenceOptions(BaseModel): persist_document: bool = Field( default=True, description=( - "Persist the normalized document into Cortex " - "metadata storage. Best default: `true`." + "Persist the normalized document into Cortex metadata storage. Best default: `true`." ), ) persist_artifacts: bool = Field( @@ -590,16 +583,14 @@ class ParsePersistenceOptions(BaseModel): dataset_id: str | None = Field( default=None, description=( - "Optional dataset to associate with the persisted " - "document. Best default: omit." + "Optional dataset to associate with the persisted document. Best default: omit." ), examples=["dset_9558cfc9178444e4a4c60d5658db78f5"], ) object_prefix: str | None = Field( default=None, description=( - "Optional object storage prefix for persisted " - "parse artifacts. Best default: omit." + "Optional object storage prefix for persisted parse artifacts. Best default: omit." ), examples=["parsed/demo/"], ) @@ -698,10 +689,7 @@ def _normalize_legacy_source_shapes(cls, value: Any) -> Any: normalized["sources"] = [normalized["sources"]] raw_sources = normalized.get("sources") if isinstance(raw_sources, list): - normalized["sources"] = [ - cls._normalize_source_locator(item) - for item in raw_sources - ] + normalized["sources"] = [cls._normalize_source_locator(item) for item in raw_sources] return normalized @staticmethod diff --git a/packages/db/src/cortex_db/repositories.py b/packages/db/src/cortex_db/repositories.py index 1b1d253..640d3d2 100644 --- a/packages/db/src/cortex_db/repositories.py +++ b/packages/db/src/cortex_db/repositories.py @@ -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 ( @@ -1637,6 +1637,15 @@ 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: + """Find the highest sequence number for a given job. Returns 0 if none exist.""" + 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, diff --git a/packages/evaluation/src/cortex_evaluation/adapters/evalscope_engine.py b/packages/evaluation/src/cortex_evaluation/adapters/evalscope_engine.py index 8021a93..ed6f297 100644 --- a/packages/evaluation/src/cortex_evaluation/adapters/evalscope_engine.py +++ b/packages/evaluation/src/cortex_evaluation/adapters/evalscope_engine.py @@ -75,8 +75,7 @@ async def run(self, request: EvalSyncRequest) -> EvalRunResult: response = await client.post(endpoint, json=payload) if response.status_code >= 400: message = ( - "EvalScope request failed with status " - f"{response.status_code}: {response.text}" + f"EvalScope request failed with status {response.status_code}: {response.text}" ) raise CortexError( code="evalscope_request_failed", diff --git a/packages/evaluation/src/cortex_evaluation/adapters/manifests/metrics.py b/packages/evaluation/src/cortex_evaluation/adapters/manifests/metrics.py index aa80890..57883d7 100644 --- a/packages/evaluation/src/cortex_evaluation/adapters/manifests/metrics.py +++ b/packages/evaluation/src/cortex_evaluation/adapters/manifests/metrics.py @@ -59,8 +59,7 @@ "Determine whether the response contains misinformation or unsupported factual claims." ), "safety.harm": ( - "Determine whether the response could encourage harmful, dangerous, or disallowed " - "behavior." + "Determine whether the response could encourage harmful, dangerous, or disallowed behavior." ), "quality.correctness": ( "Determine whether the actual output is correct with respect to the expected output and " @@ -76,12 +75,9 @@ "quality.coherence": ( "Determine whether the response is coherent, logically ordered, and easy to follow." ), - "quality.fluency": ( - "Determine whether the response is fluent, natural, and well-written." - ), + "quality.fluency": ("Determine whether the response is fluent, natural, and well-written."), "quality.consistency": ( - "Determine whether the response is internally consistent and does not contradict " - "itself." + "Determine whether the response is internally consistent and does not contradict itself." ), "custom.g_eval": ( "Evaluate the response using the supplied criteria, expected output, and context. " diff --git a/packages/evaluation/src/cortex_evaluation/adapters/utils/common.py b/packages/evaluation/src/cortex_evaluation/adapters/utils/common.py index 7f5e338..5c25e29 100644 --- a/packages/evaluation/src/cortex_evaluation/adapters/utils/common.py +++ b/packages/evaluation/src/cortex_evaluation/adapters/utils/common.py @@ -43,11 +43,7 @@ def namespace_scores(metrics: list[EvalMetricResult]) -> dict[str, float]: continue namespace = metric.metric_key.split(".", maxsplit=1)[0] grouped.setdefault(namespace, []).append(metric.score) - return { - namespace: sum(scores) / len(scores) - for namespace, scores in grouped.items() - if scores - } + return {namespace: sum(scores) / len(scores) for namespace, scores in grouped.items() if scores} def perf_metric_unit(metric_key: str) -> str | None: diff --git a/packages/evaluation/src/cortex_evaluation/adapters/utils/evalscope_cleaner.py b/packages/evaluation/src/cortex_evaluation/adapters/utils/evalscope_cleaner.py index f71b346..172770e 100644 --- a/packages/evaluation/src/cortex_evaluation/adapters/utils/evalscope_cleaner.py +++ b/packages/evaluation/src/cortex_evaluation/adapters/utils/evalscope_cleaner.py @@ -135,9 +135,7 @@ def _collect(self, value: Any, output: dict[str, float]) -> None: continue percentile_suffix = _percentile_metric_suffix(key) if percentile_suffix is not None and isinstance(raw_value, dict): - if self._collect_nested_percentiles( - raw_value, percentile_suffix, output - ): + if self._collect_nested_percentiles(raw_value, percentile_suffix, output): continue self._collect(raw_value, output) return @@ -167,10 +165,7 @@ def _collect_total_success_failed( if isinstance(value, list | tuple) and len(value) >= 3: numbers = [_coerce_float_like(item) for item in value[:3]] elif isinstance(value, str): - numbers = [ - _coerce_float_like(item) - for item in re.split(r"\s*/\s*|\s*,\s*", value) - ] + numbers = [_coerce_float_like(item) for item in re.split(r"\s*/\s*|\s*,\s*", value)] else: numbers = [] if len(numbers) >= 3 and all(number is not None for number in numbers[:3]): diff --git a/packages/evaluation/src/cortex_evaluation/jobs.py b/packages/evaluation/src/cortex_evaluation/jobs.py index 6b861f5..f5caf60 100644 --- a/packages/evaluation/src/cortex_evaluation/jobs.py +++ b/packages/evaluation/src/cortex_evaluation/jobs.py @@ -561,8 +561,10 @@ 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 + # Bolt Performance Optimization: + # Prevents N+1 query memory bloat by fetching max sequence directly in O(1) + max_sequence = await uow.job_events.get_max_sequence_no(job.job_id) + next_sequence = max_sequence + 1 await uow.job_events.add( JobEventRecord( job_id=job.job_id, diff --git a/packages/knowledge/src/cortex_knowledge/jobs.py b/packages/knowledge/src/cortex_knowledge/jobs.py index 63ef80a..50bc47d 100644 --- a/packages/knowledge/src/cortex_knowledge/jobs.py +++ b/packages/knowledge/src/cortex_knowledge/jobs.py @@ -480,8 +480,10 @@ 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 + # Bolt Performance Optimization: + # Prevents N+1 query memory bloat by fetching max sequence directly in O(1) + max_sequence = await uow.job_events.get_max_sequence_no(job.job_id) + next_sequence = max_sequence + 1 await uow.job_events.add( JobEventRecord( job_id=job.job_id, diff --git a/packages/knowledge/src/cortex_knowledge/operations.py b/packages/knowledge/src/cortex_knowledge/operations.py index ec13fbc..2f948ce 100644 --- a/packages/knowledge/src/cortex_knowledge/operations.py +++ b/packages/knowledge/src/cortex_knowledge/operations.py @@ -531,11 +531,7 @@ def _to_mapping(value: Any) -> dict[str, Any]: dumped = value.model_dump(mode="json") return dumped if isinstance(dumped, dict) else {} if hasattr(value, "__dict__"): - return { - key: val - for key, val in vars(value).items() - if not key.startswith("_") - } + return {key: val for key, val in vars(value).items() if not key.startswith("_")} return {} diff --git a/packages/knowledge/src/cortex_knowledge/runtime.py b/packages/knowledge/src/cortex_knowledge/runtime.py index b02d6a3..37c11db 100644 --- a/packages/knowledge/src/cortex_knowledge/runtime.py +++ b/packages/knowledge/src/cortex_knowledge/runtime.py @@ -378,11 +378,7 @@ def _object_to_mapping(value: Any) -> dict[str, Any]: dumped = value.model_dump(mode="json") return dumped if isinstance(dumped, dict) else {} if hasattr(value, "__dict__"): - return { - key: val - for key, val in vars(value).items() - if not key.startswith("_") - } + return {key: val for key, val in vars(value).items() if not key.startswith("_")} return {} async def _build_memify_kwargs( @@ -734,9 +730,7 @@ def _get_tokenizer_with_strategy(self: Any) -> Any: ).lower() if fallback in {"approximate", "none", "word"}: return _CortexApproximateTokenizer( - max_completion_tokens=int( - getattr(self, "max_completion_tokens", 8191) or 8191 - ) + max_completion_tokens=int(getattr(self, "max_completion_tokens", 8191) or 8191) ) return _CortexTiktokenEncodingTokenizer( encoding_name=str( @@ -817,9 +811,8 @@ def _init_with_fallback( def _is_tiktoken_unknown_model_error(exc: Exception) -> bool: message = str(exc).lower() - return ( - "could not automatically map" in message - and ("tokeniser" in message or "tokenizer" in message) + return "could not automatically map" in message and ( + "tokeniser" in message or "tokenizer" in message ) @@ -954,9 +947,7 @@ def _translate_db_url(db_url: str, *, provider: str, migration: bool) -> dict[st scheme = provider or parsed.scheme.split("+", 1)[0].lower() if scheme in {"sqlite"}: raw_path = ( - db_url.split("sqlite:///", 1)[1] - if db_url.startswith("sqlite:///") - else parsed.path + db_url.split("sqlite:///", 1)[1] if db_url.startswith("sqlite:///") else parsed.path ) path = Path(unquote(raw_path)) if not path.is_absolute(): diff --git a/packages/parse/src/cortex_parse/adapters/crawl4ai.py b/packages/parse/src/cortex_parse/adapters/crawl4ai.py index a1f5050..00f3b62 100644 --- a/packages/parse/src/cortex_parse/adapters/crawl4ai.py +++ b/packages/parse/src/cortex_parse/adapters/crawl4ai.py @@ -236,8 +236,7 @@ def _adapt_browser_kwargs_for_sdk( not in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD) } accepts_kwargs = any( - parameter.kind is inspect.Parameter.VAR_KEYWORD - for parameter in parameters.values() + parameter.kind is inspect.Parameter.VAR_KEYWORD for parameter in parameters.values() ) if accepts_kwargs: return normalized @@ -255,11 +254,7 @@ def _adapt_browser_kwargs_for_sdk( if "use_undetected_browser" not in accepted_names: normalized.pop("use_undetected_browser", None) - return { - key: value - for key, value in normalized.items() - if key in accepted_names - } + return {key: value for key, value in normalized.items() if key in accepted_names} def _ensure_base_directory(self) -> str: configured = self._string_value(self._config.get("base_directory")) diff --git a/packages/parse/src/cortex_parse/adapters/docling.py b/packages/parse/src/cortex_parse/adapters/docling.py index a8f4223..13b6d32 100644 --- a/packages/parse/src/cortex_parse/adapters/docling.py +++ b/packages/parse/src/cortex_parse/adapters/docling.py @@ -161,10 +161,6 @@ def _format(source_ref: str) -> str: ".html": "text/html", ".htm": "text/html", ".pdf": "application/pdf", - ".docx": ( - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" - ), - ".pptx": ( - "application/vnd.openxmlformats-officedocument.presentationml.presentation" - ), + ".docx": ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), + ".pptx": ("application/vnd.openxmlformats-officedocument.presentationml.presentation"), }.get(suffix, "application/octet-stream") diff --git a/packages/parse/src/cortex_parse/adapters/jina_reader.py b/packages/parse/src/cortex_parse/adapters/jina_reader.py index 689805c..c2be506 100644 --- a/packages/parse/src/cortex_parse/adapters/jina_reader.py +++ b/packages/parse/src/cortex_parse/adapters/jina_reader.py @@ -56,7 +56,9 @@ async def execute(self, context: EngineExecutionContext) -> EngineExecutionResul raise ValidationError("Jina Reader can only parse publicly reachable HTTP(S) URLs.") base_url = str( - context.engine_options.get("base_url", self._config.get("base_url", "https://r.jina.ai")) + context.engine_options.get( + "base_url", self._config.get("base_url", "https://r.jina.ai") + ) ).rstrip("/") reader_url = f"{base_url}/{target_url}" headers = self._headers(context) diff --git a/packages/parse/src/cortex_parse/adapters/llama_parse.py b/packages/parse/src/cortex_parse/adapters/llama_parse.py index 2fa1b4b..063e3b9 100644 --- a/packages/parse/src/cortex_parse/adapters/llama_parse.py +++ b/packages/parse/src/cortex_parse/adapters/llama_parse.py @@ -211,15 +211,9 @@ def _format(source_ref: str) -> str: suffix = Path(urlparse(source_ref).path or source_ref).suffix.lower() return { ".pdf": "application/pdf", - ".docx": ( - "application/vnd.openxmlformats-officedocument.wordprocessingml.document" - ), - ".pptx": ( - "application/vnd.openxmlformats-officedocument.presentationml.presentation" - ), - ".xlsx": ( - "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" - ), + ".docx": ("application/vnd.openxmlformats-officedocument.wordprocessingml.document"), + ".pptx": ("application/vnd.openxmlformats-officedocument.presentationml.presentation"), + ".xlsx": ("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), ".html": "text/html", ".htm": "text/html", ".jpg": "image/jpeg", diff --git a/packages/parse/src/cortex_parse/jobs.py b/packages/parse/src/cortex_parse/jobs.py index 63820aa..2145d2a 100644 --- a/packages/parse/src/cortex_parse/jobs.py +++ b/packages/parse/src/cortex_parse/jobs.py @@ -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, @@ -412,8 +410,10 @@ 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 + # Bolt Performance Optimization: + # Prevents N+1 query memory bloat by fetching max sequence directly in O(1) + max_sequence = await uow.job_events.get_max_sequence_no(job.job_id) + next_sequence = max_sequence + 1 await uow.job_events.add( JobEventRecord( job_id=job.job_id, diff --git a/packages/parse/src/cortex_parse/normalization/pipeline.py b/packages/parse/src/cortex_parse/normalization/pipeline.py index 788d241..2e97c33 100644 --- a/packages/parse/src/cortex_parse/normalization/pipeline.py +++ b/packages/parse/src/cortex_parse/normalization/pipeline.py @@ -86,9 +86,7 @@ def normalize( source_object_id=request.source.object_id, source_uri=request.source.uri or request.source.url, canonical_url=( - execution.final_url - or request.source.canonical_url - or request.source.url + execution.final_url or request.source.canonical_url or request.source.url ), title=self._resolve_title(execution, markdown, request), language_code=self._resolve_language(execution, request), diff --git a/packages/parse/src/cortex_parse/playwright_runtime.py b/packages/parse/src/cortex_parse/playwright_runtime.py index def68c4..4552155 100644 --- a/packages/parse/src/cortex_parse/playwright_runtime.py +++ b/packages/parse/src/cortex_parse/playwright_runtime.py @@ -123,8 +123,7 @@ def probe_playwright_browser(runtime: Crawl4AIPlaywrightRuntime) -> None: f"Configured browser `{runtime.browser_name}` under " f"`{runtime.browsers_path}` could not launch. " f"Install or repair it with `{install_command}` and ensure the host permits " - "Playwright child-process startup." - + (f" | Output: {output}" if output else "") + "Playwright child-process startup." + (f" | Output: {output}" if output else "") ) diff --git a/packages/parse/src/cortex_parse/request_compiler.py b/packages/parse/src/cortex_parse/request_compiler.py index f2072db..6702b41 100644 --- a/packages/parse/src/cortex_parse/request_compiler.py +++ b/packages/parse/src/cortex_parse/request_compiler.py @@ -205,8 +205,7 @@ def _default_persistence_options() -> dict[str, Any]: scene_id="deep_web", profile_ref="crawl4ai_deep_web", description=( - "High-intensity Crawl4AI preset with artifacts, full-page scan, " - "and diagnostics." + "High-intensity Crawl4AI preset with artifacts, full-page scan, and diagnostics." ), source_kinds=(ParseInputKind.URL, ParseInputKind.URI, ParseInputKind.OBJECT), timeout_seconds=90, @@ -351,8 +350,7 @@ def __init__( self._preset_values = presets or PRESETS self._default_scene_by_engine = default_scene_by_engine or DEFAULT_SCENE_BY_ENGINE self._presets = { - (preset.engine_key, preset.scene_id): preset - for preset in self._preset_values + (preset.engine_key, preset.scene_id): preset for preset in self._preset_values } def describe_engines(self, descriptors: ParseEngineList) -> ParseEngineList: diff --git a/packages/parse/src/cortex_parse/router.py b/packages/parse/src/cortex_parse/router.py index 46cb896..d476398 100644 --- a/packages/parse/src/cortex_parse/router.py +++ b/packages/parse/src/cortex_parse/router.py @@ -44,9 +44,7 @@ def resolve(self, request: ParseSyncRequest) -> RouterSelection: if allowed: allowed_set = set(allowed) descriptors = [ - descriptor - for descriptor in descriptors - if descriptor.engine_key in allowed_set + descriptor for descriptor in descriptors if descriptor.engine_key in allowed_set ] preferred = request.parser.preferred_engine_key or ( profile.descriptor.preferred_engine_key if profile else None diff --git a/packages/parse/src/cortex_parse/service.py b/packages/parse/src/cortex_parse/service.py index 48cda6b..922ec42 100644 --- a/packages/parse/src/cortex_parse/service.py +++ b/packages/parse/src/cortex_parse/service.py @@ -112,9 +112,7 @@ async def persist( crawl_profile=request.crawl.model_dump(mode="json"), normalization=request.normalization.model_dump(mode="json"), output_profile=request.output.model_dump(mode="json"), - fallback_chain=[ - snapshot.attempt.engine_key for snapshot in attempt_snapshots - ], + fallback_chain=[snapshot.attempt.engine_key for snapshot in attempt_snapshots], diagnostics=normalization.result.diagnostics.model_dump(mode="json"), telemetry_context=normalization.result.telemetry.model_dump(mode="json") if normalization.result.telemetry @@ -212,7 +210,9 @@ async def _ensure_profile_catalog( normalization=selection.profile.descriptor.normalization_defaults.model_dump( mode="json" ), - fallback_policy=selection.profile.descriptor.fallback_policy.model_dump(mode="json"), + fallback_policy=selection.profile.descriptor.fallback_policy.model_dump( + mode="json" + ), engine_overrides=dict(selection.profile.engine_overrides), source_constraints=dict(selection.profile.source_constraints), created_by=created_by, @@ -466,9 +466,7 @@ async def _execute_job( ) except Exception as exc: error_code = ( - exc.code - if isinstance(exc, CortexError) - else "engine_execution_failed" + exc.code if isinstance(exc, CortexError) else "engine_execution_failed" ) attempt = ParseEngineAttempt( attempt_no=attempt_no, @@ -570,8 +568,7 @@ def _failure_detail(attempt_snapshots: list[AttemptSnapshot]) -> str: if len(warning) > 240: warning = f"{warning[:237]}..." detail = ( - f"{snapshot.attempt.engine_key}:" - f"{snapshot.attempt.error_code or 'unknown_error'}" + f"{snapshot.attempt.engine_key}:{snapshot.attempt.error_code or 'unknown_error'}" ) if warning: detail = f"{detail} ({warning})" diff --git a/packages/storage/src/cortex_storage/client.py b/packages/storage/src/cortex_storage/client.py index 50ceed4..88b63fd 100644 --- a/packages/storage/src/cortex_storage/client.py +++ b/packages/storage/src/cortex_storage/client.py @@ -364,8 +364,7 @@ def _raise_client_error(action: str, exc: ClientError) -> NoReturn: raise CortexError( code="storage_provider_error", detail=( - f"S3-compatible storage request `{action}` " - f"failed with `{error_code}`: {message}" + f"S3-compatible storage request `{action}` failed with `{error_code}`: {message}" ), status_code=502, extra={"provider_error_code": error_code}, diff --git a/packages/synthesis/src/cortex_synthesis/adapters.py b/packages/synthesis/src/cortex_synthesis/adapters.py index 37056a2..2f2f042 100644 --- a/packages/synthesis/src/cortex_synthesis/adapters.py +++ b/packages/synthesis/src/cortex_synthesis/adapters.py @@ -370,8 +370,7 @@ def __init__( ) if status == "degraded" else ( - "Enable DeepEval Synthesizer in runtime config and install the optional " - "dependency." + "Enable DeepEval Synthesizer in runtime config and install the optional dependency." ) ) self._model = model @@ -651,11 +650,7 @@ def _coerce_preview_row(golden: Any) -> dict[str, Any]: if isinstance(golden, dict): return {str(key): value for key, value in golden.items()} if hasattr(golden, "__dict__"): - return { - str(key): value - for key, value in vars(golden).items() - if not key.startswith("_") - } + return {str(key): value for key, value in vars(golden).items() if not key.startswith("_")} return {"value": str(golden)} @@ -901,11 +896,7 @@ def _texts_from_jsonish(value: Any, *, preferred_key: str) -> list[str]: def _texts_from_plain_content(content: str) -> list[str]: stripped = _strip_markdown_fence(content) - lines = [ - _clean_text_item(line) - for line in stripped.splitlines() - if _clean_text_item(line) - ] + lines = [_clean_text_item(line) for line in stripped.splitlines() if _clean_text_item(line)] if lines: return lines cleaned = _clean_text_item(stripped) diff --git a/packages/synthesis/src/cortex_synthesis/jobs.py b/packages/synthesis/src/cortex_synthesis/jobs.py index 547cd85..087d875 100644 --- a/packages/synthesis/src/cortex_synthesis/jobs.py +++ b/packages/synthesis/src/cortex_synthesis/jobs.py @@ -401,8 +401,10 @@ 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 + # Bolt Performance Optimization: + # Prevents N+1 query memory bloat by fetching max sequence directly in O(1) + max_sequence = await uow.job_events.get_max_sequence_no(job.job_id) + next_sequence = max_sequence + 1 await uow.job_events.add( JobEventRecord( job_id=job.job_id, diff --git a/scripts/dev/live_observability_probe.py b/scripts/dev/live_observability_probe.py index f3e7449..f827213 100644 --- a/scripts/dev/live_observability_probe.py +++ b/scripts/dev/live_observability_probe.py @@ -37,9 +37,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" @@ -131,11 +133,7 @@ def _wait_for_metric_name( deadline = time.time() + timeout_seconds while time.time() < deadline: metrics_text = httpx.get("http://127.0.0.1:8889/metrics", timeout=10).text - matching_lines = [ - line - for line in metrics_text.splitlines() - if substring in line.lower() - ] + matching_lines = [line for line in metrics_text.splitlines() if substring in line.lower()] if matching_lines: return metrics_text, matching_lines time.sleep(1) diff --git a/specs/cortex-api.yaml b/specs/cortex-api.yaml index 2ce838a..68092de 100644 --- a/specs/cortex-api.yaml +++ b/specs/cortex-api.yaml @@ -14,34 +14,29 @@ servers: description: Development / 测试环境 tags: - name: Health - description: 运行存活与依赖就绪探针。/ Runtime liveness and dependency readiness probes. + description: Runtime liveness and dependency readiness probes. / 运行存活与依赖就绪探针。 - name: Observability - description: 对齐 OpenTelemetry 的遥测与指标接口。/ OpenTelemetry-aligned telemetry and - metrics endpoints. + description: OpenTelemetry-aligned telemetry and metrics endpoints. / 对齐 OpenTelemetry 的遥测与指标接口。 - name: Jobs - description: 通用长任务查询与控制。/ Generic long-running job inspection and control. + description: Generic long-running job inspection and control. / 通用长任务查询与控制。 - name: Parse - description: 面向 URL 与文件的引擎无关解析、标准化与持久化。/ Engine-agnostic parsing, normalization, - and persistence for URLs and files. + description: Engine-agnostic parsing, normalization, and persistence for URLs and files. / 面向 URL 与文件的引擎无关解析、标准化与持久化。 - name: Storage - description: 上传、查询和下载 S3 后端对象。 / Upload, inspect, and download S3-backed objects. + description: Upload, inspect, and download S3-backed objects. / 上传、查询和下载 S3 后端对象。 - name: Knowledge - description: Dataset lifecycle plus Cognee-backed Add, Cognify, Memify, and - Search operations. / 数据集生命周期与基于 Cognee 的 Add、Cognify、Memify、Search 操作。 + description: Dataset lifecycle plus Cognee-backed Add, Cognify, Memify, and Search operations. / 数据集生命周期与基于 Cognee 的 Add、Cognify、Memify、Search 操作。 - name: Evaluation - description: AI 评测引擎、指标目录、同步校验与异步评测作业。/ AI evaluation engines, metric catalogs, - synchronous checks, and asynchronous evaluation jobs. + description: AI evaluation engines, metric catalogs, synchronous checks, and asynchronous evaluation jobs. / AI 评测引擎、指标目录、同步校验与异步评测作业。 - name: Synthesis - description: 数据合成引擎以及同步、异步生成工作流。/ Synthetic data engines plus synchronous and - asynchronous generation workflows. + description: Synthetic data engines plus synchronous and asynchronous generation workflows. / 数据合成引擎以及同步、异步生成工作流。 - name: Dev Auth - description: 仅本地开发环境启用的 token 生成辅助接口。/ Local-development-only token issuance helper. + description: Local-development-only token issuance helper. / 仅本地开发环境启用的 token 生成辅助接口。 paths: /v1/health/live: get: tags: - Health - summary: Liveness probe + summary: Liveness probe / 存活探针 operationId: getLiveness responses: "200": diff --git a/tests/integration/test_api_auth_jobs.py b/tests/integration/test_api_auth_jobs.py index 61cdafd..9a25e3f 100644 --- a/tests/integration/test_api_auth_jobs.py +++ b/tests/integration/test_api_auth_jobs.py @@ -41,9 +41,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" diff --git a/tests/integration/test_api_e2e.py b/tests/integration/test_api_e2e.py index 66e324b..e50a444 100644 --- a/tests/integration/test_api_e2e.py +++ b/tests/integration/test_api_e2e.py @@ -99,9 +99,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" @@ -206,7 +208,7 @@ def create_download_request( object_key: str, disposition: str, expires_in: int, - ) -> PresignedRequestDescriptor: + ) -> PresignedRequestDescriptor: return PresignedRequestDescriptor( method="GET", url=( diff --git a/tests/integration/test_api_eval_synthesis.py b/tests/integration/test_api_eval_synthesis.py index ca78f8a..1d8c4f6 100644 --- a/tests/integration/test_api_eval_synthesis.py +++ b/tests/integration/test_api_eval_synthesis.py @@ -78,9 +78,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" @@ -444,8 +446,7 @@ async def _seed_eval_dataset(db_path: Path, *, tenant_id: str, dataset_id: str) metadata={ "question": "What does Cortex provide?", "answer": ( - "Cortex provides parse, storage, knowledge, eval, " - "and synthesis APIs." + "Cortex provides parse, storage, knowledge, eval, and synthesis APIs." ), "expected": "A unified AI data and evaluation API plane.", "contexts": [ @@ -553,9 +554,10 @@ def test_evaluation_api_lists_catalog_and_runs_sync_eval( assert sync_response.json()["status"] == "succeeded" assert sync_response.json()["summary"]["overall_passed"] is True assert sync_response.json()["job_id"].startswith("job_") - assert sync_response.json()["source_summary"]["evalscope_task_id"] == sync_response.json()[ - "job_id" - ] + assert ( + sync_response.json()["source_summary"]["evalscope_task_id"] + == sync_response.json()["job_id"] + ) assert sync_response.json()["eval_run_id"].startswith("erun_") assert sync_response.json()["artifacts"][0]["label"] == "evaluation_report" assert sync_response.json()["artifacts"][0]["uri"].startswith("s3://") @@ -662,9 +664,7 @@ def test_evaluation_job_submit_hydrates_synthesis_output_object( "source_type": "documents", "preview_rows": [ { - "scenario": ( - "A support agent needs to parse a private customer file." - ), + "scenario": ("A support agent needs to parse a private customer file."), "expected_outcome": ( "The agent asks for the object_id before calling parse." ), @@ -676,9 +676,7 @@ def test_evaluation_job_submit_hydrates_synthesis_output_object( { "input": "Parse the private file for the customer.", "expected_output": "Ask for the object_id first.", - "retrieval_contexts": [ - "Private file parsing requires an object_id." - ], + "retrieval_contexts": ["Private file parsing requires an object_id."], }, ], }, @@ -709,9 +707,7 @@ def test_evaluation_job_submit_hydrates_synthesis_output_object( }, ) job_id = accepted_response.json()["job_id"] - worker_status = asyncio.run( - _run_evaluation_worker_once(db_path, object_store=object_store) - ) + worker_status = asyncio.run(_run_evaluation_worker_once(db_path, object_store=object_store)) completed_result = client.get(f"/v1/eval/jobs/{job_id}/result", headers=headers) assert accepted_response.status_code == 202 @@ -790,9 +786,7 @@ def test_synthesis_api_lists_catalog_and_runs_sync_job( }, "config": { "sample_count": 5, - "quality_gates": [ - {"metric_key": "quality.correctness", "threshold": 0.8} - ], + "quality_gates": [{"metric_key": "quality.correctness", "threshold": 0.8}], }, "output": {"output_format": "json"}, }, @@ -829,16 +823,12 @@ def test_synthesis_job_submit_worker_and_result(monkeypatch: pytest.MonkeyPatch) "source": { "type": "inline_records", "inline_records": [ - { - "document": "Cortex uses pluggable engines for parsing and evaluation." - } + {"document": "Cortex uses pluggable engines for parsing and evaluation."} ], }, "config": { "sample_count": 3, - "quality_gates": [ - {"metric_key": "quality.correctness", "threshold": 0.8} - ], + "quality_gates": [{"metric_key": "quality.correctness", "threshold": 0.8}], }, "output": {"output_format": "jsonl"}, }, @@ -853,16 +843,12 @@ def test_synthesis_job_submit_worker_and_result(monkeypatch: pytest.MonkeyPatch) "source": { "type": "inline_records", "inline_records": [ - { - "document": "Cortex uses pluggable engines for parsing and evaluation." - } + {"document": "Cortex uses pluggable engines for parsing and evaluation."} ], }, "config": { "sample_count": 3, - "quality_gates": [ - {"metric_key": "quality.correctness", "threshold": 0.8} - ], + "quality_gates": [{"metric_key": "quality.correctness", "threshold": 0.8}], }, "output": {"output_format": "jsonl"}, }, @@ -882,8 +868,7 @@ def test_synthesis_job_submit_worker_and_result(monkeypatch: pytest.MonkeyPatch) assert completed_result.json()["synthesis_run_id"].startswith("srun_") assert completed_result.json()["output_dataset_id"] == "ds_synth_preview" assert any( - output["label"] == "synthesis_output" - and output["object_id"].startswith("obj_") + output["label"] == "synthesis_output" and output["object_id"].startswith("obj_") for output in completed_result.json()["outputs"] ) assert [event["event_type"] for event in events_response.json()] == [ diff --git a/tests/integration/test_api_knowledge.py b/tests/integration/test_api_knowledge.py index 2fcc2b5..6b3b02d 100644 --- a/tests/integration/test_api_knowledge.py +++ b/tests/integration/test_api_knowledge.py @@ -74,9 +74,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" @@ -157,9 +159,7 @@ async def search(self, *, payload: dict[str, object]) -> dict[str, object]: {"id": "node_1", "label": "Product"}, {"id": "node_2", "label": "Rule"}, ], - "edges": [ - {"source": "node_1", "target": "node_2", "type": "related_to"} - ], + "edges": [{"source": "node_1", "target": "node_2", "type": "related_to"}], } ], } diff --git a/tests/integration/test_api_parse.py b/tests/integration/test_api_parse.py index 3c31fcd..2140854 100644 --- a/tests/integration/test_api_parse.py +++ b/tests/integration/test_api_parse.py @@ -64,9 +64,11 @@ def _dev_bearer_token(*, tenant_id: str, actor_id: str, scopes: list[str]) -> st "actor_ref": f"{actor_id}@example.com", "scope": " ".join(scopes), } - encoded = base64.urlsafe_b64encode( - json.dumps(claims, separators=(",", ":")).encode("utf-8") - ).decode("ascii").rstrip("=") + encoded = ( + base64.urlsafe_b64encode(json.dumps(claims, separators=(",", ":")).encode("utf-8")) + .decode("ascii") + .rstrip("=") + ) return f"Bearer dev:{encoded}" @@ -279,8 +281,7 @@ def test_parse_api_lists_catalog_and_runs_sync_parse(monkeypatch: pytest.MonkeyP assert sync_response.json()["engine_id"] == "auto" assert sync_response.json()["results"][0]["document"]["title"] == "API Parse" assert ( - sync_response.json()["results"][0]["document"]["parser"]["engine_key"] - == "api_test_engine" + sync_response.json()["results"][0]["document"]["parser"]["engine_key"] == "api_test_engine" ) assert ( sync_response.json()["results"][0]["diagnostics"]["selected_engine_key"] @@ -836,6 +837,4 @@ def test_parse_worker_recovers_stale_lease(monkeypatch: pytest.MonkeyPatch) -> N assert claimed_job_id == job_id assert worker_status == "succeeded" assert completed_result.status_code == 200 - assert "parse.job.lease_recovered" in [ - event["event_type"] for event in events_response.json() - ] + assert "parse.job.lease_recovered" in [event["event_type"] for event in events_response.json()] diff --git a/tests/integration/test_parse_additional_adapters.py b/tests/integration/test_parse_additional_adapters.py index bb9dc00..0061afd 100644 --- a/tests/integration/test_parse_additional_adapters.py +++ b/tests/integration/test_parse_additional_adapters.py @@ -184,8 +184,7 @@ async def test_docling_adapter_uses_optional_converter(monkeypatch: pytest.Monke uri="file:///tmp/slides.pptx", filename="slides.pptx", expected_content_type=( - "application/vnd.openxmlformats-officedocument." - "presentationml.presentation" + "application/vnd.openxmlformats-officedocument.presentationml.presentation" ), ) ), @@ -194,8 +193,7 @@ async def test_docling_adapter_uses_optional_converter(monkeypatch: pytest.Monke uri="file:///tmp/slides.pptx", filename="slides.pptx", expected_content_type=( - "application/vnd.openxmlformats-officedocument." - "presentationml.presentation" + "application/vnd.openxmlformats-officedocument.presentationml.presentation" ), ), engine_options={ diff --git a/tests/integration/test_runtime_stack.py b/tests/integration/test_runtime_stack.py index 40a1adb..fdef2bb 100644 --- a/tests/integration/test_runtime_stack.py +++ b/tests/integration/test_runtime_stack.py @@ -136,9 +136,7 @@ def _temporary_postgres_database(prefix: str) -> Iterator[tuple[str, str]]: database_name = f"{prefix}_{time.time_ns()}".replace("-", "_") with psycopg.connect(config.admin_driverless_dsn, autocommit=True) as conn: with conn.cursor() as cur: - cur.execute( - sql.SQL("CREATE DATABASE {}").format(sql.Identifier(database_name)) - ) + cur.execute(sql.SQL("CREATE DATABASE {}").format(sql.Identifier(database_name))) try: yield config.sync_dsn(database_name), config.async_dsn(database_name) finally: @@ -153,9 +151,7 @@ def _temporary_postgres_database(prefix: str) -> Iterator[tuple[str, str]]: (database_name,), ) cur.execute( - sql.SQL("DROP DATABASE IF EXISTS {}").format( - sql.Identifier(database_name) - ) + sql.SQL("DROP DATABASE IF EXISTS {}").format(sql.Identifier(database_name)) ) diff --git a/tests/unit/test_eval_synthesis_adapters.py b/tests/unit/test_eval_synthesis_adapters.py index 47d95ac..2f80383 100644 --- a/tests/unit/test_eval_synthesis_adapters.py +++ b/tests/unit/test_eval_synthesis_adapters.py @@ -275,9 +275,7 @@ def create(self, **kwargs): self._owner.requests.append(kwargs) return types.SimpleNamespace( choices=[ - types.SimpleNamespace( - message=types.SimpleNamespace(content='{"score": 1}') - ) + types.SimpleNamespace(message=types.SimpleNamespace(content='{"score": 1}')) ] ) @@ -290,9 +288,7 @@ async def create(self, **kwargs): self._owner.loops.append(asyncio.get_running_loop()) return types.SimpleNamespace( choices=[ - types.SimpleNamespace( - message=types.SimpleNamespace(content='{"score": 1}') - ) + types.SimpleNamespace(message=types.SimpleNamespace(content='{"score": 1}')) ] ) @@ -441,12 +437,8 @@ def test_evaluation_service_routes_only_to_engines_supporting_eval_type() -> Non def test_evalscope_runtime_extra_declares_service_server_dependency() -> None: - pyproject = ( - Path(__file__).resolve().parents[2] / "packages" / "evaluation" / "pyproject.toml" - ) - optional_dependencies = tomllib.loads(pyproject.read_text())["project"][ - "optional-dependencies" - ] + pyproject = Path(__file__).resolve().parents[2] / "packages" / "evaluation" / "pyproject.toml" + optional_dependencies = tomllib.loads(pyproject.read_text())["project"]["optional-dependencies"] assert "evalscope[service]>=1.6,<2" in optional_dependencies["evalscope"] assert "fastapi>=0.115,<1" in optional_dependencies["evalscope"] @@ -585,10 +577,7 @@ def test_evalscope_openqa_rejects_overstrict_min_prompt_length() -> None: def test_evaluation_error_redaction_handles_openrouter_json_api_keys() -> None: key = "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" - message = ( - f'{{"api_key": "{key}", ' - f'"headers": {{"Authorization": "Bearer {key}"}}}}' - ) + message = f'{{"api_key": "{key}", "headers": {{"Authorization": "Bearer {key}"}}}}' redacted = _redact_error_message(message) diff --git a/workers/parse-worker/src/cortex_worker_parse/bootstrap.py b/workers/parse-worker/src/cortex_worker_parse/bootstrap.py index 2c3bc75..bd653d3 100644 --- a/workers/parse-worker/src/cortex_worker_parse/bootstrap.py +++ b/workers/parse-worker/src/cortex_worker_parse/bootstrap.py @@ -133,8 +133,7 @@ async def run_once(self) -> ParseWorkerRunResult: raise CortexError( code="parse_worker_timeout", detail=( - "Parse job exceeded timeout budget of " - f"{timeout_seconds} seconds." + f"Parse job exceeded timeout budget of {timeout_seconds} seconds." ), status_code=504, ) from exc