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,550 @@
|
|
|
1
|
+
"""Application use cases backed by provider-neutral runtime gateways."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import logging
|
|
7
|
+
import uuid
|
|
8
|
+
from collections.abc import AsyncGenerator
|
|
9
|
+
|
|
10
|
+
from harborrag_adapters.connectors.policies.network import DestinationPolicy
|
|
11
|
+
from harborrag_core.contracts.errors import HarborUnavailableError
|
|
12
|
+
from harborrag_core.contracts.events import HarborEvent, event_prefix
|
|
13
|
+
from harborrag_core.ports.events import EventBusPort
|
|
14
|
+
from harborrag_engine.chat.scope import ControlPlaneConnectionCatalog
|
|
15
|
+
from harborrag_engine.control_plane.connection_views import IngestionLinks
|
|
16
|
+
from harborrag_engine.ingestion.failure_inbox import FailureInboxService
|
|
17
|
+
from harborrag_engine.ingestion.operations import IngestionOperations
|
|
18
|
+
from harborrag_engine.ingestion.presenters import STATUS_NAMES, TERMINAL_STATES
|
|
19
|
+
from harborrag_runtime.composition import CompositionRoot, ControlPlaneRepositories
|
|
20
|
+
from harborrag_runtime.config.connectors.importing import import_connectors_once
|
|
21
|
+
from harborrag_runtime.config.settings import RuntimeSettings
|
|
22
|
+
from harborrag_runtime.config_store.defaults import registry_for
|
|
23
|
+
from harborrag_runtime.memory.administration import MemoryAdministrationService
|
|
24
|
+
from harborrag_runtime.memory.directory import ConversationDirectoryService
|
|
25
|
+
from harborrag_runtime.memory.locks import SessionLocks
|
|
26
|
+
from harborrag_runtime.memory.projects import project_lookup
|
|
27
|
+
|
|
28
|
+
from ..chat import ChatApplicationService, ChatClientMixin
|
|
29
|
+
from ..control_plane.connections import ConnectionService
|
|
30
|
+
from ..control_plane.effect_recovery import (
|
|
31
|
+
EFFECT_RECOVERY_LEASE_NAME,
|
|
32
|
+
EFFECT_RECOVERY_LEASE_TTL_SECONDS,
|
|
33
|
+
recover_pending_control_plane_effects,
|
|
34
|
+
)
|
|
35
|
+
from ..control_plane.projects import ProjectService
|
|
36
|
+
from ..control_plane.settings_import import SeedOutcome, SettingsImporter, seed_settings_once
|
|
37
|
+
from ..control_plane.settings_service import SettingsService
|
|
38
|
+
from ..control_plane.tenants import tenant_has_data, tenants_with_data
|
|
39
|
+
from ..errors import failure_response
|
|
40
|
+
from ..ingestion.client import PublicIngestionClientMixin
|
|
41
|
+
from ..ingestion.connection_sources import ConnectionSourceInputBuilder
|
|
42
|
+
from ..ingestion.direct import DirectIngestionClientMixin, DirectIngestionOperations
|
|
43
|
+
from ..ingestion.follower import IngestionFollower
|
|
44
|
+
from ..ingestion.ports import PublicTaskStore, SourceInputBuilder
|
|
45
|
+
from ..ingestion.progress_bridge import (
|
|
46
|
+
LEASE_NAME,
|
|
47
|
+
LEASE_TTL_SECONDS,
|
|
48
|
+
RUN_COMPLETED,
|
|
49
|
+
STREAMED_EVENTS,
|
|
50
|
+
sync_ingestion_progress,
|
|
51
|
+
sync_run,
|
|
52
|
+
)
|
|
53
|
+
from ..ingestion.service import IngestionApplicationService
|
|
54
|
+
from ..ingestion.starter import IngestionStarter
|
|
55
|
+
from ..memory import (
|
|
56
|
+
ConversationSessionService,
|
|
57
|
+
MemoryAdminClientMixin,
|
|
58
|
+
conversation_memory,
|
|
59
|
+
long_term_memories,
|
|
60
|
+
long_term_memory_index,
|
|
61
|
+
model_usage_records,
|
|
62
|
+
)
|
|
63
|
+
from ..ports import BaseAppService
|
|
64
|
+
from ..scheduling.service import ScheduleApplicationService
|
|
65
|
+
from ..schemas import AppResponse
|
|
66
|
+
from .factories import AppServiceFactories
|
|
67
|
+
from .resources import AppResources
|
|
68
|
+
|
|
69
|
+
REMOVAL_RECOVERY_LEASE_NAME = "connection_removal_recovery"
|
|
70
|
+
|
|
71
|
+
logger = logging.getLogger("harborrag.runtime.application.composition.service")
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class AppService(
|
|
75
|
+
ChatClientMixin,
|
|
76
|
+
MemoryAdminClientMixin,
|
|
77
|
+
PublicIngestionClientMixin,
|
|
78
|
+
DirectIngestionClientMixin,
|
|
79
|
+
BaseAppService,
|
|
80
|
+
):
|
|
81
|
+
"""Application service that keeps transport concerns outside the ingestion execution path.
|
|
82
|
+
|
|
83
|
+
Each process holds a random instance ID, stable for its lifetime, that identifies it as a
|
|
84
|
+
lease holder to the ingestion progress bridge and the recovery loops.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
composition: Runtime composition root that owns the repositories and gateways.
|
|
88
|
+
settings: Runtime settings; read from the environment when omitted.
|
|
89
|
+
factories: Overrides for the ingestion description, source preparation and
|
|
90
|
+
gateway factories; production defaults when omitted.
|
|
91
|
+
"""
|
|
92
|
+
|
|
93
|
+
def __init__(
|
|
94
|
+
self,
|
|
95
|
+
composition: CompositionRoot,
|
|
96
|
+
settings: RuntimeSettings | None = None,
|
|
97
|
+
*,
|
|
98
|
+
factories: AppServiceFactories | None = None,
|
|
99
|
+
) -> None:
|
|
100
|
+
self._composition = composition
|
|
101
|
+
self._settings = settings or RuntimeSettings()
|
|
102
|
+
self._instance_id = uuid.uuid4().hex
|
|
103
|
+
selected = factories or AppServiceFactories()
|
|
104
|
+
self._runtime_config = selected.ingestion_description(self._settings)
|
|
105
|
+
self._source_input_builder: SourceInputBuilder = (
|
|
106
|
+
selected.source_input_builder
|
|
107
|
+
or ConnectionSourceInputBuilder(self._control_plane, selected.source_preparation)
|
|
108
|
+
)
|
|
109
|
+
self._resources = AppResources(
|
|
110
|
+
self._settings,
|
|
111
|
+
factories=selected,
|
|
112
|
+
composition=composition,
|
|
113
|
+
)
|
|
114
|
+
self._public_ingestions = IngestionApplicationService(
|
|
115
|
+
self._settings,
|
|
116
|
+
client_provider=self._resources.runtime_client,
|
|
117
|
+
task_store_provider=self._resources.public_task_store,
|
|
118
|
+
source_input_builder=self._source_input_builder,
|
|
119
|
+
)
|
|
120
|
+
self._schedules = ScheduleApplicationService(
|
|
121
|
+
self._settings,
|
|
122
|
+
backend_provider=self._resources.schedule_backend,
|
|
123
|
+
source_input_builder=self._source_input_builder,
|
|
124
|
+
)
|
|
125
|
+
memory = conversation_memory(self._composition)
|
|
126
|
+
memories = long_term_memories(self._composition)
|
|
127
|
+
index = long_term_memory_index(self._composition)
|
|
128
|
+
projects = project_lookup(self._composition)
|
|
129
|
+
usage = model_usage_records(self._composition)
|
|
130
|
+
self._sessions = ConversationSessionService(memory)
|
|
131
|
+
locks = SessionLocks(memory)
|
|
132
|
+
self._memory_admin = MemoryAdministrationService(
|
|
133
|
+
conversations=memory,
|
|
134
|
+
memories=memories,
|
|
135
|
+
index=index,
|
|
136
|
+
pending_effects=getattr(
|
|
137
|
+
getattr(composition, "control_plane", None), "pending_effects", None
|
|
138
|
+
),
|
|
139
|
+
)
|
|
140
|
+
self._conversation_directory = ConversationDirectoryService(memory, self._memory_admin)
|
|
141
|
+
self._chat = ChatApplicationService(
|
|
142
|
+
self._resources.runtime_sdk,
|
|
143
|
+
self._settings,
|
|
144
|
+
memory=memory,
|
|
145
|
+
locks=locks,
|
|
146
|
+
projects=projects,
|
|
147
|
+
usage=usage,
|
|
148
|
+
config=getattr(getattr(composition, "control_plane", None), "config", None),
|
|
149
|
+
catalog=ControlPlaneConnectionCatalog(
|
|
150
|
+
lambda: self._control_plane().connections, self._resources.task_registry
|
|
151
|
+
),
|
|
152
|
+
)
|
|
153
|
+
self._direct = DirectIngestionOperations(self._resources.runtime_sdk)
|
|
154
|
+
self._connection_service = ConnectionService(
|
|
155
|
+
control_plane=self._control_plane,
|
|
156
|
+
runs=self._resources.task_registry,
|
|
157
|
+
ingestions=self._public_ingestions,
|
|
158
|
+
removal=self._resources.connection_removal,
|
|
159
|
+
destination_policy=DestinationPolicy.from_environ(),
|
|
160
|
+
instance_id=self._instance_id,
|
|
161
|
+
settings=self._settings,
|
|
162
|
+
)
|
|
163
|
+
self._ingestion_operations = IngestionOperations(
|
|
164
|
+
connections=lambda: self._control_plane().connections,
|
|
165
|
+
runs=self._resources.task_registry,
|
|
166
|
+
actions=self._public_ingestions,
|
|
167
|
+
links=IngestionLinks(
|
|
168
|
+
temporal_ui_url=self._settings.temporal_ui_url,
|
|
169
|
+
temporal_namespace=self._settings.temporal_namespace,
|
|
170
|
+
grafana_url=self._settings.grafana_url,
|
|
171
|
+
),
|
|
172
|
+
)
|
|
173
|
+
self._failure_inbox = FailureInboxService(
|
|
174
|
+
connections=lambda: self._control_plane().connections,
|
|
175
|
+
runs=self._resources.task_registry,
|
|
176
|
+
actions=self._public_ingestions,
|
|
177
|
+
)
|
|
178
|
+
self._project_service = ProjectService(
|
|
179
|
+
control_plane=self._control_plane,
|
|
180
|
+
runs=self._resources.task_registry,
|
|
181
|
+
connections=self._connection_service,
|
|
182
|
+
)
|
|
183
|
+
self._ingestion_starter = IngestionStarter(
|
|
184
|
+
projects=self._project_service,
|
|
185
|
+
connections=self._connection_service,
|
|
186
|
+
runs=self._ingestion_operations,
|
|
187
|
+
)
|
|
188
|
+
self._ingestion_follower = IngestionFollower(
|
|
189
|
+
runs=self._ingestion_operations, events=self.stream_ingestion_events
|
|
190
|
+
)
|
|
191
|
+
settings_registry = registry_for(self._settings)
|
|
192
|
+
self._settings_service = SettingsService(
|
|
193
|
+
control_plane=self._control_plane, registry=settings_registry
|
|
194
|
+
)
|
|
195
|
+
self._settings_importer = SettingsImporter(
|
|
196
|
+
control_plane=self._control_plane, registry=settings_registry
|
|
197
|
+
)
|
|
198
|
+
|
|
199
|
+
@property
|
|
200
|
+
def default_tenant_id(self) -> str:
|
|
201
|
+
"""Tenant ``HARBORRAG_TENANT_ID`` names, for callers that send no tenant."""
|
|
202
|
+
return self._settings.tenant_id
|
|
203
|
+
|
|
204
|
+
@property
|
|
205
|
+
def ingestion_operations(self) -> IngestionOperations:
|
|
206
|
+
"""Run reads behind the ``/v1/admin/ingestions`` routes."""
|
|
207
|
+
return self._ingestion_operations
|
|
208
|
+
|
|
209
|
+
@property
|
|
210
|
+
def ingestion_starter(self) -> IngestionStarter:
|
|
211
|
+
"""Starts the ingestions behind ``POST /v1/admin/ingestions``."""
|
|
212
|
+
return self._ingestion_starter
|
|
213
|
+
|
|
214
|
+
@property
|
|
215
|
+
def ingestion_follower(self) -> IngestionFollower:
|
|
216
|
+
"""Follows the run events behind ``/v1/admin/ingestions/{ingestion_id}/events``."""
|
|
217
|
+
return self._ingestion_follower
|
|
218
|
+
|
|
219
|
+
@property
|
|
220
|
+
def failure_inbox(self) -> FailureInboxService:
|
|
221
|
+
"""Failure inbox behind the ``/v1/admin/failures`` routes."""
|
|
222
|
+
return self._failure_inbox
|
|
223
|
+
|
|
224
|
+
@property
|
|
225
|
+
def connection_service(self) -> ConnectionService:
|
|
226
|
+
"""Connection use cases behind the ``/v1/admin`` connection routes."""
|
|
227
|
+
return self._connection_service
|
|
228
|
+
|
|
229
|
+
@property
|
|
230
|
+
def settings_service(self) -> SettingsService:
|
|
231
|
+
"""Runtime settings use cases behind the ``/v1/admin/settings`` routes."""
|
|
232
|
+
return self._settings_service
|
|
233
|
+
|
|
234
|
+
@property
|
|
235
|
+
def settings_importer(self) -> SettingsImporter:
|
|
236
|
+
"""Settings file import, export and seeding."""
|
|
237
|
+
return self._settings_importer
|
|
238
|
+
|
|
239
|
+
@property
|
|
240
|
+
def project_service(self) -> ProjectService:
|
|
241
|
+
"""Project use cases behind the ``/v1/admin/projects`` routes."""
|
|
242
|
+
return self._project_service
|
|
243
|
+
|
|
244
|
+
async def import_connectors_once(self) -> int:
|
|
245
|
+
"""Import ``connectors.yaml`` on the first start of a fresh install."""
|
|
246
|
+
control_plane = self._control_plane()
|
|
247
|
+
plan = await import_connectors_once(
|
|
248
|
+
self._settings.connector_config_path,
|
|
249
|
+
tenant_id=self._settings.tenant_id,
|
|
250
|
+
projects=control_plane.projects,
|
|
251
|
+
connections=control_plane.connections,
|
|
252
|
+
settings=control_plane.settings,
|
|
253
|
+
)
|
|
254
|
+
return len(plan.writes) if plan is not None else 0
|
|
255
|
+
|
|
256
|
+
async def seed_settings_once(self) -> SeedOutcome:
|
|
257
|
+
"""Seed ``config/settings.yaml`` into an empty settings log, else report drift."""
|
|
258
|
+
return await seed_settings_once(
|
|
259
|
+
self._settings_importer,
|
|
260
|
+
self._settings.settings_seed_path,
|
|
261
|
+
tenant_id=self._settings.tenant_id,
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
async def resume_connection_removals(self) -> int:
|
|
265
|
+
"""Finish removals a previous process left running; one API process at a time."""
|
|
266
|
+
acquired = await self._control_plane().leases.try_acquire(
|
|
267
|
+
REMOVAL_RECOVERY_LEASE_NAME,
|
|
268
|
+
self._instance_id,
|
|
269
|
+
ttl_seconds=EFFECT_RECOVERY_LEASE_TTL_SECONDS,
|
|
270
|
+
)
|
|
271
|
+
if not acquired:
|
|
272
|
+
return 0
|
|
273
|
+
return await self._connection_service.resume_deletions() + (
|
|
274
|
+
await self._project_service.resume_deletions()
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
def _control_plane(self) -> ControlPlaneRepositories:
|
|
278
|
+
"""Return the control-plane repositories, or fail when no database is configured."""
|
|
279
|
+
control_plane = self._composition.control_plane
|
|
280
|
+
if control_plane is None:
|
|
281
|
+
raise HarborUnavailableError("control-plane database is not configured")
|
|
282
|
+
return control_plane
|
|
283
|
+
|
|
284
|
+
def health(self) -> AppResponse:
|
|
285
|
+
"""Report whether the composition's runtime diagnostics say it is ready."""
|
|
286
|
+
diagnostics = self._composition.diagnostics()
|
|
287
|
+
runtime = diagnostics.get("runtime")
|
|
288
|
+
ready = bool(runtime.get("ready")) if isinstance(runtime, dict) else False
|
|
289
|
+
return AppResponse(
|
|
290
|
+
ok=ready,
|
|
291
|
+
data={"diagnostics": diagnostics},
|
|
292
|
+
error=None if ready else "runtime not ready",
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
async def runtime_health(self) -> AppResponse:
|
|
296
|
+
"""Probe the ingestion runtime within its health timeout; failures become an envelope."""
|
|
297
|
+
try:
|
|
298
|
+
async with asyncio.timeout(self._runtime_config.health_timeout_seconds):
|
|
299
|
+
client = await self._resources.runtime_client()
|
|
300
|
+
ready = await client.health()
|
|
301
|
+
return AppResponse(
|
|
302
|
+
ready,
|
|
303
|
+
{
|
|
304
|
+
"runtime": {
|
|
305
|
+
"provider": self._runtime_config.provider,
|
|
306
|
+
"ready": ready,
|
|
307
|
+
**self._runtime_config.details,
|
|
308
|
+
}
|
|
309
|
+
},
|
|
310
|
+
None if ready else "Ingestion workflow service is not ready",
|
|
311
|
+
)
|
|
312
|
+
except Exception as exc: # noqa: BLE001 - service returns a stable error envelope
|
|
313
|
+
return failure_response(logger, exc, "check ingestion runtime health")
|
|
314
|
+
|
|
315
|
+
async def tenants_with_data(self) -> list[str]:
|
|
316
|
+
"""List the tenants holding a project or a connection."""
|
|
317
|
+
return await tenants_with_data(self._control_plane())
|
|
318
|
+
|
|
319
|
+
async def tenant_has_data(self, tenant_id: str) -> bool:
|
|
320
|
+
"""Whether the tenant holds a project, a connection or a saved settings revision."""
|
|
321
|
+
return await tenant_has_data(self._control_plane(), tenant_id)
|
|
322
|
+
|
|
323
|
+
async def sync_declared_schedules(self) -> dict[str, int] | None:
|
|
324
|
+
"""Reconcile the declared schedules into the schedule engine; ``None`` when it has none."""
|
|
325
|
+
return await self._schedules.sync_declared()
|
|
326
|
+
|
|
327
|
+
async def recover_pending_control_plane_effects(self, *, limit: int = 100) -> int:
|
|
328
|
+
"""Drain one pass of the durable secret-retirement/audit-logging outbox.
|
|
329
|
+
|
|
330
|
+
Gated by the same DB-backed lease pattern as sync_ingestion_progress:
|
|
331
|
+
when the API runs as more than one process, only the lease holder
|
|
332
|
+
drains the queue this pass -- every other instance's pass is a
|
|
333
|
+
no-op, so two processes never replay the same pending effect at
|
|
334
|
+
once (which could double-log the same activity entry).
|
|
335
|
+
"""
|
|
336
|
+
acquired = await self._control_plane().leases.try_acquire(
|
|
337
|
+
EFFECT_RECOVERY_LEASE_NAME,
|
|
338
|
+
self._instance_id,
|
|
339
|
+
ttl_seconds=EFFECT_RECOVERY_LEASE_TTL_SECONDS,
|
|
340
|
+
)
|
|
341
|
+
if not acquired:
|
|
342
|
+
return 0
|
|
343
|
+
return await recover_pending_control_plane_effects(
|
|
344
|
+
self._control_plane(),
|
|
345
|
+
limit=limit,
|
|
346
|
+
erasure_handler=self._memory_admin.recover_erasure,
|
|
347
|
+
connection_busy=self._connection_service.connection_busy,
|
|
348
|
+
)
|
|
349
|
+
|
|
350
|
+
async def sync_ingestion_progress(self) -> int:
|
|
351
|
+
"""One poll tick fanning active tasks' progress out via the event bus.
|
|
352
|
+
|
|
353
|
+
Gated by a DB-backed lease (single-API-process deployment constraint,
|
|
354
|
+
see deploy/README.md): when the API runs as more than one process,
|
|
355
|
+
only the instance currently holding the lease ticks -- every other
|
|
356
|
+
instance's tick is a no-op, so their progress bridges never race each
|
|
357
|
+
other into double-appending the same task's event log.
|
|
358
|
+
|
|
359
|
+
This upfront acquire only proves ownership at this instant, not for
|
|
360
|
+
the tick's whole duration -- list_active() is paginated, so with
|
|
361
|
+
enough active tasks a tick can run long enough to outlive the
|
|
362
|
+
lease's ttl. ``still_leader`` re-runs this same try_acquire (renewing
|
|
363
|
+
the ttl) before every page sync_ingestion_progress reads, so a tick
|
|
364
|
+
that overruns loses the lease and stops instead of continuing to
|
|
365
|
+
write alongside whichever process took over next.
|
|
366
|
+
"""
|
|
367
|
+
acquired = await self._control_plane().leases.try_acquire(
|
|
368
|
+
LEASE_NAME, self._instance_id, ttl_seconds=LEASE_TTL_SECONDS
|
|
369
|
+
)
|
|
370
|
+
if not acquired:
|
|
371
|
+
return 0
|
|
372
|
+
store = await self._resources.public_task_store()
|
|
373
|
+
|
|
374
|
+
async def still_leader() -> bool:
|
|
375
|
+
return await self._control_plane().leases.try_acquire(
|
|
376
|
+
LEASE_NAME, self._instance_id, ttl_seconds=LEASE_TTL_SECONDS
|
|
377
|
+
)
|
|
378
|
+
|
|
379
|
+
return await sync_ingestion_progress(
|
|
380
|
+
store,
|
|
381
|
+
self._resources.event_bus(),
|
|
382
|
+
still_leader=still_leader,
|
|
383
|
+
)
|
|
384
|
+
|
|
385
|
+
async def stream_ingestion_events(
|
|
386
|
+
self, task_id: str, *, after_seq: int | None = None
|
|
387
|
+
) -> AsyncGenerator[HarborEvent, None]:
|
|
388
|
+
"""Backlog replay then a live tail of a task's events, ending after ``run.completed``.
|
|
389
|
+
|
|
390
|
+
``after_seq`` resumes a reconnecting client (Last-Event-ID) after its last-seen
|
|
391
|
+
sequence. The backlog is read in pages until a short page arrives. The stream
|
|
392
|
+
subscribes before reading it so nothing published in between is dropped; an event
|
|
393
|
+
delivered by both is yielded once, so sequences only increase.
|
|
394
|
+
|
|
395
|
+
The stream finishes a run itself: after the backlog and on every idle check it
|
|
396
|
+
re-reads the run, and if the run ended without a stored ``run.completed`` it appends
|
|
397
|
+
one (at most once across streams and replicas) and replays it. A watcher therefore
|
|
398
|
+
never depends on the progress bridge having seen the run end.
|
|
399
|
+
|
|
400
|
+
``live`` is closed in a ``finally`` so an abandoned caller deterministically
|
|
401
|
+
unsubscribes instead of waiting for the event bus's GC finalizer.
|
|
402
|
+
"""
|
|
403
|
+
task = await self._public_ingestions.get_task(task_id)
|
|
404
|
+
store = await self._resources.public_task_store()
|
|
405
|
+
bus = self._resources.event_bus()
|
|
406
|
+
terminal_names = {STATUS_NAMES[state] for state in TERMINAL_STATES}
|
|
407
|
+
live = None if task["status"] in terminal_names else bus.subscribe(event_prefix(task_id))
|
|
408
|
+
cursor = _StreamCursor(task_id, after_seq)
|
|
409
|
+
tail = None if live is None else _LiveTail(live)
|
|
410
|
+
check_ended = True
|
|
411
|
+
try:
|
|
412
|
+
async for event in _backlog(store, cursor):
|
|
413
|
+
yield event
|
|
414
|
+
while not cursor.done:
|
|
415
|
+
if check_ended and await _complete_if_ended(store, bus, task_id):
|
|
416
|
+
async for event in _backlog(store, cursor):
|
|
417
|
+
yield event
|
|
418
|
+
return
|
|
419
|
+
if tail is None:
|
|
420
|
+
return
|
|
421
|
+
live_event = await tail.next(_END_CHECK_SECONDS)
|
|
422
|
+
check_ended = live_event is None
|
|
423
|
+
if live_event is not None:
|
|
424
|
+
async for event in _deliver(store, cursor, live_event):
|
|
425
|
+
yield event
|
|
426
|
+
finally:
|
|
427
|
+
if tail is not None:
|
|
428
|
+
await tail.close()
|
|
429
|
+
|
|
430
|
+
async def start_config_provider(self) -> None:
|
|
431
|
+
"""Start the composition's configuration provider."""
|
|
432
|
+
await self._composition.start()
|
|
433
|
+
|
|
434
|
+
async def aclose(self) -> None:
|
|
435
|
+
"""Close the connection service, resources and composition in turn."""
|
|
436
|
+
try:
|
|
437
|
+
await self._connection_service.aclose()
|
|
438
|
+
finally:
|
|
439
|
+
try:
|
|
440
|
+
await self._resources.aclose()
|
|
441
|
+
finally:
|
|
442
|
+
await self._composition.aclose()
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
_END_CHECK_SECONDS = 2.0
|
|
446
|
+
_REPLAY_PAGE_LIMIT = 500
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
class _LiveTail:
|
|
450
|
+
"""A live subscription read with a timeout that never cancels the subscription itself."""
|
|
451
|
+
|
|
452
|
+
def __init__(self, live: AsyncGenerator[HarborEvent, None]) -> None:
|
|
453
|
+
self._live = live
|
|
454
|
+
self._pending: asyncio.Future[HarborEvent] | None = None
|
|
455
|
+
|
|
456
|
+
async def next(self, timeout_seconds: float) -> HarborEvent | None:
|
|
457
|
+
"""Return the next live event, or ``None`` when none arrived within the timeout."""
|
|
458
|
+
if self._pending is None:
|
|
459
|
+
self._pending = asyncio.ensure_future(anext(self._live))
|
|
460
|
+
done, _ = await asyncio.wait({self._pending}, timeout=timeout_seconds)
|
|
461
|
+
if not done:
|
|
462
|
+
return None
|
|
463
|
+
event, self._pending = self._pending.result(), None
|
|
464
|
+
return event
|
|
465
|
+
|
|
466
|
+
async def close(self) -> None:
|
|
467
|
+
"""Cancel the pending read, then close the subscription."""
|
|
468
|
+
if self._pending is not None:
|
|
469
|
+
self._pending.cancel()
|
|
470
|
+
await asyncio.gather(self._pending, return_exceptions=True)
|
|
471
|
+
await self._live.aclose()
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
class _StreamCursor:
|
|
475
|
+
"""Where a run's event stream stands: the last sequence seen and whether it has ended."""
|
|
476
|
+
|
|
477
|
+
def __init__(self, task_id: str, after_seq: int | None) -> None:
|
|
478
|
+
self.last_seq = after_seq
|
|
479
|
+
self.done = False
|
|
480
|
+
self._completed = f"{event_prefix(task_id)}{RUN_COMPLETED}"
|
|
481
|
+
self.task_id = task_id
|
|
482
|
+
|
|
483
|
+
def take(self, event: HarborEvent) -> bool:
|
|
484
|
+
"""Advance past ``event``; return whether it should be sent.
|
|
485
|
+
|
|
486
|
+
``run.completed`` is always sent once, whatever its sequence, because a duplicate with
|
|
487
|
+
a higher sequence can overtake it. Only the four explorer events are sent.
|
|
488
|
+
"""
|
|
489
|
+
if event.name == self._completed:
|
|
490
|
+
first = not self.done
|
|
491
|
+
self.done = True
|
|
492
|
+
self._advance(event)
|
|
493
|
+
return first
|
|
494
|
+
if event.seq is not None and self.last_seq is not None and event.seq <= self.last_seq:
|
|
495
|
+
return False
|
|
496
|
+
self._advance(event)
|
|
497
|
+
return event.name.removeprefix(event_prefix(self.task_id)) in STREAMED_EVENTS
|
|
498
|
+
|
|
499
|
+
def skipped_before(self, event: HarborEvent) -> bool:
|
|
500
|
+
"""Whether stored events lie between the last one seen and ``event``."""
|
|
501
|
+
return event.seq is not None and event.seq > (self.last_seq or 0) + 1
|
|
502
|
+
|
|
503
|
+
def _advance(self, event: HarborEvent) -> None:
|
|
504
|
+
if event.seq is not None and (self.last_seq is None or event.seq > self.last_seq):
|
|
505
|
+
self.last_seq = event.seq
|
|
506
|
+
|
|
507
|
+
|
|
508
|
+
async def _deliver(
|
|
509
|
+
store: PublicTaskStore, cursor: _StreamCursor, live_event: HarborEvent
|
|
510
|
+
) -> AsyncGenerator[HarborEvent, None]:
|
|
511
|
+
"""Yield ``live_event``, first reading back from the store any events the tail skipped."""
|
|
512
|
+
if cursor.skipped_before(live_event):
|
|
513
|
+
async for event in _backlog(store, cursor):
|
|
514
|
+
yield event
|
|
515
|
+
if cursor.take(live_event):
|
|
516
|
+
yield live_event
|
|
517
|
+
|
|
518
|
+
|
|
519
|
+
async def _backlog(
|
|
520
|
+
store: PublicTaskStore, cursor: _StreamCursor
|
|
521
|
+
) -> AsyncGenerator[HarborEvent, None]:
|
|
522
|
+
"""Yield the stored events past the cursor that should be sent, up to ``run.completed``."""
|
|
523
|
+
async for event in _replay(store, cursor.task_id, cursor.last_seq):
|
|
524
|
+
if cursor.take(event):
|
|
525
|
+
yield event
|
|
526
|
+
if cursor.done:
|
|
527
|
+
return
|
|
528
|
+
|
|
529
|
+
|
|
530
|
+
async def _replay(
|
|
531
|
+
store: PublicTaskStore, task_id: str, after_seq: int | None
|
|
532
|
+
) -> AsyncGenerator[HarborEvent, None]:
|
|
533
|
+
"""Yield the stored events after ``after_seq``, page by page until a short page."""
|
|
534
|
+
cursor = after_seq
|
|
535
|
+
while True:
|
|
536
|
+
page = await store.list_task_events(task_id, after_seq=cursor, limit=_REPLAY_PAGE_LIMIT)
|
|
537
|
+
for event in page:
|
|
538
|
+
yield event
|
|
539
|
+
if len(page) < _REPLAY_PAGE_LIMIT:
|
|
540
|
+
return
|
|
541
|
+
cursor = page[-1].seq
|
|
542
|
+
|
|
543
|
+
|
|
544
|
+
async def _complete_if_ended(store: PublicTaskStore, bus: EventBusPort, task_id: str) -> bool:
|
|
545
|
+
"""Store and publish the run's missing events when it has ended; whether it had."""
|
|
546
|
+
task = await store.get(task_id)
|
|
547
|
+
if task is None or task.status not in TERMINAL_STATES:
|
|
548
|
+
return False
|
|
549
|
+
await sync_run(store, bus, task)
|
|
550
|
+
return True
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"""Resolve the control-plane ports per-tenant chat models need, or nothing.
|
|
2
|
+
|
|
3
|
+
Off unless the deployment asked for it. Turning it on moves chat credentials
|
|
4
|
+
from the process environment into the encrypted control-plane store and gives
|
|
5
|
+
every configured tenant its own client, so it is an explicit choice rather
|
|
6
|
+
than something a schema migration switches on underneath a running
|
|
7
|
+
deployment. Wired the way ``memories`` and ``model_usage`` are: read off the
|
|
8
|
+
control plane if it is there, warn and carry on if it is not.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
from __future__ import annotations
|
|
12
|
+
|
|
13
|
+
import logging
|
|
14
|
+
from typing import Any
|
|
15
|
+
|
|
16
|
+
from harborrag_core.ports.model_catalog import TenantModelCatalogPort
|
|
17
|
+
from harborrag_core.ports.secrets import SecretsPort
|
|
18
|
+
from harborrag_runtime.chat import TenantModelSources
|
|
19
|
+
from harborrag_runtime.config.settings import RuntimeSettings
|
|
20
|
+
|
|
21
|
+
logger = logging.getLogger("harborrag.runtime.application.composition.tenant_models")
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def tenant_model_sources(
|
|
25
|
+
composition: Any,
|
|
26
|
+
settings: RuntimeSettings,
|
|
27
|
+
) -> TenantModelSources | None:
|
|
28
|
+
"""Return the catalog and secrets ports for per-tenant chat, or None to stay shared.
|
|
29
|
+
|
|
30
|
+
``None`` is the default and means every tenant keeps answering from the
|
|
31
|
+
process-wide ``config/models/models.yaml`` catalog, exactly as before.
|
|
32
|
+
"""
|
|
33
|
+
if not settings.chat_tenant_catalogs_enabled:
|
|
34
|
+
return None
|
|
35
|
+
control_plane = getattr(composition, "control_plane", None)
|
|
36
|
+
catalog: TenantModelCatalogPort | None = getattr(control_plane, "model_catalog", None)
|
|
37
|
+
secrets: SecretsPort | None = getattr(control_plane, "secrets", None)
|
|
38
|
+
if catalog is None or secrets is None:
|
|
39
|
+
logger.warning(
|
|
40
|
+
"HARBORRAG_CHAT_TENANT_CATALOGS_ENABLED is set but no model catalog and "
|
|
41
|
+
"secret store are wired on the control plane; every tenant will keep "
|
|
42
|
+
"answering from the process-wide chat catalog."
|
|
43
|
+
)
|
|
44
|
+
return None
|
|
45
|
+
return TenantModelSources(catalog=catalog, secrets=secrets)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Control-plane read use cases and projections."""
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"""Find a connection's active ingestion run after settling runs whose workflow ended unrecorded.
|
|
2
|
+
|
|
3
|
+
Settling the newest active run can reveal an older one, such as a sync behind a terminated
|
|
4
|
+
retry run, so the lookup repeats. A run still active after ``SETTLE_ROUNDS`` settles counts as
|
|
5
|
+
active, so the guards that use it fail closed.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import TYPE_CHECKING
|
|
11
|
+
|
|
12
|
+
from harborrag_core.ingestion import IngestionTask
|
|
13
|
+
|
|
14
|
+
if TYPE_CHECKING:
|
|
15
|
+
from .connection_models import ConnectionRuns, IngestionSubmitter
|
|
16
|
+
|
|
17
|
+
SETTLE_ROUNDS = 5
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
async def active_run(
|
|
21
|
+
runs: ConnectionRuns, ingestions: IngestionSubmitter, *, tenant_id: str, source_key: str
|
|
22
|
+
) -> IngestionTask | None:
|
|
23
|
+
"""Return the connection's pending, running or paused run, settling ended runs first.
|
|
24
|
+
|
|
25
|
+
Args:
|
|
26
|
+
runs: Run registry of the connection.
|
|
27
|
+
ingestions: Settles a run whose workflow ended without recording it.
|
|
28
|
+
tenant_id: Tenant of the connection.
|
|
29
|
+
source_key: The connection's source key.
|
|
30
|
+
|
|
31
|
+
Returns:
|
|
32
|
+
The first active run that does not settle, or ``None`` once no active run is left.
|
|
33
|
+
"""
|
|
34
|
+
for _ in range(SETTLE_ROUNDS):
|
|
35
|
+
active = await runs.active_for_connection(tenant_id=tenant_id, connection_id=source_key)
|
|
36
|
+
if active is None or not await ingestions.settle_if_ended(active.task_id):
|
|
37
|
+
return active
|
|
38
|
+
return await runs.active_for_connection(tenant_id=tenant_id, connection_id=source_key)
|