Skip to content

feat(serve): image upload, prompt images and image read route (#558) - #562

Merged
ajianaz merged 2 commits into
developfrom
feat/558-serve-images
Nov 11, 2026
Merged

ajianaz merged 2 commits into
developfrom
feat/558-serve-images

Conversation

@ajianaz

@ajianaz ajianaz commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

What

Image input for graphical clients on tole serve, behind the existing bearer token:

  • POST /sessions/{id}/images: upload one image as the raw request body. Answers {id, mime, bytes, fnv1a64}. The id is the content hash, and no server path is returned.
  • POST /sessions/{id}/prompt accepts "images": [<id>] next to text.
  • GET /sessions/{id}/images/{fnv1a64}: returns the bytes of an image recorded in that session's log.

Closes #558

Why

Image input exists in tole-core (#212) and the CLI (--image), but the serve face had no way to attach or show an image. Mobile, desktop and web clients (epic #523) need it.

Changes

  • New tole_cli::serve_images module (UploadStore, read_recorded, ImageApiError, Quotas).
    • Upload: the type is sniffed from the bytes; the request's Content-Type is ignored. The body goes through the same core path as --image: tole_core::image::resolve (jail, 10 MiB cap, sniff, FNV-1a 64). It is stored content-addressed at <spill dir>/serve/<session id>/<fnv1a64>.<ext>. The provider already reads images from the spill dir, so images are re-sent after a restart and resume. Uploading the same bytes again returns the same reference and uses no extra quota.
    • Prompt: an id resolves only against that session's uploads and is checked against its hash before the turn starts. The turn records the reference exactly like --image; the image data is never written to the log.
    • Read: an image is served only when the session log records it. The jail, the recorded size and type, and the hash of the bytes actually served are re-checked on every read. The response has a fixed Content-Type (the recorded mime), X-Content-Type-Options: nosniff and Cache-Control: private, no-store.
    • Quotas (bytes): TOLE_SERVE_IMAGE_SESSION_QUOTA (default 64 MiB per session) and TOLE_SERVE_IMAGE_TOTAL_QUOTA (default 512 MiB across all serve sessions). A quota check and its write happen under one lock, so two concurrent uploads cannot both pass a check that only one fits. 0 disables uploads.
    • Status codes: 400 empty body, bad id, or images that is not an array of ids · 401 · 403 recorded image outside the jail · 404 unknown session, upload or unrecorded image · 409 hash, size or type mismatch (reported, never served or sent) · 413 · 415 not an image · 503 no data dir · 507 quota exceeded.
  • serve.rs:
    • Request bodies are now kept as raw bytes; the JSON routes decode them as before.
    • The 10 MiB upload ceiling applies only when the headers already carry the right token. Otherwise the 1 MiB body cap stays, so nothing larger is buffered before auth.
    • A declared body over the cap is authenticated before it is answered (Cora finding): a wrong or missing token gets 401 and counts against the per-IP auth limiter (429 after 10 failures a minute); only the right token gets 413. Without this the size answer was a token-validity oracle outside the limiter.
    • An authenticated upload body gets a 120 s read budget instead of 15 s.
    • Uploads count within the 32-connection cap.
    • Added reason phrases for 415 and 507.
  • Plumbing: the serve prompt route uses session_host::run_session_turn_with_images and turn_runner::run_turn_on_with_images from feat(core,cli): images on any turn, ACP image blocks, and a decision on an image-reading tool #559 (rebased onto develop after feat(cli): images on any turn and ACP image blocks (#559) #563; no duplicate helpers). image_roots and spill_dir are now pub(crate).
  • No tole-core change.
  • Docs: docs/mobile-control.md (new Images section), README (status line and image input section), docs/epics.md, CHANGELOG Unreleased.
  • Known limitations (documented):
    • Unused uploads are not garbage-collected and keep counting against the quotas.
    • No inline data: URLs in the prompt JSON (by decision).
    • The total quota covers serve uploads only, not CLI --image spills.

Testing

  • After the rebase onto develop: cargo fmt --all -- --check and cargo clippy --workspace --all-targets -- -D warnings clean; cargo check -p tole-cli --no-default-features clean; cargo test --workspace: 1233 passed, 0 failed.
  • New end-to-end tests over the real binary in crates/tole-cli/tests/serve_images.rs (5 tests):
    • Upload, then prompt with images: the provider receives a data:image/png part and the log holds the reference only. Reading the image back returns the exact bytes with Content-Type and nosniff.
    • Restart: the image is 404 before resume, served after resume, and re-sent on the next turn.
    • A swapped file returns 409 on read and on prompt. Images that are unrecorded, belong to another session, or have a junk hash return 404.
    • Refusals: wrong type sniffed despite an image/png header (415), empty body (400), more than 10 MiB with the token (413), unknown session (404), unauthenticated upload and read (401), an unauthenticated 2 MiB declared upload (401). Bad images values return 400/404. Refused uploads leave no file behind.
    • An oversized upload with a wrong token: 401 ten times, then 429, after which even the right token is refused for that IP.
    • Per-session and total quotas return 507, and the message names the env variable.
  • 4 unit tests in serve_images cover quota parsing, dedupe and session scoping, non-image/empty/quota refusals, and read checks: jail 403, swapped 409, missing 404.
  • Mutation checks: each security check was disabled in turn and the suite run; each mutation made at least one test fail, then the code was restored: the read hash/size/type check, the per-session quota, the total quota, the auth condition on the 10 MiB pre-auth ceiling, the auth check before answering 413, the read jail (roots widened to /), "served only when recorded", the id validation on prompt lookup, the content sniff.
  • cora review --staged was not run because cora is not installed in this environment. A manual review of the staged diff covered correctness, jail and path traversal, size-limit and quota bypass, and error paths; the CI Cora Review is the gate.
  • Cross-Compile (aarch64-apple-ios) is cancelled before it starts (no log), as on already-merged PRs such as fix(storage): retry a writer lock that names our own pid (#552) #557; it is a runner issue, not this change.

@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🔍 Cora AI Code Review

✅ No issues found. Code looks good!


Review powered by cora-code · BYOK · MIT

POST /sessions/{id}/images stores a raw body (type sniffed from the bytes) through the core image resolve path, content-addressed under the spill dir; prompts reference uploads with an images array and record durable references only; GET /sessions/{id}/images/{fnv1a64} serves an image only when the session log records it, re-checking jail, size and hash. Per-session and total upload quotas are env-configurable.
The upload ceiling depends on the token, so answering 413 before auth let a
client tell a right token from a wrong one with no recorded failure, outside
the per-IP limiter. An oversized body now goes through the limiter first: a
wrong or missing token gets 401 and counts as a failure, only the right
token gets 413.
@ajianaz
ajianaz force-pushed the feat/558-serve-images branch from 103a2ef to 8056622 Compare November 11, 2026 01:50
@ajianaz
ajianaz merged commit aaff09c into develop Nov 11, 2026
19 of 20 checks passed
@ajianaz
ajianaz deleted the feat/558-serve-images branch November 11, 2026 04:10
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.

feat(serve): image input and an image read route for graphical clients

2 participants