HarnessRouter Community Edition: the self-hosted, Apache-2.0 edition of the unified interface for agent harnesses. Run Codex, Claude Code, Hermes, PI, DSH, and more through one API, with sessions, streaming, files, cancellation, and failure handling. Implements the Unified Harness Protocol (UHP), an open standard. Your keys, your infrastructure.
Python agent-apiagent-frameworkagent-harnessai-agentsai-infrastructurecoding-agentconformance-testingdeepseek-harnessharness-engineeringhermesllmopen-protocolopen-sourceopen-standardpi-coding-agentself-hosteduhpunified-apiunified-harness-protocolunified-interface
fix(media): a sound nobody could measure, a floor nobody recorded, and streamed speech cut to its first delta
richard-epsillae20d27d17a22
diff --git a/gateway/media_catalog.json b/gateway/media_catalog.jsonindex c6112c71..695ce62a 100644--- a/gateway/media_catalog.json+++ b/gateway/media_catalog.json@@ -836,8 +836,9 @@"format": "mp3","output_mime": "audio/mpeg","latency_s": 5.59,+ "duration_min_s": 3,"note": "The endpoint takes NO model id \u2014 `elevenlabs/music-v1` is our name for the candidate, which is what the chain reports, quarantines and prices on. Length is `music_length_ms`, in MILLISECONDS, where every other duration in this file is seconds.",- "measured": "e2e run 2026-08-15 against the live account, through the ordinary chain: the earlier 402 paid_plan_required is gone (tier `payg`) and POST /v1/music answered 200 with 160541 raw bytes of audio/mpeg \u2014 a 10.031 s mp3 in 5.588 s, for music_length_ms 10000. It HONOURS the length asked for; 10.031 against 10.000 is the encoder's frame alignment, not a model ignoring an argument, so no duration_ignored here. Cost 69 characters of the 64945 budget (provider's own /v1/usage/character-stats, breakdown_type=product_type). Only a 10 s render was measured, so no floor and no ceiling are recorded \u2014 a duration nobody asked for is worse than no duration, and this endpoint bills. No `usd`: this account is metered in characters and a dollar figure would be one somebody invented.",+ "measured": "e2e run 2026-08-15 against the live account, through the ordinary chain: the earlier 402 paid_plan_required is gone (tier `payg`) and POST /v1/music answered 200 with 160541 raw bytes of audio/mpeg \u2014 a 10.031 s mp3 in 5.588 s, for music_length_ms 10000. It HONOURS the length asked for; 10.031 against 10.000 is the encoder's frame alignment, not a model ignoring an argument, so no duration_ignored here. Cost 69 characters of the 64945 budget (provider's own /v1/usage/character-stats, breakdown_type=product_type). The FLOOR is measured too, and by the provider itself: music_length_ms 1000 answers HTTP 422 `Input should be greater than or equal to 3000`, so duration_min_s is 3 and a shorter ask is refused here rather than paid for there. No ceiling is recorded \u2014 nobody has asked for one, and a duration nobody measured is worse than no duration on an endpoint that bills. No `usd`: this account is metered in characters and a dollar figure would be one somebody invented.","verification": "verified"}]diff --git a/gateway/media_plane.py b/gateway/media_plane.pyindex a54a15ea..f7f5957f 100644--- a/gateway/media_plane.py+++ b/gateway/media_plane.py@@ -1165,8 +1165,15 @@ def _read_stream_audio(cand: dict, chunks: list[str]) -> Payload:transcript.append(str(aud["transcript"]))if not b64_parts:raise MediaEmpty("the provider streamed no audio")+ # EACH DELTA IS ITS OWN BASE64, so each is decoded on its own and the BYTES are joined.+ # Joining the strings and decoding once — which is what this did — corrupts everything after+ # the first delta whose byte count is not a multiple of three, because that one ends in+ # padding and padding does not belong in the middle. Streams arrive in whatever sizes the+ # provider feels like, so that is the ordinary case, not the corner. It went unnoticed+ # because a sound was never checked for a duration: the wreckage still began with a RIFF+ # header, so it sniffed as audio and was called a success.fmt = str(cand.get("format") or "wav")- return Payload(data=_b64("".join(b64_parts)), mime=f"audio/{fmt}",+ return Payload(data=b"".join(_b64(p) for p in b64_parts), mime=f"audio/{fmt}",transcript="".join(transcript))@@ -1358,6 +1365,23 @@ def provider_message(doc, status: int) -> str:for k2 in ("message", "detail", "msg"):if isinstance(v.get(k2), str) and v[k2].strip():return scrub(str(v[k2]).strip())+ # A LIST IS WHAT A VALIDATION ERROR LOOKS LIKE. Pydantic v2 — so FastAPI, so a large+ # share of providers — reports `detail` as a list of failures, each naming the field+ # and the bound it broke. Read as a str or a dict, all of it was dropped, and the one+ # sentence that says what to do instead ("must be >= 3000") never reached the agent.+ if isinstance(v, list):+ said = []+ for item in v[:3]:+ if isinstance(item, str) and item.strip():+ said.append(item.strip())+ elif isinstance(item, dict):+ msg = str(item.get("msg") or item.get("message") or "").strip()+ where = ".".join(str(x) for x in (item.get("loc") or [])+ if not isinstance(x, int))+ if msg:+ said.append(f"{where}: {msg}" if where else msg)+ if said:+ return scrub("; ".join(said))if isinstance(doc.get("code"), str) and doc["code"] and doc["code"] != "success":return scrub(str(doc["code"]))return f"the provider answered HTTP {status}"@@ -1436,9 +1460,14 @@ def verify(kind: str, data: bytes) -> dict:out = {"mime": mime, "ext": ext, "bytes": len(data), "width": 0, "height": 0, "seconds": 0.0}if want in ("video", "audio"):probed = probe(data, ext)- if want == "video" and not probed.get("seconds"):- # A video whose duration cannot be read cannot be cut, timed or summed. Refusing is- # the honest answer; a zero-length shot in a timeline is not.+ if not probed.get("seconds"):+ # A file whose duration cannot be read cannot be cut, timed or summed. Refusing is the+ # honest answer; a zero-length shot in a timeline is not.+ #+ # THIS GUARDED VIDEO ONLY, AND EVERY WORD OF IT IS AS TRUE OF SOUND: a bed lives on+ # the same timeline, is summed by timeline_total and cut by assemble. Twenty-three+ # bytes labelled audio/mpeg came back "succeeded", showed as ready on the canvas, and+ # carried a null duration into the arithmetic.if have_ffmpeg():raise MediaEmpty("the file came back unreadable — no duration in it")out.update({k: v for k, v in probed.items() if v})diff --git a/gateway/tests/test_media_mcp.py b/gateway/tests/test_media_mcp.pyindex 14e3142e..6be0a476 100644--- a/gateway/tests/test_media_mcp.py+++ b/gateway/tests/test_media_mcp.py@@ -101,12 +101,46 @@ _all_calls: list[dict] = []# ── tiny real media, so verification is exercised on bytes and not on a stub ──────PNG_1PX = base64.b64decode("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==")-WAV_SILENCE = (b"RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00"- b"\x80>\x00\x00\x02\x00\x10\x00data\x00\x00\x00\x00")-# One MPEG-1 Layer III frame header and its payload. ElevenLabs answers with mp3 BYTES and no JSON-# at all, so what the raw shapes are fed here has to be bytes that `sniff` genuinely identifies —-# a JSON stub would let the raw path pass a test the product fails on the first real call.-MP3_SILENCE = b"\xff\xfb\x90\x00" + bytes(414)+def _wav(seconds: float = 1.0) -> bytes:+ """A real wav with real samples in it. The header alone declared a data chunk of length ZERO+ — a file of no duration, which is what the product now refuses, and a speech candidate that+ returned one was failing the whole chain down to the next model."""+ with tempfile.TemporaryDirectory() as d:+ out = os.path.join(d, "a.wav")+ try:+ subprocess.run(["ffmpeg", "-nostdin", "-y", "-f", "lavfi",+ "-i", f"sine=frequency=330:duration={seconds}", out],+ capture_output=True, check=True)+ except (OSError, subprocess.CalledProcessError):+ return (b"RIFF$\x00\x00\x00WAVEfmt \x10\x00\x00\x00\x01\x00\x01\x00\x40\x1f\x00\x00"+ b"\x80>\x00\x00\x02\x00\x10\x00data\x00\x00\x00\x00")+ return Path(out).read_bytes()+++WAV_SILENCE = _wav()+def _mp3(seconds: float = 2.0) -> bytes:+ """A real, tiny mp3 — one ffprobe can read a DURATION out of.++ It used to be a single frame header and 414 zero bytes: enough for `sniff` to call it audio,+ and not enough to be a sound. The product now refuses a file whose length cannot be read (a+ bed goes onto a timeline that sums durations), so a stub here would be a fixture asserting+ the opposite of what ships. ElevenLabs answers with mp3 BYTES and no JSON at all, so what the+ raw shapes are fed has to be bytes that behave like the real thing all the way down.+ """+ with tempfile.TemporaryDirectory() as d:+ out = os.path.join(d, "a.mp3")+ try:+ subprocess.run(["ffmpeg", "-nostdin", "-y", "-f", "lavfi",+ "-i", f"sine=frequency=440:duration={seconds}",+ "-c:a", "libmp3lame", "-b:a", "64k", out],+ capture_output=True, check=True)+ except (OSError, subprocess.CalledProcessError):+ # No ffmpeg: the duration check does not run either, so the old stub is honest here.+ return b"\xff\xfb\x90\x00" + bytes(414)+ return Path(out).read_bytes()+++MP3_SILENCE = _mp3()def _mp4(seconds: float = 1.0, size: str = "128x72") -> bytes:@@ -3077,3 +3111,78 @@ def test_a_preference_naming_an_unknown_model_orders_nothing():assert [c["model"] for c in media_plane.capability("text_to_video")["candidates"]] == beforefinally:media_plane.set_policy({})+++# ── what a provider said, and what came back ──────────────────────────────────────+# Both of these were measured against ElevenLabs and both are the same failure in the end: the+# product knew something was wrong and said nothing useful about it.++def test_a_validation_error_reaches_the_agent_in_the_providers_own_words():+ """Pydantic v2 — so FastAPI, so a large share of providers — reports `detail` as a LIST.++ Read as a str or a dict, all of it was dropped and the agent got "the provider answered HTTP+ 422". The one sentence that says what to do instead ("greater than or equal to 3000") was+ right there in the body.+ """+ doc = {"detail": [{"type": "greater_than_equal",+ "loc": ["body", "music_length_ms"],+ "msg": "Input should be greater than or equal to 3000",+ "input": 1000, "ctx": {"ge": 3000}}]}+ said = media_plane.provider_message(doc, 422)+ assert "greater than or equal to 3000" in said, said+ assert "music_length_ms" in said, said # and WHICH field it was about+ assert "HTTP 422" not in said, said # not our paraphrase over the top of it+++def test_a_music_ask_below_the_floor_is_refused_before_it_is_spent():+ """The floor is the provider's own 422, recorded as measurement. Asking for a one-second+ sting — the obvious thing to ask a music tool for — used to reach the provider, fail, and+ teach the agent nothing."""+ cand = next(c for c in media_plane.capability("text_to_music").get("candidates") or []+ if c.get("model") == "elevenlabs/music-v1")+ assert cand.get("duration_min_s") == 3, cand.get("duration_min_s")+ why = media_plane.can_serve(cand, {"seconds": 1})+ assert why and "shortest is 3" in why, why+ assert not media_plane.can_serve(cand, {"seconds": 10}), "a 10 s ask was refused"+++@needs_ffmpeg+def test_an_unreadable_sound_is_refused_exactly_like_an_unreadable_shot():+ """23 bytes labelled audio/mpeg used to come back "succeeded", show as ready on the canvas,+ and carry a null duration into a timeline that sums durations.++ The rule was written for video and every word of it is as true of sound: a bed lives on the+ same timeline, is summed by timeline_total and cut by assemble.+ """+ junk = b"\xff\xfb\x90\x00" + bytes(19)+ with pytest.raises(media_plane.MediaEmpty) as e:+ media_plane.verify("audio", junk)+ assert "no duration" in str(e.value), str(e.value)+ # A REAL mp3 still passes, so this is a duration check and not a size check — without this+ # half the test would be satisfied by refusing all sound.+ with tempfile.TemporaryDirectory() as d:+ mp3 = os.path.join(d, "a.mp3")+ subprocess.run(["ffmpeg", "-nostdin", "-y", "-f", "lavfi",+ "-i", "sine=frequency=440:duration=2", "-c:a", "libmp3lame", mp3],+ capture_output=True, check=True)+ ok = media_plane.verify("audio", Path(mp3).read_bytes())+ assert float(ok["seconds"]) > 1.5, ok+++def test_streamed_audio_deltas_are_decoded_one_by_one_and_joined_as_bytes():+ """Each delta carries its own complete base64; the BYTES are what get joined.++ Joining the strings and decoding once corrupts everything after the first delta whose byte+ count is not a multiple of three — that one ends in padding, and padding does not belong in+ the middle of a stream. Providers send whatever sizes they like, so that is the ordinary+ case. It survived because a sound was never checked for a duration: the wreckage still began+ with a RIFF header, so it sniffed as audio and was called a success.+ """+ want = bytes(range(256)) * 7 # 1792 bytes: not a multiple of 3+ cuts = [want[:22], want[22:1000], want[1000:]] # 22 % 3 == 1, 978 % 3 == 0, and a tail+ frames = ['data: ' + json.dumps({"choices": [{"delta": {"audio": {+ "data": base64.b64encode(c).decode(), "transcript": t}}}]})+ for c, t in zip(cuts, ("one ", "two ", "three"))] + ["data: [DONE]"]+ got = media_plane._read_stream_audio({"format": "wav"}, frames)+ assert got.data == want, (len(got.data), len(want))+ assert got.transcript == "one two three"