Skip to content

fix(agents): dedupe async media completion sends - #74393

Closed
steipete wants to merge 1 commit into
mainfrom
fix/async-music-double-post
Closed

steipete wants to merge 1 commit into
mainfrom
fix/async-music-double-post

Conversation

@steipete

Copy link
Copy Markdown
Contributor

Summary

  • drop pending generated media at agent-end when the message tool already sent the same attachment
  • keep orphaned generated media flushing for the no-message-tool path
  • add regressions for the music completion double-post case

Test

  • pnpm vitest run src/agents/pi-embedded-subscribe.handlers.messages.test.ts src/agents/pi-embedded-subscribe.handlers.lifecycle.test.ts src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.subscribeembeddedpisession.test.ts
  • committer gate / pnpm check

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S maintainer Maintainer-authored PR labels Apr 29, 2026
@greptile-apps

greptile-apps Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a double-post bug for async media completions (e.g. music) by introducing discardAlreadySentPendingToolMedia, which filters out URLs from pendingToolMediaUrls that were already delivered via a messaging tool (tracked in messagingToolSentMediaUrls) before the agent-end flush. The no-message-tool orphan-flush path is preserved, and regressions are added for the music completion double-post case.

Confidence Score: 5/5

This PR is safe to merge — the fix is narrowly scoped, well-tested, and does not affect any code paths that aren't related to the deduplication logic.

The change is small and targeted: one new exported function with a clear invariant, called at a single site in the agent-end flush. Both the new function and the lifecycle integration have dedicated unit tests covering all relevant branches (no-op early exits, partial discard, full discard + voice flag clear). No existing behaviour is altered outside the double-post scenario.

No files require special attention.

Reviews (1): Last reviewed commit: "fix(agents): dedupe async media completi..." | Re-trigger Greptile

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a35c3388be

ℹ️ 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 on lines +151 to +152
if (state.pendingToolMediaUrls.length === 0) {
state.pendingToolAudioAsVoice = false;

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.

P2 Badge Recompute voice flag after dropping already-sent media

After filtering pendingToolMediaUrls, the code only clears pendingToolAudioAsVoice when the list becomes empty. If pending media contains both audio and non-audio entries, and only the audio URL is removed as already sent, the remaining non-audio media is still emitted with audioAsVoice: true at agent end. This can mislabel the outbound payload and affect downstream channel delivery behavior for mixed-media turns where the messaging tool already delivered just the audio attachment.

Useful? React with 👍 / 👎.

@clawsweeper

clawsweeper Bot commented Apr 29, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

What this changes:

The PR adds an agent-end helper that removes pending generated media URLs already recorded as sent by a messaging tool, wires it before pending-media flush, and adds focused unit/lifecycle regressions.

Maintainer follow-up before merge:

This is an open maintainer-labeled implementation PR for a real, narrow bug, but the branch is dirty/unmergeable and should go through maintainer rebase/review instead of an automated replacement or cleanup close.

Best possible solution:

Rebase and adapt the PR onto current main so agent-end pending-media flushing subtracts URLs already delivered by successful messaging tools while preserving orphaned media delivery, no-block-callback retention, and trusted-media metadata semantics; then run the focused agent tests and the changed gate.

Acceptance criteria:

  • pnpm test src/agents/pi-embedded-subscribe.handlers.messages.test.ts src/agents/pi-embedded-subscribe.handlers.lifecycle.test.ts src/agents/pi-embedded-subscribe.subscribe-embedded-pi-session.subscribeembeddedpisession.test.ts
  • pnpm check:changed

What I checked:

  • Current main flushes pending tool media at agent end without media-send dedupe: handleAgentEnd calls consumePendingToolMediaReply(ctx.state) and emits the resulting media-only block reply, but current main does not first subtract messagingToolSentMediaUrls. (src/agents/pi-embedded-subscribe.handlers.lifecycle.ts:177, b7db63751b99)
  • Pending-media consumption clears and emits whatever is still queued: consumePendingToolMediaReply reads pending media from pendingToolMediaUrls/voice/trust state and then clears it, so the agent-end path can produce a second attachment if a messaging tool already sent the same URL. (src/agents/pi-embedded-subscribe.handlers.messages.ts:229, b7db63751b99)
  • Messaging tool media is tracked separately today: Successful messaging sends commit URLs into messagingToolSentMediaUrls, which gives the PR a concrete state source to dedupe against. (src/agents/pi-embedded-subscribe.handlers.tools.ts:883, b7db63751b99)
  • Async music completion is the documented user path: The music-generation docs say session-backed music runs as a background task and wakes the agent to post the finished audio back to the original channel, matching the PR's async completion scenario. Public docs: docs/tools/music-generation.md. (docs/tools/music-generation.md:15, b7db63751b99)
  • PR diff is narrow and targeted: The fetched PR diff touches only four agent source/test files and adds discardAlreadySentPendingToolMedia, a lifecycle call before pending-media flush, and regression tests for already-sent generated media. (src/agents/pi-embedded-subscribe.handlers.messages.ts:137, a35c3388be93)
  • PR is not merge-ready on GitHub: GitHub reports mergeable: false, mergeable_state: dirty, rebaseable: false; the compare API reports the branch is 1 commit ahead and 7,436 commits behind the current base SHA. (a35c3388be93)

Likely related people:

  • steipete: Recent GitHub history on the exact agent subscribe/media paths includes media-only lifecycle handling, trusted tool-media metadata, and block-fragment flushing; the current main SHA is also a recent agents commit by this maintainer. (role: recent maintainer and adjacent media/lifecycle owner; confidence: high; commits: 39343088edb8, 82eb90b8a286, 5e2f6ce2943e; files: src/agents/pi-embedded-subscribe.handlers.lifecycle.ts, src/agents/pi-embedded-subscribe.handlers.messages.ts, src/agents/pi-embedded-subscribe.handlers.tools.ts)
  • pashpashpash: Commit 5404bbbb7196db3a31f0f54f4393223c1733a76d added the existing consumePendingToolMediaIntoReply duplicate generated-media guard and related tests directly next to the PR's proposed helper. (role: introduced adjacent duplicate-media behavior; confidence: high; commits: 5404bbbb7196; files: src/agents/pi-embedded-subscribe.handlers.messages.ts, src/agents/pi-embedded-subscribe.handlers.messages.test.ts)

Remaining risk / open question:

  • The PR branch is dirty/unmergeable and far behind current main, so the patch must be rebased carefully onto the current onBlockReply guard and pendingToolTrustedLocalMedia state shape.
  • The targeted tests listed in the PR body were not independently run in this read-only review, and the visible public checks do not prove those lanes passed.

Codex review notes: model gpt-5.5, reasoning high; reviewed against b7db63751b99.

@steipete

Copy link
Copy Markdown
Contributor Author

Closing this stale-branch symptom patch. Superseded by #74402, which fixes the shared async media wake instruction instead of lifecycle deduping.

@steipete steipete closed this Apr 29, 2026
@shakkernerd
shakkernerd deleted the fix/async-music-double-post branch October 19, 2026 23:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant