Skip to content

fix(agents): suppress duplicate user persistence on fallback retries - #63696

Merged
altaywtf merged 4 commits into
openclaw:mainfrom
dashhuang:codex/agents-turn-scoped-fallback-persistence-v2
May 3, 2026
Merged

altaywtf merged 4 commits into
openclaw:mainfrom
dashhuang:codex/agents-turn-scoped-fallback-persistence-v2

Conversation

@dashhuang

Copy link
Copy Markdown
Contributor

Summary

  • suppress duplicate user-message persistence on embedded fallback retries once the current turn has already been written to the session transcript
  • track current-turn persistence from the actual transcript append path instead of inferring it from whether the session already existed
  • add focused regression coverage for the guard path and fallback-loop wiring

Why

#58301 preserves the original message body on fallback retries, which is the right fix for prompt continuity. But once the original body is preserved, fallback retries can still append that same user turn again if the current turn already flushed before the retry.

This patch narrows the suppression decision to the current turn only:

  • when the embedded runner actually persists the current user message, it fires a callback
  • the surrounding fallback loop records that state for this run only
  • later retries suppress just the duplicate persistence, while still allowing the retry prompt to be sent to the model

That keeps retry context intact without duplicating the transcript.

What changed

  • src/agents/session-tool-result-guard.ts
    • add one-shot suppression for the next persisted user message
    • add an onUserMessagePersisted callback that fires only after a user message is successfully written
  • src/agents/session-tool-result-guard-wrapper.ts
    • thread the suppression flag and callback through guardSessionManager(...)
  • src/agents/pi-embedded-runner/run/params.ts
  • src/agents/pi-embedded-runner/run.ts
  • src/agents/pi-embedded-runner/run/attempt.ts
    • forward the suppression/callback plumbing into each embedded attempt
  • src/agents/command/attempt-execution.ts
    • forward onUserMessagePersisted and suppressPromptPersistenceOnRetry into runEmbeddedPiAgent(...)
  • src/agents/agent-command.ts
    • keep a run-local currentTurnUserMessagePersisted flag and suppress duplicate persistence only on fallback retries after that flag flips true
  • tests:
    • src/agents/session-tool-result-guard.test.ts
    • src/agents/agent-command.live-model-switch.test.ts

Scope

  • embedded-agent fallback persistence only
  • no Telegram delivery changes
  • no change to the fallback prompt-selection policy itself

Related

Validation

Ran locally on a fresh main clone:

  • corepack pnpm exec vitest run --config vitest.config.ts src/agents/session-tool-result-guard.test.ts src/agents/agent-command.live-model-switch.test.ts src/agents/pi-embedded-runner.guard.test.ts src/agents/command/attempt-execution.test.ts
    • result: 4 files passed, 52 tests passed
  • corepack pnpm exec vitest run --config vitest.e2e.config.ts src/agents/model-fallback.run-embedded.e2e.test.ts
    • result: 1 file passed, 13 tests passed

Also checked:

  • git diff --check

Notes

corepack pnpm build currently fails on fresh main before this diff is applied due an unrelated baseline resolver error:

  • Could not resolve 'acpx/runtime' in extensions/acpx/src/runtime.ts

I did not change any acpx files in this PR.

@greptile-apps

greptile-apps Bot commented Apr 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds a one-shot suppression mechanism to prevent duplicate user-message persistence when the embedded runner retries with a fallback model after the current turn has already been written to the session transcript. The approach is sound: a callback fires on actual persistence, a run-scoped flag records that, and subsequent fallback attempts receive suppressNextUserMessagePersistence: true only when warranted.

Confidence Score: 5/5

Safe to merge; no correctness issues found. The suppression mechanism is correctly scoped to the current turn and properly resets across outer retry iterations.

All findings are P2 (style/type consistency). The core logic — tracking persistence via callback, suppressing only on fallback retries after the flag flips, resetting per outer loop iteration — is sound and well-tested.

No files require special attention.

Vulnerabilities

No security concerns identified.

Prompt To Fix All With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run/params.ts
Line: 133

Comment:
**`onUserMessagePersisted` signature is narrower than the guard's**

`RunEmbeddedPiAgentParams` declares `onUserMessagePersisted?: () => void`, but `installSessionToolResultGuard` calls it as `opts?.onUserMessagePersisted?.(finalMessage)`, passing the persisted user message as an argument. TypeScript's callback assignability makes this compile without error, but the narrower signature in `params.ts` (and the matching declaration in `attempt-execution.ts`) hides that the callback receives the user message. Any future caller who wants to inspect the persisted message from this hook would have to discover the richer guard-level signature independently.

Consider aligning the declaration here with the guard's full signature, or add a comment noting that the underlying guard does supply the message argument.

How can I resolve this? If you propose a fix, please make it concise.

Reviews (1): Last reviewed commit: "fix(agents): suppress duplicate user per..." | Re-trigger Greptile

*/
allowTransientCooldownProbe?: boolean;
suppressNextUserMessagePersistence?: boolean;
onUserMessagePersisted?: () => void;

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 onUserMessagePersisted signature is narrower than the guard's

RunEmbeddedPiAgentParams declares onUserMessagePersisted?: () => void, but installSessionToolResultGuard calls it as opts?.onUserMessagePersisted?.(finalMessage), passing the persisted user message as an argument. TypeScript's callback assignability makes this compile without error, but the narrower signature in params.ts (and the matching declaration in attempt-execution.ts) hides that the callback receives the user message. Any future caller who wants to inspect the persisted message from this hook would have to discover the richer guard-level signature independently.

Consider aligning the declaration here with the guard's full signature, or add a comment noting that the underlying guard does supply the message argument.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/agents/pi-embedded-runner/run/params.ts
Line: 133

Comment:
**`onUserMessagePersisted` signature is narrower than the guard's**

`RunEmbeddedPiAgentParams` declares `onUserMessagePersisted?: () => void`, but `installSessionToolResultGuard` calls it as `opts?.onUserMessagePersisted?.(finalMessage)`, passing the persisted user message as an argument. TypeScript's callback assignability makes this compile without error, but the narrower signature in `params.ts` (and the matching declaration in `attempt-execution.ts`) hides that the callback receives the user message. Any future caller who wants to inspect the persisted message from this hook would have to discover the richer guard-level signature independently.

Consider aligning the declaration here with the guard's full signature, or add a comment noting that the underlying guard does supply the message argument.

How can I resolve this? If you propose a fix, please make it concise.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in 5b523195c7.

I aligned the callback signature across run/params.ts, attempt-execution.ts, and the embedded attempt wiring so onUserMessagePersisted now explicitly receives the persisted user message instead of hiding that richer contract behind a () => void type.

Copy link
Copy Markdown
Contributor Author

Status note from our side: this is the clean v2 PR to review for the duplicate-persistence-on-fallback fix.

#61228 was the earlier bad-base version and can be treated as superseded. On this v2 branch, the only review note we saw was the narrower onUserMessagePersisted callback signature, and that was addressed in 5b523195c7.

If maintainers still want this behavior fixed upstream, this is the minimal agents-only diff we would point at rather than the older PR.

@clawsweeper

clawsweeper Bot commented Apr 29, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The PR threads a turn-scoped user-message persistence callback and one-shot retry suppression through embedded fallback attempts, with focused regression tests and a changelog entry.

Reproducibility: yes. Source inspection on current main shows runWithModelFallback can invoke embedded agent execution multiple times, and the guarded session append path persists every final user message without retry suppression.

Next step before merge
No repair lane is needed; this open implementation PR has no blocking review finding and needs normal maintainer merge/CI handling.

Security
Cleared: The PR only changes in-process agents persistence plumbing, tests, and changelog text, with no dependency, workflow, package, permission, secret, or external execution changes.

Review details

Best possible solution:

Land this PR or an equivalent agents-only fix after exact-head CI, keeping suppression tied to actual current-turn append success.

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

Yes. Source inspection on current main shows runWithModelFallback can invoke embedded agent execution multiple times, and the guarded session append path persists every final user message without retry suppression.

Is this the best way to solve the issue?

Yes. The callback-based one-shot guard is the narrowest maintainable solution because it suppresses only duplicate retry transcript writes after a real append while preserving the retry prompt sent to the model.

What I checked:

  • Current main fallback loop lacks persistence state: agentCommandInternal tracks only fallbackAttemptIndex before repeatedly calling runAgentAttempt; it does not know whether the active user turn was already appended before a fallback retry. (src/agents/agent-command.ts:968, 89ac1261800d)
  • Current main guard appends every final message: The session guard applies transforms/hooks and then calls originalAppend(finalMessage) without a user-message suppression gate or persisted-user callback. (src/agents/session-tool-result-guard.ts:449, 89ac1261800d)
  • Embedded retry path uses the guarded session: Embedded attempts create a guarded SessionManager and later submit the prompt through activeSession.prompt(...), which is the persistence path the PR targets. (src/agents/pi-embedded-runner/run/attempt.ts:1421, 89ac1261800d)
  • Fallback runner can execute multiple attempts: runWithModelFallback iterates fallback candidates and calls the supplied run function for each attempted candidate, so the same command turn can reach embedded execution more than once. (src/agents/model-fallback.ts:961, 89ac1261800d)
  • PR patch uses actual append success as the signal: The latest PR diff adds currentTurnUserMessagePersisted, passes suppression only on fallback retries after that flag flips, and adds a one-shot suppress-next-user-message guard plus callback forwarding. (src/agents/agent-command.ts:966, a5fea2257932)
  • Earlier callback signature review was addressed: The PR discussion records Greptile's narrower-callback concern and the follow-up fix; the latest file patch types onUserMessagePersisted with the persisted user AgentMessage. (src/agents/pi-embedded-runner/run/params.ts:180, a5fea2257932)

Likely related people:

  • steipete: Peter Steinberger appears in the relevant history for splitting agent command execution helpers, centralizing model fallback resolution, and introducing/maintaining the session tool-result guard path. (role: feature-history and recent maintainer; confidence: high; commits: 046a950877f6, 9beec48e9c67, f5d5661adfec; files: src/agents/agent-command.ts, src/agents/command/attempt-execution.ts, src/agents/session-tool-result-guard.ts)
  • Ayaan Zaidi: Recent local history shows Ayaan Zaidi maintaining transcript persistence and agent command paths adjacent to this fallback/session flow. (role: recent adjacent maintainer; confidence: medium; commits: 898fd0482a40, 3a3fae0eac0f, b8ef507cc082; files: src/agents/agent-command.ts, src/agents/command/attempt-execution.ts)

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

@altaywtf altaywtf self-assigned this May 2, 2026
@altaywtf
altaywtf force-pushed the codex/agents-turn-scoped-fallback-persistence-v2 branch 3 times, most recently from 831a96b to a90c197 Compare May 3, 2026 10:17
@altaywtf
altaywtf force-pushed the codex/agents-turn-scoped-fallback-persistence-v2 branch from a90c197 to a5fea22 Compare May 3, 2026 13:47
@altaywtf
altaywtf merged commit d35c79e into openclaw:main May 3, 2026
103 of 109 checks passed
@altaywtf

altaywtf commented May 3, 2026

Copy link
Copy Markdown
Member

Merged via squash.

Thanks @dashhuang!

lxe pushed a commit to lxe/openclaw that referenced this pull request May 6, 2026
…penclaw#63696)

* fix(agents): suppress duplicate user persistence on fallback retries

* refactor(agents): align persisted-user callback types

* docs: note fallback transcript dedupe

* refactor(agents): remove fallback persistence casts

---------

Co-authored-by: Altay <[email protected]>
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 24, 2026
…penclaw#63696)

* fix(agents): suppress duplicate user persistence on fallback retries

* refactor(agents): align persisted-user callback types

* docs: note fallback transcript dedupe

* refactor(agents): remove fallback persistence casts

---------

Co-authored-by: Altay <[email protected]>
jameslcowan pushed a commit to jameslcowan/openclaw that referenced this pull request Jun 2, 2026
…penclaw#63696)

* fix(agents): suppress duplicate user persistence on fallback retries

* refactor(agents): align persisted-user callback types

* docs: note fallback transcript dedupe

* refactor(agents): remove fallback persistence casts

---------

Co-authored-by: Altay <[email protected]>
sablehead pushed a commit to sablehead/openclaw that referenced this pull request Jun 10, 2026
…penclaw#63696)

* fix(agents): suppress duplicate user persistence on fallback retries

* refactor(agents): align persisted-user callback types

* docs: note fallback transcript dedupe

* refactor(agents): remove fallback persistence casts

---------

Co-authored-by: Altay <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants