-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
1401 lines (1282 loc) · 52.3 KB
/
api.py
File metadata and controls
1401 lines (1282 loc) · 52.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from __future__ import annotations
import asyncio
import json
import logging
import os
import sys
from copy import deepcopy
from datetime import datetime, timezone
from importlib.metadata import PackageNotFoundError
from importlib.metadata import version as package_version
from time import perf_counter
from typing import Annotated, Any, Literal
from urllib.parse import urlparse
from uuid import uuid4
from fastapi import APIRouter, Depends, Path, Query, Request, status
from fastapi.responses import JSONResponse
from rdflib import Graph as RDFGraph
from src.v2.agents import AgentRuntime, ProviderSet, parse_agent_runtime
from src.v2.api_models import (
V2ErrorResponse,
V2ErrorType,
V2ExtractJob,
V2ExtractJobAccepted,
V2ExtractJobStatus,
V2ExtractRequest,
V2ExtractResponse,
V2FieldError,
V2HealthResponse,
V2JSONLDOutput,
V2JSONOutputEnvelope,
)
from src.v2.auth import verify_token
from src.v2.config import V2Config
from src.v2.dependencies import _resolve_provider_cache, get_provider_set
from src.v2.ingest.cache import ProviderCache
from src.v2.ingest.detection import UnsupportedGitHubURL, classify_github_url
from src.v2.jobs import JobStore
from src.v2.observation.github_rate_limit import (
GitHubRateLimitSummary,
probe_github_rate_limit,
)
from src.v2.observation.query_log import QueryLog, query_log_var
from src.v2.pipeline import PipelineOrchestrator
from src.v2.pipeline.stages import (
AssembledOutput,
RootEntityValidationError,
apply_link_pruning_to_assembled_output,
assemble_output,
build_json_output,
build_jsonld_output,
compute_stats,
guarantee_repo_author,
infer_github_handle_parents,
infer_org_units,
infer_owners,
promote_failed_id_entities,
prune_dangling_refs,
reconcile_entities,
run_concept_tagging_stage,
run_link_veracity_stage,
run_llm_critic_stage,
run_llm_dedup_stage,
run_org_relationships_stage,
run_refine_with_llm_stage,
validate_articles,
validate_author_classes,
validate_ownership,
)
from src.v2.pipeline.stages.validate_org_github_handles import (
validate_org_github_handles,
)
from src.v2.pipeline.stages.refine_with_llm import (
is_enabled as _hybrid_refiner_is_enabled,
)
from src.v2.pipeline.stages.concept_tagging import (
is_enabled as _concept_tagging_is_enabled,
)
from src.v2.pipeline.stages.concept_tagging import (
resolve_backend as _resolve_concept_tagging_backend,
)
from src.v2.pipeline.stages.concept_tagging import (
resolve_epfl_min_score as _resolve_concept_tagging_epfl_min_score,
)
from src.v2.pipeline.stages.concept_tagging import (
resolve_related_enrichment as _resolve_concept_tagging_related_enrichment,
)
from src.v2.pipeline.stages.context_gather import RequiredProviderUnavailableError
from src.v2.schema import load_jsonld_context
from src.v2.validation import (
SHACLValidator,
StrictSchemaValidator,
load_ontology_shapes_graph,
)
from src.v2.validation.shacl_validation import SHACLRuntimeUnavailableError
MIN_SUPPORTED_PYTHON = (3, 10)
PACKAGE_NAME = "git-metadata-extractor"
try:
PACKAGE_VERSION = package_version(PACKAGE_NAME)
except PackageNotFoundError:
PACKAGE_VERSION = "unknown"
MIN_SUBRESOURCE_PATH_SEGMENTS = 3
SUBRESOURCE_SEGMENT_INDEX = 2
JSONLD_CONTEXT_FALLBACK = {
"schema": "http://schema.org/",
"pulse": "https://open-pulse.epfl.ch/ontology#",
"org": "http://www.w3.org/ns/org#",
}
logger = logging.getLogger(__name__)
STAGE_CLASSIFY_URL = "classify_url"
STAGE_PERMISSIVE_VALIDATION = "permissive_validation"
STAGE_STRICT_VALIDATION = "strict_validation"
STAGE_RECONCILIATION = "reconciliation"
STAGE_LLM_DEDUP = "llm_dedup"
STAGE_LLM_CRITIC = "llm_critic"
STAGE_SHACL_GATE = "shacl_gate"
STAGE_OUTPUT_ASSEMBLY = "output_assembly"
STAGE_JSONLD_BUILD = "jsonld_build"
STAGE_LINK_VERACITY = "link_veracity"
STAGE_REFINE_WITH_LLM = "refine_with_llm"
v2_router = APIRouter(prefix="/v2")
_TRUTHY_ENV_VALUES = {"1", "true", "t", "yes", "y", "on"}
def _is_pipeline_cache_enabled() -> bool:
"""Read `V2_PIPELINE_CACHE_ENABLED` env var (default true).
When false, `extract()` neither reads nor writes the pipeline-level cache,
so every request runs the full pipeline. Sub-level caches (provider
responses, agent verdicts, Selenium fetches, link-veracity) are unaffected.
"""
raw = os.getenv("V2_PIPELINE_CACHE_ENABLED")
if raw is None:
return True
return raw.strip().lower() in _TRUTHY_ENV_VALUES
def _is_link_veracity_enabled() -> bool:
"""Read `V2_LINK_VERACITY_ENABLED` env var (default true).
When false, the link-veracity LLM stage is skipped entirely. Useful for
broad batch runs where you want fast extraction and don't need every
URL re-verified against fetched page content. Saves ~1–3 minutes per
repo and a chunk of LLM calls.
"""
raw = os.getenv("V2_LINK_VERACITY_ENABLED")
if raw is None:
return True
return raw.strip().lower() in _TRUTHY_ENV_VALUES
def _should_apply_critic_pruning() -> bool:
"""Read `V2_APPLY_CRITIC_PRUNING` env var (default false).
When false (the default), the LLM critic stage is skipped entirely so no
entities get dropped from the output. Set to true to re-enable the critic
stage's drop suggestions.
"""
raw = os.getenv("V2_APPLY_CRITIC_PRUNING")
if raw is None:
return False
return raw.strip().lower() in _TRUTHY_ENV_VALUES
def _format_duration(seconds: float) -> str:
"""Render a duration in compact human form, e.g. '7.3s' or '4m 12s'."""
if seconds < 60.0:
return f"{seconds:.1f}s"
minutes = int(seconds // 60)
remainder = seconds - minutes * 60
return f"{minutes}m {remainder:.0f}s"
def _jsonld_to_graph(payload: dict[str, Any]) -> RDFGraph | None:
try:
graph = RDFGraph()
graph.parse(data=json.dumps(payload), format="json-ld")
except Exception: # noqa: BLE001
return None
else:
return graph
def _extract_path_kind(source_url: str) -> str | None:
candidate_url = source_url.strip()
if "://" not in candidate_url:
candidate_url = f"https://{candidate_url}"
parsed_url = urlparse(candidate_url)
path_segments = [segment for segment in parsed_url.path.split("/") if segment]
if len(path_segments) >= MIN_SUBRESOURCE_PATH_SEGMENTS:
return path_segments[SUBRESOURCE_SEGMENT_INDEX].lower()
return None
def _resolve_max_concurrent_agents() -> int:
"""Read `V2_MAX_CONCURRENT_AGENTS` env var (default 8).
Caps how many work items per stage (person agents, contribution agents,
link-veracity calls, etc.) run in parallel within a single /extract
request. Higher values speed up wide-fanout repos at the cost of more
concurrent LLM calls — keep within the LLM provider's rate limit.
Default raised from 6 to 8 after profiling a 50-person repo
(deeplabcut/deeplabcut): person+membership stages were spending ~12
minutes waiting on the semaphore. The RCP/LLM stack absorbed 8
in-flight calls without thermal throttling in that test.
"""
raw = os.getenv("V2_MAX_CONCURRENT_AGENTS")
if raw is None:
return 8
try:
value = int(raw.strip())
except ValueError:
return 8
return max(1, value)
def _get_orchestrator(request: Request) -> PipelineOrchestrator:
existing = getattr(request.app.state, "v2_orchestrator", None)
if isinstance(existing, PipelineOrchestrator):
return existing
cache = getattr(request.app.state, "v2_provider_cache", None)
if not isinstance(cache, ProviderCache):
cache = None
orchestrator = PipelineOrchestrator(
cache=cache,
max_concurrent_agents=_resolve_max_concurrent_agents(),
)
request.app.state.v2_orchestrator = orchestrator
return orchestrator
def _resolve_job_store(request: Request) -> JobStore | None:
"""Resolve a JobStore backed by the shared ProviderCache, if available."""
cache = _resolve_provider_cache(request.app.state)
if not isinstance(cache, ProviderCache):
return None
return JobStore(cache)
def _track_background_task(request: Request, task: asyncio.Task[Any]) -> None:
"""Hold a strong reference to a background task so it isn't GC'd mid-flight."""
tasks: set[asyncio.Task[Any]] | None = getattr(
request.app.state,
"_v2_job_tasks",
None,
)
if tasks is None:
tasks = set()
request.app.state._v2_job_tasks = tasks # noqa: SLF001
tasks.add(task)
task.add_done_callback(tasks.discard)
def _job_status_path(job_id: str) -> str:
return f"/v2/jobs/{job_id}"
async def _run_extract_job(
*,
payload: V2ExtractRequest,
request: Request,
providers: ProviderSet,
job_store: JobStore,
job_id: str,
) -> None:
"""Execute the extract pipeline for a job and persist the outcome.
Runs the existing GET handler as a plain async function, then unwraps the
success/error result onto the persisted V2ExtractJob record.
"""
try:
existing = job_store.get(job_id)
if existing is None:
return
existing.status = V2ExtractJobStatus.RUNNING
existing.started_at = datetime.now(timezone.utc)
job_store.set(existing)
result = await extract(
full_path=payload.source_url,
request=request,
output_format=payload.output_format,
agent_runtime=payload.agent_runtime,
include_context_summary=payload.include_context_summary,
providers=providers,
_token="",
)
finished = job_store.get(job_id) or existing
finished.completed_at = datetime.now(timezone.utc)
if isinstance(result, V2ExtractResponse):
finished.status = V2ExtractJobStatus.COMPLETED
finished.result = result
else:
finished.status = V2ExtractJobStatus.FAILED
try:
error_payload = json.loads(result.body)
finished.error = V2ErrorResponse.model_validate(error_payload)
except Exception: # noqa: BLE001
finished.error = V2ErrorResponse(
error_type=V2ErrorType.PIPELINE_ERROR,
detail="extraction failed with non-decodable error payload",
source_url=payload.source_url,
)
job_store.set(finished)
except Exception as exc:
logger.exception("extract job %s failed", job_id)
record = job_store.get(job_id)
if record is None:
return
record.status = V2ExtractJobStatus.FAILED
record.completed_at = datetime.now(timezone.utc)
record.error = V2ErrorResponse(
error_type=V2ErrorType.PIPELINE_ERROR,
detail=str(exc),
source_url=payload.source_url,
)
job_store.set(record)
def _append_unique_warning(warnings: list[str], warning: str) -> None:
if warning and warning not in warnings:
warnings.append(warning)
def _iter_reconciled_entities(
*,
reconciled_entities: dict[str, list[dict[str, Any]]],
memberships: list[dict[str, Any]],
contributions: list[dict[str, Any]],
) -> list[tuple[str, dict[str, Any]]]:
payloads: list[tuple[str, dict[str, Any]]] = []
for entity_type, entities in (
("person", reconciled_entities.get("persons", [])),
("organization", reconciled_entities.get("organizations", [])),
("repository", reconciled_entities.get("repositories", [])),
("article", reconciled_entities.get("articles", [])),
):
payloads.extend((entity_type, entity) for entity in entities if isinstance(entity, dict))
payloads.extend(("membership", membership) for membership in memberships if isinstance(membership, dict))
payloads.extend(
("contribution", contribution)
for contribution in contributions
if isinstance(contribution, dict)
)
return payloads
def _extract_jsonld_context() -> dict[str, Any]:
try:
return load_jsonld_context()
except (OSError, TypeError, ValueError):
return dict(JSONLD_CONTEXT_FALLBACK)
def _root_entity_type_for_detected_type(
detected_type: Literal["repository", "user", "organization"],
) -> Literal["repository", "person", "organization"]:
if detected_type == "user":
return "person"
return detected_type
def _build_rootless_assembled_output(
*,
reconciled: Any,
strict_batch: Any,
root_warning: str,
) -> AssembledOutput:
related_entities: list[dict[str, Any]] = []
for _, payload in strict_batch.valid_entities:
if isinstance(payload, dict):
related_entities.append(deepcopy(payload))
excluded_entities: list[dict[str, Any]] = []
warnings = [*reconciled.link_warnings, root_warning]
for entity_type, payload, validation in strict_batch.invalid_entities:
if not isinstance(payload, dict):
continue
reason = list(validation.errors)
excluded_entities.append(
{
"entity_type": entity_type,
"entity": deepcopy(payload),
"reason": reason,
},
)
warnings.append(
f"Excluded {entity_type} entity '{payload.get('id')}' due to strict validation errors: {reason}",
)
return AssembledOutput(
root_entity=None,
related_entities=related_entities,
excluded_entities=excluded_entities,
warnings=warnings,
)
@v2_router.get(
"/extract/{full_path:path}",
response_model=V2ExtractResponse,
response_model_exclude_none=True,
)
async def extract( # noqa: C901, PLR0911, PLR0912, PLR0913, PLR0915
full_path: Annotated[
str,
Path(
description="GitHub repository, user, or organization URL or handle.",
openapi_examples={
"gimie": {
"summary": "GIMIE repository",
"value": "https://github.com/sdsc-ordes/gimie",
},
"sdsc-ordes": {
"summary": "SDSC ORDES organization",
"value": "https://github.com/sdsc-ordes",
},
"cmdoret": {
"summary": "cmdoret user",
"value": "https://github.com/cmdoret",
},
},
),
],
request: Request,
*,
output_format: Annotated[Literal["jsonld", "json"], Query()] = "jsonld",
agent_runtime: Annotated[Literal["rule_based", "llm", "hybrid"] | None, Query()] = None,
include_context_summary: Annotated[bool, Query()] = False,
providers: Annotated[ProviderSet, Depends(get_provider_set)],
_token: Annotated[str, Depends(verify_token)],
) -> V2ExtractResponse | JSONResponse:
"""Run the v2 extraction pipeline for a GitHub path."""
config = V2Config()
run_id: str | None = None
try:
classification = classify_github_url(full_path)
except UnsupportedGitHubURL as exc:
logger.warning("classify_url: unsupported url=%s reason=%s", exc.normalized_url, exc.reason)
error_payload = V2ErrorResponse(
error_type=V2ErrorType.UNSUPPORTED_URL,
detail=exc.reason,
source_url=exc.normalized_url,
detected_path_kind=_extract_path_kind(full_path),
)
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=error_payload.model_dump(mode="json", exclude_none=True),
)
except ValueError as exc:
logger.warning("classify_url: invalid input %s: %s", full_path, exc)
error_payload = V2ErrorResponse(
error_type=V2ErrorType.UNSUPPORTED_URL,
detail=str(exc),
source_url=full_path,
detected_path_kind=_extract_path_kind(full_path),
)
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=error_payload.model_dump(mode="json", exclude_none=True),
)
run_id = str(uuid4())
request.state.v2_run_id = run_id
resolved_runtime = parse_agent_runtime(
agent_runtime,
default=config.V2_AGENT_RUNTIME_DEFAULT,
field_name="agent_runtime",
)
total_started_at = perf_counter()
link_veracity_seconds = 0.0
logger.info(
"extract: run_id=%s url=%s detected_type=%s runtime=%s",
run_id,
classification.normalized_url,
classification.detected_type.value,
resolved_runtime.value,
)
# Per-request query log: every external-service tool call lands here.
query_log = QueryLog(
run_id=run_id,
extract_full_path=classification.normalized_url,
)
query_log_var.set(query_log)
# Resolve the cache singleton once. Sub-stages always receive it (so
# provider caches, agent verdicts, Selenium fetches, link-veracity, and
# DuckDuckGo searches all keep working). The pipeline-level lookup/store
# is gated separately by V2_PIPELINE_CACHE_ENABLED so it can be toggled
# off without disabling the sub-caches.
pipeline_cache = getattr(request.app.state, "v2_provider_cache", None)
if not isinstance(pipeline_cache, ProviderCache):
pipeline_cache = None
pipeline_cache_enabled = pipeline_cache is not None and _is_pipeline_cache_enabled()
pipeline_cache_key: str | None = None
if pipeline_cache_enabled:
pipeline_cache_key = ProviderCache.make_key(
"pipeline",
"extract",
url=classification.normalized_url,
output_format=output_format,
agent_runtime=resolved_runtime.value,
include_context_summary=bool(include_context_summary),
)
cached_response = pipeline_cache.get(pipeline_cache_key)
if isinstance(cached_response, dict):
try:
response_model = V2ExtractResponse.model_validate(cached_response)
except Exception: # noqa: BLE001
logger.warning(
"pipeline cache hit but cached payload failed validation; "
"falling through to fresh extraction (run_id=%s)",
run_id,
)
else:
cached_seconds = perf_counter() - total_started_at
logger.info(
"pipeline cache hit: url=%s run_id=%s elapsed=%s",
classification.normalized_url,
run_id,
_format_duration(cached_seconds),
)
return response_model
try:
orchestrator = _get_orchestrator(request)
execution_plan = orchestrator.get_execution_plan(classification.detected_type)
pipeline_result = await orchestrator.execute(
plan=execution_plan,
providers=providers,
context={
"source_url": classification.normalized_url,
"url_info": classification,
"agent_runtime": resolved_runtime.value,
"run_id": run_id,
},
)
except RequiredProviderUnavailableError as exc:
logger.exception("provider_preflight failed: run_id=%s", run_id)
error_payload = V2ErrorResponse(
error_type=V2ErrorType.PROVIDER_ERROR,
detail=str(exc),
source_url=classification.normalized_url,
)
return JSONResponse(
status_code=status.HTTP_502_BAD_GATEWAY,
content=error_payload.model_dump(mode="json", exclude_none=True),
)
except Exception as exc:
logger.exception("pipeline_execute failed: run_id=%s", run_id)
error_payload = V2ErrorResponse(
error_type=V2ErrorType.PIPELINE_ERROR,
detail=str(exc),
source_url=classification.normalized_url,
)
return JSONResponse(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
content=error_payload.model_dump(mode="json", exclude_none=True),
)
warnings = list(pipeline_result.warnings)
pipeline_outputs_for_prompt = {
agent_name: agent_result.data
for agent_name, agent_result in pipeline_result.agent_results.items()
if isinstance(agent_result.data, dict) and agent_result.data
}
gathered_context = (
deepcopy(pipeline_result.gathered_context)
if isinstance(pipeline_result.gathered_context, dict)
else {}
)
typed_entity_buckets = pipeline_result.resolved_typed_entity_buckets().to_dict()
permissive_entity_count = sum(len(bucket) for bucket in typed_entity_buckets.values())
logger.info("%s: entity_count=%d", STAGE_PERMISSIVE_VALIDATION, permissive_entity_count)
llm_dedup_executed = False
if resolved_runtime == AgentRuntime.LLM:
llm_dedup_executed = True
stage_started_at = perf_counter()
try:
dedup_result = await run_llm_dedup_stage(
typed_entity_buckets=typed_entity_buckets,
source_url=classification.normalized_url,
detected_type=classification.detected_type.value,
providers=providers,
pipeline_outputs=pipeline_outputs_for_prompt,
initial_context=gathered_context,
max_concurrency=orchestrator.max_concurrent_agents,
)
except Exception as exc:
logger.exception("%s stage failed", STAGE_LLM_DEDUP)
_append_unique_warning(warnings, f"llm_dedup stage failed: {exc}")
else:
typed_entity_buckets = dedup_result.typed_entity_buckets
logger.info(
"%s: accepted=%d rejected=%d remap=%d in %.2fs",
STAGE_LLM_DEDUP,
dedup_result.accepted_cluster_count,
dedup_result.rejected_cluster_count,
dedup_result.remap_count,
perf_counter() - stage_started_at,
)
for warning in dedup_result.warnings:
_append_unique_warning(warnings, warning)
llm_critic_executed = False
critic_pruned_excluded_entities: list[dict[str, Any]] = []
stage_started_at = perf_counter()
reconciled = reconcile_entities(typed_entity_buckets)
logger.info(
"%s: persons=%d orgs=%d repos=%d articles=%d memberships=%d contributions=%d in %.2fs",
STAGE_RECONCILIATION,
len(reconciled.entities.get("persons", [])),
len(reconciled.entities.get("organizations", [])),
len(reconciled.entities.get("repositories", [])),
len(reconciled.entities.get("articles", [])),
len(reconciled.memberships),
len(reconciled.contributions),
perf_counter() - stage_started_at,
)
for warning in reconciled.link_warnings:
_append_unique_warning(warnings, warning)
apply_critic_pruning = _should_apply_critic_pruning()
if resolved_runtime == AgentRuntime.LLM and not apply_critic_pruning:
logger.info(
"%s: skipped (V2_APPLY_CRITIC_PRUNING=false — entities preserved)",
STAGE_LLM_CRITIC,
)
if resolved_runtime == AgentRuntime.LLM and apply_critic_pruning:
llm_critic_executed = True
stage_started_at = perf_counter()
try:
critic_result = await run_llm_critic_stage(
reconciled=reconciled,
source_url=classification.normalized_url,
detected_type=classification.detected_type.value,
providers=providers,
initial_context=gathered_context,
pipeline_outputs=pipeline_outputs_for_prompt,
max_concurrency=orchestrator.max_concurrent_agents,
cache=pipeline_cache,
)
except Exception as exc:
logger.exception("%s stage failed", STAGE_LLM_CRITIC)
_append_unique_warning(warnings, f"llm_critic stage failed: {exc}")
else:
reconciled = critic_result.reconciled
critic_pruned_excluded_entities = critic_result.pruned_excluded_entities
logger.info(
"%s: proposed_drop=%d applied_drop=%d protected_roots=%d in %.2fs",
STAGE_LLM_CRITIC,
critic_result.applied.get("proposed_drop_count", 0),
critic_result.applied.get("applied_drop_count", 0),
len(critic_result.applied.get("protected_root_ids", [])),
perf_counter() - stage_started_at,
)
for warning in critic_result.warnings:
_append_unique_warning(warnings, warning)
if resolved_runtime == AgentRuntime.HYBRID and _hybrid_refiner_is_enabled():
stage_started_at = perf_counter()
try:
refine_result = await run_refine_with_llm_stage(
reconciled=reconciled,
gathered_context=gathered_context,
epfl_graph_provider=providers.epfl_graph_rag,
max_concurrency=orchestrator.max_concurrent_agents,
)
except Exception as exc:
logger.exception("%s stage failed", STAGE_REFINE_WITH_LLM)
_append_unique_warning(
warnings,
f"refine_with_llm stage failed: {exc}",
)
else:
reconciled = refine_result.reconciled
logger.info(
"%s: refined=%d skipped=%d failed=%d in %.2fs",
STAGE_REFINE_WITH_LLM,
refine_result.refined_count,
refine_result.skipped_count,
refine_result.failed_count,
perf_counter() - stage_started_at,
)
for warning in refine_result.warnings:
_append_unique_warning(warnings, warning)
# KNOWN BUG salvage: when reconciliation drops unresolvable
# `schema:author` references, a repository can end up with an empty
# author array, which strict validation rejects (schema requires
# non-empty). Fall back to the github owner if it's in the graph.
stage_started_at = perf_counter()
reconciled, repo_author_warnings = guarantee_repo_author(reconciled)
logger.info(
"guarantee_repo_author: salvaged=%d in %.2fs",
len(repo_author_warnings),
perf_counter() - stage_started_at,
)
for warning in repo_author_warnings:
_append_unique_warning(warnings, warning)
# Validate `@handle`-style org names against GitHub before strict
# validation so we either stamp the missing handle or drop the
# hallucinated entity (rather than just losing it to anyOf).
stage_started_at = perf_counter()
reconciled, org_handle_warnings = validate_org_github_handles(reconciled, providers)
logger.info(
"validate_org_github_handles: actions=%d in %.2fs",
len(org_handle_warnings),
perf_counter() - stage_started_at,
)
for warning in org_handle_warnings:
_append_unique_warning(warnings, warning)
stage_started_at = perf_counter()
strict_validation_entities = _iter_reconciled_entities(
reconciled_entities=reconciled.entities,
memberships=reconciled.memberships,
contributions=reconciled.contributions,
)
strict_batch = StrictSchemaValidator().validate_batch(strict_validation_entities)
logger.info(
"%s: valid=%d invalid=%d in %.2fs",
STAGE_STRICT_VALIDATION,
len(strict_batch.valid_entities),
len(strict_batch.invalid_entities),
perf_counter() - stage_started_at,
)
for warning in strict_batch.warnings:
_append_unique_warning(warnings, f"Strict validation: {warning}")
jsonld_context = _extract_jsonld_context()
stage_started_at = perf_counter()
try:
assembled_output = assemble_output(
reconciled,
strict_batch,
root_entity_type=_root_entity_type_for_detected_type(
classification.detected_type.value,
),
)
except RootEntityValidationError as exc:
failure_message = (
f"Root {exc.entity_type} entity '{exc.entity_id}' failed strict validation"
)
logger.warning("%s: %s", STAGE_OUTPUT_ASSEMBLY, failure_message)
error_payload = V2ErrorResponse(
error_type=V2ErrorType.VALIDATION_ERROR,
detail=failure_message,
source_url=classification.normalized_url,
errors=[
V2FieldError(
field=error.get("path", "<root>"),
message=error.get("message", "validation error"),
value=error.get("expected"),
)
for error in exc.validation_errors
],
)
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_ENTITY,
content=error_payload.model_dump(mode="json", exclude_none=True),
)
except ValueError as exc:
logger.warning("%s: rootless output — %s", STAGE_OUTPUT_ASSEMBLY, exc)
assembled_output = _build_rootless_assembled_output(
reconciled=reconciled,
strict_batch=strict_batch,
root_warning=str(exc),
)
logger.info(
"%s: related=%d excluded=%d warnings=%d in %.2fs",
STAGE_OUTPUT_ASSEMBLY,
len(assembled_output.related_entities),
len(assembled_output.excluded_entities),
len(assembled_output.warnings),
perf_counter() - stage_started_at,
)
if critic_pruned_excluded_entities:
for excluded_entity in critic_pruned_excluded_entities:
if not isinstance(excluded_entity, dict):
continue
assembled_output.excluded_entities.append(deepcopy(excluded_entity))
entity_payload = excluded_entity.get("entity")
entity_id = entity_payload.get("id") if isinstance(entity_payload, dict) else None
assembled_output.warnings.append(
(
f"Excluded {excluded_entity.get('entity_type', 'entity')} entity "
f"'{entity_id}' due to critic pruning"
),
)
for warning in assembled_output.warnings:
_append_unique_warning(warnings, warning)
entities_for_link_validation: list[dict[str, Any]] = []
if isinstance(assembled_output.root_entity, dict):
entities_for_link_validation.append(deepcopy(assembled_output.root_entity))
entities_for_link_validation.extend(
deepcopy(entity)
for entity in assembled_output.related_entities
if isinstance(entity, dict)
)
provider_cache = getattr(request.app.state, "v2_provider_cache", None)
if not isinstance(provider_cache, ProviderCache):
provider_cache = None
link_veracity_started_at = perf_counter()
link_veracity_result = None
if resolved_runtime != AgentRuntime.LLM:
# link_veracity calls an LLM per link, so it has no place in
# `agent_runtime=rule_based` (the whole point of rule-based mode is
# zero LLM calls). The `V2_LINK_VERACITY_ENABLED` env var still
# gates the stage *within* LLM mode for users who want fast LLM
# extracts without per-link verification.
logger.info(
"%s: skipped (agent_runtime=%s — link veracity is LLM-only)",
STAGE_LINK_VERACITY,
resolved_runtime.value,
)
elif not _is_link_veracity_enabled():
logger.info(
"%s: skipped (V2_LINK_VERACITY_ENABLED=false)",
STAGE_LINK_VERACITY,
)
else:
try:
link_veracity_result = await run_link_veracity_stage(
entities=entities_for_link_validation,
source_url=classification.normalized_url,
providers=providers,
max_concurrency=orchestrator.max_concurrent_agents,
cache=provider_cache,
)
except Exception as exc:
logger.exception("%s stage failed", STAGE_LINK_VERACITY)
_append_unique_warning(warnings, f"Link veracity stage failed: {exc}")
else:
logger.info(
"%s: checked=%d supported=%d unsupported=%d failed=%d invalid_links=%d in %.2fs",
STAGE_LINK_VERACITY,
link_veracity_result.checked_count,
link_veracity_result.supported_count,
link_veracity_result.unsupported_count,
link_veracity_result.failed_count,
len(link_veracity_result.invalid_links),
perf_counter() - link_veracity_started_at,
)
_append_unique_warning(
warnings,
(
"Link veracity summary: "
f"checked={link_veracity_result.checked_count}, "
f"supported={link_veracity_result.supported_count}, "
f"unsupported={link_veracity_result.unsupported_count}, "
f"failed={link_veracity_result.failed_count}"
),
)
for warning in link_veracity_result.warnings:
_append_unique_warning(warnings, warning)
invalid_links = set(link_veracity_result.invalid_links)
if invalid_links:
assembled_output, id_rewrites, promotion_warnings = promote_failed_id_entities(
assembled=assembled_output,
invalid_links=invalid_links,
)
for warning in promotion_warnings:
_append_unique_warning(warnings, warning)
if id_rewrites:
logger.info(
"%s: promoted %d entity id(s) past failed url(s)",
STAGE_LINK_VERACITY,
len(id_rewrites),
)
# The rewritten entities now expose their new id; remove the
# old urls from invalid_links so we don't also try to prune them.
invalid_links = invalid_links - set(id_rewrites)
# Rebuild entity_link_map with the new ids so downstream pruning
# acts on the right entity references.
rebuilt_entity_link_map: dict[str, list[str]] = {}
for old_id, links in link_veracity_result.entity_link_map.items():
new_id = id_rewrites.get(old_id, old_id)
rebuilt_entity_link_map.setdefault(new_id, []).extend(links)
else:
rebuilt_entity_link_map = link_veracity_result.entity_link_map
if invalid_links:
assembled_output, link_pruning_warnings = apply_link_pruning_to_assembled_output(
assembled=assembled_output,
invalid_links=invalid_links,
entity_link_map=rebuilt_entity_link_map,
article_identifier_link_map=link_veracity_result.article_identifier_link_map,
)
for warning in link_pruning_warnings:
_append_unique_warning(warnings, warning)
# Article-validation runs whether or not link-veracity ran. If
# link-veracity was skipped, supported/unsupported sets will be empty,
# and the stage will only drop articles with placeholder/sentinel DOIs
# (i.e. its non-veracity-dependent rules still apply).
veracity_records = (
link_veracity_result.records if link_veracity_result is not None else []
)
stage_started_at = perf_counter()
assembled_output, article_validation_warnings = validate_articles(
assembled_output,
veracity_records=veracity_records,
)
logger.info(
"validate_articles: warnings=%d in %.2fs",
len(article_validation_warnings),
perf_counter() - stage_started_at,
)
for warning in article_validation_warnings:
_append_unique_warning(warnings, warning)
stage_started_at = perf_counter()
assembled_output, author_class_warnings = validate_author_classes(assembled_output)
logger.info(
"author_class_validation: pruned=%d in %.2fs",
len(author_class_warnings),
perf_counter() - stage_started_at,
)
for warning in author_class_warnings:
_append_unique_warning(warnings, warning)
link_veracity_seconds = perf_counter() - link_veracity_started_at
stage_started_at = perf_counter()
assembled_output, ownership_warnings = validate_ownership(assembled_output)
logger.info(
"ownership_check: dropped=%d in %.2fs",
len(ownership_warnings),
perf_counter() - stage_started_at,
)
for warning in ownership_warnings:
_append_unique_warning(warnings, warning)
# Run `infer_owners` BEFORE `prune_dangling_refs` so it can materialise
# minimal Person stubs for github owners that have no matching entity
# (otherwise prune would clear `pulse:ownedBy` first and the stub
# opportunity is lost).
stage_started_at = perf_counter()
assembled_output, owner_inference_warnings = infer_owners(assembled_output)
logger.info(
"owner_inference: stamped=%d in %.2fs",
len(owner_inference_warnings),
perf_counter() - stage_started_at,
)
for warning in owner_inference_warnings:
_append_unique_warning(warnings, warning)
stage_started_at = perf_counter()
assembled_output, prune_warnings = prune_dangling_refs(assembled_output)
logger.info(
"prune_dangling_refs: actions=%d in %.2fs",
len(prune_warnings),
perf_counter() - stage_started_at,
)
for warning in prune_warnings:
_append_unique_warning(warnings, warning)
# Fuzzy-search ROR for parent organizations of every github-only org in
# the graph. The github org always remains as a standalone entity; ROR
# matches get added as additional org entities and the best match
# becomes the github org's `unitOf` parent.
# Runs before the LLM relationship stage so it sees the new ROR entities
# and can refine the unitOf decision; runs before `infer_org_units` so
# the token-overlap fallback also gets the broader graph.
stage_started_at = perf_counter()
assembled_output, github_parent_warnings = infer_github_handle_parents(
assembled_output,
providers=providers,
)
logger.info(
"github_handle_parents: actions=%d in %.2fs",
len(github_parent_warnings),
perf_counter() - stage_started_at,