Skip to content

feat(opencode): attach prompt images in agent sessions - #4922

Merged
ananthsub merged 8 commits into
mainfrom
ananthsub/opencode-session-images
Nov 10, 2026
Merged

ananthsub merged 8 commits into
mainfrom
ananthsub/opencode-session-images

Conversation

@ananthsub

@ananthsub ananthsub commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

Where this sits

flowchart BT
  main["main, which has 4917 media helpers,<br/>4934 telemetry and 5862 close-fence staging"]
  p4922["4922: OpenCode session images"]
  p4923["4923: Pi session images"]
  p5719["5719, yfw: images in tool replies,<br/>lent sandboxes, OpenCode settings"]
  p4922 --> main
  p4923 --> main
  p5719 --> p4922
  classDef current stroke:#d97706,stroke-width:3px
  class p4922 current
Loading

Arrows point from each PR to its base. #4917 has merged into main, so #4922 and #4923 are based on main and can merge in either order. yfw's #5719 is stacked on #4922.

What changed

flowchart TD
  req["Activation request"] --> parse["Split the user task into text and images<br/>once per request"]
  parse -->|"image outside the user task,<br/>non-data URI, unsupported content"| r422["422 naming the problem"]
  parse --> cfg["Session model entry<br/>modalities.input: text and image"]
  cfg --> stage["stage_activation, inside the close fence;<br/>a continuation activation's deadline bounds it"]
  stage --> sm["stage_media: upload each distinct image once,<br/>rename onto media_path<br/>/tmp/nemo-gym-media/sha256.ext"]
  sm --> run["opencode run --file path for each image"]
  run --> att["OpenCode attaches each file<br/>to the first user message"]
  att --> model["Model request with image parts<br/>same prompt for every rollout of a task"]
Loading

opencode_agent sandbox sessions now pass prompt images to OpenCode. When a session declares image input, OpenCode also passes images it reads in the sandbox to the model.

  • A session accepts base64 data-URI input_image parts in the user task, alongside nonempty text. Inside stage_activation, the adapter writes each image to /tmp/nemo-gym-media/<sha256>.<ext> with nemo_gym.sandbox.stage_media and adds --file <path> to opencode run, which attaches it to the first message, before the task text.
  • The session's model entry declares image input (modalities.input: ["text", "image"]) when the prompt has images or when the new image_input setting is true, the same setting the Pi agent has (feat(pi): attach prompt images in agent sessions #4923). Once an activation declares it, later activations of the session keep it, because OpenCode resends earlier images with the session history. Without that declaration OpenCode 1.17.11 replaces every image in its model requests, including images its read tool returns, with an error text telling the model it cannot read images. OpenCode 1.17.11 never reads the model attachment key, so the entry does not set it.
  • A session that declares image input also lifts OpenCode's image limits (attachment.image), as opencode_sandboxed_agent does. Otherwise OpenCode shrinks or re-encodes any image over 2000 px on a side or 5 MiB of base64, so the model would get different bytes than the row holds. Session opencode_config now accepts attachment, so a run on an endpoint with image limits can restore them.
  • OpenCode shows each attached file's path to the model. Naming the file by its content keeps the prompt the same across rollouts of a task. stage_media uploads each distinct image once and renames it into place atomically, so sessions sharing a sandbox never read a partly written file.
  • Staging runs inside the session's execute, so session close cancels an image upload before it releases the sandbox. The activation's budget also bounds it: a continuation activation's deadline, or for a first activation, a budget that starts once it holds a concurrency slot, as Pi's does. Staging spends the budget and OpenCode runs for the rest.
  • With fix(agents): stage activation files inside the close fence #5862 on main, stage_activation stages, in order: the session instructions, the task-environment plugin for a continuation, the MCP plugin and grant files when the session grants MCP tools, the prompt, the images, and then the runner parameters.
  • Each request's images are base64-decoded once. A continuation activation reuses the input its validation parsed, and a retried activation is compared with the stored request without decoding it again.
  • Images outside the user task (for example in a system message), image URLs that are not data URIs, and other unsupported content still get a 422 with a message naming the problem.
  • Saved trajectories keep only the text of each message, so they do not contain the images.
  • Decoding uses nemo_gym.core.split_text_and_media and staging uses nemo_gym.sandbox.stage_media, both from feat: binary sandbox files and shared media helpers #4917, now on main.

The legacy opencode_sandboxed_agent and the local /run path are unchanged.

Why

Multimodal tasks put images in the prompt as input_image parts, and the session path rejected them outright. A text-only declaration also hid images from the model whenever OpenCode opened an image file during a task, with nothing in the trajectory showing the image was dropped.

No issue: this is one harness's part of multimodal support, and the change is self-contained. The Pi session change (#4923) is a sibling PR on the same base.

Compatibility

image_input defaults to false, so a session without prompt images behaves as before on a text-only deployment: OpenCode replaces an image it reads with its error text. A session with prompt images, or with image_input: true, also sends images its tools return, which a text-only deployment rejects. Set image_input: true for a vision model whose tasks keep their images in the sandbox, where the prompt has none. A session that declares image input sends images over 2000 px on a side or 5 MiB of base64 unchanged, so an endpoint with image limits may reject one; set attachment in opencode_config to restore OpenCode's resizing.

In a borrowed sandbox, prompt images remain under /tmp/nemo-gym-media after the session closes, because another session may be reading the same file. They are bounded by the number of distinct images. A sandbox that isolates its /tmp drops them when it is destroyed; the local provider, and Apptainer without --contain, share the host's /tmp, where they stay until the host clears it.

A first activation's OpenCode run now gets its budget minus the time spent staging, rather than the whole budget, and a budget that runs out before launch fails the activation.

Validation

  • On the current head (49324d1a20): pytest -n 16 responses_api_agents/opencode_agent/tests tests/unit_tests/sandbox/test_sandbox_media.py tests/unit_tests/core/test_media.py passed.
  • New tests check, through the HTTP session flow:
    • two prompt images are staged byte-for-byte at their content-named paths with no partial files left, passed with --file in order, decoded once even when the request is retried, and declared on the model entry;
    • in a continuation session, validation and activation decode an image once between them;
    • an image upload that outlasts a continuation activation's budget is cancelled, and the activation ends as incomplete without launching OpenCode;
    • a provider timeout or a refused rename while staging images fails the activation as a server error, without launching OpenCode;
    • a first activation whose image upload outlasts its budget fails promptly, cancels the upload and frees its concurrency slot, without launching OpenCode;
    • a text-only prompt declares image input only when image_input is true, and in a continuation session a prompt image keeps the declaration for the next activation;
    • a session lifts OpenCode's image limits only when it declares image input, and a session's attachment setting replaces them;
    • closing a session while an image upload is stalled cancels the upload before the sandbox is released, and OpenCode never launches;
    • an image URL, an image in a system message, and an image without text each get the adapter's own 422 without launching OpenCode.
  • The attachment, decode and continuation tests fail on the PR's first commit. Removing the system-message rejection, or dropping a URL image instead of rejecting it, fails the rejection test. Moving image staging back outside stage_activation fails the close test. Treating a provider error while staging like the activation's own deadline fails the staging-error test. Without the first-activation budget, the stalled-upload test waits for the whole upload and fails. Dropping the declaration in a later activation fails the continuation declaration test. Lifting the image limits for every session, or rejecting attachment in opencode_config, fails the image-limit tests.
  • On the current head: pre-commit run --files <changed files> passed.

Rollout evidence

The latest run used 6664177f9f. Since then, the base moved to main with #4917 merged, and this branch added three changes: a first activation's budget now bounds its staging, image input is declared only for prompt images or image_input, and declaring it lifts OpenCode's image limits. The prompt-image tasks still declare it, and their images are 768 by 768 px, under OpenCode's limits, so the run covers them; the image staging and attachment code is otherwise the same. #4917 has since replaced mv -fT with a directory check and plain mv -f, because BSD mv has no -T, so the run exercised the earlier rename. #4917 was rebased onto main, which brought in #4934's transfer telemetry and #5862's MCP staging in stage_activation. The smoke tasks grant no MCP tools, so the run does not exercise #5862's staging.

The run used Qwen3-VL-30B-A3B-Instruct on vLLM 0.29.0 (default sampling) and OpenCode 1.17.11 in Docker sandboxes. single_agent_turn_legacy drove it so each row runs as an agent session, and mcqa graded it (ten options, so a model that cannot see the image scores about 10% by guessing). The dataset has five image questions (color, shape, number, dot count and word) plus a text-only control.

  • 4 of 6 correct. One miss was a dot miscount, as in every earlier run. The other was the text-only control: the model answered 42 correctly but left out the boxed letter.
  • Every image reached opencode run --file at /tmp/nemo-gym-media/<sha256>.png. The five paths match the run on 7255d05066, so the prompt does not change between rollouts, and feat: binary sandbox files and shared media helpers #4917's staging at that time (directory creation, readable partial files, mv -fT) worked in Docker sandboxes.
  • No session-directory image path appears in the output, and staging logged no errors.

Earlier runs: 7255d05066 scored 5 of 6 on the same prompt images, missing only the dot count. bc4afe4b15, from before the package reorganization, scored 5 of 6 on prompt images. On images OpenCode opened with its read tool, it answered 8 of the 10 rows where the model called read, over three runs; those sessions always declared image input, which now takes image_input: true. In the same tasks through opencode_sandboxed_agent, which does not declare image input, only the text control was correct.

@ananthsub ananthsub added feature New capabilities, enhancements, or enablement work area:agent Agent harnesses and Responses API agent behavior labels Oct 22, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested review from a team October 22, 2026 14:13
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the needs-review PR is ready for code review and waiting on a reviewer label Oct 22, 2026
@ananthsub
ananthsub force-pushed the ananthsub/opencode-session-images branch from ee4fdd0 to bc4afe4 Compare October 22, 2026 14:33
@ananthsub
ananthsub changed the base branch from main to ananthsub/sandbox-spec-file-bytes October 22, 2026 14:33
@github-actions github-actions Bot added the sla:triage-overdue Review assignment is over the one-business-day SLA label Oct 23, 2026
yfw pushed a commit that referenced this pull request Nov 6, 2026
The opencode_agent session path rejected any input_image part with a 422,
and its model entry declared no image input.
OpenCode omits every image from model requests for a model without image input,
including images its read tool returns, while still reporting the read as successful.

A session now accepts base64 data-URI input_image parts in the user task.
The adapter decodes them with nemo_gym.media_utils, uploads them into the session directory,
and passes each one to `opencode run --file`.
The session model entry declares image input, so images the read tool returns reach the model.
Media outside the user task and image URLs are still rejected with a 422.

Ported onto main 792fa14 for the Agentic-MME stack (#5720): main's session input now also takes
request sampling, and test_unset_extensions_keep_setup_activation_and_capture_unchanged now expects the
image options in the model entry. The #4922 test reads the staged activation through params().

Signed-off-by: Ananth Subramaniam <[email protected]>
Signed-off-by: Yi-Fu Wu <[email protected]>
@github-actions github-actions Bot removed the sla:triage-overdue Review assignment is over the one-business-day SLA label Nov 6, 2026
@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 6, 2026 22:04

@yaoyu-33 yaoyu-33 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-checked aa7838b after the rebase onto #4917 with #5862. Moving the MCP grant files into stage_activation, beside the prompt images, looks right: close now fences all of the activation's uploads.

One consistency point with Pi (#4923), inline.

The opencode_agent session path rejected any input_image part with a 422,
and its model entry declared no image input.
OpenCode omits every image from model requests for a model without image input,
including images its read tool returns, while still reporting the read as successful.

A session now accepts base64 data-URI input_image parts in the user task.
The adapter decodes them with nemo_gym.core.split_text_and_media, uploads them into the
session directory with nemo_gym.sandbox.upload_media within the activation's budget,
and passes each one to `opencode run --file`.
The session model entry declares image input, so images the read tool returns reach the model.
Media outside the user task and image URLs are still rejected with a 422.

Signed-off-by: Ananth Subramaniam <[email protected]>
Each request's images are now base64-decoded once. A continuation
activation reuses the input that validation already parsed, and a retried
single activation is compared with the stored request without parsing it
again.

Prompt images are uploaded to <session_dir>/input-media. The extra
activation segment was "None" outside continuation sessions, and the path
reaches the model through OpenCode's read-tool text.

The model entry now declares only modalities. OpenCode 1.17.11 never
reads the model attachment key on the request path; modalities.input
alone lets images through, and without it OpenCode replaces each image
with an error text.

The rejection test now sends valid image parts, so the adapter's own
checks reject them instead of request validation, and it asserts the
adapter's message. New tests cover image order and exact paths, an image
without text, the continuation path, decoding once, and the upload
spending a continuation activation's budget.

Signed-off-by: Ananth Subramaniam <[email protected]>
OpenCode tells the model each attached file's path, and the path held
the session's random directory, so the prompt differed on every rollout
of the same task. Images now go to /tmp/nemo-gym-media/<sha256>.<ext>
through nemo_gym.sandbox.stage_media, which uploads each distinct image
once and renames it into place atomically, so sessions sharing a
sandbox never read a partly written file.

Staging now runs in stage_activation, inside the session's execute, so
session close cancels an image upload before it releases the sandbox.
stage_activation appends the --file arguments from the paths
stage_media returns, so each image is hashed once.

Tests cover the content-named paths and close cancelling a stalled
image upload before the sandbox is released.

Signed-off-by: Ananth Subramaniam <[email protected]>
Follow #4917's staging changes: the fake sandbox now renames files with
the separate mv -fT command. A new test checks that a provider timeout or a
refused rename while staging images fails the activation as a server error
rather than ending it incomplete. The README now limits the claim that a
timeout before launch leaves the session usable to the activation's own
budget.

Signed-off-by: Ananth Subramaniam <[email protected]>
A first activation passed no deadline, so staging its files, prompt
images included, could hold a concurrency slot until the session
closed. Its budget now starts once it holds a slot, as Pi's does:
staging spends it, OpenCode runs for the rest, and a budget that runs
out before launch fails the activation.

Signed-off-by: Ananth Subramaniam <[email protected]>
#4917 now checks for a directory at each target path and renames with
plain mv -f, since BSD mv has no -T. The fake sandbox and the
refused-rename test match that command.

Signed-off-by: Ananth Subramaniam <[email protected]>
…figured

Sessions declared image input on every model call. OpenCode then sends
the model any image its tools return, so on a text-only deployment a
task whose sandbox holds an image failed where the model used to get
OpenCode's error text.

A session now declares image input when the prompt has images or when
the new image_input setting is on, matching the Pi agent's setting.
Once an activation declares it, later activations of the session keep
it, because OpenCode resends earlier images with the history. Set
image_input for a vision model whose tasks keep their images in the
sandbox, where the prompt has none.

Signed-off-by: Ananth Subramaniam <[email protected]>
OpenCode 1.17.11 shrinks or re-encodes any image over 2000 px on a
side or 5 MiB of base64, so the model could get different bytes than
the row holds. A session that declares image input now lifts these
limits, as opencode_sandboxed_agent does, so the model gets each
image's own bytes. Session opencode_config now accepts attachment, so
a run on an endpoint with image limits can restore them.

Signed-off-by: Ananth Subramaniam <[email protected]>

This branch was previously deployed

1 inactive deployment
public — 49324d1a Deployed Nov 10, 2026 by copy-pr-bot[bot] via release / finalize / notify #11241
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:agent Agent harnesses and Responses API agent behavior feature New capabilities, enhancements, or enablement work needs-review PR is ready for code review and waiting on a reviewer

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants