Repository navigation
fix(subagents): deduplicate completion announce — prevent duplicate internal-context turns in parent session on retry - #61525
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eced002a2a
ℹ️ 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".
Greptile SummaryThis PR introduces Confidence Score: 5/5Safe to merge — the deduplication logic is correct, retry paths preserve all required side-effects, and the previously flagged gaps have been addressed. All remaining findings are P2 or lower. The core logic correctly gates on No files require special attention. Reviews (2): Last reviewed commit: "fix(subagents): dedupe delivered complet..." | Re-trigger Greptile |
There was a problem hiding this comment.
Pull request overview
This PR addresses subagent completion-announcement retries causing duplicate internal-context turns in the parent session by persisting a “delivered” marker on the subagent run record and short-circuiting subsequent announce attempts.
Changes:
- Add
completionAnnouncedAtto persistedSubagentRunRecordto mark successful completion announce delivery. - Skip re-running the announce flow when
completionAnnouncedAtis already set, and finish cleanup bookkeeping instead. - Add a regression test covering re-entry after a successful delivery.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/agents/subagent-registry.types.ts | Adds completionAnnouncedAt to the run record schema. |
| src/agents/subagent-registry-lifecycle.ts | Sets/persists delivery marker and short-circuits announce cleanup flow based on it. |
| src/agents/subagent-registry-lifecycle.test.ts | Adds regression test ensuring announce flow isn’t re-run once delivery is marked. |
Comments suppressed due to low confidence (2)
src/agents/subagent-registry-lifecycle.ts:395
completionAnnouncedAtis set before several operations that can throw (e.g.,setDetachedTaskDeliveryStatusByRunId, hook emission, attachment cleanup). If any of those throws, the entry will still havecompletionAnnouncedAtset, and the next re-entry will short-circuit announce+cleanup (seestartSubagentAnnounceCleanupFlow) and can skip important cleanup steps (attachment deletion, hook emission, clearing wake/fallback state). Consider either (a) wrapping the post-announce cleanup steps so they can’t abort the function, and only setting/persistingcompletionAnnouncedAtonce the whole finalize path has safely passed the point of no return, or (b) changing the short-circuit path to run the remaining cleanup steps whencompletionAnnouncedAtis present (skip delivery, but still do hooks/attachments/state cleanup).
if (didAnnounce) {
entry.completionAnnouncedAt = Date.now();
setDetachedTaskDeliveryStatusByRunId({
runId,
runtime: "subagent",
sessionKey: entry.childSessionKey,
deliveryStatus: "delivered",
});
entry.wakeOnDescendantSettle = undefined;
entry.fallbackFrozenResultText = undefined;
entry.fallbackFrozenResultCapturedAt = undefined;
const completionReason = resolveCleanupCompletionReason(entry);
await emitCompletionEndedHookIfNeeded(entry, completionReason);
const shouldDeleteAttachments = cleanup === "delete" || !entry.retainAttachmentsOnKeep;
if (shouldDeleteAttachments) {
await safeRemoveAttachmentsDir(entry);
}
if (cleanup === "delete") {
entry.frozenResultText = undefined;
entry.frozenResultCapturedAt = undefined;
}
completeCleanupBookkeeping({
runId,
entry,
cleanup,
completedAt: Date.now(),
});
src/agents/subagent-registry-lifecycle.ts:376
- In the success path (
didAnnounce === true),setDetachedTaskDeliveryStatusByRunId(...)is called directly and can throw (there’s already a dedicatedsafeSetSubagentTaskDeliveryStatuswrapper used on the failure/give-up path). If this throws, it will abort the finalize path and also preventcompletionAnnouncedAtfrom being persisted, reintroducing duplicate announce injection after restart. Recommend using the safe wrapper (or an equivalent try/catch) here as well.
entry.completionAnnouncedAt = Date.now();
setDetachedTaskDeliveryStatusByRunId({
runId,
runtime: "subagent",
sessionKey: entry.childSessionKey,
deliveryStatus: "delivered",
});
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8f42b955ef
ℹ️ 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".
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 69562014c7
ℹ️ 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".
|
Thanks for the review. The retry path issue (missing |
|
Related issues: This PR also relates to #59996 (heartbeat model config ignored unless heartbeat session has persisted override), #56941 (heartbeat isolatedSession: true does not create fresh sessions), and #52015 (overlapping heartbeat runs overwrite session and cause nested session keys). These share the same session routing/key management code paths. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: eb6919e62f
ℹ️ 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".
|
Refreshed this PR on current What is in scope now:
What is intentionally out of scope now:
Those cleanup/orphan changes are being split away so they can be reviewed independently from the duplicate-announcement fix. Targeted validation:
|
|
@greptile-apps review |
|
Tip: Greploop — Automatically fix all review issues by running Use the Greptile plugin for Claude Code to query reviews, search comments, and manage custom context directly from your terminal. |
obviyus
left a comment
There was a problem hiding this comment.
Verified the duplicate subagent completion-turn bug and confirmed the fix keeps retry/re-entry on one cleanup path while skipping only the already-delivered announce injection.
Maintainer follow-up: distilled the delivered/failed detached-task delivery-status writes onto the shared safe helper and added the changelog entry in the active Unreleased fixes block.
Local gate: pnpm test src/agents/subagent-registry-lifecycle.test.ts
obviyus
left a comment
There was a problem hiding this comment.
Verified the duplicate subagent completion-turn bug and confirmed the fix keeps retry/re-entry on one cleanup path while skipping only the already-delivered announce injection.
Maintainer follow-up: distilled the delivered/failed detached-task delivery-status writes onto the shared safe helper, rebased onto current main, and kept the changelog entry at the end of the active Unreleased fixes block.
Local gate: pnpm test src/agents/subagent-registry-lifecycle.test.ts
|
Landed on main. Thanks @100yenadmin. |
…(thanks @100yenadmin) * fix(subagents): dedupe delivered completion announces * refactor(subagents): distill cleanup delivery status writes * fix: dedupe delivered subagent completion announces (openclaw#61525) (thanks @100yenadmin) --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
…(thanks @100yenadmin) * fix(subagents): dedupe delivered completion announces * refactor(subagents): distill cleanup delivery status writes * fix: dedupe delivered subagent completion announces (openclaw#61525) (thanks @100yenadmin) --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
Reproducing on v2026.5.2 (Telegram)I can confirm this issue is still reproducible on OpenClaw 2026.5.2 with Telegram channel. Environment
SymptomsWhen a subagent (X bookmark topic scanning) completes, the parent session receives the completion announcement multiple times and sends duplicate messages to the Telegram user. From session logs:
Impact
WorkaroundCurrently adding manual dedup rules in AGENTS.md, but this should be handled at the framework level. Would appreciate a fix for this. The deterministic idempotency keys from #17150 helped with some cases, but the retry/re-entry path still causes duplicates. |
…(thanks @100yenadmin) * fix(subagents): dedupe delivered completion announces * refactor(subagents): distill cleanup delivery status writes * fix: dedupe delivered subagent completion announces (openclaw#61525) (thanks @100yenadmin) --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
…(thanks @100yenadmin) * fix(subagents): dedupe delivered completion announces * refactor(subagents): distill cleanup delivery status writes * fix: dedupe delivered subagent completion announces (openclaw#61525) (thanks @100yenadmin) --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
…(thanks @100yenadmin) * fix(subagents): dedupe delivered completion announces * refactor(subagents): distill cleanup delivery status writes * fix: dedupe delivered subagent completion announces (openclaw#61525) (thanks @100yenadmin) --------- Co-authored-by: Eva <[email protected]> Co-authored-by: Ayaan Zaidi <[email protected]>
…arent send replies (openclaw#61525)
What this fixes (plain English)
When a sub-agent completed its task but the completion announcement failed partway, retrying the cleanup would re-announce the completion to the parent agent — causing duplicate messages. This fix tracks whether the announcement was already delivered and skips re-announcing on retry, only performing cleanup bookkeeping.
Technical details
Root cause: No deduplication of the completion announce flow. On retry/re-entry after a partial cleanup failure, the entire announce+inject cycle re-ran even if the parent had already received the completion message.
Fix:
completionAnnouncedAton the subagent run record after successful deliveryFiles changed:
src/agents/subagent-registry.ts— sweep integration with dedup tracking,completionAnnouncedAtpersistencesrc/agents/subagent-registry.types.ts— type definition forcompletionAnnouncedAtfield on subagent run recordssrc/agents/subagent-registry-lifecycle.ts— announce dedup and delivery tracking logicsrc/agents/subagent-registry-lifecycle.test.ts— lifecycle regression tests for dedup announce behaviorRelated
Test plan