Assemble complete modular LAB recordings on Worker before readiness
This commit is contained in:
@@ -506,3 +506,63 @@ def test_composition_prepares_an_unopened_recording_before_submit(tmp_path: Path
|
||||
assert response.status_code == 200
|
||||
assert response.json()["dispatch"]["ready"] is True
|
||||
assert calls == ["check", "prepare-check", "submit"]
|
||||
|
||||
|
||||
def test_new_composition_is_not_ready_until_worker_recording_is_published(tmp_path):
|
||||
registry = ModuleRegistry.from_file(
|
||||
Path(__file__).parents[1] / "config/observatory-ai-modules.json"
|
||||
)
|
||||
tgs = next(m for m in registry.modules if m.module_id == "tgs")
|
||||
composition = registry.compose(
|
||||
{
|
||||
"schema_version": COMPOSITION_SCHEMA,
|
||||
"selections": [
|
||||
{
|
||||
"group": tgs.group,
|
||||
"module_id": tgs.module_id,
|
||||
"module_sha256": tgs.sha256,
|
||||
"parameters": {},
|
||||
}
|
||||
],
|
||||
}
|
||||
)
|
||||
runs = CompositionRunStore(tmp_path / "runs")
|
||||
run = runs.save(
|
||||
source_session_id="source-1",
|
||||
composition=composition,
|
||||
setup_ids=("setup-1",),
|
||||
job_ids=("job-1",),
|
||||
idempotency_key="new",
|
||||
created_at_utc="2026-11-10T00:00:00Z",
|
||||
replay_policy="worker-ready-v1",
|
||||
)
|
||||
state = {"state": "building"}
|
||||
job = SimpleNamespace(
|
||||
state="succeeded", publication_state="published", result_id="result", as_dict=lambda: {}
|
||||
)
|
||||
app = FastAPI()
|
||||
app.include_router(
|
||||
build_modular_observatory_router(
|
||||
store=SimpleNamespace(
|
||||
get_session=lambda _: SimpleNamespace(
|
||||
summary=SimpleNamespace(display_name="source")
|
||||
)
|
||||
),
|
||||
compositions=ModularCompositionStore(tmp_path / "compositions", registry),
|
||||
composition_runs=runs,
|
||||
queue=SimpleNamespace(get=lambda _: job),
|
||||
ontology=SimpleNamespace(project_module_ids=lambda _: {"configuration_label": "TGS"}),
|
||||
replay_finalization=SimpleNamespace(status=lambda _: state),
|
||||
)
|
||||
)
|
||||
client = TestClient(app)
|
||||
path = "/api/v1/observatory/ai-composition-runs?source_session_id=source-1"
|
||||
row = client.get(path).json()["items"][0]
|
||||
assert row["state"] == "running"
|
||||
# Preserve the existing strict frontend projection contract.
|
||||
assert "replay_policy" not in row and "replay_publication" not in row
|
||||
state["state"] = "failed"
|
||||
assert client.get(path).json()["items"][0]["state"] == "failed"
|
||||
state["state"] = "ready"
|
||||
assert client.get(path).json()["items"][0]["state"] == "ready"
|
||||
assert runs.get(run.run_id).replay_policy == "worker-ready-v1"
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
import hashlib
|
||||
import json
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from k1link.laboratory.data_deletion import LabDeletionError, _write
|
||||
from k1link.laboratory.replay_build import (
|
||||
BUILD_PREFIX,
|
||||
BuildMember,
|
||||
ReplayBuildPlan,
|
||||
ReplayBuilds,
|
||||
build_replay_build_router,
|
||||
)
|
||||
from k1link.laboratory.replay_publication import ReplayPublications
|
||||
from k1link.observatory.portable_replay import PortableReplayService
|
||||
from k1link.observatory.portable_tgs_replay import PortableReplayError
|
||||
|
||||
RUN = "ai-composition-" + "a" * 64
|
||||
RESULT = "ai-layer-ddrnet-" + "b" * 64
|
||||
|
||||
|
||||
def fixture(tmp_path):
|
||||
blobs = {}
|
||||
|
||||
def add(value):
|
||||
digest = hashlib.sha256(value).hexdigest()
|
||||
blobs[digest] = value
|
||||
return {"sha256": digest, "byte_length": len(value)}
|
||||
|
||||
base = add(b"RRF2source")
|
||||
init = add(b"init")
|
||||
segment = add(b"frame")
|
||||
bundle = add(
|
||||
json.dumps(
|
||||
{
|
||||
"source_session_id": "source",
|
||||
"camera": {
|
||||
"epoch": {
|
||||
"ordinal": 1,
|
||||
"media_type": "video/mp4",
|
||||
"init": init,
|
||||
"timeline_start_seconds": 0.0,
|
||||
"timeline_end_seconds": 0.1,
|
||||
"segments": [
|
||||
{
|
||||
**segment,
|
||||
"sequence": 1,
|
||||
"random_access": True,
|
||||
"end_time_seconds": 0.1,
|
||||
}
|
||||
],
|
||||
}
|
||||
},
|
||||
}
|
||||
).encode()
|
||||
)
|
||||
view = {
|
||||
"result_id": RESULT,
|
||||
"source_session_id": "source",
|
||||
"artifacts": [],
|
||||
"artifact_manifest_id": "c" * 64,
|
||||
}
|
||||
service = PortableReplayService(
|
||||
view=SimpleNamespace(read=lambda _: view),
|
||||
data_dir=tmp_path,
|
||||
media=None,
|
||||
recording_source=lambda _: None,
|
||||
ffmpeg_path=tmp_path / "ffmpeg",
|
||||
)
|
||||
key, _ = service._composition_key(RUN, (RESULT,), base["sha256"])
|
||||
plan = ReplayBuildPlan(
|
||||
schema_version="missioncore.replay-build/v1",
|
||||
result_id=RUN,
|
||||
source_session_id="source",
|
||||
base_sha256=base["sha256"],
|
||||
cache_key=key,
|
||||
members=[BuildMember(view=view, bundle_sha256=bundle["sha256"])],
|
||||
files={key: len(value) for key, value in blobs.items()},
|
||||
)
|
||||
builds = ReplayBuilds(ReplayPublications(tmp_path / "worker"), tmp_path / "ffmpeg")
|
||||
app = FastAPI()
|
||||
app.include_router(build_replay_build_router(builds))
|
||||
return builds, TestClient(app), plan, blobs
|
||||
|
||||
|
||||
def test_transfer_is_verified_before_queue_and_identity_is_immutable(tmp_path):
|
||||
builds, client, plan, blobs = fixture(tmp_path)
|
||||
assert client.get(BUILD_PREFIX + "/" + RUN).status_code == 404
|
||||
assert client.post(BUILD_PREFIX, json=plan.model_dump()).status_code == 200
|
||||
prefix = BUILD_PREFIX + "/" + RUN
|
||||
assert client.post(prefix + "/commit").status_code == 409
|
||||
digest = next(iter(blobs))
|
||||
bad = b"x" * len(blobs[digest])
|
||||
assert client.put(prefix + "/inputs/" + digest, content=bad).status_code == 422
|
||||
assert not list(builds.root.glob("**/.upload-*"))
|
||||
for sha, value in blobs.items():
|
||||
assert client.put(prefix + "/inputs/" + sha, content=value).status_code == 200
|
||||
changed = plan.model_copy(update={"cache_key": "d" * 64})
|
||||
assert client.post(BUILD_PREFIX, json=changed.model_dump()).status_code == 409
|
||||
assert client.post(prefix + "/commit").json()["state"] == "queued"
|
||||
assert client.put(prefix + "/inputs/" + digest, content=blobs[digest]).status_code == 409
|
||||
|
||||
|
||||
def test_restart_finishes_publication_then_retires_only_owned_staging(tmp_path, monkeypatch):
|
||||
builds, client, plan, blobs = fixture(tmp_path)
|
||||
builds.register(plan)
|
||||
for sha, value in blobs.items():
|
||||
assert client.put(f"{BUILD_PREFIX}/{RUN}/inputs/{sha}", content=value).status_code == 200
|
||||
builds.commit(RUN)
|
||||
folder = builds.directory(RUN)
|
||||
_write(folder / "state.json", {"state": "building", "attempts": 1})
|
||||
crash_partial = folder / "inputs" / ".upload-abcdefgh"
|
||||
crash_partial.write_bytes(b"interrupted upload")
|
||||
protected = builds.root.parent / "protected.rrd"
|
||||
protected.write_bytes(b"existing working LAB")
|
||||
builds = ReplayBuilds(builds.publications, builds.ffmpeg)
|
||||
assert builds.status(RUN)["state"] == "queued"
|
||||
assert not crash_partial.exists()
|
||||
|
||||
def render(service, *args):
|
||||
service.cache.mkdir(parents=True)
|
||||
path = service.cache / "test.rrd"
|
||||
payload = b"RRF2complete"
|
||||
path.write_bytes(payload)
|
||||
return SimpleNamespace(
|
||||
path=path, byte_length=len(payload), sha256=hashlib.sha256(payload).hexdigest()
|
||||
)
|
||||
|
||||
monkeypatch.setattr(PortableReplayService, "prepare_composition", render)
|
||||
builds.run_once()
|
||||
assert builds.status(RUN)["state"] == "ready"
|
||||
manifest, recording = builds.publications.read(RUN, plan.base_sha256)
|
||||
assert recording.read_bytes() == b"RRF2complete"
|
||||
assert manifest.cache_key == plan.cache_key
|
||||
assert not (folder / "inputs").exists() and not (folder / "assembly").exists()
|
||||
assert protected.read_bytes() == b"existing working LAB"
|
||||
builds.run_once()
|
||||
assert builds.status(RUN)["attempts"] == 2
|
||||
assert builds.register(plan)["missing"] == []
|
||||
receipt = builds.publications.runtime / "state/data-ownership/receipts" / (RUN + ".json")
|
||||
_write(receipt, {"deleted": True})
|
||||
with pytest.raises(LabDeletionError):
|
||||
builds.register(plan)
|
||||
|
||||
|
||||
def test_assembly_failure_remains_retryable_without_model_execution(tmp_path, monkeypatch):
|
||||
builds, client, plan, blobs = fixture(tmp_path)
|
||||
builds.register(plan)
|
||||
for sha, value in blobs.items():
|
||||
client.put(f"{BUILD_PREFIX}/{RUN}/inputs/{sha}", content=value)
|
||||
builds.commit(RUN)
|
||||
monkeypatch.setattr(
|
||||
builds, "build", lambda _: (_ for _ in ()).throw(ValueError("codec failed"))
|
||||
)
|
||||
builds.run_once()
|
||||
assert builds.status(RUN)["state"] == "failed"
|
||||
assert not list(builds.publications.root.glob("**/manifest.json"))
|
||||
builds.run_once()
|
||||
assert builds.status(RUN)["attempts"] == 1
|
||||
assert builds.commit(RUN, retry=True)["state"] == "queued"
|
||||
assert len(list((builds.directory(RUN) / "inputs").iterdir())) == len(blobs)
|
||||
|
||||
|
||||
def test_new_run_never_falls_back_to_operator_assembly(tmp_path):
|
||||
builds, _, plan, _ = fixture(tmp_path)
|
||||
|
||||
def forbidden(_):
|
||||
pytest.fail("Operator source/render path must not execute")
|
||||
|
||||
service = PortableReplayService(
|
||||
view=SimpleNamespace(read=lambda _: plan.members[0].view),
|
||||
data_dir=tmp_path / "client",
|
||||
media=None,
|
||||
recording_source=forbidden,
|
||||
ffmpeg_path=tmp_path / "ffmpeg",
|
||||
worker_required=lambda _: True,
|
||||
remote_replays=SimpleNamespace(restore=lambda *args: None),
|
||||
)
|
||||
with pytest.raises(PortableReplayError, match="published by Worker"):
|
||||
service.prepare_composition(RUN, (RESULT,), plan.base_sha256)
|
||||
assert not service.cache.exists()
|
||||
Reference in New Issue
Block a user