harborrag-runtime 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harborrag_runtime/__init__.py +22 -0
- harborrag_runtime/application/README.md +39 -0
- harborrag_runtime/application/__init__.py +39 -0
- harborrag_runtime/application/chat/__init__.py +7 -0
- harborrag_runtime/application/chat/citations.py +149 -0
- harborrag_runtime/application/chat/client.py +201 -0
- harborrag_runtime/application/chat/completion.py +92 -0
- harborrag_runtime/application/chat/completion_request.py +130 -0
- harborrag_runtime/application/chat/evidence.py +202 -0
- harborrag_runtime/application/chat/options.py +43 -0
- harborrag_runtime/application/chat/preparation.py +135 -0
- harborrag_runtime/application/chat/presenters.py +130 -0
- harborrag_runtime/application/chat/prompting.py +127 -0
- harborrag_runtime/application/chat/query_scope.py +146 -0
- harborrag_runtime/application/chat/replay.py +141 -0
- harborrag_runtime/application/chat/request_filters.py +134 -0
- harborrag_runtime/application/chat/retrieval.py +127 -0
- harborrag_runtime/application/chat/service.py +371 -0
- harborrag_runtime/application/composition/__init__.py +1 -0
- harborrag_runtime/application/composition/factories.py +106 -0
- harborrag_runtime/application/composition/resources.py +141 -0
- harborrag_runtime/application/composition/selection.py +65 -0
- harborrag_runtime/application/composition/service.py +550 -0
- harborrag_runtime/application/composition/tenant_models.py +45 -0
- harborrag_runtime/application/control_plane/__init__.py +1 -0
- harborrag_runtime/application/control_plane/active_runs.py +38 -0
- harborrag_runtime/application/control_plane/connection_models.py +109 -0
- harborrag_runtime/application/control_plane/connection_testing.py +91 -0
- harborrag_runtime/application/control_plane/connection_writes.py +227 -0
- harborrag_runtime/application/control_plane/connections.py +749 -0
- harborrag_runtime/application/control_plane/effect_recovery.py +235 -0
- harborrag_runtime/application/control_plane/projects.py +313 -0
- harborrag_runtime/application/control_plane/settings_import.py +239 -0
- harborrag_runtime/application/control_plane/settings_service.py +233 -0
- harborrag_runtime/application/control_plane/tenants.py +55 -0
- harborrag_runtime/application/errors.py +110 -0
- harborrag_runtime/application/ingestion/__init__.py +1 -0
- harborrag_runtime/application/ingestion/client.py +65 -0
- harborrag_runtime/application/ingestion/connection_sources.py +96 -0
- harborrag_runtime/application/ingestion/direct.py +120 -0
- harborrag_runtime/application/ingestion/follower.py +83 -0
- harborrag_runtime/application/ingestion/ports.py +174 -0
- harborrag_runtime/application/ingestion/progress_bridge.py +221 -0
- harborrag_runtime/application/ingestion/retry_selection.py +71 -0
- harborrag_runtime/application/ingestion/service.py +564 -0
- harborrag_runtime/application/ingestion/starter.py +78 -0
- harborrag_runtime/application/memory/__init__.py +21 -0
- harborrag_runtime/application/memory/client.py +114 -0
- harborrag_runtime/application/memory/composition.py +90 -0
- harborrag_runtime/application/memory/context.py +45 -0
- harborrag_runtime/application/memory/sessions.py +117 -0
- harborrag_runtime/application/ports.py +133 -0
- harborrag_runtime/application/retrieval/__init__.py +1 -0
- harborrag_runtime/application/retrieval/access.py +26 -0
- harborrag_runtime/application/scheduling/service.py +58 -0
- harborrag_runtime/application/schemas.py +15 -0
- harborrag_runtime/chat/README.md +40 -0
- harborrag_runtime/chat/__init__.py +18 -0
- harborrag_runtime/chat/composition.py +53 -0
- harborrag_runtime/chat/facade.py +46 -0
- harborrag_runtime/chat/service.py +183 -0
- harborrag_runtime/chat/tenant_clients.py +341 -0
- harborrag_runtime/chat/tenant_config.py +235 -0
- harborrag_runtime/chat/tenant_models.py +22 -0
- harborrag_runtime/composition/__init__.py +5 -0
- harborrag_runtime/composition/config_store.py +53 -0
- harborrag_runtime/composition/connection_removal.py +120 -0
- harborrag_runtime/composition/connection_stores.py +107 -0
- harborrag_runtime/composition/control_plane.py +401 -0
- harborrag_runtime/composition/mcp_auth.py +39 -0
- harborrag_runtime/composition/readers.py +113 -0
- harborrag_runtime/composition/resources.py +336 -0
- harborrag_runtime/composition/storage_providers.py +113 -0
- harborrag_runtime/config/__init__.py +84 -0
- harborrag_runtime/config/checkout.py +212 -0
- harborrag_runtime/config/chunking.py +167 -0
- harborrag_runtime/config/connectors/__init__.py +19 -0
- harborrag_runtime/config/connectors/fingerprints.py +54 -0
- harborrag_runtime/config/connectors/importing.py +611 -0
- harborrag_runtime/config/connectors/loader.py +95 -0
- harborrag_runtime/config/connectors/models/__init__.py +30 -0
- harborrag_runtime/config/connectors/models/atlassian.py +199 -0
- harborrag_runtime/config/connectors/models/builder.py +208 -0
- harborrag_runtime/config/connectors/models/common.py +45 -0
- harborrag_runtime/config/connectors/models/fields.py +134 -0
- harborrag_runtime/config/connectors/models/github.py +143 -0
- harborrag_runtime/config/connectors/models/local_file.py +119 -0
- harborrag_runtime/config/connectors/models/registry.py +149 -0
- harborrag_runtime/config/connectors/models/sharepoint.py +186 -0
- harborrag_runtime/config/connectors/models/spec.py +563 -0
- harborrag_runtime/config/connectors/parsing.py +275 -0
- harborrag_runtime/config/connectors/providers.py +177 -0
- harborrag_runtime/config/connectors/schemas.py +215 -0
- harborrag_runtime/config/errors.py +26 -0
- harborrag_runtime/config/graph_build.py +335 -0
- harborrag_runtime/config/graph_build_loading.py +38 -0
- harborrag_runtime/config/loading.py +245 -0
- harborrag_runtime/config/models.py +95 -0
- harborrag_runtime/config/ontology_loading.py +76 -0
- harborrag_runtime/config/parsers/__init__.py +19 -0
- harborrag_runtime/config/parsers/loader.py +85 -0
- harborrag_runtime/config/parsers/parsing.py +285 -0
- harborrag_runtime/config/parsers/providers.py +305 -0
- harborrag_runtime/config/parsers/schemas.py +614 -0
- harborrag_runtime/config/settings.py +378 -0
- harborrag_runtime/config/settings_document.py +133 -0
- harborrag_runtime/config/temporal.py +331 -0
- harborrag_runtime/config/temporal_loading.py +310 -0
- harborrag_runtime/config_store/__init__.py +5 -0
- harborrag_runtime/config_store/defaults.py +47 -0
- harborrag_runtime/config_store/provider.py +153 -0
- harborrag_runtime/config_store/run_settings.py +72 -0
- harborrag_runtime/connections/__init__.py +27 -0
- harborrag_runtime/connections/secrets.py +348 -0
- harborrag_runtime/contracts.py +237 -0
- harborrag_runtime/document_stage_catalog.py +82 -0
- harborrag_runtime/errors.py +64 -0
- harborrag_runtime/events/__init__.py +3 -0
- harborrag_runtime/events/in_process.py +100 -0
- harborrag_runtime/execution/__init__.py +26 -0
- harborrag_runtime/execution/contracts.py +32 -0
- harborrag_runtime/execution/direct.py +169 -0
- harborrag_runtime/execution/gateway.py +97 -0
- harborrag_runtime/execution/source_conversion.py +61 -0
- harborrag_runtime/execution/source_submission.py +278 -0
- harborrag_runtime/execution/submission.py +64 -0
- harborrag_runtime/execution/task_registry.py +387 -0
- harborrag_runtime/execution/temporal.py +165 -0
- harborrag_runtime/ingestion/README.md +79 -0
- harborrag_runtime/ingestion/__init__.py +59 -0
- harborrag_runtime/ingestion/chunking_profile.py +97 -0
- harborrag_runtime/ingestion/composition.py +279 -0
- harborrag_runtime/ingestion/connection_checks.py +239 -0
- harborrag_runtime/ingestion/connections.py +507 -0
- harborrag_runtime/ingestion/document/__init__.py +1 -0
- harborrag_runtime/ingestion/document/normalization.py +14 -0
- harborrag_runtime/ingestion/document/normalizers/__init__.py +9 -0
- harborrag_runtime/ingestion/document/normalizers/builtins.py +39 -0
- harborrag_runtime/ingestion/document/normalizers/transform.py +30 -0
- harborrag_runtime/ingestion/document/run_tools.py +152 -0
- harborrag_runtime/ingestion/failure_classification.py +82 -0
- harborrag_runtime/ingestion/field_indexes.py +56 -0
- harborrag_runtime/ingestion/maintenance/__init__.py +1 -0
- harborrag_runtime/ingestion/maintenance/connection_removal.py +115 -0
- harborrag_runtime/ingestion/maintenance/projection_admin.py +180 -0
- harborrag_runtime/ingestion/maintenance/relation_repair.py +555 -0
- harborrag_runtime/ingestion/maintenance/retention.py +353 -0
- harborrag_runtime/ingestion/observability.py +3 -0
- harborrag_runtime/ingestion/profiles.py +127 -0
- harborrag_runtime/ingestion/runtime_builder.py +339 -0
- harborrag_runtime/ingestion/source/__init__.py +1 -0
- harborrag_runtime/ingestion/source/descriptor_mapping.py +144 -0
- harborrag_runtime/ingestion/source/discovery.py +290 -0
- harborrag_runtime/ingestion/source/documents.py +184 -0
- harborrag_runtime/ingestion/source/finalization.py +288 -0
- harborrag_runtime/ingestion/source/operations.py +175 -0
- harborrag_runtime/ingestion/source/plan.py +413 -0
- harborrag_runtime/ingestion/source/retry.py +150 -0
- harborrag_runtime/ingestion/source/service.py +470 -0
- harborrag_runtime/ingestion/source/skips.py +172 -0
- harborrag_runtime/ingestion_contracts.py +107 -0
- harborrag_runtime/memory/__init__.py +81 -0
- harborrag_runtime/memory/access.py +47 -0
- harborrag_runtime/memory/administration.py +460 -0
- harborrag_runtime/memory/context/__init__.py +5 -0
- harborrag_runtime/memory/context/result.py +20 -0
- harborrag_runtime/memory/directory.py +166 -0
- harborrag_runtime/memory/erasure.py +122 -0
- harborrag_runtime/memory/locks.py +170 -0
- harborrag_runtime/memory/messages.py +202 -0
- harborrag_runtime/memory/presenters.py +117 -0
- harborrag_runtime/memory/projects.py +38 -0
- harborrag_runtime/memory/tiers/__init__.py +1 -0
- harborrag_runtime/memory/tiers/in_memory.py +491 -0
- harborrag_runtime/memory/titles.py +32 -0
- harborrag_runtime/memory/usage.py +131 -0
- harborrag_runtime/observability/__init__.py +1 -0
- harborrag_runtime/observability/connector_metrics.py +14 -0
- harborrag_runtime/observability/discovery_metrics.py +46 -0
- harborrag_runtime/observability/model.py +410 -0
- harborrag_runtime/observability/observability_types.py +47 -0
- harborrag_runtime/observability/stage_observation.py +96 -0
- harborrag_runtime/observability/tool_audit.py +17 -0
- harborrag_runtime/plugins.py +56 -0
- harborrag_runtime/rate_limiting.py +39 -0
- harborrag_runtime/reader_contracts.py +149 -0
- harborrag_runtime/retrieval/__init__.py +1 -0
- harborrag_runtime/retrieval/composition.py +174 -0
- harborrag_runtime/retrieval/facades.py +224 -0
- harborrag_runtime/scheduling/__init__.py +48 -0
- harborrag_runtime/scheduling/config.py +185 -0
- harborrag_runtime/scheduling/errors.py +37 -0
- harborrag_runtime/scheduling/models.py +146 -0
- harborrag_runtime/scheduling/service.py +297 -0
- harborrag_runtime/scheduling/validation.py +213 -0
- harborrag_runtime/sdk/__init__.py +86 -0
- harborrag_runtime/sdk/configuration.py +44 -0
- harborrag_runtime/sdk/facades.py +47 -0
- harborrag_runtime/sdk/runtime.py +242 -0
- harborrag_runtime/security/__init__.py +1 -0
- harborrag_runtime/security/api_key_operations.py +236 -0
- harborrag_runtime/serialization.py +35 -0
- harborrag_runtime/temporal/__init__.py +19 -0
- harborrag_runtime/temporal/activity_observability.py +313 -0
- harborrag_runtime/temporal/client.py +580 -0
- harborrag_runtime/temporal/connection.py +39 -0
- harborrag_runtime/temporal/conversion.py +147 -0
- harborrag_runtime/temporal/dispatch.py +38 -0
- harborrag_runtime/temporal/document_workflow.py +154 -0
- harborrag_runtime/temporal/failure_handling.py +68 -0
- harborrag_runtime/temporal/gateway.py +121 -0
- harborrag_runtime/temporal/heartbeats.py +67 -0
- harborrag_runtime/temporal/identity.py +14 -0
- harborrag_runtime/temporal/ingestion_activities.py +435 -0
- harborrag_runtime/temporal/maintenance_activities.py +251 -0
- harborrag_runtime/temporal/maintenance_schemas.py +88 -0
- harborrag_runtime/temporal/optional.py +19 -0
- harborrag_runtime/temporal/plan_resolver.py +149 -0
- harborrag_runtime/temporal/policies.py +18 -0
- harborrag_runtime/temporal/reindex_workflow.py +71 -0
- harborrag_runtime/temporal/retry_activities.py +147 -0
- harborrag_runtime/temporal/retry_workflow.py +222 -0
- harborrag_runtime/temporal/sandbox.py +51 -0
- harborrag_runtime/temporal/schedules.py +341 -0
- harborrag_runtime/temporal/schemas.py +482 -0
- harborrag_runtime/temporal/source_activities.py +590 -0
- harborrag_runtime/temporal/source_batch_workflow.py +226 -0
- harborrag_runtime/temporal/source_workflow.py +908 -0
- harborrag_runtime/temporal/submission.py +23 -0
- harborrag_runtime/temporal/worker.py +478 -0
- harborrag_runtime/temporal/worker_registry.py +141 -0
- harborrag_runtime/temporal_models.py +227 -0
- harborrag_runtime/testing/chat_fakes.py +309 -0
- harborrag_runtime/testing/chat_service_double.py +126 -0
- harborrag_runtime/testing/connection_fakes.py +278 -0
- harborrag_runtime/testing/memory_fakes.py +128 -0
- harborrag_runtime/topology/__init__.py +1 -0
- harborrag_runtime/topology/budgeted_extractor.py +110 -0
- harborrag_runtime/topology/chunk_runner.py +41 -0
- harborrag_runtime/topology/composition.py +168 -0
- harborrag_runtime/topology/configuration.py +126 -0
- harborrag_runtime/topology/contextual.py +140 -0
- harborrag_runtime/topology/derived.py +238 -0
- harborrag_runtime/topology/derived_coverage.py +5 -0
- harborrag_runtime/topology/derived_dispatch.py +63 -0
- harborrag_runtime/topology/derived_factory.py +175 -0
- harborrag_runtime/topology/derived_models.py +251 -0
- harborrag_runtime/topology/derived_projection.py +127 -0
- harborrag_runtime/topology/description_factory.py +81 -0
- harborrag_runtime/topology/embedding_profile.py +85 -0
- harborrag_runtime/topology/entity_summary_index.py +304 -0
- harborrag_runtime/topology/maintenance.py +126 -0
- harborrag_runtime/topology/operations.py +277 -0
- harborrag_runtime/topology/parent_materializer.py +167 -0
- harborrag_runtime/topology/run_cost.py +43 -0
- harborrag_runtime/topology/security_operations.py +46 -0
- harborrag_runtime/topology/service.py +349 -0
- harborrag_runtime/topology/summary_factory.py +164 -0
- harborrag_runtime/topology/summary_inputs.py +129 -0
- harborrag_runtime/topology/summary_operations.py +128 -0
- harborrag_runtime/topology/summary_policy.py +39 -0
- harborrag_runtime/topology/summary_products.py +43 -0
- harborrag_runtime/topology/summary_service.py +625 -0
- harborrag_runtime/topology/summary_worker.py +129 -0
- harborrag_runtime/topology/summary_workflow.py +32 -0
- harborrag_runtime/topology/worker.py +101 -0
- harborrag_runtime/topology/workflow.py +32 -0
- harborrag_runtime-2.0.0.dist-info/METADATA +267 -0
- harborrag_runtime-2.0.0.dist-info/RECORD +271 -0
- harborrag_runtime-2.0.0.dist-info/WHEEL +4 -0
- harborrag_runtime-2.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
"""Authoritative source discovery planning."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import logging
|
|
7
|
+
import threading
|
|
8
|
+
from collections.abc import Iterator, Sequence
|
|
9
|
+
from dataclasses import replace
|
|
10
|
+
from time import perf_counter
|
|
11
|
+
|
|
12
|
+
from harborrag_adapters.connectors.base import BaseConnector
|
|
13
|
+
from harborrag_adapters.connectors.descriptors import (
|
|
14
|
+
ConnectorDocumentDescriptor,
|
|
15
|
+
)
|
|
16
|
+
from harborrag_adapters.connectors.harbor_connector import HarborConnector
|
|
17
|
+
from harborrag_adapters.repositories.database import (
|
|
18
|
+
IngestionControlPlaneDatabase,
|
|
19
|
+
)
|
|
20
|
+
from harborrag_core.domain.source import SourceRecord
|
|
21
|
+
from harborrag_engine.ingestion.source.models import (
|
|
22
|
+
PlannedDocumentRelease,
|
|
23
|
+
SourceDiscoveryPage,
|
|
24
|
+
SourceIngestionRequest,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
from .descriptor_mapping import PendingSourceRelease, SourceDescriptorMapper
|
|
28
|
+
from .skips import bind_connector_run, discovery_skip_summary
|
|
29
|
+
|
|
30
|
+
_LEGACY_ROOT_BATCH_SIZE = 50
|
|
31
|
+
_REGISTRATION_BATCH_SIZE = 200
|
|
32
|
+
|
|
33
|
+
logger = logging.getLogger("harborrag.runtime.ingestion.source_discovery")
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class SourceDiscoveryPlanner:
|
|
37
|
+
"""Persist one authoritative scan and prepare globally dispatchable jobs."""
|
|
38
|
+
|
|
39
|
+
def __init__(self, control: IngestionControlPlaneDatabase) -> None:
|
|
40
|
+
self._control = control
|
|
41
|
+
self._descriptors = SourceDescriptorMapper()
|
|
42
|
+
|
|
43
|
+
async def discover(
|
|
44
|
+
self,
|
|
45
|
+
request: SourceIngestionRequest,
|
|
46
|
+
connector: BaseConnector | HarborConnector,
|
|
47
|
+
*,
|
|
48
|
+
scan_id: str,
|
|
49
|
+
) -> tuple[PlannedDocumentRelease, ...]:
|
|
50
|
+
"""Discover the source's documents and plan a release for each."""
|
|
51
|
+
if bool(getattr(getattr(connector, "capabilities", None), "pagination", False)):
|
|
52
|
+
return await self._discover_native_pages(request, connector, scan_id=scan_id)
|
|
53
|
+
return await self._discover_legacy(request, connector, scan_id=scan_id)
|
|
54
|
+
|
|
55
|
+
async def discover_page(
|
|
56
|
+
self,
|
|
57
|
+
request: SourceIngestionRequest,
|
|
58
|
+
connector: BaseConnector | HarborConnector,
|
|
59
|
+
*,
|
|
60
|
+
scan_id: str,
|
|
61
|
+
cursor: str | None,
|
|
62
|
+
page_size: int | None = None,
|
|
63
|
+
) -> SourceDiscoveryPage:
|
|
64
|
+
"""Fetch, describe, and register one native provider page.
|
|
65
|
+
|
|
66
|
+
A paged connector replays its traversal from the start, so its skips after any page cover
|
|
67
|
+
every page so far.
|
|
68
|
+
"""
|
|
69
|
+
bind_connector_run(connector, request.task_id)
|
|
70
|
+
provider_started = perf_counter()
|
|
71
|
+
page = await asyncio.to_thread(
|
|
72
|
+
connector.discover_page,
|
|
73
|
+
request.query,
|
|
74
|
+
cursor=cursor,
|
|
75
|
+
page_size=page_size or request.discovery_page_size,
|
|
76
|
+
)
|
|
77
|
+
provider_seconds = perf_counter() - provider_started
|
|
78
|
+
descriptor_started = perf_counter()
|
|
79
|
+
descriptor_concurrency = self._descriptor_concurrency(
|
|
80
|
+
connector,
|
|
81
|
+
request.discovery_concurrency,
|
|
82
|
+
)
|
|
83
|
+
descriptors = await self._describe_roots(
|
|
84
|
+
connector,
|
|
85
|
+
page.records,
|
|
86
|
+
concurrency=descriptor_concurrency,
|
|
87
|
+
)
|
|
88
|
+
descriptor_seconds = perf_counter() - descriptor_started
|
|
89
|
+
pending = [
|
|
90
|
+
release
|
|
91
|
+
for descriptor in descriptors
|
|
92
|
+
for release in self._descriptors.releases(request, descriptor)
|
|
93
|
+
]
|
|
94
|
+
planned: list[PlannedDocumentRelease] = []
|
|
95
|
+
while pending:
|
|
96
|
+
batch = pending[:_REGISTRATION_BATCH_SIZE]
|
|
97
|
+
del pending[:_REGISTRATION_BATCH_SIZE]
|
|
98
|
+
planned.extend(await self._record_batch(scan_id, batch))
|
|
99
|
+
await self._record_skips(request, connector)
|
|
100
|
+
logger.info(
|
|
101
|
+
"Source discovery page task_id=%s roots=%d documents=%d has_next=%s "
|
|
102
|
+
"provider_ms=%.1f descriptor_ms=%.1f concurrency=%d",
|
|
103
|
+
request.task_id,
|
|
104
|
+
len(page.records),
|
|
105
|
+
len(planned),
|
|
106
|
+
page.next_cursor is not None,
|
|
107
|
+
provider_seconds * 1000,
|
|
108
|
+
descriptor_seconds * 1000,
|
|
109
|
+
descriptor_concurrency,
|
|
110
|
+
)
|
|
111
|
+
return SourceDiscoveryPage(
|
|
112
|
+
planned=tuple(planned),
|
|
113
|
+
next_cursor=page.next_cursor,
|
|
114
|
+
root_count=len(page.records),
|
|
115
|
+
provider_seconds=provider_seconds,
|
|
116
|
+
descriptor_seconds=descriptor_seconds,
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
async def _discover_native_pages(
|
|
120
|
+
self,
|
|
121
|
+
request: SourceIngestionRequest,
|
|
122
|
+
connector: BaseConnector | HarborConnector,
|
|
123
|
+
*,
|
|
124
|
+
scan_id: str,
|
|
125
|
+
) -> tuple[PlannedDocumentRelease, ...]:
|
|
126
|
+
planned: list[PlannedDocumentRelease] = []
|
|
127
|
+
cursor: str | None = None
|
|
128
|
+
root_count = 0
|
|
129
|
+
while True:
|
|
130
|
+
remaining = (
|
|
131
|
+
request.query.limit - root_count if request.query.limit is not None else None
|
|
132
|
+
)
|
|
133
|
+
if remaining is not None and remaining <= 0:
|
|
134
|
+
return tuple(planned)
|
|
135
|
+
page = await self.discover_page(
|
|
136
|
+
request,
|
|
137
|
+
connector,
|
|
138
|
+
scan_id=scan_id,
|
|
139
|
+
cursor=cursor,
|
|
140
|
+
page_size=(
|
|
141
|
+
min(request.discovery_page_size, remaining)
|
|
142
|
+
if remaining is not None
|
|
143
|
+
else request.discovery_page_size
|
|
144
|
+
),
|
|
145
|
+
)
|
|
146
|
+
planned.extend(page.planned)
|
|
147
|
+
root_count += page.root_count
|
|
148
|
+
await self._record_progress(request, root_count=root_count, planned=len(planned))
|
|
149
|
+
if page.next_cursor is None or (
|
|
150
|
+
request.query.limit is not None and root_count >= request.query.limit
|
|
151
|
+
):
|
|
152
|
+
return tuple(planned)
|
|
153
|
+
if page.next_cursor == cursor:
|
|
154
|
+
raise ValueError("connector returned a non-advancing discovery cursor")
|
|
155
|
+
cursor = page.next_cursor
|
|
156
|
+
|
|
157
|
+
async def _discover_legacy(
|
|
158
|
+
self,
|
|
159
|
+
request: SourceIngestionRequest,
|
|
160
|
+
connector: BaseConnector | HarborConnector,
|
|
161
|
+
*,
|
|
162
|
+
scan_id: str,
|
|
163
|
+
) -> tuple[PlannedDocumentRelease, ...]:
|
|
164
|
+
"""Walk the connector's roots in a worker thread, a bounded batch at a time.
|
|
165
|
+
|
|
166
|
+
Cancelling the walk cannot interrupt the thread, so the walk sets ``stop`` as it
|
|
167
|
+
exits and the thread stops reading the source after the root in hand.
|
|
168
|
+
"""
|
|
169
|
+
bind_connector_run(connector, request.task_id)
|
|
170
|
+
roots = iter(connector.discover(request.query))
|
|
171
|
+
stop = threading.Event()
|
|
172
|
+
planned: list[PlannedDocumentRelease] = []
|
|
173
|
+
pending: list[PendingSourceRelease] = []
|
|
174
|
+
root_count = 0
|
|
175
|
+
try:
|
|
176
|
+
while root_batch := await asyncio.to_thread(self._next_roots, roots, stop):
|
|
177
|
+
descriptors = await self._describe_roots(
|
|
178
|
+
connector,
|
|
179
|
+
root_batch,
|
|
180
|
+
concurrency=self._descriptor_concurrency(
|
|
181
|
+
connector,
|
|
182
|
+
request.discovery_concurrency,
|
|
183
|
+
),
|
|
184
|
+
)
|
|
185
|
+
for descriptor in descriptors:
|
|
186
|
+
pending.extend(self._descriptors.releases(request, descriptor))
|
|
187
|
+
root_count += 1
|
|
188
|
+
while len(pending) >= _REGISTRATION_BATCH_SIZE:
|
|
189
|
+
batch = pending[:_REGISTRATION_BATCH_SIZE]
|
|
190
|
+
del pending[:_REGISTRATION_BATCH_SIZE]
|
|
191
|
+
planned.extend(await self._record_batch(scan_id, batch))
|
|
192
|
+
if pending:
|
|
193
|
+
planned.extend(await self._record_batch(scan_id, pending))
|
|
194
|
+
pending.clear()
|
|
195
|
+
await self._record_progress(request, root_count=root_count, planned=len(planned))
|
|
196
|
+
finally:
|
|
197
|
+
stop.set()
|
|
198
|
+
await self._record_skips(request, connector)
|
|
199
|
+
return tuple(planned)
|
|
200
|
+
|
|
201
|
+
async def _record_skips(
|
|
202
|
+
self,
|
|
203
|
+
request: SourceIngestionRequest,
|
|
204
|
+
connector: BaseConnector | HarborConnector,
|
|
205
|
+
) -> None:
|
|
206
|
+
skips = discovery_skip_summary(connector)
|
|
207
|
+
if skips is not None:
|
|
208
|
+
await self._control.tasks.update_summary(request.task_id, skips)
|
|
209
|
+
|
|
210
|
+
@staticmethod
|
|
211
|
+
async def _describe_roots(
|
|
212
|
+
connector: BaseConnector | HarborConnector,
|
|
213
|
+
roots: Sequence[SourceRecord],
|
|
214
|
+
*,
|
|
215
|
+
concurrency: int,
|
|
216
|
+
) -> tuple[ConnectorDocumentDescriptor, ...]:
|
|
217
|
+
"""Describe roots concurrently while preserving provider result order."""
|
|
218
|
+
descriptors: list[ConnectorDocumentDescriptor] = []
|
|
219
|
+
for offset in range(0, len(roots), concurrency):
|
|
220
|
+
batch = roots[offset : offset + concurrency]
|
|
221
|
+
descriptors.extend(
|
|
222
|
+
await asyncio.gather(
|
|
223
|
+
*(asyncio.to_thread(connector.describe, root) for root in batch)
|
|
224
|
+
)
|
|
225
|
+
)
|
|
226
|
+
return tuple(descriptors)
|
|
227
|
+
|
|
228
|
+
async def _record_progress(
|
|
229
|
+
self,
|
|
230
|
+
request: SourceIngestionRequest,
|
|
231
|
+
*,
|
|
232
|
+
root_count: int,
|
|
233
|
+
planned: int,
|
|
234
|
+
) -> None:
|
|
235
|
+
await self._control.tasks.update_summary(
|
|
236
|
+
request.task_id,
|
|
237
|
+
{
|
|
238
|
+
"stage": "DISCOVERING",
|
|
239
|
+
"discovery_roots": root_count,
|
|
240
|
+
"discovered": planned,
|
|
241
|
+
"admitted": planned,
|
|
242
|
+
},
|
|
243
|
+
)
|
|
244
|
+
logger.info(
|
|
245
|
+
"Source discovery progress task_id=%s roots=%d documents=%d",
|
|
246
|
+
request.task_id,
|
|
247
|
+
root_count,
|
|
248
|
+
planned,
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
@staticmethod
|
|
252
|
+
def _descriptor_concurrency(
|
|
253
|
+
connector: BaseConnector | HarborConnector,
|
|
254
|
+
requested: int,
|
|
255
|
+
) -> int:
|
|
256
|
+
capabilities = getattr(connector, "capabilities", None)
|
|
257
|
+
return requested if bool(getattr(capabilities, "concurrent_describe", False)) else 1
|
|
258
|
+
|
|
259
|
+
async def _record_batch(
|
|
260
|
+
self,
|
|
261
|
+
scan_id: str,
|
|
262
|
+
pending: list[PendingSourceRelease],
|
|
263
|
+
) -> tuple[PlannedDocumentRelease, ...]:
|
|
264
|
+
registrations = await self._control.source_scans.record_seen_many(
|
|
265
|
+
scan_id=scan_id,
|
|
266
|
+
items=tuple(item.item for item in pending),
|
|
267
|
+
)
|
|
268
|
+
return tuple(
|
|
269
|
+
replace(
|
|
270
|
+
item.release,
|
|
271
|
+
request=replace(
|
|
272
|
+
item.release.request,
|
|
273
|
+
discovery_decision=registration.decision,
|
|
274
|
+
),
|
|
275
|
+
)
|
|
276
|
+
for item, registration in zip(pending, registrations, strict=True)
|
|
277
|
+
)
|
|
278
|
+
|
|
279
|
+
@staticmethod
|
|
280
|
+
def _next_roots(
|
|
281
|
+
roots: Iterator[SourceRecord], stop: threading.Event
|
|
282
|
+
) -> tuple[SourceRecord, ...]:
|
|
283
|
+
"""Read up to a batch of roots, checking ``stop`` before each one."""
|
|
284
|
+
batch: list[SourceRecord] = []
|
|
285
|
+
while len(batch) < _LEGACY_ROOT_BATCH_SIZE and not stop.is_set():
|
|
286
|
+
root = next(roots, None)
|
|
287
|
+
if root is None:
|
|
288
|
+
break
|
|
289
|
+
batch.append(root)
|
|
290
|
+
return tuple(batch)
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""Source-level document release and result recording."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
|
|
7
|
+
from harborrag_adapters.connectors.base import BaseConnector
|
|
8
|
+
from harborrag_adapters.connectors.harbor_connector import HarborConnector
|
|
9
|
+
from harborrag_adapters.connectors.registry import connector_registry
|
|
10
|
+
from harborrag_adapters.repositories.database import IngestionControlPlaneDatabase
|
|
11
|
+
from harborrag_core.ingestion import (
|
|
12
|
+
BindingKind,
|
|
13
|
+
DocumentIngestionOutcome,
|
|
14
|
+
SourceAdmissionDecision,
|
|
15
|
+
TaskDocumentResult,
|
|
16
|
+
is_retryable_failure_code,
|
|
17
|
+
)
|
|
18
|
+
from harborrag_core.schemas.ids import DocumentId, DocumentVersionId
|
|
19
|
+
from harborrag_engine.ingestion.release.models import DocumentReleaseOutcome
|
|
20
|
+
from harborrag_engine.ingestion.release.service import DocumentReleaseService
|
|
21
|
+
from harborrag_engine.ingestion.source.models import PlannedDocumentRelease
|
|
22
|
+
|
|
23
|
+
from ..failure_classification import ingestion_failure_classifier
|
|
24
|
+
|
|
25
|
+
logger = logging.getLogger("harborrag.runtime.ingestion.source_documents")
|
|
26
|
+
|
|
27
|
+
_NO_INDEXABLE_CONTENT = "no_indexable_content"
|
|
28
|
+
_SKIPPED_STATUS = "skipped"
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class SourceDocumentService:
|
|
32
|
+
"""Release one document and persist its bounded public task outcome."""
|
|
33
|
+
|
|
34
|
+
def __init__(
|
|
35
|
+
self,
|
|
36
|
+
*,
|
|
37
|
+
control: IngestionControlPlaneDatabase,
|
|
38
|
+
documents: DocumentReleaseService,
|
|
39
|
+
) -> None:
|
|
40
|
+
self._control = control
|
|
41
|
+
self._documents = documents
|
|
42
|
+
|
|
43
|
+
async def release_one(
|
|
44
|
+
self,
|
|
45
|
+
task_id: str,
|
|
46
|
+
planned: PlannedDocumentRelease,
|
|
47
|
+
connector: BaseConnector | HarborConnector,
|
|
48
|
+
) -> DocumentIngestionOutcome:
|
|
49
|
+
"""Release one planned document and record its outcome."""
|
|
50
|
+
try:
|
|
51
|
+
return await self.publish_one(task_id, planned, connector)
|
|
52
|
+
except Exception as error:
|
|
53
|
+
stage = getattr(error, "harborrag_stage", None)
|
|
54
|
+
if not isinstance(stage, str) or not stage:
|
|
55
|
+
stage = "FetchAndCaptureRaw"
|
|
56
|
+
code = ingestion_failure_classifier().classify(stage, error).code
|
|
57
|
+
logger.warning(
|
|
58
|
+
"Document release failed task_id=%s document=%s stage=%s code=%s",
|
|
59
|
+
task_id,
|
|
60
|
+
planned.document_id,
|
|
61
|
+
stage,
|
|
62
|
+
code,
|
|
63
|
+
exc_info=error if logger.isEnabledFor(logging.DEBUG) else None,
|
|
64
|
+
)
|
|
65
|
+
await self.record_failed_document(
|
|
66
|
+
task_id,
|
|
67
|
+
planned,
|
|
68
|
+
error_type=code,
|
|
69
|
+
failed_stage=stage,
|
|
70
|
+
skip_reason=getattr(error, "skip_reason", None),
|
|
71
|
+
)
|
|
72
|
+
return DocumentIngestionOutcome.FAILED
|
|
73
|
+
|
|
74
|
+
async def publish_one(
|
|
75
|
+
self,
|
|
76
|
+
task_id: str,
|
|
77
|
+
planned: PlannedDocumentRelease,
|
|
78
|
+
connector: BaseConnector | HarborConnector,
|
|
79
|
+
) -> DocumentIngestionOutcome:
|
|
80
|
+
"""Publish one planned document and record its outcome."""
|
|
81
|
+
outcome = await self._documents.release(planned.request, connector)
|
|
82
|
+
return await self.record_published_document(task_id, planned, outcome)
|
|
83
|
+
|
|
84
|
+
async def record_published_document(
|
|
85
|
+
self,
|
|
86
|
+
task_id: str,
|
|
87
|
+
planned: PlannedDocumentRelease,
|
|
88
|
+
outcome: DocumentReleaseOutcome,
|
|
89
|
+
) -> DocumentIngestionOutcome:
|
|
90
|
+
"""Record the outcome of a document that was released.
|
|
91
|
+
|
|
92
|
+
A document whose active version was retired because it no longer yields
|
|
93
|
+
evidence is stored as skipped, with a skip reason. It still returns
|
|
94
|
+
UNCHANGED, which is how the run's dispatch summary counts it.
|
|
95
|
+
"""
|
|
96
|
+
status = (
|
|
97
|
+
DocumentIngestionOutcome.PUBLISHED
|
|
98
|
+
if outcome.published
|
|
99
|
+
else DocumentIngestionOutcome.UNCHANGED
|
|
100
|
+
)
|
|
101
|
+
retired = outcome.decision == SourceAdmissionDecision.REMOVED_CANDIDATE
|
|
102
|
+
stored_status = _SKIPPED_STATUS if retired else status.value
|
|
103
|
+
await self._control.tasks.record_document_result(
|
|
104
|
+
TaskDocumentResult(
|
|
105
|
+
task_id=task_id,
|
|
106
|
+
document_id=DocumentId(planned.document_id),
|
|
107
|
+
document_version_id=(
|
|
108
|
+
DocumentVersionId(outcome.document_version_id)
|
|
109
|
+
if outcome.document_version_id is not None
|
|
110
|
+
else None
|
|
111
|
+
),
|
|
112
|
+
status=stored_status,
|
|
113
|
+
result={
|
|
114
|
+
**self._document_summary(planned),
|
|
115
|
+
"decision": outcome.decision.value,
|
|
116
|
+
"evidence_chunks": outcome.evidence_chunks,
|
|
117
|
+
"graph_nodes": outcome.graph_nodes,
|
|
118
|
+
"graph_relations": outcome.graph_relations,
|
|
119
|
+
**({"skip_reason": _NO_INDEXABLE_CONTENT} if retired else {}),
|
|
120
|
+
},
|
|
121
|
+
)
|
|
122
|
+
)
|
|
123
|
+
logger.info(
|
|
124
|
+
"Document ingestion completed task_id=%s document_id=%s status=%s decision=%s",
|
|
125
|
+
task_id,
|
|
126
|
+
planned.document_id,
|
|
127
|
+
stored_status,
|
|
128
|
+
outcome.decision.value,
|
|
129
|
+
)
|
|
130
|
+
return status
|
|
131
|
+
|
|
132
|
+
async def record_failed_document(
|
|
133
|
+
self,
|
|
134
|
+
task_id: str,
|
|
135
|
+
planned: PlannedDocumentRelease,
|
|
136
|
+
*,
|
|
137
|
+
error_type: str,
|
|
138
|
+
failed_stage: str = "FetchAndCaptureRaw",
|
|
139
|
+
skip_reason: str | None = None,
|
|
140
|
+
) -> None:
|
|
141
|
+
"""Record a document whose release failed."""
|
|
142
|
+
normalized = error_type.strip().lower()
|
|
143
|
+
if not normalized:
|
|
144
|
+
raise ValueError("document failure error type must be non-empty")
|
|
145
|
+
safe_error_code = f"document_release_{normalized}"
|
|
146
|
+
await self._control.tasks.record_document_result(
|
|
147
|
+
TaskDocumentResult(
|
|
148
|
+
task_id=task_id,
|
|
149
|
+
document_id=DocumentId(planned.document_id),
|
|
150
|
+
status="failed",
|
|
151
|
+
result={
|
|
152
|
+
**self._document_summary(planned),
|
|
153
|
+
"safe_error_code": safe_error_code,
|
|
154
|
+
"failure_stage": failed_stage,
|
|
155
|
+
"retryable": is_retryable_failure_code(safe_error_code),
|
|
156
|
+
**({"skip_reason": skip_reason} if skip_reason else {}),
|
|
157
|
+
},
|
|
158
|
+
)
|
|
159
|
+
)
|
|
160
|
+
logger.warning(
|
|
161
|
+
"Document ingestion failed task_id=%s document_id=%s stage=%s error_code=%s",
|
|
162
|
+
task_id,
|
|
163
|
+
planned.document_id,
|
|
164
|
+
failed_stage,
|
|
165
|
+
safe_error_code,
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
@staticmethod
|
|
169
|
+
def _document_summary(planned: PlannedDocumentRelease) -> dict[str, object]:
|
|
170
|
+
identity = planned.request.source_identity
|
|
171
|
+
source = planned.request.source
|
|
172
|
+
if identity.binding.kind == BindingKind.ATTACHMENT:
|
|
173
|
+
document_kind = "attachment"
|
|
174
|
+
else:
|
|
175
|
+
document_kind = connector_registry.get_definition(
|
|
176
|
+
identity.connector_type.value
|
|
177
|
+
).document_kind
|
|
178
|
+
title_value = source.metadata.get("title") or source.metadata.get("filename")
|
|
179
|
+
title = str(title_value).strip() if title_value is not None else None
|
|
180
|
+
return {
|
|
181
|
+
"source_item_id": identity.source_item_id,
|
|
182
|
+
"document_kind": document_kind,
|
|
183
|
+
"title": title,
|
|
184
|
+
}
|