325 lines
14 KiB
Python
325 lines
14 KiB
Python
"""仅操作员独立报告与本机原生成功任务共同开放新能力。"""
|
|
|
|
import json
|
|
import sqlite3
|
|
import wave
|
|
|
|
import pytest
|
|
from llm_services import video_profiles
|
|
from llm_services.store import Settings, initialize_store
|
|
|
|
|
|
def certification(tmp_path, pair=False):
|
|
settings = Settings(
|
|
tmp_path / "app",
|
|
tmp_path,
|
|
native_dialogue_enabled=True,
|
|
native_dialogue_tp2_enabled=True,
|
|
)
|
|
profile = (
|
|
"minimax-h3-fl2va-native-av-tp2-v1" if pair else "minimax-h3-fl2va-native-av-v1"
|
|
)
|
|
maximum, samples = (158, 210400) if pair else (345, 460000)
|
|
report = {
|
|
"profile": profile,
|
|
"revision": "12253298adb81e36757eabebb81d2ba1c0cc70cc",
|
|
"runtime_revision": "423ab90580b2847998591669b8326f47b6d14d8b"
|
|
if pair
|
|
else "f53d552036a0d1bd5570782a39cd40cfabf112bc",
|
|
"adapter_contract_version": "h3-fl2va-native-av-tp2-v1"
|
|
if pair
|
|
else "h3-fl2va-native-av-v1",
|
|
"input_modes": ["first_frame"] if pair else ["first_frame", "first_last_frame"],
|
|
"output_policy": "full_native",
|
|
"native_frame_counts": list(range(124, maximum + 1, 17)),
|
|
"fps": 24,
|
|
"canvases": [[1344, 768], [768, 1344]],
|
|
"upscale": False,
|
|
"audio_output": True,
|
|
"audio_sample_rate": 32000,
|
|
"audio_channels": 2,
|
|
"gpu_count": 2 if pair else 1,
|
|
"decision": "pass",
|
|
"audited": True,
|
|
"task_type": "video.generate",
|
|
"validation_job_ids": ["1" * 32, "2" * 32],
|
|
"checks": {
|
|
k: True
|
|
for k in (
|
|
"native_audio",
|
|
"tail_preserved",
|
|
"av_sync",
|
|
"quality",
|
|
"memory",
|
|
"cancel",
|
|
"preemption",
|
|
"gpu_group",
|
|
)
|
|
},
|
|
}
|
|
initialize_store(settings)
|
|
with sqlite3.connect(settings.db_path) as db:
|
|
for i, job_id in enumerate(report["validation_job_ids"]):
|
|
output = settings.outputs_dir / job_id
|
|
output.mkdir(parents=True)
|
|
video, audio = output / "video_raw.mp4", output / "audio_native.wav"
|
|
video.write_bytes(b"video fixture")
|
|
with wave.open(str(audio), "wb") as wav:
|
|
wav.setnchannels(2)
|
|
wav.setsampwidth(2)
|
|
wav.setframerate(32000)
|
|
wav.writeframes(bytes(samples * 4))
|
|
params = {
|
|
"profile": profile,
|
|
"num_frames": maximum,
|
|
"fps": 24,
|
|
"width": 1344 if i == 0 else 768,
|
|
"height": 768 if i == 0 else 1344,
|
|
"conditioning_mode": "first_frame"
|
|
if pair or i == 0
|
|
else "first_last_frame",
|
|
}
|
|
metadata = {
|
|
"profile": profile,
|
|
"revision": report["revision"],
|
|
"diffusers_source_revision": report["runtime_revision"],
|
|
"native_frames": maximum,
|
|
"requested_frames": maximum,
|
|
"native_av": {
|
|
"output_policy": "full_native",
|
|
"audio_trimmed": False,
|
|
"sample_rate": 32000,
|
|
"channels": 2,
|
|
"audio_samples": samples,
|
|
},
|
|
}
|
|
db.execute(
|
|
"INSERT INTO jobs(id,task_type,state,result_json,parameters_json,"
|
|
"input_json,timeout_seconds,request_hash,created_at,updated_at) "
|
|
"VALUES(?,?,?,?,?,'{}',3600,'fixture',0,0)",
|
|
(
|
|
job_id,
|
|
"video.generate",
|
|
"succeeded",
|
|
json.dumps(
|
|
{
|
|
"outputs": [
|
|
{"path": str(video)},
|
|
{"path": str(audio)},
|
|
],
|
|
"metadata": metadata,
|
|
}
|
|
),
|
|
json.dumps(params),
|
|
),
|
|
)
|
|
path = (
|
|
settings.app_root
|
|
/ "config"
|
|
/ ("native-av-tp2-profile.json" if pair else "native-av-profile.json")
|
|
)
|
|
path.parent.mkdir(exist_ok=True)
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
return settings, path, report, "native_av_tp2" if pair else "native_av"
|
|
|
|
|
|
@pytest.mark.parametrize("pair", [False, True])
|
|
def test_native_report_requires_matching_successful_native_jobs(tmp_path, pair):
|
|
settings, _, _, key = certification(tmp_path, pair)
|
|
cap = video_profiles.capabilities(settings)[key]
|
|
assert (
|
|
cap["available"] is True
|
|
and cap["audited"] is True
|
|
and cap["decision"] == "pass"
|
|
)
|
|
assert len(cap["report_sha256"]) == 64
|
|
with sqlite3.connect(settings.db_path) as db:
|
|
db.execute("UPDATE jobs SET state='failed'")
|
|
assert video_profiles.capabilities(settings)[key]["available"] is False
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"field,value",
|
|
[
|
|
("profile", "minimax-h3-fl2va-bf16-tp2-v1"),
|
|
("runtime_revision", "0" * 40),
|
|
("audio_output", False),
|
|
("audited", False),
|
|
],
|
|
)
|
|
def test_native_report_cannot_reuse_silent_or_stale_evidence(tmp_path, field, value):
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report[field] = value
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
assert video_profiles.capabilities(settings)[key]["available"] is False
|
|
|
|
|
|
def test_native_report_loses_readiness_when_its_archived_audio_disappears(tmp_path):
|
|
settings, _, report, key = certification(tmp_path)
|
|
assert video_profiles.capabilities(settings)[key]["available"] is True
|
|
(
|
|
settings.outputs_dir / report["validation_job_ids"][0] / "audio_native.wav"
|
|
).unlink()
|
|
assert video_profiles.capabilities(settings)[key]["available"] is False
|
|
|
|
|
|
@pytest.mark.parametrize("canvas,index", [([1344, 768], 0), ([768, 1344], 1)])
|
|
def test_tp2_can_certify_one_canvas_with_one_full_capacity_job(tmp_path, canvas, index):
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report["canvases"] = [canvas]
|
|
report["validation_job_ids"] = [report["validation_job_ids"][index]]
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
cap = video_profiles.capabilities(settings)[key]
|
|
assert cap["available"] is True
|
|
assert cap["canvases"] == [canvas]
|
|
assert cap["native_frame_counts"] == [124, 141, 158]
|
|
assert cap["audited"] is True
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
"canvases",
|
|
[None, [], [[1344, 768], [1344, 768]], [[768, 1344], [1344, 768]],
|
|
[[True, 768]], [[1344, False]], [[1344.0, 768]], [[1344, 768.0]],
|
|
[[1344, 1344]], [[1344]], [1344, 768], "1344x768"],
|
|
)
|
|
def test_native_certification_rejects_invalid_canvas_declarations(tmp_path, canvases):
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report["canvases"] = canvases
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
cap = video_profiles.capabilities(settings)[key]
|
|
assert cap["available"] is False
|
|
assert cap["canvases"] == []
|
|
|
|
|
|
def test_native_certification_requires_canvas_declaration_and_hides_disabled_geometry(tmp_path):
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
del report["canvases"]
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
assert video_profiles.capabilities(settings)[key]["canvases"] == []
|
|
settings.native_dialogue_tp2_enabled = False
|
|
assert video_profiles.capabilities(settings)[key]["canvases"] == []
|
|
|
|
|
|
@pytest.mark.parametrize("change", ["extra_canvas", "missing_canvas", "wrong_mode", "short_job", "cancel", "preemption", "memory", "gpu_group"])
|
|
def test_subset_certification_keeps_exact_coverage_and_real_safety_checks(tmp_path, change):
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report["canvases"] = [[1344, 768]]
|
|
if change != "extra_canvas":
|
|
report["validation_job_ids"] = [report["validation_job_ids"][0]]
|
|
if change == "missing_canvas":
|
|
report["canvases"] = [[1344, 768], [768, 1344]]
|
|
elif change in {"wrong_mode", "short_job"}:
|
|
with sqlite3.connect(settings.db_path) as db:
|
|
job_id = report["validation_job_ids"][0]
|
|
params = json.loads(db.execute("SELECT parameters_json FROM jobs WHERE id=?", (job_id,)).fetchone()[0])
|
|
params["conditioning_mode" if change == "wrong_mode" else "num_frames"] = "first_last_frame" if change == "wrong_mode" else 124
|
|
db.execute("UPDATE jobs SET parameters_json=? WHERE id=?", (json.dumps(params), job_id))
|
|
elif change in {"cancel", "preemption", "memory", "gpu_group"}:
|
|
report["checks"][change] = False
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
cap = video_profiles.capabilities(settings)[key]
|
|
assert cap["available"] is False
|
|
assert cap["canvases"] == []
|
|
|
|
|
|
def test_canvas_certificate_limits_http_admission_and_worker_rechecks(tmp_path):
|
|
import threading
|
|
from types import SimpleNamespace
|
|
|
|
from fastapi.testclient import TestClient
|
|
from llm_services.api import create_app
|
|
from llm_services.worker import Worker
|
|
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report["canvases"] = [[1344, 768]]
|
|
report["validation_job_ids"] = [report["validation_job_ids"][0]]
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
settings.key_file.write_text("native-canvas-test-secret-123456789", encoding="utf-8")
|
|
source = tmp_path / "first.png"
|
|
source.write_bytes(b"first frame fixture")
|
|
body = {
|
|
"task_type": "video.generate",
|
|
"input": {"prompt": "A woman says hello.", "image_path": str(source)},
|
|
"parameters": {"profile": report["profile"], "num_frames": 158},
|
|
"scheduling": {"principal_id": "a" * 32, "execution_mode": "interactive"},
|
|
}
|
|
headers = {"Authorization": "Bearer native-canvas-test-secret-123456789"}
|
|
app = create_app(settings)
|
|
with TestClient(app) as client:
|
|
cap = client.get("/capabilities", headers=headers).json()["video_generation"][key]
|
|
assert cap["available"] is True and cap["canvases"] == [[1344, 768]]
|
|
# Omitted geometry uses the same landscape defaults as normalization,
|
|
# while the stored request stays byte-for-byte idempotency compatible.
|
|
created = client.post("/jobs", json=body, headers=headers)
|
|
assert created.status_code == 202, created.text
|
|
job = app.state.store.get(created.json()["id"])
|
|
assert "width" not in job["parameters"]
|
|
holder = SimpleNamespace(settings=settings, store=app.state.store,
|
|
_active_lock=threading.RLock(), _blocked_jobs={})
|
|
assert Worker._reference_admission_reason(holder, job) is None
|
|
|
|
portrait = {**body, "parameters": {**body["parameters"], "width": 768, "height": 1344}}
|
|
denied = client.post("/jobs", json=portrait, headers=headers)
|
|
assert denied.status_code == 422, denied.text
|
|
# An already queued task must not bypass narrowed/revoked certification.
|
|
job["parameters"].update(width=768, height=1344)
|
|
assert Worker._reference_admission_reason(holder, job)
|
|
assert job["id"] in holder._blocked_jobs
|
|
job["parameters"].pop("width")
|
|
job["parameters"].pop("height")
|
|
settings.native_dialogue_tp2_enabled = False
|
|
assert Worker._reference_admission_reason(holder, job)
|
|
|
|
|
|
def test_private_portrait_ticket_does_not_expand_landscape_public_capability(tmp_path):
|
|
import hashlib
|
|
import threading
|
|
import time
|
|
from types import SimpleNamespace
|
|
|
|
from fastapi.testclient import TestClient
|
|
from llm_services import native_av_calibration
|
|
from llm_services.api import create_app
|
|
from llm_services.worker import Worker
|
|
|
|
settings, path, report, key = certification(tmp_path, True)
|
|
report["canvases"] = [[1344, 768]]
|
|
report["validation_job_ids"] = [report["validation_job_ids"][0]]
|
|
path.write_text(json.dumps(report), encoding="utf-8")
|
|
settings.key_file.write_text("native-canvas-test-secret-123456789", encoding="utf-8")
|
|
source = tmp_path / "first.png"
|
|
source.write_bytes(b"private calibration first frame")
|
|
owner, idem = "a" * 32, "cmvr-visual:" + "b" * 32
|
|
inputs = {"prompt": "A woman says hello.", "image_path": str(source)}
|
|
params = {"profile": report["profile"], "num_frames": 158, "width": 768, "height": 1344}
|
|
body = {"task_type": "video.generate", "input": inputs, "parameters": params,
|
|
"scheduling": {"principal_id": owner, "execution_mode": "interactive"}}
|
|
headers = {"Authorization": "Bearer native-canvas-test-secret-123456789",
|
|
"Idempotency-Key": idem}
|
|
digest = native_av_calibration.request_digest("video.generate", inputs, params, owner, idem)
|
|
receipt = settings.app_root / "config" / "native-av-validation" / "calibration" / (digest + ".json")
|
|
app = create_app(settings)
|
|
with TestClient(app) as client:
|
|
assert client.post("/jobs", json=body, headers=headers).status_code == 422
|
|
receipt.parent.mkdir(parents=True)
|
|
receipt.write_text(json.dumps({
|
|
"schema_version": 1, "request_sha256": digest, "task_type": "video.generate",
|
|
"profile": report["profile"], "owner_id": owner, "idempotency_key": idem,
|
|
"source_sha256": {"image_path": hashlib.sha256(source.read_bytes()).hexdigest()},
|
|
"issued_at": time.time() - 10, "expires_at": time.time() + 900, "max_uses": 1,
|
|
}), encoding="utf-8")
|
|
receipt.chmod(0o600)
|
|
created = client.post("/jobs", json=body, headers=headers)
|
|
assert created.status_code == 202, created.text
|
|
assert client.post("/jobs", json=body, headers=headers).json()["id"] == created.json()["id"]
|
|
assert client.post("/jobs", json=body,
|
|
headers={"Authorization": headers["Authorization"]}).status_code == 422
|
|
holder = SimpleNamespace(settings=settings, store=app.state.store,
|
|
_active_lock=threading.RLock(), _blocked_jobs={})
|
|
job = app.state.store.get(created.json()["id"])
|
|
assert Worker._reference_admission_reason(holder, job) is None
|
|
assert client.get("/capabilities", headers=headers).json()["video_generation"][key]["canvases"] == [[1344, 768]]
|
|
source.write_bytes(b"changed while private portrait job waits")
|
|
assert Worker._reference_admission_reason(holder, job)
|
|
assert video_profiles.capabilities(settings)[key]["available"] is True
|