Files
VoiceStudio/tests/test_worker_protocol_gen.py
T
velixio b7caa494eb feat(workers): remote downloads, audiobook chapters, and one port that stays honest
Five workstreams that finish the remote-GPU line, plus the test hole that
let a broken signature reach a commit.

**Downloads go through the normal path** (Phase 5). Rather than a second
remote-only route, the existing Models install flow became target-aware,
so a model landing on a worker uses the same code, the same progress
events and the same UI as a local one. Progress rows key on
(target, repo_id) — the aggregator keyed on bare repo_id, so the same
model downloading here and on a worker at once collapsed into one row
that told the user nothing true about either.

**Audiobooks render chapter by chapter on the worker** (Phase 8), with
per-chapter local fallback and ONE aggregated notice. The failure that
shape exists to prevent: a remote GPU that sleeps at chapter 40 of 200
must not turn a working book into 160 rows of PROGRESS_LEASE_EXPIRED.
Dictation is deliberately NOT ported — it runs ASR per utterance inside a
live WebSocket loop, and paying queue admission plus a round trip there
would spend the one thing that route is for.

**Dubbing stays local, and says so** (Phase 7). The coarse worker
operation is not finished, so the picker still reports dubbing as local
rather than showing a green remote chip over work this machine is doing.
What could not wait is the in-loop OOM retry: it sniffed the error string
and flushed the *local* CUDA cache, which under remote execution is the
wrong machine's GPU entirely. That is fixed now, before the path that
would have exercised it exists.

**Two instances can no longer share the control plane.** A second
VoiceStudio silently bound the same worker port and coexisted, so remote
workers landed on whichever process won the race — a session that
registers with one instance and appears dead to the other. This produced
hours of misdiagnosis during hardware testing and would hit any user with
the app open twice. The second instance now keeps running locally and
explains the conflict instead of quietly competing.

**And the hole that allowed all this to be missable.** gpu_gateway called
Scheduler.submit(pinned_worker_id=...) one commit before that parameter
existed. Every remote generation raised TypeError; 5236 tests passed
anyway, because nothing exercised the gateway against the real scheduler.
tests/test_gpu_gateway_scheduler_contract.py now runs that path for real
and binds every gateway→dependency call signature. Verified by renaming
the parameter away and watching both tests fail with the original error.

Gallery previews also fall back to a local render when a downloaded clip
cannot be decoded, rather than yielding silence.

Backend 5274 passed, frontend 1812 passed.

Not yet verified on hardware: Phases 4, 5, 6, 7, 8. Only the TTS path and
its artifact transport have been proven on a real GPU.
2026-08-11 16:53:33 +05:30

77 lines
2.8 KiB
Python

"""The committed protocol stubs must match the .proto they came from.
The stubs are committed so that neither the installer, the frozen build, nor
Docker needs ``protoc``. The cost of that convenience is drift: someone edits
``worker_v1.proto``, forgets to regenerate, and the mismatch surfaces later as
a baffling attribute error at runtime — or worse, as a field that silently
never arrives. Regenerating into a temporary directory and diffing turns that
into a red test with an obvious fix.
"""
from __future__ import annotations
import os
import sys
import pytest
_REPO = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
_GEN_DIR = os.path.join(_REPO, "backend", "worker", "protocol", "gen")
_GENERATED_FILES = ("worker_v1_pb2.py", "worker_v1_pb2_grpc.py", "worker_v1_pb2.pyi")
pytest.importorskip(
"grpc_tools",
reason="grpcio-tools is a dev dependency; the committed stubs are what ship.",
)
sys.path.insert(0, os.path.join(_REPO, "scripts"))
def _normalise(text: str) -> list[str]:
"""Ignore trailing whitespace and blank-line churn between protoc builds."""
return [line.rstrip() for line in text.splitlines() if line.strip()]
@pytest.mark.parametrize("filename", _GENERATED_FILES)
def test_committed_stubs_match_the_proto(tmp_path, filename):
import gen_worker_protocol
assert gen_worker_protocol.generate(tmp_path) == 0, "protoc failed"
fresh = (tmp_path / filename).read_text(encoding="utf-8")
with open(os.path.join(_GEN_DIR, filename), encoding="utf-8") as fh:
committed = fh.read()
assert _normalise(committed) == _normalise(fresh), (
f"{filename} is out of date with worker_v1.proto. "
"Run: uv run python scripts/gen_worker_protocol.py"
)
def test_generated_package_is_importable():
"""protoc emits a flat sibling import that only resolves if the output
directory happens to be on sys.path; the generator rewrites it."""
from worker.protocol.gen import worker_v1_pb2 as pb
from worker.protocol.gen import worker_v1_pb2_grpc as pb_grpc
assert hasattr(pb_grpc, "WorkerServiceStub")
assert pb.TaskRef(task_id="t").task_id == "t"
def test_download_progress_is_additive_and_frame_14_stays_reserved():
from worker.protocol.gen import worker_v1_pb2 as pb
field = pb.WorkerMessage.DESCRIPTOR.fields_by_name["download_progress"]
assert field.number == 13
source = open(
os.path.join(_REPO, "backend", "worker", "protocol", "worker_v1.proto"),
encoding="utf-8",
).read()
assert "reserved 14;" in source
def test_stub_import_is_relative():
with open(os.path.join(_GEN_DIR, "worker_v1_pb2_grpc.py"), encoding="utf-8") as fh:
source = fh.read()
assert "from . import worker_v1_pb2" in source
assert "\nimport worker_v1_pb2" not in source