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): the submit ceiling was decorative on all three poll paths
richard-epsilla8ea2330c0b90
diff --git a/gateway/app.py b/gateway/app.pyindex ad0412bb..05cbc5ef 100644--- a/gateway/app.py+++ b/gateway/app.py@@ -7650,11 +7650,11 @@ def _media_json(body: bytes):# and the only place the poll walk can read it from._MEDIA_MAX_ADVANCES = 1-# How many outbound submits ONE TOOL CALL's submit walk may make AT ALL, 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. A candidate-# that answers 4xx costs nothing and IS walked past — deliberately, because leaving a broken model-# in the chain+# How many outbound submits ONE REQUEST may make AT ALL, whatever the provider answers to any of+# them — where a request is one tool call, one browser poll or one sweep pass. 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 sockets. 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@@ -7667,14 +7667,19 @@ _MEDIA_MAX_ADVANCES = 1# that no ceiling saw. Counting walks is what let four rounds of this bug each be "fixed" in the# branch beside the one that overspent. A ceiling written in submits has to be counted in submits.#-# WHAT IT DOES NOT BOUND, said here rather than discovered later. A ceiling that claims more than-# it counts is how the last rounds of this survived, so: this number bounds a SUBMIT WALK, not a-# request. A poll pass spends out of `for_job`, which seeds `billed` off the job and leaves-# `submits` at zero — one budget per job, so a request that polls N jobs may make N submits, one-# each. That is bounded by `_MEDIA_MAX_ADVANCES` per job and so the money rule holds, but the-# three poll entries (check_jobs, the sweeper, the session job listing) have no per-request cap on-# how many jobs they may advance in one pass. Closing that means a budget that lives for the-# request rather than for the job, and threading it through the sweeper too.+# IT BOUNDS THE POLL ENTRIES TOO, which took a sixth round to become true. It used to bound a+# SUBMIT WALK and nothing else: a poll pass built one budget per JOB out of `for_job`, which seeds+# `billed` off the job and left `submits` at zero, so `claim()` could not refuse anything and what+# actually stopped the walk was the `advances` check one branch over — a guard beside the socket,+# which is the construction this whole series exists to delete. Measured: one check_jobs over six+# failing jobs made six outbound submits, one _media_sweep() over five made five, and setting this+# number to 1 changed neither count.+#+# NOT AN OVERSPEND, and the fix is not sold as one. Per job the money rule held throughout+# (`advances` caps each job at `_MEDIA_MAX_ADVANCES + 1` renders) and the total across sweeps was+# unchanged: it was a BURST — six full-price renders started by what the agent issued as a read —+# and a ceiling that claims to bound a request while counting only a walk is how four rounds of+# this survived. So `submits` now belongs to the REQUEST and `billed` to the job; see the class.## 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@@ -7684,6 +7689,16 @@ _MEDIA_MAX_ADVANCES = 1# 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.+#+# AND ON THE POLL ENTRIES IT COSTS AN ADVANCE, not a delay — measured, because "the next pass will+# pick it up" is the first thing a reader assumes and it is not what happens. A pass that refuses+# a job's advance FAILS that job then and there, exactly as `_media_job_resubmit` has always done+# when the submit walk runs out: five failing jobs in one sweep means four get their second+# candidate and the fifth ends `failed` with the upstream error, for the agent to resubmit if it+# still wants the shot. Softening that by leaving the job running so a later pass retries is the+# tempting change and it is a re-buy: the render is banked and the model stood down BEFORE the+# submit is refused, so a second visit banks it twice (test_v42c pins this). The ceiling costs a+# retry, never a second charge._MEDIA_MAX_SUBMITS = 4@@ -7705,31 +7720,43 @@ class _MediaBudget:is the only evidence there is. That ordering is also what makes the ceiling hold when an agentfires 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.++ TWO LIFETIMES, ONE OBJECT, BECAUSE THEY ARE TWO DIFFERENT FACTS. `billed` is money and+ belongs to a JOB — it is seeded from what that job already bought and carries across the+ minutes between the tool call and the poll. `submits` is sockets and belongs to a REQUEST —+ nothing about one job says how many sockets the pass it is being polled in has opened. Keeping+ them in one object with one lifetime is what made the ceiling decorative on the poll path:+ `for_job` handed out a fresh count per job, so a request that polled six jobs made six+ submits and `_MEDIA_MAX_SUBMITS` never said a word."""- __slots__ = ("submits", "billed", "sharers")+ __slots__ = ("submits", "billed", "sharers", "walk")- def __init__(self) -> None:+ def __init__(self, walk: "_MediaBudget | None" = None) -> None:self.submits = 0self.billed = 0self.sharers = 0+ # WHOSE SUBMIT COUNT THIS SPENDS. Itself, for the budget a request opens; the request's,+ # for the per-job views `for_job` hands out below. A budget built with no argument starts+ # a NEW REQUEST'S COUNT, which is why only the three entry points may build one.+ self.walk = walk if walk is not None else self- @classmethod- def for_job(cls, job: dict) -> "_MediaBudget":- """The budget a POLL pass spends out of.+ def for_job(self, job: dict) -> "_MediaBudget":+ """The money ceiling THIS JOB carries, spending THIS REQUEST'S submit count.A poll happens minutes after the tool call that started the job, in some other request —usually the sweeper's, with no agent anywhere near it — so that call's budget is longgone. What survives is on the job: `advances` is what the submit walk and any earlier poll- already bought, and seeding it back here is what keeps the two walks sharing ONE ceiling- instead of each keeping a private count of the same money.-- ONLY THE MONEY CEILING CARRIES OVER. `submits` starts at zero because a poll is a new- request and nothing on the job records how many sockets an earlier one opened. So on this- path it is `billed` that refuses, and `_MEDIA_MAX_SUBMITS` never does — see the note on- that constant for what is therefore still uncapped.+ already bought, and seeding it back here is what keeps the two walks sharing ONE money+ ceiling instead of each keeping a private count of the same spend.++ THE SUBMIT COUNT DOES NOT RESTART, and that is the whole of round 6. This used to be a+ classmethod returning a budget with `submits` at zero, so a pass that polled N jobs made N+ submits against a ceiling of four and moving the ceiling moved nothing. It is an instance+ method now for exactly that reason: there is no way to ask for a job's money ceiling+ without already holding the request's socket count."""- b = cls()+ b = _MediaBudget(self.walk)b.billed = int(job.get("advances") or 0)return b@@ -7737,9 +7764,9 @@ class _MediaBudget:""""" 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:+ if self.walk.submits >= _MEDIA_MAX_SUBMITS:return "tried"- self.submits += 1+ self.walk.submits += 1self.billed += 1return ""@@ -7848,13 +7875,13 @@ async def _media_call(cand: dict, prov: dict, params: dict,return task_id, payload, doc if isinstance(doc, dict) else {}-# One budget per generate call — SHARED WITH EVERY CALL THAT OVERLAPS IT ON THE SAME SESSION.+# One budget per agent tool call — SHARED WITH EVERY CALL THAT OVERLAPS IT ON THE SAME SESSION._media_budgets: dict[str, _MediaBudget] = {}@contextlib.asynccontextmanagerasync def _media_budget_for(sid: str):- """The budget this generate call spends out of.+ """The budget this tool call spends out of — a generation, or the check_jobs that polls it.Per request rather than per session, because a session lives for hours and one budget for allof it would refuse an agent that has been working correctly since. But an agent parallelises:@@ -8317,9 +8344,15 @@ async def _media_job_live(job: dict) -> bool:return True-async def _media_job_advance(job: dict) -> dict:+async def _media_job_advance(job: dict, budget: _MediaBudget) -> dict:"""Move one job forward. THE one function that does — check_jobs, the app's poll route and the- sweeper all call it, so a job cannot mean one thing to a browser and another to an agent."""+ sweeper all call it, so a job cannot mean one thing to a browser and another to an agent.++ THE BUDGET IS THE PASS'S, not this job's. A poll that answers FAILURE advances the chain, and+ advancing it opens a socket: every job this function is called for in one request therefore+ counts against ONE `_MEDIA_MAX_SUBMITS`. It used to make its own per job, which is why six+ failing jobs meant six outbound submits from a single check_jobs. `for_job` below takes the+ money ceiling off the job and leaves the socket count where it is."""if job.get("status") != "running" or not job.get("provider_task_id"):return jobif float(job.get("next_poll_at") or 0) > time.time() * 1000:@@ -8342,7 +8375,7 @@ async def _media_job_advance(job: dict) -> dict:# placeholder that never resolves, which is why it sat there.if not await _media_job_live(fresh):return fresh- return await _media_job_poll(fresh)+ return await _media_job_poll(fresh, budget)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 this function looked at comes out of it@@ -8355,7 +8388,7 @@ async def _media_job_advance(job: dict) -> dict:return await _media_job_stalled(fresh)-async def _media_job_poll(job: dict) -> dict:+async def _media_job_poll(job: dict, budget: _MediaBudget) -> dict:"""One poll of one job, under its lock. Split out so the locking above reads as one thought."""now = time.time()cand = _media_candidate(str(job.get("capability") or ""), str(job.get("model") or ""))@@ -8385,7 +8418,7 @@ async def _media_job_poll(job: dict) -> dict:status, err = "failed", str(e)if status == "failed":- await _media_job_billable_failure(job, err, _MediaBudget.for_job(job))+ await _media_job_billable_failure(job, err, budget.for_job(job))return job# WHAT THE LAST POLL SAID, kept beside the status rather than in it. `unknown` is never a@@ -8495,7 +8528,7 @@ async def _media_job_billable_failure(job: dict, err: str, budget: _MediaBudget)THE BUDGET IS PASSED IN, never made here. A synchronous render reaches this INSIDE the toolcall that started it, and the submit it then makes is one of that call's — it is how onegenerate_image made five. The poll path is a different request and hands in the budget the- job carries (`_MediaBudget.for_job`). Either way the submit itself is claimed in+ job carries (`budget.for_job`). Either way the submit itself is claimed in`_media_call`; this only decides whether one is worth trying."""_media_billed(job, str(job.get("model") or ""), float(job.get("usd") or 0.0), err)@@ -8664,13 +8697,19 @@ async def _media_sweep() -> int:rows = await BACKING.graph.find(_MEDIA_JOB_LABEL, {"status": "running"})now_ms = time.time() * 1000moved = 0+ # ONE PASS IS ONE REQUEST. A poll that answers FAILURE advances the chain and that costs a+ # socket, so a sweep that finds fifty dead jobs may not fire fifty submits between two ticks+ # of a ten-second timer — the ones it refuses are simply advanced by the next pass. Per pass+ # rather than per session because the sweeper is not anybody's session: it is one job of work+ # this process does, and `_MEDIA_MAX_SUBMITS` is how much of it may leave the process at once.+ budget = _MediaBudget()for r in rows:job = _media_job_out(r)if not job or float(job.get("next_poll_at") or 0) > now_ms:continuetry:before = job.get("status")- job = await _media_job_advance(job)+ job = await _media_job_advance(job, budget)moved += int(job.get("status") != before)except Exception as e: # noqa: BLE001 — one bad job must not stop the sweepprint(f"[media] sweep: {job.get('id')} {type(e).__name__}", flush=True)@@ -8880,6 +8919,82 @@ def _media_json_text(obj) -> dict:return _tool_text(json.dumps(obj, ensure_ascii=False))+# ── the schemas above, enforced ───────────────────────────────────────────────────────────────+# Every tool declares its arguments and NOTHING CHECKED THEM. The bodies coerced by hand and+# assumed the declaration had held: `float(args["seconds"])` is a ValueError on the string "six"+# and a TypeError on {"n": 6}; `for j in args["job_ids"]` is a TypeError on a bare 5; a mistyped+# argument name read as "that argument was not sent". None of those are in the MediaError tree, so+# each one left the agent with an HTTP 500 — and a model writing "six" where a number goes is an+# ordinary turn, not an attack. The schema was already written; this makes it true.+_MCP_TYPES = {"string": str, "array": list, "object": dict}+_MCP_TYPE_WORDS = {"string": "a string", "number": "a number", "integer": "a whole number",+ "boolean": "true or false", "array": "a list", "object": "an object"}+++def _mcp_kind(v) -> str:+ """What a value IS, in the same words a refusal asks for."""+ if v is None:+ return "nothing"+ if isinstance(v, bool):+ return "true or false"+ if isinstance(v, (int, float)):+ return "a number"+ return {str: "a string", list: "a list"}.get(type(v), "an object")+++def _mcp_type_ok(want: str, v) -> bool:+ """Does `v` match a declared `type`? `True` is an int in Python and is not a number in JSON."""+ if want in ("number", "integer"):+ return (not isinstance(v, bool)+ and isinstance(v, int if want == "integer" else (int, float)))+ if want == "boolean":+ return isinstance(v, bool)+ py = _MCP_TYPES.get(want)+ return py is None or isinstance(v, py)+++def _mcp_argument_refusal(schema: dict, value, where: str = "") -> str:+ """The first way `value` does not match `schema`, said to the agent — "" when it does.++ THE PART OF THE SCHEMA THAT IS ABOUT SHAPE, and deliberately not the rest. `type`, `required`,+ `enum` and `additionalProperties` are what a tool body assumes before it touches an argument,+ and a wrong shape is what raises. `minimum`, `maxLength` and `pattern` are NOT enforced here:+ those are value limits that the tool bodies and the media plane already answer for themselves,+ and a second opinion on a limit, kept in step by hand, is the exact thing this file keeps+ finding. One rule, one place.+ """+ at = f" for {where}" if where else ""+ if "enum" in schema and value not in schema["enum"]:+ return (f"{where or 'that'} has to be one of "+ f"{', '.join(json.dumps(e) for e in schema['enum'])}; "+ f"{json.dumps(value)[:60]} was sent.")+ want = str(schema.get("type") or "")+ if want and not _mcp_type_ok(want, value):+ return (f"{where or 'that argument'} has to be {_MCP_TYPE_WORDS.get(want, want)}; "+ f"{_mcp_kind(value)} was sent.")+ if isinstance(value, dict):+ props = schema.get("properties") or {}+ for req in schema.get("required") or []:+ if req not in value:+ return f"{req} is required{at} and was not sent."+ if schema.get("additionalProperties") is False:+ for k in value:+ if k not in props:+ return (f"there is no argument called {k}{at} — the ones it takes are "+ f"{', '.join(sorted(props)) or 'none'}.")+ for k, v in value.items():+ bad = _mcp_argument_refusal(props[k], v, f"{where}.{k}" if where else k) \+ if k in props else ""+ if bad:+ return bad+ elif isinstance(value, list) and isinstance(schema.get("items"), dict):+ for i, v in enumerate(value):+ bad = _mcp_argument_refusal(schema["items"], v, f"{where}[{i}]")+ if bad:+ return bad+ return ""++async def _media_read_image(sid: str, med: str) -> tuple[str, str]:"""(base64, mime) for one image OUT OF OUR OWN STORE. Never out of a provider URL: thoseexpire, and a continuity frame that stops resolving is a different character in shot four."""@@ -9041,47 +9156,54 @@ async def _media_tool_call(name: str, args: dict, hid: str, sid: str, org: str,if name == "check_jobs":ids = [str(j) for j in (args.get("job_ids") or [])][:50]rows = []- for jid in ids:- job = await _media_job_get(jid)- if not job or str(job.get("session") or "") != sid:- # NEVER omitted: a missing row reads as "still running".- rows.append({"job_id": jid, "status": "failed",- "error": "No job with that id in this session."})- continue- try:- job = await _media_job_advance(job)- except Exception as e: # noqa: BLE001- print(f"[media] advance {jid}: {type(e).__name__}", flush=True)- row = {"job_id": jid, "status": job.get("status"),- "capability": job.get("capability"), "model": job.get("model")}- if job.get("status") == "running" and job.get("last_poll") == "unknown":- # The known background-response race. Said plainly, because a model that reads an- # empty record as failure throws away a render that is about to land.- row["status"] = "unknown"- row["note"] = ("The provider returned an empty record. This is transient — ask "- "again.")- if job.get("status") == "succeeded":- row.update({"media_id": job.get("media_id"),- "kind": _MEDIA_KIND.get(str(job.get("capability")), "image"),- "seconds": job.get("seconds") or None,- "width": job.get("width") or None, "height": job.get("height") or None,- "bytes": job.get("bytes") or None})- if job.get("usd"):- row["usd"] = round(float(job["usd"]), 4)- if job.get("spoken_text"):- row["spoken_text"] = job["spoken_text"]- row["verbatim"] = media_plane.levenshtein_ratio(- (job.get("params") or {}).get("text") or "",- str(job["spoken_text"])) >= 0.9- elif job.get("status") == "failed":- row["error"] = job.get("error") or "the render failed"- if job.get("attempts"):- row["attempts"] = job["attempts"]- elif job.get("progress"):- row["progress"] = job["progress"]- if job.get("element_id"):- row["element_id"] = job["element_id"]- rows.append(row)+ # ONE BUDGET FOR THE WHOLE CALL, not one per job. A poll that answers FAILURE advances+ # the chain, and an advance is an outbound submit — so six failing jobs in one check_jobs+ # meant six of them, from what the agent issued as a READ. The same budget a generate+ # call opens, for the same reason: overlapping work on one session shares one ceiling.+ async with _media_budget_for(sid) as budget:+ for jid in ids:+ job = await _media_job_get(jid)+ if not job or str(job.get("session") or "") != sid:+ # NEVER omitted: a missing row reads as "still running".+ rows.append({"job_id": jid, "status": "failed",+ "error": "No job with that id in this session."})+ continue+ try:+ job = await _media_job_advance(job, budget)+ except Exception as e: # noqa: BLE001+ print(f"[media] advance {jid}: {type(e).__name__}", flush=True)+ row = {"job_id": jid, "status": job.get("status"),+ "capability": job.get("capability"), "model": job.get("model")}+ if job.get("status") == "running" and job.get("last_poll") == "unknown":+ # The known background-response race. Said plainly, because a model+ # that reads an empty record as failure throws away a render that is+ # about to land.+ row["status"] = "unknown"+ row["note"] = ("The provider returned an empty record. This is transient "+ "— ask again.")+ if job.get("status") == "succeeded":+ row.update({"media_id": job.get("media_id"),+ "kind": _MEDIA_KIND.get(str(job.get("capability")), "image"),+ "seconds": job.get("seconds") or None,+ "width": job.get("width") or None,+ "height": job.get("height") or None,+ "bytes": job.get("bytes") or None})+ if job.get("usd"):+ row["usd"] = round(float(job["usd"]), 4)+ if job.get("spoken_text"):+ row["spoken_text"] = job["spoken_text"]+ row["verbatim"] = media_plane.levenshtein_ratio(+ (job.get("params") or {}).get("text") or "",+ str(job["spoken_text"])) >= 0.9+ elif job.get("status") == "failed":+ row["error"] = job.get("error") or "the render failed"+ if job.get("attempts"):+ row["attempts"] = job["attempts"]+ elif job.get("progress"):+ row["progress"] = job["progress"]+ if job.get("element_id"):+ row["element_id"] = job["element_id"]+ rows.append(row)# `unknown` never appears as a terminal state here; it is a poll that came back empty and# the tool description says to ask again.return _media_json_text({"jobs": rows})@@ -9469,6 +9591,13 @@ async def media_mcp(request: Request):THE SESSION COMES FROM THE CREDENTIAL. No tool takes one as an argument and every schema is`additionalProperties: false`, so an agent cannot address another session's canvas — it cannotmint another session's token.++ EVERY FAILURE IS A TOOL RESULT AND NEVER AN HTTP ERROR, and below it is finally STRUCTURAL+ rather than a list of exception types. `except MediaError` / `except HTTPException` covered+ the failures somebody had thought of, so a ValueError from parsing an argument, a KeyError+ from building a submit and anything the store raised all left as a 500 — a status code an+ agent cannot read, on a path a well-behaved model reaches by writing "six" where a number+ goes. The two named handlers stay, because each says something better than the last one can."""claims = _verify_hosted_cred(_broker_token(request))if not claims:@@ -9504,13 +9633,30 @@ async def media_mcp(request: Request):return _jsonrpc_result(rid, _tool_text("This agent cannot make media — no media tools are connected to it. Ask the person to ""add them, then try again — you cannot connect them yourself.", True))+ name, args = str(params.get("name") or ""), params.get("arguments") or {}+ schema = next((t["inputSchema"] for t in _MEDIA_MCP_TOOLS if t["name"] == name), None)+ # CHECKED AGAINST THE DECLARATION, before any body assumes it held. An unknown tool is not+ # checked — `_media_tool_call` has a better sentence for that than "no schema".+ bad = _mcp_argument_refusal(schema, args) if schema is not None else ""+ if bad:+ return _jsonrpc_result(rid, _tool_text(+ f"{name} was not run, because {bad} Nothing happened; correct the call and repeat it.",+ True))try:- out = await _media_tool_call(str(params.get("name") or ""), params.get("arguments") or {},- hid, sid, org, str(entry.get("id") or "mcp.media"))+ out = await _media_tool_call(name, args, hid, sid, org,+ str(entry.get("id") or "mcp.media"))except media_plane.MediaError as e:return _jsonrpc_result(rid, _tool_text(str(e), True))except HTTPException as e:return _jsonrpc_result(rid, _tool_text(_uhp_message(e), True))+ except Exception as e: # noqa: BLE001 — the promise, made structural: see this route's note+ # Named for the operator, not for the agent: what raised is ours to fix and not something+ # a model can act on, and a stack trace is not a sentence.+ print(f"[media] {name}: {type(e).__name__}: {e}", flush=True)+ return _jsonrpc_result(rid, _tool_text(+ f"{name} did not finish — something went wrong on this side, not in the way you "+ f"called it. Try it once more; if it fails the same way, tell the person rather than "+ f"working around it.", True))return _jsonrpc_result(rid, out)@@ -9626,15 +9772,30 @@ async def put_media_scene(hid: str, eid: str, sid: str, body: SceneWrite,async def get_media_jobs(hid: str, eid: str, sid: str, request: Request,ids: str = "") -> dict:"""The same _media_job_advance path the agent's check_jobs takes, so a browser and an agent- cannot see a job differently."""+ cannot see a job differently.++ EVERY job in the session, deliberately — a listing that dropped rows past a limit would be a+ canvas with clips missing from it. What is capped is not how many jobs are LISTED but how many+ outbound submits one tick of this may cause, and that is the budget below: a browser sitting+ on a session where several renders have failed used to fire one submit per failed job per+ tick, which is the same burst check_jobs made and from something the person did not ask for.++ ITS OWN BUDGET, not the session's shared one. `_media_budget_for` exists so that generate+ calls an agent issued in one turn share a ceiling — "a second render is the agent's decision,+ made with the first one's cost in front of it". A browser tick is not the agent's decision and+ is not part of that turn, so joining it there would let a person watching the canvas spend a+ ceiling the agent then gets refused against, with a sentence about "this request" that would+ be about somebody else's. One request, one budget; these are two requests.+ """await _media_route(hid, sid, eid, request)wanted = {i.strip() for i in (ids or "").split(",") if i.strip()}out = []+ budget = _MediaBudget()for job in await _media_jobs_of(sid):if wanted and job["id"] not in wanted:continuetry:- job = await _media_job_advance(job)+ job = await _media_job_advance(job, budget)except Exception: # noqa: BLE001passout.append({"job_id": job["id"], "status": job.get("status"),diff --git a/gateway/tests/test_media_attack.py b/gateway/tests/test_media_attack.pyindex 5ce8ff55..08eb015e 100644--- a/gateway/tests/test_media_attack.py+++ b/gateway/tests/test_media_attack.py@@ -1350,10 +1350,32 @@ def test_v41b_the_budget_is_claimed_where_the_socket_is_opened_and_nowhere_else(f"the budget is given back in {sorted(owner.get('free') or [])}; claim and free have to "f"be the same pair of hands or the money count drifts")- for name in ("_media_call", "_media_job_resubmit", "_media_job_billable_failure"):+ for name in ("_media_call", "_media_job_resubmit", "_media_job_billable_failure",+ "_media_job_advance", "_media_job_poll"):assert "budget" in inspect.signature(getattr(app, name)).parameters, (f"{name} takes no budget, so whatever it submits is outside the ceiling")+ # AND IT IS THE REQUEST'S BUDGET, which is the half the signature above cannot say. A caller+ # writing `_media_job_advance(job, _MediaBudget())` satisfies every assertion up to here and+ # is the original bug verbatim: a private ceiling per call bounds nothing, and that is exactly+ # what `_MediaBudget.for_job` used to hand the poll walk — one budget per JOB, `submits` back+ # at zero, so the number could not refuse a thing. A budget built with NO ARGUMENT starts a+ # new count, and a new count may only begin where a request does — an agent's turn, a sweep+ # pass, a browser tick, and nothing finer than one of those.+ fresh = set()+ for fn in ast.walk(tree):+ if not isinstance(fn, (ast.FunctionDef, ast.AsyncFunctionDef)):+ continue+ for n in ast.walk(fn):+ if isinstance(n, ast.Call) and isinstance(n.func, ast.Name) \+ and n.func.id == "_MediaBudget" and not n.args and not n.keywords:+ fresh.add(fn.name)+ assert fresh == {"_media_budget_for", "_media_sweep", "get_media_jobs"}, (+ f"a fresh submit count is started in {sorted(fresh)}. Every name here claims to be the "+ f"start of a request — an agent's turn, a sweep pass, a browser tick: one budget per "+ f"anything smaller — per job, per call, per candidate — is a ceiling that counts only "+ f"itself, and no number binds it")+def test_v41c_a_renamed_provider_field_is_a_tool_result_and_not_an_http_error(client500, client,kit, hostile):@@ -1520,3 +1542,217 @@ def test_v41g_no_test_in_this_suite_asserts_on_a_coroutine_nobody_awaited(clientassert not dead, ("these assertions are about a coroutine object and not about what it does:\n "+ "\n ".join(dead))+++# ══════════════════════════════════════════════════════════════════════════════════════════════+# ROUND 6 — THE CEILING BELONGS TO THE REQUEST, AND A BAD ARGUMENT IS STILL A SENTENCE+#+# Round 5 moved the claim to the socket, which made `_MEDIA_MAX_SUBMITS` true of the SUBMIT walk+# for a caller that had never heard of it. It left the number DECORATIVE on the three POLL+# entries: each built one budget per JOB out of what that job had already spent, so `submits`+# started at zero every time and the walk ceiling could not refuse anything. Measured: one+# check_jobs over six failing jobs made six outbound submits against a ceiling of four, and moving+# the ceiling did not move the count.+#+# NOT AN OVERSPEND, and these tests must not be read as claiming one. Per job the money rule held+# — `advances` still capped each job at `_MEDIA_MAX_ADVANCES + 1` renders — and the total across+# all sweeps was unchanged. It is a BURST, not extra money. What was wrong is that the constant+# says "how many outbound submits ONE REQUEST may make AT ALL" and that sentence was false on all+# three poll entries. A ceiling that claims more than it counts is how four rounds survived.+# ══════════════════════════════════════════════════════════════════════════════════════════════++def _running_video_jobs(client, hid, sid, n: int) -> list[str]:+ """`n` jobs that a provider has accepted and is rendering. Every one on the first candidate,+ so they all fail the same way and the count below is about the ceiling and not about which+ model happened to answer."""+ return [_ok(_call(client, _cred(hid, sid), "generate_video",+ {"prompt": f"shot {i}", "seconds": 6}))["job_id"] for i in range(n)]++[email protected]("entry", ["check_jobs", "the sweeper", "the app's jobs route"])+def test_v42_one_poll_pass_makes_no_more_submits_than_the_ceiling_it_names(client, kit, hostile,+ entry):+ """SIX SUBMITS FROM ONE READ. `_MEDIA_MAX_SUBMITS` is four.++ A poll that answers FAILURE advances the chain, and advancing the chain opens a socket with+ this deployment's key on it. The budget that submit is claimed against was built by+ `_MediaBudget.for_job`, which seeds only `billed` — so on a poll pass the walk counter was+ always zero and `claim()` always said yes. What actually bounded the path was the `advances`+ check one branch over in `_media_job_billable_failure`: a guard beside the socket, which is+ the exact construction round 5 set out to delete.++ All three poll entries are driven, because a cap on one of them is a cap on one of them: an+ agent's check_jobs, the background sweep and the browser's own tick each walk the same+ `_media_job_advance` and each used to walk it with a private ceiling per job.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ jids = _running_video_jobs(client, hid, sid, 6)+ hostile.poll_answer = httpx.Response(200, json={"code": "success", "data": {+ "status": "FAILURE", "message": "the render failed upstream"}})+ for jid in jids:+ _due(jid)+ submits = _count_submits(hostile) # installed AFTER the six starting submits++ if entry == "check_jobs":+ _call(client, _cred(hid, sid), "check_jobs", {"job_ids": jids})+ elif entry == "the sweeper":+ asyncio.run(app._media_sweep())+ else:+ r = client.get(f"/v1/harnesses/{hid}/servers/{ENTRY_ID}/sessions/{sid}/jobs",+ headers=HEADERS)+ assert r.status_code == 200, r.text++ assert submits, f"[{entry}] no job was advanced at all — this test proves nothing"+ assert len(submits) <= app._MEDIA_MAX_SUBMITS, (+ f"[{entry}] ONE PASS MADE {len(submits)} OUTBOUND SUBMITS against a ceiling of "+ f"{app._MEDIA_MAX_SUBMITS}: {submits}")+++def test_v42b_moving_the_ceiling_moves_the_count_on_the_poll_walk_too(client, kit, hostile,+ monkeypatch):+ """THE TEST THAT WOULD HAVE CAUGHT IT. A number that is real answers when you turn it.++ With `_MEDIA_MAX_SUBMITS` set to 1, one check_jobs over six failing jobs made six submits and+ zero refusals — identical to the count at four. That is what a decorative ceiling looks like+ from outside, and it is measurable without knowing a thing about which branch does the work.+ """+ monkeypatch.setattr(app, "_MEDIA_MAX_SUBMITS", 1)+ hid = _launch(client, kit)+ sid = _session(hid)+ jids = _running_video_jobs(client, hid, sid, 6)+ hostile.poll_answer = httpx.Response(200, json={"code": "success", "data": {+ "status": "FAILURE", "message": "the render failed upstream"}})+ for jid in jids:+ _due(jid)+ submits = _count_submits(hostile)+ _call(client, _cred(hid, sid), "check_jobs", {"job_ids": jids})+ assert len(submits) <= 1, (+ f"the ceiling was moved to 1 and the pass still made {len(submits)} submits: {submits} — "+ f"whatever bounds this path, it is not the number that claims to")++[email protected]("tool,args,what", [+ ("generate_video", {"prompt": "a cat", "seconds": "six"}, "a number written as a word"),+ ("generate_video", {"prompt": "a cat", "seconds": {"n": 6}}, "an object where a number goes"),+ ("generate_video", {"prompt": "a cat", "seconds": 6, "aspect": "square"}, "a value not in the"+ " enum"),+ ("generate_image", {"prompt": ["a", "cat"]}, "a list where the string goes"),+ ("arrange", {"columns": "four"}, "a count written as a word"),+ ("check_jobs", {"job_ids": 5}, "a bare number where the list of ids goes"),+ ("check_jobs", {"job_id": "mjob_1"}, "the singular of the argument's name"),+ ("place", {"items": "mjob_1"}, "a string where the array goes"),+ ("set_timeline", {"shots": [{"element_id": "e1", "in_s": "start"}]}, "a word inside an item"),+])+def test_v43_an_argument_the_schema_refuses_is_a_tool_result_and_never_an_http_error(+ client500, client, kit, hostile, tool, args, what):+ """A MODEL WROTE "six" WHERE THE SCHEMA SAYS {"type": "number"}. That is a Tuesday, not an+ attack, and it was an HTTP 500.++ Nothing validates arguments against the declared schema before `_media_tool_call` runs, and+ the tool bodies coerce by hand: `float(args.get("seconds") or 0)` raises ValueError on "six"+ and TypeError on {"n": 6}; `for j in (args.get("job_ids") or [])` raises TypeError on a bare+ number. None of those are in the MediaError tree and none are HTTPException, so they went past+ both handlers at the route and left the agent with a status code it cannot read — the same+ promise this catalog is built around, broken one level away from where round 5 fixed it.++ No money is at stake on any of these. What is at stake is that the agent gets a sentence and+ can correct itself, instead of a 500 it will read as "the tool is broken" and route around.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ r = client500.post("/v1/mcp/media",+ json={"jsonrpc": "2.0", "id": 1, "method": "tools/call",+ "params": {"name": tool, "arguments": args}},+ headers={"authorization": f"Bearer {_cred(hid, sid)}"})+ assert r.status_code == 200, (+ f"{tool} with {what} answered HTTP {r.status_code} instead of a tool result: "+ f"{r.text[:400]}")+ res = r.json()["result"]+ assert res.get("isError") is True, (+ f"{tool} with {what} was accepted rather than refused: {res}")++[email protected]("where,mod,name,exc,tool", [+ ("build_submit", "media_plane", "build_submit", KeyError("shape"), "generate_image"),+ ("_media_providers", "app", "_media_providers", RuntimeError("blip"), "generate_image"),+ ("_media_job_save", "app", "_media_job_save", RuntimeError("blip"), "generate_image"),+ ("_media_job_land", "app", "_media_job_land", ValueError("not a frame"), "generate_image"),+ ("_media_store", "app", "_media_store", OSError("no space left"), "generate_image"),+ ("_media_scene_read", "app", "_media_scene_read", RuntimeError("blip"), "describe_canvas"),+ ("_media_scene_write", "app", "_media_scene_write", RuntimeError("blip"), "arrange"),+])+def test_v44_a_failure_nobody_wrote_an_except_for_is_a_tool_result_and_never_an_http_error(+ client500, client, kit, hostile, monkeypatch, where, mod, name, exc, tool):+ """THE STRUCTURAL HALF OF THE SAME PROMISE. "Every failure is a tool result and never an HTTP+ error" is enforced by an EXCEPTION-TYPE LIST — `except MediaError`, `except HTTPException` —+ so it holds for the failures somebody thought of and for no others.++ Round 5 made this structural INSIDE `_media_call`, which is why a renamed provider field is a+ sentence now. Everywhere else it is still a list: `build_submit` sits outside that try (the+ claim is the line after it), and the store, the scene and the file writer are not in it at+ all. Each row here raises a type no handler names, from a real place, on the ordinary path.++ This test names no branch on purpose. It asserts what the promise says: whatever fails, the+ agent gets a sentence.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ target = app if mod == "app" else media_plane++ def boom(*_a, **_kw):+ raise exc++ monkeypatch.setattr(target, name, boom)+ args = {"prompt": "a cat"} if tool == "generate_image" else {}+ r = client500.post("/v1/mcp/media",+ json={"jsonrpc": "2.0", "id": 1, "method": "tools/call",+ "params": {"name": tool, "arguments": args}},+ headers={"authorization": f"Bearer {_cred(hid, sid)}"})+ assert r.status_code == 200, (+ f"{where} raising {type(exc).__name__} answered HTTP {r.status_code}, not a tool result: "+ f"{r.text[:400]}")+ res = r.json()["result"]+ assert res.get("isError") is True, f"{where} raising {type(exc).__name__} read as success: {res}"+ assert _text(res).strip(), f"{where} raising {type(exc).__name__} produced an empty sentence"+++def test_v42c_a_ceiling_that_refuses_an_advance_does_not_let_a_later_pass_buy_it_twice(+ client, kit, hostile):+ """THE MONEY RULE, ACROSS THE PASSES THE CEILING NOW SPLITS THE WORK INTO.++ The count is per request, so a pass that hits four refuses the rest — and the obvious way to+ soften that is to leave the refused jobs running so the next pass picks them up. Done without+ care that is a re-buy: `_media_job_billable_failure` has already banked the render and stood+ the model down by the time the submit is refused, so a second visit banks it again and the+ session's spend climbs for renders nobody bought.++ So this asserts what the finding this all came from was careful to say: the burst is gone and+ the TOTAL is not larger. Six failing jobs, swept to quiescence — every job terminal, every one+ of them with at most `_MEDIA_MAX_ADVANCES` advances on it, and no more outbound submits in+ total than one advance each.+ """+ hid = _launch(client, kit)+ sid = _session(hid)+ jids = _running_video_jobs(client, hid, sid, 6)+ hostile.poll_answer = httpx.Response(200, json={"code": "success", "data": {+ "status": "FAILURE", "message": "the render failed upstream"}})+ submits = _count_submits(hostile)+ for _ in range(6):+ for jid in jids:+ job = asyncio.run(app._media_job_get(jid))+ if job["status"] == "running":+ _due(jid)+ asyncio.run(app._media_sweep())++ jobs = [asyncio.run(app._media_job_get(j)) for j in jids]+ assert not [j for j in jobs if j["status"] == "running"], (+ f"six sweeps left {len([j for j in jobs if j['status'] == 'running'])} jobs running — the "+ f"ceiling stranded them instead of refusing them, and they will be re-read for ever")+ over = [(j["id"], j.get("advances")) for j in jobs+ if int(j.get("advances") or 0) > app._MEDIA_MAX_ADVANCES]+ assert not over, f"jobs advanced past the money rule once the passes were split: {over}"+ assert len(submits) <= len(jids) * app._MEDIA_MAX_ADVANCES, (+ f"{len(submits)} submits across six passes for {len(jids)} jobs allowed "+ f"{app._MEDIA_MAX_ADVANCES} advance each: {submits} — the per-request count has become a "+ f"per-pass allowance, which is the same money spent again")diff --git a/gateway/tests/test_media_mcp.py b/gateway/tests/test_media_mcp.pyindex 7c1b5928..4f493e35 100644--- a/gateway/tests/test_media_mcp.py+++ b/gateway/tests/test_media_mcp.py@@ -970,7 +970,10 @@ def test_one_finished_render_is_downloaded_and_stored_exactly_once(client, harneawait app._media_job_save(job)fresh = await app._media_job_get(jid)# Five callers, all holding the same pre-landing snapshot — the real shape of the race.- return await asyncio.gather(*[app._media_job_advance(dict(fresh)) for _ in range(5)])+ # A budget each, because that is what five racers ARE: five requests. Sharing one here+ # would let the submit ceiling settle the race and the per-job lock would go untested.+ return await asyncio.gather(*[app._media_job_advance(dict(fresh), app._MediaBudget())+ for _ in range(5)])out = asyncio.run(race())assert all(j["status"] == "succeeded" for j in out), [j["status"] for j in out]