feat(worker): supervise VPN connections and recover batched telemetry

This commit is contained in:
DCCONSTRUCTIONS
2026-11-10 01:10:37 +03:00
parent fe5900ff3b
commit ad5a929e06
46 changed files with 2996 additions and 640 deletions
+32 -14
View File
@@ -391,7 +391,7 @@ class _ValidatedClaim:
class ObservatoryWorkerAgent:
"""Runs at most one sealed recorded-job claim at a time on Worker 006."""
"""Runs one sealed job at a time under the enrolled Worker identity."""
def __init__(
self,
@@ -401,6 +401,7 @@ class ObservatoryWorkerAgent:
claim_request_id_factory: Callable[[], str] | None = None,
heartbeat_interval_seconds: float | None = None,
heartbeat_stop_timeout_seconds: float = 5.0,
contour_id: str = WORKER_006_CONTOUR_ID,
) -> None:
if heartbeat_interval_seconds is not None and not (
0.01 <= heartbeat_interval_seconds <= 300.0
@@ -408,9 +409,12 @@ class ObservatoryWorkerAgent:
raise ValueError("Worker heartbeat interval is invalid")
if not 0.1 <= heartbeat_stop_timeout_seconds <= 300.0:
raise ValueError("Worker heartbeat stop timeout is invalid")
self._contour_id = validate_worker_contour_id(contour_id)
self._transport = transport
self._executors = executors
self._claim_request_id_factory = claim_request_id_factory or _default_claim_request_id
self._claim_request_id_factory = claim_request_id_factory or (
lambda: _default_claim_request_id(self._contour_id)
)
self._heartbeat_interval_seconds = heartbeat_interval_seconds
self._heartbeat_stop_timeout_seconds = heartbeat_stop_timeout_seconds
self._cycle_lock = threading.Lock()
@@ -419,7 +423,7 @@ class ObservatoryWorkerAgent:
"""Claim, validate, execute, and seal one durable job if available."""
if not self._cycle_lock.acquire(blocking=False):
raise ObservatoryWorkerAgentBusyError("Worker 006 already owns an active claim cycle")
raise ObservatoryWorkerAgentBusyError("Worker already owns an active claim cycle")
try:
return self._run_once_locked()
finally:
@@ -430,7 +434,7 @@ class ObservatoryWorkerAgent:
if _CLAIM_REQUEST_ID.fullmatch(claim_request_id) is None:
raise ValueError("Worker claim request id is invalid")
payload = self._transport.claim_next(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
claim_request_id=claim_request_id,
supported_executor_identities=self._executors.supported_identities,
)
@@ -442,6 +446,7 @@ class ObservatoryWorkerAgent:
try:
claim = _validate_claim(
payload,
contour_id=self._contour_id,
claim_request_id=claim_request_id,
supported_executor_identities=self._executors.supported_identities,
)
@@ -457,11 +462,11 @@ class ObservatoryWorkerAgent:
except ObservatoryWorkerExecutorUnavailableError:
failure_code = "executor-not-allowlisted"
acknowledgement = self._transport.fail(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
job_id=claim.job.job_id,
claim_token=claim.claim_token,
error_code=failure_code,
message="Exact executor identity is not installed on Worker 006.",
message="Exact executor identity is not installed on this Worker.",
)
_validate_transition_acknowledgement(
acknowledgement,
@@ -476,7 +481,7 @@ class ObservatoryWorkerAgent:
)
started_payload = self._transport.start(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
job_id=claim.job.job_id,
claim_token=claim.claim_token,
)
@@ -495,6 +500,7 @@ class ObservatoryWorkerAgent:
active_job = _seal_job(started)
heartbeat = _ClaimHeartbeat(
transport=self._transport,
contour_id=self._contour_id,
job=active_job,
claim_token=claim.claim_token,
interval_seconds=(
@@ -544,7 +550,7 @@ class ObservatoryWorkerAgent:
)
failure_code = "executor-error"
acknowledgement = self._transport.fail(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
job_id=claim.job.job_id,
claim_token=claim.claim_token,
error_code=failure_code,
@@ -572,7 +578,7 @@ class ObservatoryWorkerAgent:
)
acknowledgement = self._transport.succeed(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
job_id=claim.job.job_id,
claim_token=claim.claim_token,
result_id=result.result_id,
@@ -614,9 +620,11 @@ class _ClaimHeartbeat:
interval_seconds: float,
stop_timeout_seconds: float,
clock: Callable[[], float] = time.monotonic,
contour_id: str = WORKER_006_CONTOUR_ID,
) -> None:
if interval_seconds <= 0:
raise ValueError("Worker heartbeat interval must be positive")
self._contour_id = validate_worker_contour_id(contour_id)
self._transport = transport
self._job = job
self._claim_token = claim_token
@@ -671,7 +679,7 @@ class _ClaimHeartbeat:
"Worker heartbeat lease budget expired"
)
acknowledgement = self._transport.renew_claim(
claimant_id=WORKER_006_CONTOUR_ID,
claimant_id=self._contour_id,
job_id=self._job.job_id,
claim_token=self._claim_token,
claim_generation=self._job.claim_generation,
@@ -756,19 +764,20 @@ def _validate_claim(
payload: Mapping[str, object],
*,
claim_request_id: str,
contour_id: str = WORKER_006_CONTOUR_ID,
supported_executor_identities: tuple[ObservatoryWorkerExecutorIdentity, ...],
) -> _ValidatedClaim:
try:
claim = _RecordedClaimPayload.model_validate(dict(payload))
if claim.claim_request_id != claim_request_id:
raise ObservatoryWorkerClaimRejectedError("Worker claim request identity changed")
if claim.claimant_id != WORKER_006_CONTOUR_ID:
if claim.claimant_id != contour_id:
raise ObservatoryWorkerClaimRejectedError("Worker claim belongs to another claimant")
expected_claim_request_sha256 = _sha256_document(
{
"schema_version": OBSERVATORY_RECORDED_CLAIM_SCHEMA,
"claim_request_id": claim_request_id,
"claimant_id": WORKER_006_CONTOUR_ID,
"claimant_id": contour_id,
"supported_executor_identities": [
identity.as_dict() for identity in sorted(supported_executor_identities)
],
@@ -963,8 +972,17 @@ def _validate_transition_acknowledgement(
return acknowledgement
def _default_claim_request_id() -> str:
return f"worker-006:{uuid4().hex}"
def validate_worker_contour_id(value: str) -> str:
if (
not isinstance(value, str)
or re.fullmatch(r"[a-z0-9](?:[a-z0-9-]{0,62}[a-z0-9])?", value) is None
):
raise ValueError("Worker contour identity is invalid")
return value
def _default_claim_request_id(contour_id: str = WORKER_006_CONTOUR_ID) -> str:
return f"{contour_id}:{uuid4().hex}"
def _default_heartbeat_interval(job: SealedObservatoryRecordedJob) -> float:
@@ -59,6 +59,7 @@ from k1link.observatory.worker_agent import (
ObservatoryWorkerTransientTransportError,
ObservatoryWorkerTransport,
SealedObservatoryRecordedJob,
validate_worker_contour_id,
)
from k1link.observatory.worker_source_cache import WorkerSourceCache, WorkerSourceCacheError
@@ -177,8 +178,7 @@ class ObservatoryWorkerHttpGateway(ObservatoryWorkerTransport):
self._base_url = _validated_base_url(base_url)
if _TOKEN.fullmatch(bearer_token) is None:
raise ValueError("Worker bearer credential format is invalid")
if contour_id != WORKER_006_CONTOUR_ID:
raise ValueError("portable Worker gateway is pinned to Worker 006")
self.contour_id = validate_worker_contour_id(contour_id)
if not 1 <= timeout_seconds <= 300:
raise ValueError("Worker HTTP timeout is invalid")
self._headers = {
@@ -865,9 +865,8 @@ class ObservatoryWorkerHttpGateway(ObservatoryWorkerTransport):
_CLAIM_GENERATION_HEADER: str(context.claim_generation),
}
@staticmethod
def _require_claimant(claimant_id: str) -> None:
if claimant_id != WORKER_006_CONTOUR_ID:
def _require_claimant(self, claimant_id: str) -> None:
if claimant_id != self.contour_id:
raise ObservatoryWorkerHttpError("Worker claimant identity changed")
@staticmethod
+17 -3
View File
@@ -142,8 +142,12 @@ class ObservatoryWorkerServiceConfiguration:
transport_backoff_seconds: float = DEFAULT_OBSERVATORY_WORKER_TRANSPORT_BACKOFF_SECONDS
max_consecutive_transport_failures: int = DEFAULT_OBSERVATORY_WORKER_MAX_TRANSPORT_FAILURES
source_cache_root: Path | None = None
contour_id: str = "worker-006"
def __post_init__(self) -> None:
from k1link.observatory.worker_agent import validate_worker_contour_id
validate_worker_contour_id(self.contour_id)
_validate_worker_base_url(self.base_url)
_absolute_path(self.bearer_token_file, "Worker bearer token file")
_absolute_path(self.work_root, "Worker work root")
@@ -177,6 +181,7 @@ class ObservatoryWorkerServiceConfiguration:
OBSERVATORY_WORKER_BASE_URL_ENV,
DEFAULT_OBSERVATORY_WORKER_BASE_URL,
),
contour_id=values.get("MISSIONCORE_CONTOUR_ID", "worker-006"),
bearer_token_file=token_file,
work_root=work_root,
source_cache_root=(
@@ -271,6 +276,7 @@ def compose_installed_observatory_worker_service(
try:
gateway = ObservatoryWorkerHttpGateway(
base_url=configuration.base_url,
contour_id=configuration.contour_id,
bearer_token=bearer_token,
work_root=configuration.work_root,
transport=http_transport,
@@ -283,7 +289,9 @@ def compose_installed_observatory_worker_service(
return InstalledObservatoryWorkerService(
configuration=configuration,
gateway=gateway,
agent=ObservatoryWorkerAgent(transport=gateway, executors=executors),
agent=ObservatoryWorkerAgent(
transport=gateway, executors=executors, contour_id=configuration.contour_id
),
)
@@ -309,6 +317,7 @@ def compose_installed_observatory_worker_service_from_builders(
try:
gateway = ObservatoryWorkerHttpGateway(
base_url=configuration.base_url,
contour_id=configuration.contour_id,
bearer_token=bearer_token,
work_root=configuration.work_root,
transport=http_transport,
@@ -325,7 +334,9 @@ def compose_installed_observatory_worker_service_from_builders(
return InstalledObservatoryWorkerService(
configuration=configuration,
gateway=gateway,
agent=ObservatoryWorkerAgent(transport=gateway, executors=executors),
agent=ObservatoryWorkerAgent(
transport=gateway, executors=executors, contour_id=configuration.contour_id
),
)
except Exception:
if gateway is not None:
@@ -351,6 +362,7 @@ def compose_installed_observatory_worker_service_from_packages(
try:
gateway = ObservatoryWorkerHttpGateway(
base_url=configuration.base_url,
contour_id=configuration.contour_id,
bearer_token=bearer_token,
work_root=configuration.work_root,
transport=http_transport,
@@ -368,7 +380,9 @@ def compose_installed_observatory_worker_service_from_packages(
return InstalledObservatoryWorkerService(
configuration=configuration,
gateway=gateway,
agent=ObservatoryWorkerAgent(transport=gateway, executors=executors),
agent=ObservatoryWorkerAgent(
transport=gateway, executors=executors, contour_id=configuration.contour_id
),
)
except Exception:
if gateway is not None: