Skip to content

fix(codex): don't resend a generated image the agent already sent as a copy - #186889

Open
sukhdeepjohar wants to merge 3 commits into
openclaw:mainfrom
sukhdeepjohar:fix/186719-telegram-dup-tool-image
Open

sukhdeepjohar wants to merge 3 commits into
openclaw:mainfrom
sukhdeepjohar:fix/186719-telegram-dup-tool-image

Conversation

@sukhdeepjohar

@sukhdeepjohar sukhdeepjohar commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #186719

What Problem This Solves

Fixes: in a Codex turn, the agent sends a generated image with message(action=send, media=...) using its own byte-identical copy of the file (another path), and Telegram then shows the same photo a second time when the turn ends.

User Impact

A generated image the agent already delivered as a copy is no longer sent again by the end-of-turn flush. Images the turn did not deliver are still sent, and a copy with different bytes is still treated as a different image.

Why This Change Was Made

The Codex harness decides which generated images are already delivered by matching each confirmed message() send against the generated image's known paths (the Gateway media-store copy and Codex's savedPath). A copy the agent made itself has neither path, so the generated image stays pending and, in message_tool_only mode, is delivered again as host-owned media.

This change adds content identity to that matching, recorded when the bytes are fixed:

  • At send time: when Codex message media is prepared (the step that already stages remote attachments), each attachment inside the agent workspace or the Gateway media directory gets a SHA-256 key, read through the safe file opener with symlinks rejected and within the media limit. A bare name (copy.png) or a ~/ path is resolved first the way the message send loads it (from the agent workspace, or the home directory), so a copy named without its folder is keyed too. Paths anywhere else are never read here and keep path-only matching; the message tool's own media policy still decides whether they can be sent. The confirmed delivery carries the key next to its paths, so a later overwrite of the same path cannot change what the send is taken to have delivered. This read happens during preparation, before the tool's own checks, which is why it is limited to those two directories, which the agent already writes to; the key never appears in any output and only counts once the send is confirmed delivered.
  • For generated images: the projection keys the bytes it saved to the media store, or the bytes of Codex's local savedPath.
  • A confirmed send whose key equals a generated image's key counts as delivery of that image, through the existing per-image, per-destination bookkeeping. Nothing else changes: files outside those two directories, symlinks, unreadable sources, partial sends and copies with other bytes stay unmatched, so the change can only remove a duplicate, never an unsent image.

Accepted edge case: a file rewritten in the moment between preparation and the channel reading it would be keyed by its earlier bytes.

The runtime-behavior docs now say that a confirmed send of a same-bytes copy counts as delivery.

Evidence

Test (red on main, green here). event-projector.media.test.ts: the existing "local image delivery evidence" test becomes a table driven through the real Codex dynamic-tool bridge and projection:

  • partial saved-path send: nothing counts as sent (unchanged);
  • confirmed byte-identical copy at another path: the generated image now counts as sent to that target (fails on main, where only the copy's path is recorded);
  • confirmed copy with other bytes of the same size: unmatched, the generated image stays pending;
  • copy sent with other bytes, then overwritten with the generated image's bytes before the turn ends: unmatched;
  • byte-identical copy outside the agent workspace: never read, unmatched;
  • byte-identical copy sent by bare name (copy.png): resolved in the workspace and matched (fails without the bare-name resolution, passes with it);
  • symlink in the workspace pointing at the generated image: rejected, unmatched.

With main's Codex code and this test file (run on the proof bench), exactly the byte-identical row fails; the other 15 tests in the file pass.

The final reply reads these results through the existing route-scoped dedupe (filterMessagingToolReplyPayload), which drops media listed in a matching target's mediaUrls; the new row asserts the generated image is in that list for the Telegram target, the same output shape the existing path-alias test checks.

Test lines are net +15 against main (+28/−13): the seven cases fold into the existing local-delivery test rather than new tests, and a remote-path test reuses the shared Codex image path fixture.

Checks on the current head (merged with main at 1c03d49956e, which rewrote event-projector-media.ts; isolated Linux test VM, no network):

  • The new bare-name row: fails with the previous head's resolution, passes with this one.
  • The Codex extension tests: all pass except node-app-server.runtime.test.ts › "gates native resource readiness, resumes exact state, and retains failed-cleanup custody", which fails the same way on unchanged main. dynamic-tools.test.ts (52 tests), which failed in this PR's earlier CI run, passes on main and here.
  • Core and extension typecheck shards and oxfmt on the changed files pass.

Checks on the earlier head (f34fd634a11, before the merge), on the same kind of VM:

  • The Codex extension tests, all 446 test files in three parallel slices: 5,332 passed, 10 skipped (the live and real-binary tests, which need credentials), 1 failed. The failure was run-attempt.steering.test.ts "settles submitted steering before terminal tool interruption (consumed)" ("Codex cancellation could not confirm the turn stopped" after 16.8 s, with six lanes sharing the machine). Rerun alone, that file passes 5/5 with this change and 5/5 on main.
  • The media test 5 runs in a row, plus the media-preparation, Codex bridge, reply-payload, agent-runner-payload and tool-media-payload suites (10 files, 216 tests).
  • pnpm tsgo:extensions and pnpm tsgo:extensions:test; oxlint and oxfmt on the changed files.

Live run: real Gateway, real Codex app-server, real Telegram. An isolated Gateway built from main and from this branch, with the agent on the bundled Codex app-server (0.164.0) and messages.visibleReplies: message_tool, connected to a real Telegram test bot. A person sent one DM to the bot per run. The model's replies were scripted by a local HTTPS proxy, so the turn is the reporter's sequence every time: a generated image, then message(send) with a byte-identical copy at another workspace path and the caption "copy test". Everything else was real: Codex, the Gateway's image handling, the end-of-turn delivery and Telegram. The copy was written by the harness, standing in for the agent's own cp.

main   15:58:40 STAGED: generated image + message(send) of the byte-identical copy /out/main/workspace/copy.png
       Telegram chat: the captioned photo, then the same photo again without a caption (2 photos)
branch 16:08:42 STAGED: generated image + message(send) of the byte-identical copy /out/fix/workspace/copy.png
       Telegram chat: the captioned photo only (1 photo)

Telegram chat on main (the captioned photo, then the same photo again without a caption):

main: the photo arrives twice

Telegram chat with this change (the captioned photo once):

this change: the photo arrives once

Proof artifice: the model's replies were scripted by a local HTTPS proxy, and the harness wrote the byte-identical copy in place of the agent's own cp; Codex, the Gateway, end-of-turn delivery and Telegram were real.

The live run used the earlier head; the merge and the bare-name resolution do not change the absolute-path copy it exercises. The photo counts come from the Telegram chat itself. As in the issue, the Gateway logged no outbound send line for these photos, so the log could not count them.

…a copy

A Codex turn that sent a byte-identical copy of a generated image with
message() still delivered the generated image again at the end of the turn,
because confirmed sends matched generated images only by path.

Record a content key for each attachment when message media is prepared,
for files in the agent workspace or Gateway media read without following
symlinks, and carry it on the confirmed delivery. Generated images get the
same key when saved, so a send of a same-bytes copy counts as delivery of
that image. Other paths keep path-only matching.

Fixes openclaw#186719
@clawsweeper

clawsweeper Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added docs Improvements or additions to documentation extensions: codex size: S labels Oct 28, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 28, 2026
@clawsweeper

clawsweeper Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes

This PR matches confirmed image deliveries by content as well as path so an already-sent copy is not sent again.

Example: A Codex turn sends /out/fix/workspace/copy.png with the caption "copy test", containing the generated image's exact bytes.

  • Before: Telegram receives the captioned photo followed by an uncaptioned duplicate.
  • After: Telegram receives only the captioned photo.

Review scores

Measure Result What it means
Overall readiness 🦞 diamond lobster (5/6) Focused contract-preserving repair with inspected transport proof, distinct regression controls, passing current-head gates, and no actionable findings.
Proof confidence 🦞 diamond lobster (5/6) ✨ media proof bonus Sufficient (screenshot): Inspected before/after Telegram screenshots and the declared real Gateway/Codex app-server scenario show the changed media preparation and projection reducing two photos to one; scripted inference and harness-created copying are disclosed, and subsequent changes preserve the exercised absolute-path case.
Patch quality 🦞 diamond lobster (5/6) No actionable review findings were identified.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: Telegram receives a generated photo twice when the agent first sends a byte-identical copy under a different path.
Reason: The change restores the documented per-image delivery contract without introducing configuration, public APIs, or persisted state.

Merge readiness

✅ Ready for maintainer review

This PR fixes a concrete duplicate-image bug still present on current main; the reviewed patch has no actionable findings and its current-head gates pass.

Priority: P2
Reviewed head: 4b326d1b1d5de4e5c29082da36d35c609eed6350

Before merge

None.

Findings

None.

Agent review details

How this fits together

The Codex adapter projects generated images and confirmed message-tool receipts into destination-scoped delivery evidence consumed by final-reply media deduplication.

flowchart TD
  A[Codex image events] --> B[Generated media projection]
  C[Message attachment preparation] --> D[Confirmed delivery receipt]
  D --> B
  B --> E[Destination scoped reply deduplication]
  E --> F[Telegram delivery]
Loading

Technical review

Best possible solution:

Use captured content identity within the existing confirmed-delivery projection while preserving destination matching and uncertain-send behavior.

Do we have a high-confidence way to reproduce the issue?

Current-main source explains the different-path miss, and the contributor supplies a controlled before/after Telegram reproduction; no target code was executed during this review.

Is this the best way to solve the issue?

The patch fixes identity at preparation and projection rather than adding transport-specific deduplication, preserving the existing receipt owner and final-reply consumer.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against 69af8448ba6d.

Provenance checked

  • Generated-image delivery matching keeps the original intent (8e7443e: Preserve confirmed per-artifact delivery facts without hiding unrelated or uncertain attachments.)
  • Remote attachment preparation keeps the original intent (a7cecc8: Stage authoritative remote bytes safely into Gateway-owned media before channel delivery.)
  • Shared tool-result telemetry keeps the original intent (51a27d4: Keep execution and result bookkeeping shared while Codex retains native media aliases and protocol projection.)

Testing

Proof path: shipped entry point.

Security

None.

Evidence

What I checked:

  • Current main still matches image paths only: The current-main projection registers mediaUrl and savedPath aliases, then matches confirmed delivery sourceUrls; it has no content identity for an independently copied file. (extensions/codex/src/app-server/event-projector-media.ts:431, 69af8448ba6d)
  • Introduced fix preserves receipt ownership: Preparation captures content keys alongside staged-path aliases; the existing confirmed-delivery flow supplies those aliases to the existing per-destination image projection. (extensions/codex/src/app-server/remote-workspace-media.ts:103, 4b326d1b1d5d)
  • Distinct regression controls: The bridge-to-projection table covers identical copies, different bytes, post-send overwrites, partial delivery, outside-workspace files, bare filenames, and symlinks; the body records base failures for the actual duplicate and bare-name cases. (extensions/codex/src/app-server/event-projector.media.test.ts:351, 4b326d1b1d5d)
  • Inspected Telegram before and after proof: The downloaded screenshots show two identical photos before the fix and one captioned photo afterward. The body describes a real Gateway, Codex app-server 0.164.0, and Telegram, with scripted inference and a harness-created copy explicitly disclosed; the exercised absolute-path behavior survives the subsequent merge and filename-resolution change. (f34fd634a11b)
  • Current-head CI concern resolved: The exact reviewed head has successful openclaw/ci-gate, security-sensitive-review, dependency-review, and checks-node-changed-extensions-bundle-1 outcomes; the previous review's CI rank-up request is satisfied. (4b326d1b1d5d)
  • Original delivery contract: Merged fix(codex): preserve delivery facts and native approval semantics #151863 established per-artifact confirmed delivery so unrelated or uncertain attachments remain eligible; this PR extends identity matching while retaining that contract. (extensions/codex/src/app-server/event-projector-media.ts:443, 8e7443e653ff)

Likely related people:

  • Peter Steinberger: Raw commit 8e7443e adds extensions/codex/src/app-server/event-projector-media.ts:208 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 8e7443e653ff; files: extensions/codex/src/app-server/event-projector-media.ts)
  • Sarah Fortune: Raw commit 51a27d4 adds extensions/codex/src/app-server/dynamic-tools.ts:679 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 51a27d44d5b7; files: extensions/codex/src/app-server/dynamic-tools.ts)
  • Nathaniel "Sobe" Chestnut: Raw commit 96583dd adds extensions/codex/src/app-server/event-projector.media.test.ts:48 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: medium; commits: 96583dd4e435; files: extensions/codex/src/app-server/event-projector.media.test.ts)

PR surface

Source +106, Tests +15, Docs +3. Total +124 across 5 files. Added test files: 0.

View PR surface stats
Area Files Added Removed Net
Source 3 117 11 +106
Tests 1 28 13 +15
Docs 1 5 2 +3
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 5 150 26 +124

Review metrics

Metric Value Why it matters
Production versus test changes Production +117/-11; tests +28/-13 Growth implements bounded content capture and reuses existing delivery state; the test table adds distinct positive and negative controls without new scaffolding.

Root-cause cluster

Relationship: same_root_cause
Canonical: #186719
Summary: The canonical issue and both open PRs concern a generated image remaining pending after a confirmed send of identical bytes at another path.

Members:

Proposal only: this assessment does not dispatch repair, suppress jobs, mutate sibling items, close, or merge anything.

Labels

Label changes:

  • add rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • remove rating: 🐚 platinum hermit: Current PR rating is rating: 🦞 diamond lobster, so this older rating label is no longer current.

Label justifications:

  • P2: Duplicate generated-photo delivery affects a defined Codex messaging workflow with limited blast radius.
  • rating: 🦞 diamond lobster: Overall readiness is 🦞 diamond lobster; proof is 🦞 diamond lobster and patch quality is 🦞 diamond lobster.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR.
  • proof: sufficient: Contributor real behavior proof is sufficient.
  • proof: 📸 screenshot: Contributor real behavior proof includes screenshot evidence.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

History

Review history (2 earlier review cycles)
  • reviewed 2026-10-28T16:50:41.482Z sha f34fd63 :: needs maintainer review before merge. :: none
  • reviewed 2026-10-28T17:21:28.618Z sha f34fd63 :: needs maintainer review before merge. :: none

Reviewed November 2, 2026, 9:20 PM ET / November 3, 2026, 02:20 UTC (Revision 3).

@sukhdeepjohar

Copy link
Copy Markdown
Contributor Author

The red checks-node-changed-extensions-bundle-1 job failed in dynamic-tools.test.ts › "keeps Codex apply_patch containment diagnostics operator-only" (0 logger calls). It does not reproduce: on Node 24.22.1 (the CI version), that shard's 11 test files pass 156/156 in 4 of 4 runs on this head, and the test passes alone 6/6 on this head and 6/6 on main. This change touches only the message tool's media preparation path, not apply_patch. I can't rerun the job from a fork; a maintainer rerun should clear the gate.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Oct 28, 2026
@sukhdeepjohar

Copy link
Copy Markdown
Contributor Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
Exact review queued.

Re-review progress:

@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. and removed rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. clawsweeper:intake:bug Preliminary submission classification; full source review pending. labels Nov 3, 2026
@obviyus

obviyus commented Nov 16, 2026

Copy link
Copy Markdown
Collaborator

Performance acceptance rule, set before measurement: compare main and the repaired head through an isolated built Gateway, native Codex app-server, and Telegram Test Server. Use a generated image sent from the same path so both versions perform the same delivery. Run serially in alternating baseline/candidate order, with at least five warm samples per version and cold samples reported separately. Record host load and the exact observed timing boundary. Candidate median and p95 turn/final-delivery latency must remain within 20% of main. This comment records the target, not a passing result.

@sukhdeepjohar

Copy link
Copy Markdown
Contributor Author

Measured against this rule (one run, serial, alternating order):

  • Setup: main a8a7ce94eb5 vs the same main plus this PR (head 4b326d1), each on its own isolated built Gateway with the bundled native Codex app-server. The model was scripted, so model time is the same on both sides.
  • Workload, per turn: a generated image (1.77 MB PNG) plus message(send) of a different 1.77 MB workspace image. Both versions do the same delivery: every send was confirmed, and both sessions end with 16 image parts.
  • Timing boundary: the run's own startedAt → endedAt from agent.wait. On current main the Codex turn ends once the message tool has delivered, so this covers the send and the new media preparation. Host load (1 min) was 0.71–1.02 on 8 idle cores.
main median main p95 PR median PR p95
Warm turn (n=7 each) 4,018 ms 4,404 ms 3,978 ms 4,114 ms
Cold turn (n=1 each) 8,200 ms 8,477 ms

Result: warm median -1.0% and p95 -6.6%; the single cold sample is +3.4%. All within 20%, and inside run-to-run spread (warm main ranged 3,838–4,404 ms).

Gap: I don't have access to the Telegram Test Server credentials, so delivery went to the session's own chat rather than Telegram. The added work (reading and hashing attachments up to the existing 64 MB cap) runs before the message reaches any channel, so the channel doesn't change it. Telegram's own send latency was not part of this run.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs Improvements or additions to documentation extensions: codex P2 Normal backlog priority with limited blast radius. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. proof: sufficient ClawSweeper judged the real behavior proof convincing. rating: 🦞 diamond lobster Very strong PR readiness with only minor maintainer review expected. size: S status: 👀 ready for maintainer look ClawSweeper has no concrete contributor-facing blocker left for this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegram resends a tool image already delivered by message() when the paths differ

2 participants