Skip to content

Pictures in messages and from read, counted at their token cost; Sonny's shell editing keys - #242

Merged
lydakis merged 17 commits into
mainfrom
claude/project-thread-ebs9s5
Oct 28, 2026
Merged

lydakis merged 17 commits into
mainfrom
claude/project-thread-ebs9s5

Conversation

@lydakis

@lydakis lydakis commented Oct 28, 2026 •

Copy link
Copy Markdown
Owner

Two things from day-two use of Sonny.

Pictures now work end to end:

  • they go with a message
  • read returns an image file as a picture instead of refusing its bytes
  • a picture counts at its token cost

The composer also gets the rest of the shell editing keys that Codex's composer has.

Pictures with a message

Pictures go in the message itself, as Codex and Claude Code send them. The other option was a path the agent would read later, and George picked this one.

Sonny

  • Ctrl-V takes the picture on the clipboard. Terminals paste only text, so Sonny asks for it: osascript on a Mac, wl-paste or xclip elsewhere.
  • A dropped image file, or its pasted path, attaches instead of being typed. That covers what terminals paste for a drop: escaped spaces, quotes, and file:// addresses. It applies only when every word of the paste is an image file that exists (png, jpg, jpeg, gif, webp), so ordinary text never turns into an attachment.
  • Each picture shows as [Image #N] where it goes in the message. Deleting the placeholder leaves that picture out.
  • A /side question takes the pictures its placeholders name.
  • Ctrl-C drops a draft's pictures along with its text. A picture whose placeholder is in neither the draft nor the Ctrl-Y buffer is let go at the next attachment.
  • A PNG longer than 1568 px on a side is scaled down first. That's the most Anthropic's models see, and more than OpenAI's high detail keeps of a screenshot. Other formats are sent as they are. A picture past 5 MB, even once scaled, is refused with a notice, and so is one that would bring a message's pictures past 5 MB together.

The composer with two pictures as [Image #1] and [Image #2]

The scripted model replies with what it was sent. The 3000×2000 screenshot reached it at 1568×1045:

The reply: I got 2 images with your message: a 1568×1045 PNG and a 40×30 PNG

Daemon

  • submit takes images, a list of {"media_type", "data"} objects with standard base64 data. Up to 20 are allowed, 5 MiB together once decoded.

    • Each one is checked against its type's magic bytes. A failure is invalid_image with the index it was at. Too many, or too large, is image_limit.
  • The pictures go in the user item after the words, in each family's own shape:

    • Responses: input_image data URLs.
    • Anthropic and Bedrock: base64 image blocks.

    This works for a turn of its own and for a steer. One absorption batch reads at most 8 MiB of pictures past its first steer's, and the rest wait for the next batch.

  • A queued turn keeps its pictures in the new turns.images column (schema 56, with a migration).

    • Once the prompt becomes a node, the pictures live only there, and the turn keeps their SHA-256.
    • A resend under the same request_id with other pictures is an idempotency_conflict on images.
  • History reads (history_items, and the history tool's item reads) return a person's prompt with each picture's bytes left out and omitted_bytes counted. The model is still sent them whole.

  • A request line may now be 8 MiB, enough for a prompt and its pictures.

  • agent run --image PATH sends one from the CLI. Repeat the flag for more.

read returns pictures

Before this, an agent that read a screenshot got raw bytes, and since #233 it gets file_not_text. Now read of a PNG, JPEG, GIF or WebP file returns the picture itself, as Claude Code's Read and Codex's view_image do. The picture follows a line naming its kind, its size and its bytes.

  • A picture over 1568 px on its longest side, or over 1 MiB, is scaled down to fit and sent as a PNG.
  • Scaling runs in a blocking worker, one picture at a time, so the daemon's thread keeps serving other bots. A read tells a picture by its first 12 bytes and loads the file only in its turn, so reads waiting for one hold no copy. A picture over 64 megapixels is refused as image_limit before it is decoded, and the decoder runs with an allocation limit.
  • A photograph goes as a JPEG at quality 85, and so does a PNG that is still over 1 MiB.
  • Both families take it in the tool result itself:
    • Responses: an input_text and an input_image.
    • Anthropic: a text and a base64 image block.
  • History reads leave the picture's bytes out, as they do for a prompt's.
  • Once elided, the result goes as its text, with a line saying that the file holds the picture.
  • This uses the image crate with only its png, jpeg, gif and webp decoders. That adds 7 crates to the build.

Pictures count at their token cost

Codex flagged this in review, and so did a performance review. A picture used to count by its base64 bytes, so a 1 MiB picture was about 350,000 estimated tokens. That caused two problems:

  • A pool with a learned per-minute limit refused the request before sending it.
  • A few pictures filled the context budget.

Now a picture weighs four bytes per estimated token. The estimate is width × height / 750, once the longest side is scaled to fit 1568 px, and at most 1600 tokens. That's Anthropic's rule, and OpenAI's high detail counts fewer.

  • Each node records what its pictures add beyond that weight, in a new pictured column (schema 57, with a migration). It is signed: a picture whose base64 is smaller than its weight still counts at its weight.
  • total_bytes leaves that extra out. Every window, compaction and elision budget therefore sees the weight, with no change to its query.
  • Only a request's length adds the bytes back. Windows and compaction plans carry pictured per item, for the Content-Length and the read-ahead batches.
  • The pacer estimates from Items.weight instead of Items.bytes.
  • A picture result's stub saves only what the result weighs over it, so only a large picture is elided.

Shell editing keys

Most were already there: Ctrl-A/E/B/F/D/W/U/K, Alt-B/F, Alt- or Ctrl-Backspace and arrows, plus Ctrl-H from #230. Compared with Codex's editor keymap, these were missing:

  • Ctrl-Y puts back what Ctrl-W, Ctrl-U, Ctrl-K, Alt-D or a word deletion took.
  • Alt-D and Ctrl/Alt-Delete delete the next word.
  • Ctrl-J and Alt-Enter start a new line. Alt-Enter helps in terminals that send Shift-Enter as a plain Enter.
  • Ctrl-P/N move up and down a row. They also move through the pickers and the command menu.
  • Ctrl-Alt-H deletes a word, as Alt-Backspace does.

This also fixes a related bug. With an approval pending, Ctrl-Y or Ctrl-N used to answer it the way a plain y or n would. Now only the plain keys answer an approval.

Testing

  • New tests:
    • Sonny:
      • dropped_files_read_as_their_paths
      • a_large_png_is_scaled_to_fit_and_a_small_one_kept
      • hex_from_applescript_reads_as_bytes
      • what_a_deletion_takes_comes_back_with_yank
      • the_shell_editing_keys_work_in_the_composer
      • pictures_go_with_their_placeholders_and_a_discarded_draft_drops_them, which also checks the 5 MB a message's pictures may be together
    • Codec: pictures_follow_the_words_in_each_familys_shape and a_picture_weighs_its_estimated_tokens.
    • read: read_shows_a_picture_and_refuses_other_binaries_by_kind, plus the scaling tests in src/tools/picture.rs.
    • Store: pictures_count_at_their_weight_and_go_whole, a_picture_that_compresses_below_its_cost_counts_at_its_cost, and steer_batches_bound_the_bytes_of_their_pictures.
      • A 360 KB picture and a 1.4 MB picture both fit a 64 KB budget.
      • Each item streams exactly the bytes the window counts for it, and so does a compaction plan.
      • Once elided, the result goes as its text.
    • Scaling: a_picture_too_large_to_scale_is_refused_before_it_is_decoded uses a 12000×12000 PNG header.
    • Elision: a_result_with_a_picture_reads_as_its_text_and_elides_to_it.
    • Turn: read_ahead_batches_stop_at_either_bound now counts a picture's bytes in read-ahead.
    • tests/test_images.py:
      • pictures in a turn and in a steer, as the model gets them
      • history reads leave the bytes out
      • turns keep only a digest, which a same-picture retry matches
      • a retry with other pictures conflicts
      • refusal of a TIFF, bad base64, a JPEG that isn't one, more than 5 MiB, and 21 images
      • read of a 3000×2000 PNG reaches the model at 1568×1045
      • three 160 KB pictures stay in view under a 16 KB context budget
  • cargo test --workspace passes, except three task tests that need fusermount3, which this machine lacks. Those tests don't touch anything this PR changes.
  • cargo clippy --workspace --all-targets -D warnings and cargo fmt are clean.
  • AGENT_TEST_RUNTIME=1 python3 -m unittest discover -s tests: all 500 tests pass (29 skipped).
  • bench/query_plans.py knows the PICTURELESS constant, the history-read SQL that leaves pictures out, so tests/test_query_plans.py audits those queries too.
  • Checked live: Sonny ran against a real daemon from this branch and a scripted Responses server, inside tmux. The screenshots come from that run, rendered by xterm.js.
  • Ctrl-V on macOS hasn't been tried yet, because no Mac was available here.

🤖 Generated with Claude Code

… messages

- A markdown link's underlined text is now a link too, not just the
  address drawn after it in brackets. Paths of files that exist (relative
  to the agent's folder, absolute, or under ~/, with :LINE or #L after
  them) open as file:// links.
- Ctrl-H (0x08) deletes like Backspace, in the composer and pickers.
- Each staged queued or steered message has a blank row above it, as in
  the history, and the last one a blank row before the status row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
…s in the daemon

Sonny attaches a picture from the clipboard on Ctrl-V, or an image file
whose path is pasted or dropped on the terminal, behind an [Image #N]
placeholder; PNGs are scaled to 1568px on their longest side. The
daemon's submit takes `images` (base64 PNG, JPEG, GIF or WebP), checks
them, keeps them with the turn, and sends them after the words in each
provider family's own shape. `agent run --image PATH` sends one from the
CLI. History reads leave the picture bytes out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
Ctrl-Y puts back what Ctrl-W, Ctrl-U, Ctrl-K or a word deletion took;
Alt-D and Ctrl/Alt-Delete delete the next word; Ctrl-J and Alt-Enter start
a new line; Ctrl-P/N move a row; Ctrl-Alt-H deletes a word. Ctrl-Y and
Ctrl-N no longer answer a pending approval as `y` and `n` do.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
@lydakis lydakis self-assigned this Oct 28, 2026
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T02:09:37.701580Z 45db45b New commits
🔒 Security Review ✅ Completed 2026-10-28T02:12:10.620268Z 45db45b New commits

Security findings

Advisory findings (1)

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: dcc0e58aa3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sonny/src/main.rs Resolved
Comment thread sonny/src/main.rs Resolved
…tores them once

Ctrl-C (or Esc in a side chat) drops the pictures with the text. A
/side question sends the pictures its placeholders name. Once a prompt
is a node its pictures live there; the turn keeps their SHA-256, which
a retry is checked against. History pages and reads leave pictures out
in a subquery whose cheap guard spares items without any.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar

lydakis commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c63195f22a

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/codec.rs Resolved
@chatgpt-codex-connector

Copy link
Copy Markdown

🛡️ Codex Security Review · Automatically triggered

Security review completed. No security issues were found in this pull request.

Reviewed commit: c63195f22a

View security finding report

Only the user who started this review can view the report in Codex.

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

A PNG, JPEG, GIF or WebP file comes back as a line naming it and the
picture, in each family's result shape. One longer than 1568 px on a side
is scaled down, and one over 1 MiB re-encoded as a JPEG. Other binary
files stay refused. History reads leave a result's picture bytes out, as
they do a prompt's, and elision stubs a picture result as its text.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
A picture counted by its base64 bytes, so a few large ones filled the
context budget, brought compaction early, and made the pacer estimate
a 1 MiB picture at ~350,000 tokens, which a pool with a learned limit
refused before sending.

Each picture now weighs four bytes per estimated token (width × height
/ 750 once the longest side fits 1568, at most 1600). Nodes record
what their pictures add beyond that weight (`pictured`, schema 55),
and `total_bytes` leaves it out, so every window, compaction and
elision budget sees the weight with no change to its query. Only a
request's length adds the bytes back: the window and compaction plans
carry `pictured` per item for the Content-Length and read-ahead, and
`Items.weight` is what the pacer estimates from.

A picture result's stub saves what the result weighs over it, so only
a large picture is elided.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
@lydakis lydakis changed the title Pictures with a message (Ctrl-V, dropped files, submit images) and Sonny's missing shell keys Pictures in messages and from read, counted at their token cost; Sonny's shell editing keys Oct 28, 2026

lydakis commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 821845eccc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tools/picture.rs Resolved
Comment thread src/codec.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛡️ Codex Security Review · Automatically triggered

Here are some automated security review suggestions for this pull request.

Reviewed commit: 821845eccc

ℹ️ About Codex security reviews in GitHub

This is an experimental Codex feature. Security reviews are triggered when:

  • You comment "@codex security review"
  • A regular code review gets triggered (for example, "@codex review" or when a PR is opened), and you’re opted in so security review runs alongside code review

Once complete, Codex will leave suggestions, or a comment if no findings are found.

Comment thread src/tools/picture.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

https://github.com/lydakis/agent/blob/9d5d3016cd1bbea6348670c5f9270b23a7faad61/sonny/src/attach.rs#L4651-L4653
P2 Badge Enforce the PNG byte limit before accepting an attachment

When a PNG fits the dimension limit but exceeds the daemon's byte limits, this branch accepts it unchanged, and picture() skips the MOST_BYTES check for PNGs. A valid 1568×1568 RGB noise PNG generated locally occupied 7,379,758 bytes and 9,839,680 base64 bytes: Sonny accepts that attachment, but submitting it exceeds session::LINE_LIMIT (8 MiB), causing socket_reader() to close the connection. Smaller oversized PNGs instead fail check_images()'s 5 MiB limit. Check bytes as well as dimensions, and downscale/re-encode or reject oversized PNGs before adding them to the draft.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/store/db.rs Resolved
Comment thread src/server/mod.rs Resolved
…mall ones at full cost

- `read` scales a picture in a blocking worker, one at a time, so the
  daemon's one thread keeps serving every other bot meanwhile.
- A picture that must be scaled and has more than 64 megapixels is
  `image_limit`, refused from its header before decoding, and decoding
  is capped at 256 MiB besides: a small PNG can claim 12000 × 12000.
- A picture that compresses to less than its token cost counts at its
  cost: `pictured` may be negative, so a blank 1568 × 1568 PNG of a few
  KB weighs its 1600 tokens.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
An absorption batch now reads a steer's pictures only while they fit an
8 MiB budget past its first steer's, and leaves the rest for the next
batch. Sonny refuses a picture past 5 MB once scaled, PNG too, and one
that would bring a message's pictures past 5 MB together.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_013jkdGDNh4a2FzyND7nayar
# Conflicts:
#	src/store/db.rs

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e5747cb0a2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tools/picture.rs Resolved

lydakis commented Oct 28, 2026

Copy link
Copy Markdown
Owner Author

The PNG byte limit in Sonny (from the last review's body) is fixed in 4845bd5. picture() checks the 5 MB limit after scaling, for PNGs too, and attach() refuses a picture that would bring a message's pictures past 5 MB together, with a notice. Both are tested in a_large_png_is_scaled_to_fit_and_a_small_one_kept (a 1568×1200 noise PNG) and pictures_go_with_their_placeholders_and_a_discarded_draft_drops_them.

@codex review


Generated by Claude Code

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9a0a2caad7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread sonny/src/main.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 1f4d03f8bd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/tools.rs Resolved
@lydakis
lydakis merged commit 9f795a0 into main Oct 28, 2026
3 checks passed
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.

2 participants