Skip to content

media: an agent reads and adds its session's own files, and one still is composed from several - #809

Draft
ricki-epsilla wants to merge 5 commits into
HarnessRouter:mainfrom
ricki-epsilla:feat/media-agent-files
Draft

ricki-epsilla wants to merge 5 commits into
HarnessRouter:mainfrom
ricki-epsilla:feat/media-agent-files

Conversation

@ricki-epsilla

@ricki-epsilla ricki-epsilla commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Additions to the media server, agreed on 2026-12-01 for the ViMax base (a base that edits its own film needs the bytes behind the ids, and its storyboard composes one still from several pictures). Nothing here is specific to ViMax. The base itself is a separate PR that will sit on top of this one.

CAT: the two file routes are the part to review before merge (_media_agent, media_agent_file, media_agent_upload in gateway/app.py).

What is added

1. GET /v1/mcp/media/files/{med}: the bytes behind one media id

2. POST /v1/mcp/media/files: keep one picture the agent already has

Both sit beside the MCP endpoint and follow the conditions set for them:

Condition How it is met
Only the media server's own credential _verify_hosted_cred, the same check POST /v1/mcp/media makes. An organization member's key, the model broker's turn credential, an expired or altered credential: 401.
Session and organization from the credential, never the URL Neither address has a session in it. Query parameters and session headers are not read. The harness, then the organization, then the session are read from the credential and checked against what is stored: the agent not deleted, the media tools still connected and switched on, the session existing, not deleted, on that agent and in that organization.
Another session's media id is a 404 The body is identical to the 404 for an id that does not exist. An id of any other shape than med_ + 16 hex never reaches the store lookup (the lookup is by prefix).
Upload caps PNG, JPEG or WebP only, decided from the bytes. 20 MB per file (HR_MEDIA_AGENT_UPLOAD_MAX_BYTES), held as the body arrives, so a body longer than it declares stops at the limit. 100 per session (HR_MEDIA_AGENT_UPLOADS_PER_SESSION), counted under a per-session lock. Where ffmpeg is installed, a picture that cannot be measured or a frame over 8192 x 8192 is refused. A refused upload stores nothing and counts nothing.
Counts toward storage The session vertex carries media_upload_bytes, and /internal/storage-usage adds it to the organization's total beside checkpoint_bytes.
Rate limits Per session and per route, per minute: 240 fetches, 30 uploads (HR_MEDIA_AGENT_FETCH_PER_MIN, HR_MEDIA_AGENT_UPLOAD_PER_MIN). 429 with retry-after. A refused request is not counted, and a request without a valid credential uses nobody's allowance.
Attack tests gateway/tests/test_media_agent_files.py, written as test_media_attack.py is: passing means the attack was repelled.

An upload is a picture only. A clip or a sound is made here and never brought here, so there was no reason to accept more kinds of bytes an agent chooses.

3. generate_image takes from_images

Up to 6 media ids or job ids, each resolved inside the session by the function from_image already uses.

  • Every image is sent, or the candidate is skipped (can_serve). When no candidate takes that many, the call is refused and the refusal says how many each takes. Nothing is sent with fewer images.
  • A candidate says how many it takes with max_input_images in the catalogue. Only the gemini and openai shapes can carry several. An entry on another shape that claimed more is still sent one.
  • A list of one is the from_image request: same bytes on the wire, same job record. from_image and from_images together are refused.
  • The images are not written onto the job. It keeps their ids (from_medias) and a chain that moves on reads them back, as from_media does.
  • All of them together are limited to 40 MB of base64 (HR_MEDIA_INPUT_IMAGES_MAX_BYTES).

The single-image request is held by test_the_single_image_request_is_byte_for_byte_what_it_always_was, which compares the body sent to the provider with the literal JSON.

4. list_capabilities says the session's limit and what it has spent

"session": {"budget_usd": 25.0, "spent_usd": 3.36} beside capabilities, only for a credential that names a session. An agent that plans a whole film before it renders one clip can only refuse to start what it cannot finish if it is told the limit. Until now the limit was first heard of as the refusal that hit it. spent_usd is the same measured sum the limit is held against (_media_spend). The list of capabilities itself is unchanged. Added after the PR was opened, in its own commit.

Existing code this touches

The two routes are new. Several input images add a branch in six existing places, and none of them changes what a request without from_images does:

  • the generate_image tool schema (one more optional property)
  • _media_tool_call (a block before the from_image block)
  • _media_params_stored and _media_job_resubmit (two more keys left off the job, read back on a resubmit)
  • media_plane.can_serve (one more check, only when images is present)
  • media_plane.build_submit, gemini and openai shapes (a loop after the single image, empty without images)
  • media_catalog.json (max_input_images on the one entry measured to take several, and what the other three answered)
  • /internal/storage-usage (adds media_upload_bytes, absent on every session that never uploaded)
  • list_capabilities (one more key in its answer, for a credential with a session)

Measured on 2026-12-02

How many input images each image_to_image candidate on the gemini and openai shapes takes, with one paid call per count on the test TokenRouter key. Each request was built by this PR's media_plane.build_submit. Each input was a square of one plain colour and the prompt asked for a row of squares in those colours, in order, so the picture that came back shows which inputs the model saw.

Candidate Result
google/gemini-3.1-flash-lite-image 1 to 6 images: every call answered with a picture that shows every colour sent, in the order sent. The provider counted 1120 prompt image tokens for each image. 6 to 13 s a call
google/gemini-3.1-flash-image-preview HTTP 404 "Not found." at its first call
microsoft/mai-image-2.5 HTTP 404: deprecated by Microsoft on Nov 27, 2026, use mai-image-2.6 or mai-image-2.7
openai/gpt-5-image-mini HTTP 404: no provider serves this model anymore

The catalogue now says what was measured: gemini-3.1-flash-lite-image takes 6 and is verified for image input, and the other three carry what they answered and claim one image. So several input images run on one model today, with no fallback. When that model fails, the call is refused with the reason, and is not retried on a model that would be sent fewer images (held by a test).

Not verified

  • The openai shape with several images has never reached a model. Both of its candidates answered 404. The builder is tested against the request it should make, not against a provider.
  • The three models that answered 404 are also in text_to_image, marked verified there from earlier measurements. I did not call them without an image, and did not change those entries. mai-image-2.5 is the only image model with a measured price.
  • The tests that need ffmpeg do not run on my machine (Windows, no ffmpeg). They run in CI, which is green.

Tests

gateway/tests/test_media_agent_files.py: 69 pass and 2 are skipped without ffmpeg (CI has it, and runs them). test_media_attack.py and test_media_mcp.py give the same results before and after this change. Mutation check on a scratch copy: 32 separate breakages of the new code (a session of another agent accepted, no rate limit, the count taken without the lock, a candidate that takes fewer not skipped, the first image sent twice on the single path, and so on) each turn a test red.

Things found on the way that this PR does not change

  • /internal/storage-usage counts workspace checkpoints only. Rendered media and the pictures a person imports through the app are not counted today. This PR counts the agent's uploads and leaves the other two as they are.
  • from_image with a clip stores the clip's id as from_media, and a resubmit then reads that id as an image (_media_read_image), which hands the provider an mp4. The new from_medias path reads back through _media_resolve_input_image, which takes the frame. I left the single-image path alone to keep it byte for byte.

Hosted port (HR2)

What the hosted service needs to carry over, for planning with HR2:

  1. The rate limit and the upload lock are in this process. With more than one gateway replica, the per-minute counts are per replica and two uploads on different replicas can both read the count below the limit. Hosted needs both in the shared store (Redis is already a dependency).
  2. media_upload_bytes on the session vertex has to reach whatever reads storage for billing on hosted, if that is not /internal/storage-usage.
  3. Five new settings, all with defaults: HR_MEDIA_AGENT_UPLOAD_MAX_BYTES, HR_MEDIA_AGENT_UPLOAD_MAX_PIXELS, HR_MEDIA_AGENT_UPLOADS_PER_SESSION, HR_MEDIA_AGENT_FETCH_PER_MIN, HR_MEDIA_AGENT_UPLOAD_PER_MIN, and HR_MEDIA_INPUT_IMAGES_MAX_BYTES.
  4. Request size at the edge. An upload is up to 20 MB in one request body. Any proxy in front of the hosted gateway with a smaller limit will answer before this code does.

HR2, please say whether this matches how hosted is deployed, and what else the port needs.

🤖 Generated with Claude Code

ricki-epsilla and others added 2 commits December 2, 2026 02:37
… can be composed from several

Two routes beside the media MCP endpoint, on the same per-turn credential and nothing else:
GET /v1/mcp/media/files/{med} returns the bytes of one media id, and POST /v1/mcp/media/files
keeps one picture the agent already has. The session, the agent and the organization are read
out of the credential; neither address names a session, and a media id that is not in the
credential's own session is a 404 that reads as an unknown id. An upload is a PNG, JPEG or WebP
of at most 20 MB, a session holds at most 100, both routes are limited per session per minute,
and what is kept is counted in the organization's storage.

generate_image takes from_images, up to 6 ids each resolved inside the session. Every one is
sent or the candidate is skipped; when no candidate takes that many the call is refused and
says so. A list of one is the from_image request, the same bytes on the wire. Two gemini
candidates of image_to_image say they take 6 (inferred, like the rest of that capability).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
… in for and with ffmpeg

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@vercel

vercel Bot commented Dec 2, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
unified-harness-protocol Ready Ready Preview Dec 2, 2026 5:43am UTC

Request Review

An agent that plans a whole film before it renders one clip can only refuse to start what it
cannot finish if it is told the limit. Only for a credential that names a session; the list of
capabilities itself is unchanged.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
gemini-3.1-flash-lite-image takes 1 to 6 input images and is verified for image input: every call
answered with a picture showing every colour sent, in order, and the provider counted 1120 prompt
image tokens for each image. The other three candidates on the two shapes that can carry several
answered 404 at their first call (one not found, one deprecated, one no longer served); each
entry says what it answered and claims one image.

So several input images have one model. A test holds that when it fails the call says so and no
model that would be sent fewer images is tried.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

This branch was successfully deployed

1 active deployment
Preview — 509502bb Deployed Dec 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant