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 generation that delivered nothing still bought renders
richard-epsillab4d0d93c1ef7
diff --git a/gateway/app.py b/gateway/app.pyindex 5f1af44b..5ccb195d 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -26,9 +26,11 @@ import base64import contextlibimport functoolsimport hashlib+import ipaddressimport mimetypesimport pathlibimport shutil+import socketimport subprocessimport tarfileimport tempfile@@ -2398,7 +2400,7 @@ async def _harness_plugins(harness_id: str, org: str, hdr_vals: dict[str, str] |# Same rule the console's Test connection applies. Enforced here too, because this is the# call that actually reaches the network: a URL the policy rejects must never be handed to# a sandbox, whatever is stored on the harness.- _blocked = _ssrf_check(_url)+ _blocked = await _ssrf_check(_url)if _blocked:print(f"[mcp] refusing '{s.get('name') or 'mcp'}' for harness {harness_id}: {_blocked}",flush=True)@@ -6611,13 +6613,13 @@ def _parse_jsonrpc(text: str) -> dict:return {}-def _ssrf_check(url: str) -> str | None:+async def _ssrf_check(url: str) -> str | None:"""The ONE rule for whether this server may talk to an MCP endpoint.V1C02-006: gate a caller-supplied MCP URL before the server fetches it. HTTPS only; resolve- EVERY DNS answer and reject loopback / link-local / RFC1918 / IPv6 private / cloud metadata- (169.254.169.254 falls under link-local) so a hostname can't rebind to an internal target.- Returns an error string to reject, or None to allow.+ EVERY DNS answer and reject loopback / link-local / CGNAT / RFC1918 / IPv6 private / cloud+ metadata (169.254.169.254 falls under link-local). Returns an error string to reject, or None+ to allow.Applied at BOTH config time (the console's Test connection) and run time (_harness_plugins).It used to run only on the test button, so the console refused a URL that a turn then connected@@ -6649,15 +6651,30 @@ def _ssrf_check(url: str) -> str | None:return "only https MCP endpoints are allowed"if not u.hostname:return "url has no host"- return _internal_target(u.hostname, u.port or 443)+ return await _internal_target(u.hostname, u.port or 443)-def _internal_target(host: str, port: int) -> str | None:+# 100.64.0.0/10 — carrier-grade NAT, and what a managed Kubernetes cluster hands its pods and its+# nodes. Python's ipaddress does not call it private (it is not RFC1918), so it was the one whole+# range this classifier let through: a provider naming a pod address had this server fetching from+# inside its own cluster. Named here rather than folded into `not ip.is_global`, because that would+# also reject 192.0.0.0/24 and the documentation ranges and nobody would know why.+_CGNAT = (ipaddress.ip_network("100.64.0.0/10"), ipaddress.ip_network("::ffff:100.64.0.0/106"))+++async def _internal_target(host: str, port: int) -> str | None:"""Does this hostname resolve somewhere a caller must not be able to point this server?- Every DNS answer is resolved and classified — loopback, link-local (which is where cloud- metadata lives), RFC1918, IPv6 private, reserved — so a name cannot rebind to an internal- target between the check and the connection.+ EVERY DNS answer is resolved and classified — loopback, link-local (which is where cloud+ metadata lives), RFC1918, CGNAT, IPv6 private, reserved — so a name whose second A record is+ internal is refused on the strength of that second record.++ WHAT IT DOES NOT DO, because a comment claiming otherwise is worse than no comment: it does not+ survive a DNS rebind. This resolves the name; httpx then resolves it AGAIN when it opens the+ socket, so a TTL-0 name that answers 93.184.216.34 here can answer 10.0.0.5 a millisecond+ later and this check will have passed. Closing that means pinning the address we classified+ into the connection itself, which is a different piece of work and is not what this does. What+ it buys is the multi-A case and every literal internal address, and that is the whole of it.Shared by MCP URLs, database hosts and provider-supplied file URLs because it is one question,not three: each is a target somebody else names and this server then opens a socket to. A@@ -6673,11 +6690,15 @@ def _internal_target(host: str, port: int) -> str | None:The two failures are still told apart, because the operator reading the log needs them apart:one of them is a bad URL and the other is a broken resolver.++ ASYNC BECAUSE getaddrinfo BLOCKS. It is a synchronous C call and it can sit for the resolver's+ whole timeout; run on the event loop it stops the entire process, not one request. Measured: a+ 1.01 s lookup let ZERO other tasks run. Every provider file URL is classified on every poll, so+ one relay naming a slow-resolving host froze the gateway for everybody, ten seconds apart."""- import ipaddress- import socket+ loop = asyncio.get_running_loop()try:- infos = socket.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)+ infos = await loop.getaddrinfo(host, port, proto=socket.IPPROTO_TCP)except socket.gaierror as e:no_such = {socket.EAI_NONAME, getattr(socket, "EAI_NODATA", socket.EAI_NONAME)}return "host does not resolve" if e.errno in no_such else "host could not be resolved"@@ -6686,7 +6707,8 @@ def _internal_target(host: str, port: int) -> str | None:for info in infos:ip = ipaddress.ip_address(info[4][0])if (ip.is_private or ip.is_loopback or ip.is_link_local or ip.is_reserved- or ip.is_multicast or ip.is_unspecified):+ or ip.is_multicast or ip.is_unspecified+ or any(ip in net for net in _CGNAT if ip.version == net.version)):return "target resolves to a disallowed (internal) address"return None@@ -6695,7 +6717,7 @@ async def _mcp_list_tools(url: str, token: str) -> dict:"""Probe a remote MCP server: initialize → notifications/initialized → tools/list. Returns{ok, server, tools:[{name,description}]} or {ok:false, error}. Used by the config UI's'Test connection'. Token may be a literal bearer or a vault:<ref> (resolved by the caller)."""- ssrf = _ssrf_check(url)+ ssrf = await _ssrf_check(url)if ssrf:return {"ok": False, "error": ssrf}headers = {"content-type": "application/json", "accept": "application/json, text/event-stream"}@@ -7141,11 +7163,12 @@ class DatabaseTestBody(BaseModel):connection_string: str-def _db_validate(engine: str, conn: str) -> tuple[str, str, str, str]:+async def _db_validate(engine: str, conn: str) -> tuple[str, str, str, str]:"""(engine, connection string, host, database) — or the 400 that says what is wrong with it.- Pure, so kit launch can check what it was given BEFORE it provisions anything: a typo in a- connection string should not leave a half-configured Harness behind."""+ Provisions nothing, so kit launch can check what it was given BEFORE it provisions anything: a+ typo in a connection string should not leave a half-configured Harness behind. It is not free,+ though: classifying the host is a DNS lookup, which is why it is awaited."""eng = _db_engine(engine)c = (conn or "").strip()if not c:@@ -7158,8 +7181,8 @@ def _db_validate(engine: str, conn: str) -> tuple[str, str, str, str]:# database on localhost is the normal case rather than an attack.if not _pool_is_local():hostname, _, port = host.partition(":")- blocked = _internal_target(hostname, int(port) if port.isdigit() else- (5432 if eng == "postgres" else 3306))+ blocked = await _internal_target(hostname, int(port) if port.isdigit() else+ (5432 if eng == "postgres" else 3306))if blocked:raise uhp_error(400, "unreachable_host",f"This server cannot connect to {hostname}: {blocked}.",@@ -7215,7 +7238,7 @@ async def _db_probe(engine: str, conn: str) -> dict:"""# Validated exactly as attaching would validate it, so a string this button accepts is a# string the save accepts. A test that does not predict what happens next is worse than none.- engine, conn, host, db = _db_validate(engine, conn)+ engine, conn, host, db = await _db_validate(engine, conn)try:# Not SELECT 1: reaching the database proves the credential, and reading the catalog# proves the account can see tables — which is the failure people actually hit when they@@ -7596,9 +7619,9 @@ class _MediaNoModel(Exception):because "nothing was charged" is the one sentence an agent must never be told wrongly."""- def __init__(self, text: str, attempts: list[dict]):+ def __init__(self, text: str, attempts: list[dict], billed_usd: float = 0.0):super().__init__(text)- self.text, self.attempts = text, attempts+ self.text, self.attempts, self.billed_usd = text, attempts, billed_usddef _media_json(body: bytes):@@ -7646,15 +7669,111 @@ async def _media_call(cand: dict, prov: dict, params: dict) -> tuple[str, object# Every advance is another full-price render, so a second one is the agent's decision, made with# the first one's cost in front of it.#-# ONE BUDGET, TWO WALKS. A request can fall down the chain in two places — here at SUBMIT, when a+# ONE BUDGET, TWO WALKS. A request can fall down the chain in two places — at SUBMIT, when a# candidate answers 200 with nothing in it, and later at POLL, when a render that billed comes-# back failed — and they are the same event to whoever is paying. The submit walk was uncapped-# while the poll walk was capped, so one generate_image whose candidates all answered-# 200-with-no-image bought the entire eleven-model chain inside a single tool call. What the-# submit walk spent is therefore written onto the job (`advances`), which is the only thing that-# outlives the tool call and the only place the poll walk can read it from.+# back failed — and they are the same event to whoever is paying. What the submit walk spent is+# therefore written onto the job (`advances`), which is the only thing that outlives the tool call+# and the only place the poll walk can read it from._MEDIA_MAX_ADVANCES = 1+# How many outbound submits ONE SUBMIT WALK may make, whatever the provider answers to any of+# them. Separate from the budget above because it is a different fact: that one is about money and+# stops at the second render bought; this one is about the walk itself. A candidate that answers+# 4xx costs nothing and IS walked past — deliberately, because leaving a broken model in the chain+# is what let the owner's preferred one start working again on its own — but "costs nothing" is+# not "free": each submit carries the provider key out of this process, and a chain eleven long+# meant one tool call could make eleven of them. Four is the measured outage (one broken model at+# rank 1) with room, and a long way short of any chain in the catalog.+#+# WHERE IT DOES NOT HOLD YET, said plainly because a ceiling that claims more than it counts is+# how the last three rounds of this bug survived: it is claimed at exactly one place, the top of+# `_media_chain`'s loop, so it bounds the SUBMIT walk and nothing else. `_media_job_resubmit`+# opens its own socket without asking, and a synchronous shape whose file will not land reaches it+# inside the same tool call — so a chain with four or more candidates can still make five. Closing+# that means claiming the budget in `_media_call`, which is the one place every outbound submit+# actually passes through; it is not what this does.+#+# WHAT THIS COSTS, so the next reader does not have to find out the hard way: a free refusal is+# never quarantined, so every call starts the walk at rank 1 again. If the FIRST FOUR candidates+# all answer 4xx, no call ever reaches the fifth and the capability is refused until one of the+# four recovers. That is judged acceptable because the chains interleave providers on purpose —+# the first four of text_to_image span three of them — so four consecutive refusals means the+# relay itself is down, and the fifth candidate would not have answered either. If a chain ever+# stacks one provider's models at the top, this number and that fact have to be looked at+# together.+_MEDIA_MAX_SUBMITS = 4+++class _MediaBudget:+ """What one generate call may buy and what it may try, and the ONE PLACE that decides it.++ THE COUNT LIVES WHERE THE LOOP IS, NOT WHERE THE EXITS ARE. This same overspend was closed+ twice and found three times, each time in the branch beside the one that was patched: the+ poll walk was capped and the submit walk was not; then `except MediaEmpty` was capped and+ `except MediaError` was not; then `_media_job_resubmit` turned out to be doing the same+ bookkeeping by hand. Every one of those fixes was a check in an `except`, so every new failure+ shape arrived through a branch nobody had capped yet. A submit is claimed HERE, before the+ request goes out, so the ceiling holds for an answer nobody has imagined and for an exception+ type invented next year.++ A CLAIM IS BILLABLE UNTIL THE ANSWER SAYS OTHERWISE. `claim` counts the submit against the+ money budget immediately and `free` gives it back only when the provider answers with a status+ code, which is the only evidence there is that it did not do the work. That ordering is also+ what makes the ceiling hold when an agent fires four generate calls at once: the claim and the+ check happen with no await between them, so two concurrent walks cannot both see the last+ slot.+ """++ __slots__ = ("submits", "billed", "sharers")++ def __init__(self) -> None:+ self.submits = 0+ self.billed = 0+ self.sharers = 0++ def claim(self) -> str:+ """"" if another outbound submit is allowed, else the clause saying why it is not."""+ if self.billed > _MEDIA_MAX_ADVANCES:+ return "billed"+ if self.submits >= _MEDIA_MAX_SUBMITS:+ return "tried"+ self.submits += 1+ self.billed += 1+ return ""++ def free(self) -> None:+ """The provider said no with a status code: nothing ran, so the claim goes back."""+ self.billed -= 1+++# One budget per generate call — SHARED WITH EVERY CALL THAT OVERLAPS IT ON THE SAME SESSION.+_media_budgets: dict[str, _MediaBudget] = {}+++async def _media_budget_for(sid: str):+ """The budget this generate call spends out of.++ Per request rather than per session, because a session lives for hours and one budget for all+ of it would refuse an agent that has been working correctly since. But an agent parallelises:+ four generate_image calls issued in ONE turn each opened a private budget and bought the whole+ chain between them. The rule this budget exists to enforce is that a second render is the+ agent's DECISION, made with the first one's cost in front of it — and that is not true of a+ call which was already in flight when the first one billed. So overlapping calls share one+ budget, and the first call to arrive after they have all answered opens a fresh one: "the+ agent has seen an answer" is exactly the boundary, and in-flight is exactly how to spell it.+ """+ b = _media_budgets.get(sid)+ if b is None:+ b = _media_budgets[sid] = _MediaBudget()+ b.sharers += 1+ try:+ yield b+ finally:+ b.sharers -= 1+ if b.sharers <= 0:+ _media_budgets.pop(sid, None)+def _media_billed_out(err: str, billed: int) -> str:"""What an agent is told when its request has spent the whole billable budget. ONE sentence@@ -7664,45 +7783,66 @@ def _media_billed_out(err: str, billed: int) -> str:f"Ask again if you want another attempt.")-async def _media_chain(cap: str, params: dict, providers: dict) -> tuple[dict, str, object, dict, dict]:+def _media_walk_over(cap: str, reasons: list[str], billed: int, stopped: str,+ turn_billed: int) -> str:+ """What the agent is told when a walk ended with no media. One builder for every way it can+ end, so "nothing was charged" — the one sentence an agent must never be told wrongly —+ survives on exactly the paths where it is true, and so does the advice that follows it.+ """+ if billed:+ return _media_billed_out("; ".join(reasons), billed)+ if stopped == "billed":+ # This call bought nothing and a call issued ALONGSIDE it spent the shared budget. Its own+ # sentence, because the two others would each be wrong here: `refusal` would tell the agent+ # to go and ask somebody to connect a provider, and the billed-out sentence would charge+ # this call for renders it did not make.+ return (f"Another generation issued in the same turn has already bought the "+ f"{turn_billed} renders one turn is allowed and none of them delivered. Nothing "+ f"was generated here and nothing was charged for it. Ask again — one at a time — "+ f"if you want another attempt.")+ if stopped == "tried":+ return (f"{'; '.join(reasons)} — this request has made the {_MEDIA_MAX_SUBMITS} attempts "+ f"it is allowed and none of them produced anything. Nothing was generated and "+ f"nothing was charged. Ask again if you want the remaining models tried.")+ return media_plane.refusal(cap, reasons)+++async def _media_chain(cap: str, params: dict, providers: dict,+ budget: _MediaBudget) -> tuple[dict, str, object, dict, dict]:"""Walk the capability's chain until one candidate actually runs. Returns the tally of whatthe walk cost along with what it produced, because the job that comes out of it has to carrythe spend forward.- A candidate that fails AT SUBMIT is retried on the very next call and never quarantined —- retrying is free, and that is precisely what lets `dreamina-seedance-2-5-hc` sit permanently at- rank 1 and start working the day the relay's mapping is fixed, with no code change and no cost.- A candidate that fails having ALREADY BILLED is `_media_billed` — written down, stood down and- counted — and counted against _MEDIA_MAX_ADVANCES, which is the whole request's budget and not- this walk's: free failures and paid ones look identical in a list of attempts, and only one of- them may end the walk.+ ONE GATE, ASKED BEFORE THE SOCKET IS OPENED. Whether this walk may make another submit is a+ fact about the walk, so it is asked once, at the top of the loop, about the budget — never+ about the last answer and never inside an `except`. What the answer WAS decides only two+ things, and there is one branch for both: a 200 that gave nothing billed (write it down, stand+ the model down, keep the claim), and a status code did not (give the claim back, and leave the+ model in the chain — retrying it is free, and that is precisely what lets+ `dreamina-seedance-2-5-hc` sit permanently at rank 1 and start working the day the relay's+ mapping is fixed, with no code change and no cost)."""tally: dict = {"attempts": [], "billed_usd": 0.0}tried: set[str] = set()while True:cand, skipped = media_plane.resolve(cap, params, set(providers), _media_quarantine,exclude=tried)- billed = sum(1 for a in tally["attempts"] if a.get("billed"))- if not cand:+ stopped = budget.claim() if cand else "chain"+ if stopped:reasons = [f"{a['model']} — {a['error']}" for a in tally["attempts"]] + skipped- # `refusal` ends with "nothing was charged", which is only true when nothing billed.- text = (media_plane.refusal(cap, reasons) if not billed- else _media_billed_out("; ".join(reasons), billed))- raise _MediaNoModel(text, tally["attempts"])+ billed = sum(1 for a in tally["attempts"] if a.get("billed"))+ raise _MediaNoModel(_media_walk_over(cap, reasons, billed, stopped, budget.billed),+ tally["attempts"], float(tally["billed_usd"]))model = str(cand.get("model") or "")tried.add(model)try:task_id, payload, doc = await _media_call(cand, providers[cand["provider"]], params)- except media_plane.MediaEmpty as e:- # It answered and it billed. Stand it down so the next request does not pay again —- # and stop here if this request has now bought everything it is allowed to buy.- _media_billed(tally, model, media_plane.estimated_usd(cand) or 0.0, str(e))- if billed + 1 > _MEDIA_MAX_ADVANCES:- raise _MediaNoModel(_media_billed_out(str(e), billed + 1),- tally["attempts"]) from e- continueexcept media_plane.MediaError as e:- tally["attempts"].append({"model": model, "error": str(e)})+ if isinstance(e, media_plane.MediaEmpty):+ _media_billed(tally, model, media_plane.estimated_usd(cand) or 0.0, str(e))+ else:+ budget.free()+ tally["attempts"].append({"model": model, "error": str(e)})continuereturn cand, task_id, payload, doc, tally@@ -7833,7 +7973,7 @@ async def _media_meta(sid: str, med: str) -> dict | None:return None-def _provider_url_check(url: str) -> str | None:+async def _provider_url_check(url: str) -> str | None:"""Whether this server may fetch an address THE PROVIDER named. The rule _ssrf_check appliesto a customer's MCP endpoint, minus its self-hosted exemption.@@ -7854,7 +7994,7 @@ def _provider_url_check(url: str) -> str | None:return refusedif u.scheme not in ("http", "https") or not u.hostname:return refused- bad = _internal_target(u.hostname, u.port or (443 if u.scheme == "https" else 80))+ bad = await _internal_target(u.hostname, u.port or (443 if u.scheme == "https" else 80))if not bad:return None# The host, in the log, where an operator can act on it — and only the host: a result_url@@ -7875,19 +8015,35 @@ async def _media_fetch(url: str) -> bytes:storing the answer. No credential rides along and the body is never echoed, but a status anda size still leak through `attempts`, and that is a probe of our own network either way."""- refused = _provider_url_check(url)+ refused = await _provider_url_check(url)if refused:raise media_plane.MediaEmpty(refused)buf = bytearray()- async with _media_client().stream("GET", url, timeout=300) as r:- if r.status_code >= 400:- raise media_plane.MediaEmpty(f"the finished file could not be fetched "- f"(HTTP {r.status_code})")- async for chunk in r.aiter_bytes():- buf.extend(chunk)- if len(buf) > media_plane.MAX_BYTES:- raise media_plane.MediaEmpty(- f"the provider returned more than {media_plane.MAX_BYTES} bytes")+ try:+ async with _media_client().stream("GET", url, timeout=300) as r:+ if r.status_code >= 400:+ raise media_plane.MediaEmpty(f"the finished file could not be fetched "+ f"(HTTP {r.status_code})")+ async for chunk in r.aiter_bytes():+ buf.extend(chunk)+ if len(buf) > media_plane.MAX_BYTES:+ raise media_plane.MediaEmpty(+ f"the provider returned more than {media_plane.MAX_BYTES} bytes")+ except media_plane.MediaError:+ raise+ except Exception as e: # noqa: BLE001+ # EVERY WAY THIS FAILS IS A MediaError, which is this function's contract and not a+ # courtesy. Its three callers all handle MediaError and none of them handles anything+ # else, so an httpx.ConnectError — a CDN host that resolves and then refuses, resets or+ # times out the socket — escaped the poll BEFORE the backoff was bumped and before the age+ # check, and the blanket `except Exception` in the sweeper, in check_jobs and in the+ # browser's jobs route each swallowed it in silence. The job stayed `running` for ever:+ # one provider poll CARRYING THE KEY plus one CDN fetch every ten seconds, presenting to+ # the person as a placeholder that never resolves and never errors. The same rule+ # `_media_call` already holds for the submit — a network failure is a failure with a+ # sentence, not a crash.+ raise media_plane.MediaEmpty(+ f"the finished file could not be fetched ({type(e).__name__})") from ereturn bytes(buf)@@ -8069,13 +8225,23 @@ async def _media_job_advance(job: dict) -> dict:return freshif float(fresh.get("next_poll_at") or 0) > time.time() * 1000:return fresh- return await _media_job_poll(fresh)+ try:+ return await _media_job_poll(fresh)+ except Exception as e: # noqa: BLE001+ # THE INVARIANT OF THIS FUNCTION, and the reason it is stated once here rather than+ # guarded at each raise inside the poll: a job that was polled comes out of it either+ # TERMINAL or DUE LATER. `_media_job_poll` bumps the backoff on every path it returns+ # by; if it raised instead, nobody did — and every caller of this function (the+ # sweeper, check_jobs, the browser's jobs route) swallows the exception, so the job+ # was re-polled on the very next sweep, ten seconds later, for ever. Whatever raised,+ # the backoff still applies and JOB_MAX_S still ends it.+ print(f"[media] poll of {fresh.get('id')} raised {type(e).__name__}: {e}", flush=True)+ return await _media_job_stalled(fresh)async def _media_job_poll(job: dict) -> dict:"""One poll of one job, under its lock. Split out so the locking above reads as one thought."""now = time.time()- age_s = (now * 1000 - float(job.get("created_at") or 0)) / 1000.0cand = _media_candidate(str(job.get("capability") or ""), str(job.get("model") or ""))providers = await _media_providers()prov = providers.get(str(job.get("provider") or ""))@@ -8110,14 +8276,27 @@ async def _media_job_poll(job: dict) -> dict:# state a job is IN — it is what one answer was — so recording it here lets check_jobs tell the# agent "ask again" without a poll that came back empty ever becoming terminal.job["last_poll"] = status- if status == "unknown":+ return await _media_job_stalled(job, back_off=status == "unknown")+++async def _media_job_stalled(job: dict, back_off: bool = True) -> dict:+ """A poll that told us nothing terminal: back the job off, or end it because it is too old.++ THE ONE PLACE a job that did not finish is put down again, so "a polled job comes out either+ terminal or due later" is a property of the poll loop and not of each way a poll can end. Its+ other caller is `_media_job_advance`, for a poll that RAISED — every caller up there swallows+ exceptions, so without this the job kept its old `next_poll_at`, was due immediately, and was+ polled again on every sweep for as long as the gateway ran.+ """+ if back_off:job["poll_backoff_s"] = min(60, int(job.get("poll_backoff_s") or 20) * 2)+ age_s = (time.time() * 1000 - float(job.get("created_at") or 0)) / 1000.0if age_s > media_plane.JOB_MAX_S:job.update({"status": "failed", "error": "The provider never finished this render."})await _media_job_save(job)await _media_element_failed(job)return job- job["next_poll_at"] = int((now + int(job.get("poll_backoff_s") or 20)) * 1000)+ job["next_poll_at"] = int((time.time() + int(job.get("poll_backoff_s") or 20)) * 1000)await _media_job_save(job)return job@@ -8223,17 +8402,16 @@ async def _media_job_resubmit(job: dict) -> bool:usd = media_plane.estimated_usd(cand) or 0.0try:task_id, payload, doc = await _media_call(cand, providers[cand["provider"]], params)- except media_plane.MediaEmpty as e:- # The SECOND render this request bought, and it answered with nothing either. Banked and- # stood down exactly as the first one was — the submit walk in _media_chain treats this- # case that way, and a candidate that bills for nothing must not stay in the chain because- # it happened to be reached from the poll path instead.- _media_billed(job, model, usd, str(e))- return Falseexcept media_plane.MediaError as e:- # Refused before any billable work started; retrying it is free and it is not stood down.- job.setdefault("attempts", []).append({"model": model, "error": str(e)})- job["attempts_json"] = json.dumps(job["attempts"])+ # ONE BRANCH, asking the same question `_media_chain` asks, in the same words. This is the+ # place round 2 found doing this bookkeeping by hand, and a hand-written copy of a rule is+ # a copy that gets a different subset of it right: a second render that answered 200 with+ # nothing was neither banked nor stood down here, so the next request bought it again.+ if isinstance(e, media_plane.MediaEmpty):+ _media_billed(job, model, usd, str(e))+ else:+ job.setdefault("attempts", []).append({"model": model, "error": str(e)})+ job["attempts_json"] = json.dumps(job["attempts"])return Falsejob.update({"model": model, "provider": str(cand.get("provider") or ""), "usd": usd})if task_id:@@ -8250,11 +8428,16 @@ async def _media_job_resubmit(job: dict) -> bool:async def _media_generate(cap: str, params: dict, hid: str, sid: str, org: str,- entry_id: str) -> dict:- """Resolve the chain, start the work, and record a job. Nothing is written when nothing ran:- a refusal leaves no vertex behind, because nothing happened."""+ entry_id: str, budget: _MediaBudget) -> dict:+ """Resolve the chain, start the work, and record a job. A refusal leaves no vertex behind when+ nothing ran — but a walk that BOUGHT renders and delivered none of them is not nothing, and+ what it spent is written down before the refusal goes back."""providers = await _media_providers()- cand, task_id, payload, doc, tally = await _media_chain(cap, params, providers)+ try:+ cand, task_id, payload, doc, tally = await _media_chain(cap, params, providers, budget)+ except _MediaNoModel as e:+ await _media_walk_spend(cap, params, hid, sid, org, entry_id, e)+ raiseattempts = tally["attempts"]now = int(time.time() * 1000)job = {"id": "mjob_" + uuid.uuid4().hex[:16], "org": org, "harness": hid, "session": sid,@@ -8291,6 +8474,36 @@ async def _media_generate(cap: str, params: dict, hid: str, sid: str, org: str,return job+async def _media_walk_spend(cap: str, params: dict, hid: str, sid: str, org: str, entry_id: str,+ e: _MediaNoModel) -> None:+ """A submit walk that bought renders and delivered nothing still leaves the money where the+ session can see it.++ Same rule the job store already states one function up — an untraceable charge is worse than a+ visible failure — applied to the one path that had no job to write it on. A generate_video+ whose candidates each answered 200 and handed back nothing bought up to the whole chain, and+ because the refusal never reached `_media_job_save`, `_media_spend` read $0.00 and the session+ budget that exists to stop a runaway never saw a cent of it.++ Nothing is written when nothing billed: a refusal for a capability nobody has connected is+ still not an event, and a failed job for it would be noise on the person's canvas.+ """+ billed = [a for a in (e.attempts or []) if a.get("billed")]+ if not billed:+ return+ now = int(time.time() * 1000)+ await _media_job_save({+ "id": "mjob_" + uuid.uuid4().hex[:16], "org": org, "harness": hid, "session": sid,+ "entry": entry_id, "capability": cap, "status": "failed", "error": e.text,+ "params_json": json.dumps(_media_params_stored(params)), "params": params,+ "model": str(billed[-1].get("model") or ""), "provider": "", "provider_task_id": "",+ "attempts_json": json.dumps(e.attempts), "attempts": e.attempts,+ "next_poll_at": 0, "poll_backoff_s": 20, "media_id": "", "bytes": 0, "width": 0,+ "height": 0, "seconds": 0.0, "quota": 0.0, "usd": 0.0,+ "advances": len(billed), "billed_usd": float(e.billed_usd or 0.0),+ "element_id": "", "created_at": now, "updated_at": now})++# ── the sweeper ───────────────────────────────────────────────────────────────────────────────_MEDIA_SWEEP_EVERY_S = int(os.environ.get("HR_MEDIA_SWEEP_S", "10"))@@ -8634,7 +8847,10 @@ async def _media_tool_call(name: str, args: dict, hid: str, sid: str, org: str,params.update({"image": b64, "image_mime": mime, "from_media": med})cap = "image_to_video" if name == "generate_video" else "image_to_image"try:- job = await _media_generate(cap, params, hid, sid, org, entry_id)+ # The budget is opened HERE, around the whole generation, because it belongs to the+ # call and not to the walk: the synchronous shapes bill inside `_media_generate` too.+ async with _media_budget_for(sid) as budget:+ job = await _media_generate(cap, params, hid, sid, org, entry_id, budget)except _MediaNoModel as e:return _tool_text(e.text, True)except media_plane.MediaError as e:@@ -10098,7 +10314,7 @@ async def launch_kit(kit_id: str, request: Request, body_in: KitLaunchBody | Nonf"The '{kit_id}' kit does not read a database.", "database")# Checked before anything is provisioned: a typo in a connection string should cost the person# a corrected form, not a half-configured Harness to find and fix.- checked = _db_validate(db_in.engine, db_in.connection_string) if db_in else None+ checked = await _db_validate(db_in.engine, db_in.connection_string) if db_in else Noneexisting = next((r for r in await _vg_list_by_org("Harness", org)if str(r.get("kit") or "") == kit_id and str(r.get("deleted") or "0") != "1"),diff --git a/gateway/media_plane.py b/gateway/media_plane.pyindex fdbaa9fc..121362c1 100644--- a/gateway/media_plane.py+++ b/gateway/media_plane.py@@ -57,13 +57,28 @@ class MediaError(Exception):class MediaRefused(MediaError):- """The provider refused before any billable work started — an HTTP error at submit, a- timeout before a task id existed. Retrying the candidate is free, so it is NOT quarantined."""+ """The provider SAID NO: an HTTP error status at submit, or no answer at all. Retrying the+ candidate is free, so it is NOT quarantined — which is what lets the owner's preferred model+ sit at rank 1 through an outage and start working again the day the relay is fixed.++ A 200 IS NEVER THIS. It used to be, twice: a 200 whose body was not an object, and a 200 with+ no task id in it — whose own sentence read "the provider accepted the request but returned no+ task id". Both are the provider ANSWERING, which is the only evidence anyone has that it+ billed, and both were classified as free. One generate_video where every candidate answered+ that way walked the whole six-model chain, stood none of them down, and told the agent+ "nothing was charged". The line is the status code, because the status code is the only thing+ here that says whether the provider got as far as doing the work.+ """class MediaEmpty(MediaError):"""The provider answered 200 and returned no media. Billable, and therefore a failure that- quarantines the candidate — this is the gemini-3-pro rule."""+ quarantines the candidate — this is the gemini-3-pro rule.++ "Returned no media" covers a body that is not the shape at all, not only an empty list in the+ right shape: `data: []` and a bare JSON array are the same event one container type apart, and+ charging one of them to the request and not the other is an accounting artefact.+ """# ── the catalog ───────────────────────────────────────────────────────────────────@@ -513,21 +528,24 @@ def _read_submit(cand: dict, status: int, doc) -> tuple[str, Payload | None]:A shape that answers with a task id returns ("task_…", None). A synchronous shape returns("", payload). Anything else raises — and WHICH exception decides whether the candidate is- quarantined, so the distinction is the point:- MediaRefused the provider said no before any billable work started; retry is free.- MediaEmpty it answered 200 and returned nothing; it billed, so stand it down.+ quarantined, so the distinction is the point. ONE QUESTION DECIDES IT, asked once, here:+ DID THE PROVIDER ANSWER?+ MediaRefused a status of 400 or worse: it said no, nothing ran, retry is free.+ MediaEmpty a 200 that carried no media: it answered, so it billed — stand it down.+ Every shape below therefore raises MediaEmpty, whatever is wrong with the body: 200 is the+ line, and "which particular way the 200 was useless" is a sentence, not a policy."""if status >= 400:raise MediaRefused(provider_message(doc, status))if not isinstance(doc, dict):- raise MediaRefused("the provider returned a body that is not an object")+ raise MediaEmpty("the provider answered with a body that is not an object")shape = str(cand.get("shape") or "")if shape == "video-generation":tid = str(doc.get("task_id") or doc.get("id") or "")if not tid:- raise MediaRefused(provider_message(doc, status) or- "the provider accepted the request but returned no task id")+ raise MediaEmpty(provider_message(doc, status) or+ "the provider accepted the request but returned no task id")return tid, Noneif shape == "image-generation":@@ -709,6 +727,15 @@ def scrub(text: str) -> str:Exact first — the keys this deployment actually sends — then by shape, because a provider alsosays other people's tokens, its own bearer and an org id out loud, and a redaction that onlyknows our own string only ever catches the leak we already found.++ WHAT THIS GUARANTEES, AND WHERE IT STOPS. The exact half is a guarantee: a credential THIS+ DEPLOYMENT sends is removed from anywhere in a provider document, error field or not. The+ shape half is a net, not a guarantee — `hf_`, `xai-`, `gsk_`, `r8_`, `nvapi-`, a bare hex+ string and a JWT are not in the list, so a THIRD PARTY's token quoted in a field nobody treats+ as an error (a progress string, a transcript) can survive. The list is deliberately not grown+ to cover every vendor's prefix: a provider's task id is made of the same characters, and this+ same function cleans the task id we poll with — a redaction that eats one is a render nobody+ can ever collect. Widening it means proving that first. See the test that pins both halves."""out = str(text or "")for secret in _SECRETS:diff --git a/gateway/tests/test_media_attack.py b/gateway/tests/test_media_attack.pyindex b7221467..729016a0 100644--- a/gateway/tests/test_media_attack.py+++ b/gateway/tests/test_media_attack.py@@ -699,11 +699,11 @@ def test_v28_the_shared_classifier_still_refuses_what_it_could_not_classify(clie"""One rule, one answer: a host that does not resolve is refused for the MCP and databasecallers too, and that is their whole contract."""bad_name = "this-name-does-not-exist-hr-test.invalid"- assert app._internal_target(bad_name, 443) is not None+ assert asyncio.run(app._internal_target(bad_name, 443)) is not Nonemonkeypatch.setattr(app, "_pool_is_local", lambda: False)- assert app._ssrf_check(f"https://{bad_name}/x") is not None- assert app._ssrf_check("https://169.254.169.254/") is not None- assert app._ssrf_check("https://10.0.0.9/mcp") is not None+ assert asyncio.run(app._ssrf_check(f"https://{bad_name}/x")) is not None+ assert asyncio.run(app._ssrf_check("https://169.254.169.254/")) is not None+ assert asyncio.run(app._ssrf_check("https://10.0.0.9/mcp")) is not Nonedef test_v28b_a_transient_dns_failure_does_not_open_the_provider_url_check(client, monkeypatch):@@ -720,7 +720,7 @@ def test_v28b_a_transient_dns_failure_does_not_open_the_provider_url_check(clienreturn real(host, port, *a, **kw)monkeypatch.setattr(socket, "getaddrinfo", flaky)- assert app._provider_url_check("http://rebind.attacker.example/x.mp4") is not None, (+ assert asyncio.run(app._provider_url_check("http://rebind.attacker.example/x.mp4")) is not None, ("a name whose lookup merely TIMED OUT was accepted as an address to fetch; the same name ""resolving to 10.0.0.5 on the next lookup is the whole SSRF")@@ -737,8 +737,8 @@ def test_a_name_that_does_not_exist_and_one_we_could_not_look_up_are_told_apart(return real(host, port, *a, **kw)monkeypatch.setattr(socket, "getaddrinfo", flaky)- gone = app._internal_target("this-name-does-not-exist-hr-test.invalid", 443)- unknown = app._internal_target("flaky.attacker.example", 443)+ gone = asyncio.run(app._internal_target("this-name-does-not-exist-hr-test.invalid", 443))+ unknown = asyncio.run(app._internal_target("flaky.attacker.example", 443))assert gone and unknown and gone != unknown, (gone, unknown)@@ -785,3 +785,436 @@ def test_an_ordinary_render_still_lands(client, kit, hostile):job = asyncio.run(app._media_job_get(jid))assert job["status"] == "succeeded", jobassert job["media_id"], job+++# ══════════════════════════════════════════════════════════════════════════════════════════════+# ROUND 3 — THE CEILING IS ON THE WALK, NOT ON THE EXITS+#+# The same finding was closed twice and found three times, each time in a sibling path, because+# each fix capped the `except` the finding NAMED. `MediaEmpty` was capped and `MediaError` was+# not; `_media_job_billable_failure` was capped and `_media_chain` was not. So the count moved to+# where the loop is: one gate, read before the socket is opened, so an exception type invented+# next year is bounded by it without anyone remembering that it should be.+# ══════════════════════════════════════════════════════════════════════════════════════════════++def test_v38_a_200_with_no_task_id_does_not_walk_the_whole_video_chain(client, kit, hostile):+ """THE MOST EXPENSIVE CHAIN IN THE CATALOG, bought whole by one tool call.++ Every candidate answers HTTP 200 with a body that carries no task id — which media_plane's own+ sentence calls "the provider accepted the request but returned no task id". That was raised as+ `MediaRefused`, whose docstring says "before any billable work started", so the cap that keyed+ on `MediaEmpty` never saw it: six submits, none of them stood down, and the agent was told+ "nothing was generated and nothing was charged".+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle++ def no_task_id(req):+ if str(req.url).endswith("/video/generations") and req.method == "POST":+ return httpx.Response(200, json={"object": "video", "status": "queued"})+ return real(req)++ hostile.handle = no_task_id+ submits = _count_submits(hostile)+ res = _call(client, _cred(hid, sid), "generate_video", {"prompt": "a cat", "seconds": 6})+ assert res.get("isError") is True, res+ assert len(submits) <= app._MEDIA_MAX_ADVANCES + 1, (+ f"ONE generate_video CALL BOUGHT {len(submits)} 200-ANSWERS: {submits}")+ assert "charged" not in _text(res) or "billed" in _text(res).lower(), (+ f"models billed and the agent was told nothing was charged: {_text(res)}")+++def test_v38b_a_200_whose_body_is_not_an_object_does_not_walk_the_image_chain(client, kit,+ hostile):+ """The same event as `data: []`, one JSON container type over. `data: []` was `MediaEmpty` and+ capped; a bare list was `MediaRefused` and uncapped — eight submits from one call."""+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle++ def not_an_object(req):+ u = str(req.url)+ if (u.endswith("/images/generations") or ":generateContent" in u+ or u.endswith("/chat/completions")):+ return httpx.Response(200, json=[{"b64_json": "not-where-we-look"}])+ return real(req)++ hostile.handle = not_an_object+ submits = _count_submits(hostile)+ res = _call(client, _cred(hid, sid), "generate_image", {"prompt": "a cat"})+ assert res.get("isError") is True, res+ assert len(submits) <= app._MEDIA_MAX_ADVANCES + 1, (+ f"ONE generate_image CALL BOUGHT {len(submits)} 200-ANSWERS: {submits}")+++def test_v38c_free_failures_interleaved_with_billable_ones_still_cap_the_billable(client, kit,+ hostile):+ """A 4xx costs nothing and IS walked past — that is the policy that keeps the owner's+ preferred model at rank 1 through an outage. Interleaving them with billable answers must not+ launder the billable ones past the cap."""+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle+ seen: list[str] = []++ def mixed(req):+ u = str(req.url)+ if (u.endswith("/images/generations") or ":generateContent" in u+ or u.endswith("/chat/completions")):+ seen.append(u)+ if len(seen) % 2 == 1:+ return httpx.Response(429, json={"error": {"message": "rate limited"}})+ if u.endswith("/images/generations"):+ return httpx.Response(200, json={"created": 1, "data": []})+ if ":generateContent" in u:+ return httpx.Response(200, json={"candidates": [{"content": {"parts": []}}]})+ return httpx.Response(200, json={"choices": [{"message": {"content": "sorry"}}]})+ return real(req)++ hostile.handle = mixed+ submits = _count_submits(hostile)+ res = _call(client, _cred(hid, sid), "generate_image", {"prompt": "a cat"})+ assert res.get("isError") is True, res+ billable = [s for i, s in enumerate(seen) if i % 2 == 1]+ assert len(billable) <= app._MEDIA_MAX_ADVANCES + 1, (+ f"ONE CALL BOUGHT {len(billable)} BILLABLE RENDERS among {len(submits)} submits")+++def test_v38d_parallel_calls_in_one_turn_share_one_budget(client, kit, hostile, monkeypatch):+ """THE AGENT PARALLELISES. The whole rule is that a second render is the agent's decision made+ with the first one's cost in front of it — which is not true of a call that was already in+ flight when the first one billed. Four generate_image calls in flight at once, every candidate+ answering 200-with-no-image: four private budgets buy the entire chain in one turn.++ The four are held at the gateway door until all four have arrived, because "were they really+ concurrent" is thread-scheduling luck and this test is not about luck: it asserts that calls+ which ARE concurrent share one budget. What bounds the calls that do not overlap is the+ stand-down, asserted below — no model may be bought twice across the whole turn.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle++ def empty200(req):+ u = str(req.url)+ if u.endswith("/images/generations"):+ return httpx.Response(200, json={"created": 1, "data": []})+ if ":generateContent" in u:+ return httpx.Response(200, json={"candidates": [{"content": {"parts": []}}]})+ if u.endswith("/chat/completions"):+ return httpx.Response(200, json={"choices": [{"message": {"content": "sorry"}}]})+ return real(req)++ hostile.handle = empty200+ submits = _count_submits(hostile)+ tok = _cred(hid, sid)++ real_generate = app._media_generate+ arrived = {"n": 0}++ async def all_four_first(*a, **kw):+ """Every call is inside its budget by the time any of them submits — which is exactly the+ turn the agent issued, and not four turns that happened to be quick."""+ arrived["n"] += 1+ deadline = time.time() + 10+ while arrived["n"] < 4 and time.time() < deadline:+ await asyncio.sleep(0.005)+ assert arrived["n"] >= 4, "the four calls never overlapped; this test proved nothing"+ return await real_generate(*a, **kw)++ monkeypatch.setattr(app, "_media_generate", all_four_first)+ import concurrent.futures as cf+ with cf.ThreadPoolExecutor(max_workers=4) as ex:+ list(ex.map(lambda _: _call(client, tok, "generate_image", {"prompt": "a cat"}), range(4)))+ assert len(submits) <= app._MEDIA_MAX_ADVANCES + 1, (+ f"ONE TURN (4 parallel tool calls) BOUGHT {len(submits)} BILLABLE RENDERS: {submits}")+++def test_v38e_a_200_that_delivered_nothing_stands_the_model_down_too(client, kit, hostile):+ """What stops a loop across CALLS is the stand-down, not the per-request cap. A failure mode+ that bills and does not quarantine buys the same models again on the very next call — three+ sequential generate_video calls bought eighteen renders."""+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle++ def no_task_id(req):+ if str(req.url).endswith("/video/generations") and req.method == "POST":+ return httpx.Response(200, json={"object": "video", "status": "queued"})+ return real(req)++ hostile.handle = no_task_id+ submits = _count_submits(hostile)+ tok = _cred(hid, sid)+ for _ in range(3):+ _call(client, tok, "generate_video", {"prompt": "a cat", "seconds": 6})+ assert len(submits) <= 3 * (app._MEDIA_MAX_ADVANCES + 1), (+ f"THREE generate_video CALLS BOUGHT {len(submits)} 200-ANSWERS: {submits}")+ assert len(set(submits)) == len(submits), (+ f"a model that billed for nothing was picked twice: {submits}")+++def test_v38f_an_exception_type_nobody_wrote_an_except_for_is_bounded_too(client, kit, hostile,+ monkeypatch):+ """THE STRUCTURAL ONE, and the reason this round exists.++ Every previous fix capped the `except` the finding named, so the next failure shape found the+ next uncapped branch. Here a failure type that did not exist when the cap was written is+ raised out of the submit. Nobody has written a branch for it and nobody ever will — the walk+ must still stop, because the count is taken where the loop is and not where the exits are.+ """+ class _AFailureNobodyPlannedFor(media_plane.MediaError):+ pass++ def raising(cand, status, doc):+ raise _AFailureNobodyPlannedFor("a shape this code has never seen")++ monkeypatch.setattr(media_plane, "read_submit", raising)+ hid = _launch(client, kit)+ sid = _session(hid)+ submits = _count_submits(hostile)+ res = _call(client, _cred(hid, sid), "generate_image", {"prompt": "a cat"})+ assert res.get("isError") is True, res+ assert len(submits) <= app._MEDIA_MAX_SUBMITS, (+ f"AN UNPLANNED FAILURE TYPE WALKED {len(submits)} SUBMITS OF THE CHAIN: {submits}")+++def test_v38g_a_walk_that_billed_and_delivered_nothing_is_in_the_session_spend(client, kit,+ hostile):+ """An untraceable charge is worse than a visible failure — the rule the job store already+ states about a fetch that fails after a synchronous render. A submit walk that bought two+ renders and delivered none left NO record at all, so the session read $0.00 and the budget+ that is supposed to stop a runaway session never saw a cent of it."""+ hid = _launch(client, kit)+ sid = _session(hid)+ real = hostile.handle++ def no_task_id(req):+ if str(req.url).endswith("/video/generations") and req.method == "POST":+ return httpx.Response(200, json={"object": "video", "status": "queued"})+ return real(req)++ hostile.handle = no_task_id+ res = _call(client, _cred(hid, sid), "generate_video", {"prompt": "a cat", "seconds": 6})+ assert res.get("isError") is True, res+ # MiniMax-Hailuo-2.3 is the one candidate this walk reaches with a MEASURED price ($0.28).+ assert asyncio.run(app._media_spend(sid)) >= 0.28, (+ "two renders billed inside one submit walk and the session reports none of it")+++# ══════════════════════════════════════════════════════════════════════════════════════════════+# ROUND 3 — A POLL THAT RAISES MUST STILL LEAVE THE JOB SOMEWHERE+# ══════════════════════════════════════════════════════════════════════════════════════════════++@needs_ffmpeg+def test_v39_a_dead_socket_on_a_resolvable_cdn_does_not_poll_for_ever(client, kit, hostile):+ """THE FETCH, not the resolver. Only the resolver-failure INPUT was closed last round; the+ mechanism was never touched. `_media_fetch` is wrapped in `except media_plane.MediaError` and+ httpx raises its own class, so any CDN host that resolves and then refuses the socket escapes+ the poll BEFORE next_poll_at is bumped and before the age check — and every caller swallows+ it. Production polls every ten seconds: one provider poll CARRYING THE KEY plus one CDN fetch,+ for ever, presenting to the person as a placeholder that never resolves and never errors.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ hostile.video_bytes = _mp4()+ jid = _ok(_call(client, _cred(hid, sid), "generate_video",+ {"prompt": "r", "seconds": 6}))["job_id"]+ real = hostile.handle++ def dead_socket(req):+ if req.url.host == "cdn.provider.example":+ raise httpx.ConnectError("connection refused", request=req)+ return real(req)++ hostile.handle = dead_socket+ fetches: list[int] = []+ for _ in range(5):+ before = len([c for c in hostile.calls if c["host"] == "cdn.provider.example"])+ _sweep(jid)+ fetches.append(len([c for c in hostile.calls if c["host"] == "cdn.provider.example"])+ - before)+ job = asyncio.run(app._media_job_get(jid))+ assert job["status"] != "running", (+ f"five sweeps and the job is still running; fetches per sweep {fetches}, "+ f"next_poll_at {job['next_poll_at']}, backoff {job['poll_backoff_s']}")+ assert job.get("error"), "the job ended with no sentence for the person"+++@needs_ffmpeg+def test_v39b_a_poll_that_raises_anything_still_leaves_the_job_due_later(client, kit, hostile,+ monkeypatch):+ """The general shape of the one above. Whatever a poll raises — including a bug of ours — the+ job it was polling is left either terminal or due later. Otherwise `next_poll_at` stays where+ it was, the age check never runs, and the sweeper re-polls the same job every ten seconds for+ ever while every caller's blanket `except` says nothing."""+ hid = _launch(client, kit)+ sid = _session(hid)+ hostile.video_bytes = _mp4()+ jid = _ok(_call(client, _cred(hid, sid), "generate_video",+ {"prompt": "r", "seconds": 6}))["job_id"]++ async def boom(job):+ raise RuntimeError("a bug nobody has found yet")++ monkeypatch.setattr(app, "_media_job_poll", boom)+ _sweep(jid)+ job = asyncio.run(app._media_job_get(jid))+ assert job["status"] != "running" or job["next_poll_at"] > time.time() * 1000, (+ f"the poll raised and left the job due immediately for ever: {job}")+++@needs_ffmpeg+def test_v39c_a_credential_in_the_exempt_result_url_reaches_no_sink(client, kit, hostile):+ """`Payload.url` is deliberately not scrubbed, because a signed result_url IS its query+ string. The claim that makes that safe is that it is fetched once and never persisted or+ relayed — so put this deployment's own key in it and check every sink."""+ hid = _launch(client, kit)+ sid = _session(hid)+ hostile.result_url = f"https://cdn.provider.example/out.mp4?api_key={PROVIDER_KEY}"+ hostile.video_bytes = _mp4()+ jid = _ok(_call(client, _cred(hid, sid), "generate_video",+ {"prompt": "r", "seconds": 6}))["job_id"]+ _sweep(jid)+ tool = _text(_call(client, _cred(hid, sid), "check_jobs", {"job_ids": [jid]}))+ vertex = json.dumps(_vertex(jid))+ browser = client.get(f"/v1/harnesses/{hid}/servers/{ENTRY_ID}/sessions/{sid}/jobs",+ headers=HEADERS).text+ _assert_clean(PROVIDER_KEY, "the exempt result_url", tool, vertex, browser)+++@needs_ffmpeg+def test_v39d_the_scrub_removes_our_keys_and_names_the_shape_it_cannot(client, kit, hostile):+ """THE STATED LIMIT, pinned so the next reader finds it as a fact rather than as a surprise.++ Our own credentials are exact-matched and are removed from anywhere in a provider document.+ A THIRD PARTY's token, in a field that is not an error, survives unless its shape happens to+ be in the pattern list — and the list is deliberately not grown to catch every vendor prefix,+ because the same characters are what a provider's task ids are made of and a redacted task id+ is a render nobody can poll. This test holds both halves: the guarantee, and its edge.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ hostile.video_bytes = _mp4()+ jid = _ok(_call(client, _cred(hid, sid), "generate_video",+ {"prompt": "r", "seconds": 6}))["job_id"]+ foreign = "hf_QeRtYuIoPaSdFgHjKlZxCvBnM1234567890"+ hostile.poll_answer = httpx.Response(200, json={"code": "success", "data": {+ "status": "IN_PROGRESS",+ "progress": f"30% for {PROVIDER_KEY} (worker {foreign})"}})+ _due(jid)+ tool = _text(_call(client, _cred(hid, sid), "check_jobs", {"job_ids": [jid]}))+ assert PROVIDER_KEY not in tool and PROVIDER_KEY not in json.dumps(_vertex(jid)), (+ f"OUR OWN key survived a non-error field: {tool[:300]}")+ assert media_plane.scrub(foreign) == foreign, (+ "the pattern list grew to cover third-party prefixes — check it cannot eat a task id, "+ "then update the note in media_plane.scrub that says it does not")+++# ══════════════════════════════════════════════════════════════════════════════════════════════+# ROUND 3 — THE RESOLVER: what it classifies, what it costs, and what it does NOT promise+# ══════════════════════════════════════════════════════════════════════════════════════════════+[email protected]("mapped", ["::ffff:127.0.0.1", "::ffff:10.0.0.5",+ "::ffff:169.254.169.254", "64:ff9b::7f00:1", "fd00::1",+ "::ffff:100.64.0.1"])+def test_v40_ipv6_forms_of_an_internal_address_are_classified(monkeypatch, mapped):+ real = socket.getaddrinfo++ def fake(host, port, *a, **kw):+ if host == "v6.attacker.example":+ return [(socket.AF_INET6, socket.SOCK_STREAM, 6, "", (mapped, int(port or 443), 0, 0))]+ return real(host, port, *a, **kw)++ monkeypatch.setattr(socket, "getaddrinfo", fake)+ assert asyncio.run(app._internal_target("v6.attacker.example", 443)) is not None, mapped+ assert asyncio.run(app._provider_url_check("http://v6.attacker.example/x.mp4")) is not None, (+ mapped)++[email protected]("addr", ["100.64.0.1", "100.127.255.254", "100.64.99.7"])+def test_v40b_carrier_grade_nat_is_internal(monkeypatch, addr):+ """100.64.0.0/10 is what a managed Kubernetes cluster hands its pods and nodes. Python's+ ipaddress does not call it private, so it was the one whole range this classifier allowed —+ a provider naming a pod address would have had this server fetching from inside the cluster.+ """+ real = socket.getaddrinfo++ def fake(host, port, *a, **kw):+ if host == "cgnat.attacker.example":+ return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (addr, int(port or 443)))]+ return real(host, port, *a, **kw)++ monkeypatch.setattr(socket, "getaddrinfo", fake)+ assert asyncio.run(app._internal_target("cgnat.attacker.example", 443)) is not None, addr+++def test_v40c_a_slow_lookup_does_not_stall_the_event_loop():+ """Every provider URL is resolved on every poll. Resolving on the event loop stalls the whole+ process for the resolver's timeout: measured, a 1.01 s lookup produced ZERO heartbeat ticks,+ so one relay naming a slow-resolving host froze every other request in the gateway."""+ real = socket.getaddrinfo++ def slow(host, port, *a, **kw):+ if host == "slow.attacker.test":+ time.sleep(1.0)+ raise socket.gaierror(socket.EAI_NONAME, "nope")+ return real(host, port, *a, **kw)++ async def drive():+ ticks = {"n": 0}++ async def heartbeat():+ while True:+ ticks["n"] += 1+ await asyncio.sleep(0.02)++ hb = asyncio.create_task(heartbeat())+ await asyncio.sleep(0.1)+ base = ticks["n"]+ await asyncio.to_thread(lambda: None) # warm the pool+ t0 = time.time()+ await app._provider_url_check("http://slow.attacker.test/x.mp4")+ wall = time.time() - t0+ gained = ticks["n"] - base+ hb.cancel()+ return wall, gained++ saved = socket.getaddrinfo+ socket.getaddrinfo = slow+ try:+ wall, gained = asyncio.run(drive())+ finally:+ socket.getaddrinfo = saved+ assert gained > wall * 20, (+ f"the event loop made {gained} ticks while a {wall:.2f}s DNS lookup ran; it was blocked")+++def test_v40d_the_classifier_does_not_promise_to_stop_a_dns_rebind(monkeypatch):+ """WHAT IT DOES NOT DO, pinned because a comment claiming otherwise is worse than no comment.++ Resolving every answer defeats a multi-A record whose second address is internal. It does NOT+ defeat a TTL-0 rebind: httpx opens the socket with its OWN lookup, so a name that answers+ public here can answer 10.0.0.5 a millisecond later. Two halves are held: the classifier still+ catches what it claims to catch (a name whose FIRST answer is internal), and the docstring no+ longer tells the next reader that the connection is pinned to what was checked.+ """+ real = socket.getaddrinfo+ n = {"i": 0}++ def rebinding(host, port, *a, **kw):+ if host == "rebind.attacker.test":+ n["i"] += 1+ ip = "93.184.216.34" if n["i"] == 1 else "10.0.0.5"+ return [(socket.AF_INET, socket.SOCK_STREAM, 6, "", (ip, int(port or 443)))]+ return real(host, port, *a, **kw)++ monkeypatch.setattr(socket, "getaddrinfo", rebinding)+ assert asyncio.run(app._provider_url_check("http://rebind.attacker.test/x.mp4")) is None+ assert asyncio.run(app._internal_target("rebind.attacker.test", 80)) is not None+ doc = (app._internal_target.__doc__ or "").lower()+ assert "cannot rebind" not in doc, (+ "the docstring still promises the check survives a rebind between the check and the "+ "connection; it does not, and the next reader will build on the promise")+ assert "rebind" in doc, "the limit is not written down where the next reader will find it"