feat(simulation): add Worker AI polygon runtime and terrain navigation
This commit is contained in:
@@ -1,33 +1,41 @@
|
||||
"""Observation-only laboratory orchestration contracts."""
|
||||
"""Observation contracts; platform-specific services are loaded only on demand.
|
||||
|
||||
from k1link.observatory.canonical_result import (
|
||||
is_admitted_observatory_recorded_result,
|
||||
)
|
||||
from k1link.observatory.run_preparations import (
|
||||
MAX_RUN_PREPARATION_RECORDS,
|
||||
MAX_RUN_PREPARATION_STORAGE_BYTES,
|
||||
OBSERVATORY_RUN_PREPARATION_REQUEST_SCHEMA,
|
||||
OBSERVATORY_RUN_PREPARATION_SCHEMA,
|
||||
RUN_PREPARATION_DATABASE_NAME,
|
||||
RUN_PREPARATION_SQLITE_LOCK_TIMEOUT_SECONDS,
|
||||
ObservatoryRunPreparation,
|
||||
ObservatoryRunPreparationCapacityError,
|
||||
ObservatoryRunPreparationConflictError,
|
||||
ObservatoryRunPreparationError,
|
||||
ObservatoryRunPreparationIntegrityError,
|
||||
ObservatoryRunPreparationIntent,
|
||||
ObservatoryRunPreparationLedger,
|
||||
ObservatoryRunPreparationNotFoundError,
|
||||
load_observatory_run_preparation_ledger,
|
||||
observatory_run_preparation_request_sha256,
|
||||
)
|
||||
from k1link.observatory.setups import (
|
||||
LABORATORY_SETUP_CATALOG_SCHEMA,
|
||||
LABORATORY_SETUP_REGISTRY_SCHEMA,
|
||||
OBSERVATORY_CALCULATION_PROFILE_SCHEMA,
|
||||
LaboratorySetupRegistry,
|
||||
LaboratorySetupRegistryError,
|
||||
)
|
||||
Importing a portable composition on Windows must not import Core's POSIX
|
||||
artifact gateway or eagerly initialize session/recording infrastructure.
|
||||
"""
|
||||
|
||||
from importlib import import_module
|
||||
from typing import TYPE_CHECKING
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from k1link.observatory.canonical_result import (
|
||||
is_admitted_observatory_recorded_result,
|
||||
)
|
||||
from k1link.observatory.run_preparations import (
|
||||
MAX_RUN_PREPARATION_RECORDS,
|
||||
MAX_RUN_PREPARATION_STORAGE_BYTES,
|
||||
OBSERVATORY_RUN_PREPARATION_REQUEST_SCHEMA,
|
||||
OBSERVATORY_RUN_PREPARATION_SCHEMA,
|
||||
RUN_PREPARATION_DATABASE_NAME,
|
||||
RUN_PREPARATION_SQLITE_LOCK_TIMEOUT_SECONDS,
|
||||
ObservatoryRunPreparation,
|
||||
ObservatoryRunPreparationCapacityError,
|
||||
ObservatoryRunPreparationConflictError,
|
||||
ObservatoryRunPreparationError,
|
||||
ObservatoryRunPreparationIntegrityError,
|
||||
ObservatoryRunPreparationIntent,
|
||||
ObservatoryRunPreparationLedger,
|
||||
ObservatoryRunPreparationNotFoundError,
|
||||
load_observatory_run_preparation_ledger,
|
||||
observatory_run_preparation_request_sha256,
|
||||
)
|
||||
from k1link.observatory.setups import (
|
||||
LABORATORY_SETUP_CATALOG_SCHEMA,
|
||||
LABORATORY_SETUP_REGISTRY_SCHEMA,
|
||||
OBSERVATORY_CALCULATION_PROFILE_SCHEMA,
|
||||
LaboratorySetupRegistry,
|
||||
LaboratorySetupRegistryError,
|
||||
)
|
||||
|
||||
__all__ = [
|
||||
"LABORATORY_SETUP_CATALOG_SCHEMA",
|
||||
@@ -53,3 +61,41 @@ __all__ = [
|
||||
"load_observatory_run_preparation_ledger",
|
||||
"observatory_run_preparation_request_sha256",
|
||||
]
|
||||
|
||||
_EXPORTS = {
|
||||
"is_admitted_observatory_recorded_result": "k1link.observatory.canonical_result",
|
||||
"MAX_RUN_PREPARATION_RECORDS": "k1link.observatory.run_preparations",
|
||||
"MAX_RUN_PREPARATION_STORAGE_BYTES": "k1link.observatory.run_preparations",
|
||||
"OBSERVATORY_RUN_PREPARATION_REQUEST_SCHEMA": "k1link.observatory.run_preparations",
|
||||
"OBSERVATORY_RUN_PREPARATION_SCHEMA": "k1link.observatory.run_preparations",
|
||||
"RUN_PREPARATION_DATABASE_NAME": "k1link.observatory.run_preparations",
|
||||
"RUN_PREPARATION_SQLITE_LOCK_TIMEOUT_SECONDS": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparation": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationCapacityError": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationConflictError": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationError": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationIntegrityError": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationIntent": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationLedger": "k1link.observatory.run_preparations",
|
||||
"ObservatoryRunPreparationNotFoundError": "k1link.observatory.run_preparations",
|
||||
"load_observatory_run_preparation_ledger": "k1link.observatory.run_preparations",
|
||||
"observatory_run_preparation_request_sha256": "k1link.observatory.run_preparations",
|
||||
"LABORATORY_SETUP_CATALOG_SCHEMA": "k1link.observatory.setups",
|
||||
"LABORATORY_SETUP_REGISTRY_SCHEMA": "k1link.observatory.setups",
|
||||
"OBSERVATORY_CALCULATION_PROFILE_SCHEMA": "k1link.observatory.setups",
|
||||
"LaboratorySetupRegistry": "k1link.observatory.setups",
|
||||
"LaboratorySetupRegistryError": "k1link.observatory.setups",
|
||||
}
|
||||
|
||||
|
||||
def __getattr__(name: str):
|
||||
module = _EXPORTS.get(name)
|
||||
if module is None:
|
||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
|
||||
value = getattr(import_module(module), name)
|
||||
globals()[name] = value
|
||||
return value
|
||||
|
||||
|
||||
def __dir__():
|
||||
return sorted(set(globals()) | set(__all__))
|
||||
|
||||
@@ -12,7 +12,7 @@ from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
from typing import Final, cast
|
||||
|
||||
from k1link.observatory.portable_run_definitions import canonical_sha256
|
||||
from k1link.artifacts import canonical_json_sha256 as canonical_sha256
|
||||
|
||||
COMPOSITION_SCHEMA: Final = "missioncore.observatory-ai-composition/v1"
|
||||
MODULE_SCHEMA: Final = "missioncore.observatory-ai-module/v1"
|
||||
@@ -179,6 +179,11 @@ class CompositionSpec:
|
||||
"""Source-independent graph, in deterministic topological execution order."""
|
||||
|
||||
nodes: tuple[CompositionNode, ...]
|
||||
execution_mode: str = "recorded-observation-only"
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if self.execution_mode not in ("recorded-observation-only", "worker-local-simulation"):
|
||||
raise CompositionError("unsupported composition execution mode")
|
||||
|
||||
@property
|
||||
def source_capabilities(self) -> tuple[str, ...]:
|
||||
@@ -212,7 +217,10 @@ class CompositionSpec:
|
||||
"nodes": [node.as_dict() for node in self.nodes],
|
||||
"source_capabilities": list(self.source_capabilities),
|
||||
"outputs": list(self.outputs),
|
||||
"execution": {"max_parallel_nodes": 1, "mode": "recorded-observation-only"},
|
||||
"execution": {
|
||||
"max_parallel_nodes": 2 if self.execution_mode == "worker-local-simulation" else 1,
|
||||
"mode": self.execution_mode,
|
||||
},
|
||||
}
|
||||
|
||||
@property
|
||||
@@ -286,7 +294,9 @@ class ModuleRegistry:
|
||||
],
|
||||
}
|
||||
|
||||
def compose(self, document: object) -> CompositionSpec:
|
||||
def compose(
|
||||
self, document: object, *, execution_mode: str = "recorded-observation-only"
|
||||
) -> CompositionSpec:
|
||||
root = _object(document, {"schema_version", "selections"})
|
||||
if root["schema_version"] != COMPOSITION_SCHEMA:
|
||||
raise CompositionError("unsupported composition schema")
|
||||
@@ -381,7 +391,7 @@ class ModuleRegistry:
|
||||
for key in ready:
|
||||
ordered.append(pending.pop(key))
|
||||
emitted.add(key)
|
||||
return CompositionSpec(tuple(ordered))
|
||||
return CompositionSpec(tuple(ordered), execution_mode=execution_mode)
|
||||
|
||||
|
||||
def node_input_identity(
|
||||
|
||||
@@ -97,6 +97,11 @@ _AUTHORITY: Final = {
|
||||
}
|
||||
|
||||
_SCHEMA_SQL = """
|
||||
CREATE TABLE IF NOT EXISTS simulation_worker_reservation (
|
||||
singleton INTEGER PRIMARY KEY CHECK (singleton = 1),
|
||||
owner_id TEXT NOT NULL,
|
||||
created_at_utc TEXT NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS observatory_recorded_jobs (
|
||||
job_id TEXT PRIMARY KEY,
|
||||
idempotency_key TEXT NOT NULL UNIQUE,
|
||||
@@ -1185,6 +1190,46 @@ class ObservatoryRecordedJobQueue:
|
||||
self._lock = threading.RLock()
|
||||
self._initialize()
|
||||
|
||||
def reserve_simulation(self, owner_id: str) -> None:
|
||||
"""Reserve this Worker's GPU atomically against recorded/live admission.
|
||||
|
||||
A lost heartbeat does not prove GPU release. This reservation survives
|
||||
restarts and is removed only by the exact simulator's release receipt.
|
||||
"""
|
||||
if not re.fullmatch(r"airun-[a-f0-9]{32}", owner_id):
|
||||
raise ValueError("invalid simulation resource owner")
|
||||
with self._transaction() as connection:
|
||||
existing = connection.execute(
|
||||
"SELECT owner_id FROM simulation_worker_reservation"
|
||||
).fetchone()
|
||||
if existing is not None:
|
||||
if existing["owner_id"] == owner_id:
|
||||
return
|
||||
raise ObservatoryRecordedQueueBusyError("Worker занят другим прогоном симуляции.")
|
||||
busy = connection.execute(
|
||||
"SELECT job_id FROM observatory_recorded_jobs "
|
||||
"WHERE state IN ('claimed', 'running', 'paused', 'preemption-pending', "
|
||||
"'reconciliation-required') LIMIT 1"
|
||||
).fetchone()
|
||||
if busy is not None or self._open_live_lease_row(connection) is not None:
|
||||
raise ObservatoryRecordedQueueBusyError("Worker занят задачей AI Inference.")
|
||||
connection.execute(
|
||||
"INSERT INTO simulation_worker_reservation VALUES (1, ?, ?)",
|
||||
(owner_id, self._timestamp()),
|
||||
)
|
||||
|
||||
def release_simulation(self, owner_id: str) -> None:
|
||||
"""Called only after the trusted simulator reports all GPU work stopped."""
|
||||
with self._transaction() as connection:
|
||||
existing = connection.execute(
|
||||
"SELECT owner_id FROM simulation_worker_reservation"
|
||||
).fetchone()
|
||||
if existing is not None and existing["owner_id"] != owner_id:
|
||||
raise ObservatoryRecordedQueueConflictError("simulation resource owner changed")
|
||||
connection.execute(
|
||||
"DELETE FROM simulation_worker_reservation WHERE owner_id = ?", (owner_id,)
|
||||
)
|
||||
|
||||
def resolve_definition(
|
||||
self,
|
||||
setup_id: str,
|
||||
@@ -1444,7 +1489,10 @@ class ObservatoryRecordedJobQueue:
|
||||
label="legacy claim receipt",
|
||||
)
|
||||
row = None
|
||||
if self._open_live_lease_row(connection) is None:
|
||||
simulation = connection.execute(
|
||||
"SELECT owner_id FROM simulation_worker_reservation"
|
||||
).fetchone()
|
||||
if self._open_live_lease_row(connection) is None and simulation is None:
|
||||
active_owner = connection.execute(
|
||||
"SELECT job_id FROM observatory_recorded_jobs "
|
||||
"WHERE state IN ('claimed', 'running', 'preemption-pending', "
|
||||
@@ -2382,6 +2430,10 @@ class ObservatoryRecordedJobQueue:
|
||||
_validate_pattern(lease_id, _LEASE_ID, "live lease id")
|
||||
self.recover_stale_claims()
|
||||
with self._transaction() as connection:
|
||||
if connection.execute("SELECT owner_id FROM simulation_worker_reservation").fetchone():
|
||||
raise ObservatoryRecordedQueueBusyError(
|
||||
"simulation has not released the Worker GPU"
|
||||
)
|
||||
lease = self._get_live_lease(connection, lease_id)
|
||||
if lease.state == "active":
|
||||
return lease
|
||||
|
||||
Reference in New Issue
Block a user