Skip to content

fix(agents): persist CLI user turns before attempts#86624

Closed
ai-hpc wants to merge 1 commit into
openclaw:mainfrom
ai-hpc:fix/86592-persist-inbound-user-turns
Closed

fix(agents): persist CLI user turns before attempts#86624
ai-hpc wants to merge 1 commit into
openclaw:mainfrom
ai-hpc:fix/86592-persist-inbound-user-turns

Conversation

@ai-hpc

@ai-hpc ai-hpc commented May 25, 2026

Copy link
Copy Markdown
Member

Summary

  • Persist accepted CLI user turns to the session transcript after before_agent_run hooks pass and before the backend attempt or nonblocking llm_input notification can fail.
  • Carry the persisted-user signal through post-run transcript mirroring so successful CLI turns append only the assistant and do not duplicate the same user message.
  • Thread the same signal through direct and queued fallback dispatch so a later fallback candidate does not persist the same inbound twice.

Fixes #86592

Verification

  • pnpm test src/agents/cli-runner.reliability.test.ts src/agents/command/attempt-execution.cli.test.ts src/commands/agent.test.ts src/auto-reply/reply/agent-runner-execution.test.ts src/auto-reply/reply/followup-runner.test.ts -- --reporter=verbose
  • pnpm format:check src/agents/agent-command.ts src/agents/cli-runner.reliability.test.ts src/agents/cli-runner.ts src/agents/cli-runner/types.ts src/agents/command/attempt-execution.cli.test.ts src/agents/command/attempt-execution.ts src/auto-reply/reply/agent-runner-execution.test.ts src/auto-reply/reply/agent-runner-execution.ts src/auto-reply/reply/followup-runner.test.ts src/auto-reply/reply/followup-runner.ts src/commands/agent.test.ts
  • pnpm tsgo:core
  • pnpm tsgo:core:test
  • node scripts/run-oxlint.mjs --tsconfig config/tsconfig/oxlint.core.json src/agents/agent-command.ts src/agents/cli-runner.reliability.test.ts src/agents/cli-runner.ts src/agents/cli-runner/types.ts src/agents/command/attempt-execution.cli.test.ts src/agents/command/attempt-execution.ts src/auto-reply/reply/agent-runner-execution.test.ts src/auto-reply/reply/agent-runner-execution.ts src/auto-reply/reply/followup-runner.test.ts src/auto-reply/reply/followup-runner.ts src/commands/agent.test.ts
  • git diff --check

Real behavior proof

Behavior addressed: accepted Telegram topic user turns are persisted before the agent attempt, so a follow-up in the same topic/session can read the prior user turn instead of losing it when the first candidate owns persistence.

Real environment tested: local OpenClaw source checkout at 052bde7ac426524808a04ccf97a2aebf6a0cf2e1; gateway running locally; Telegram bot channel in polling mode; Telegram supergroup topic; default model route resolved to openai/gpt-5.5.

Exact steps or command run after this patch:

  1. Configured Telegram group allowlist and restarted the gateway.
  2. Sent @Tfungbot Reply exactly: group topic route works in a Telegram topic.
  3. Sent @Tfungbot Reply exactly: topic first turn persisted in the same topic.
  4. Without clearing session/history, sent @Tfungbot What did I ask you to reply exactly? in the same topic.
  5. Ran pnpm openclaw models status.
  6. Ran pnpm openclaw channels status --channel telegram --probe --json.
  7. Ran pnpm openclaw logs --limit 160 --plain.

Evidence after fix:

  • Telegram Desktop screenshot captured from the live topic shows:
    User: @Tfungbot Reply exactly: group topic route works
    Bot:  group topic route works
    User: @Tfungbot Reply exactly: topic first turn persisted
    Bot:  topic first turn persisted
    User: @Tfungbot What did I ask you to reply exactly?
    Bot:  topic first turn persisted
    
  • pnpm openclaw models status resolved the default model route to openai/gpt-5.5 with no missing provider in use.
  • pnpm openclaw channels status --channel telegram --probe --json reported Telegram configured, running, connected, polling mode, and probe.ok: true.
  • Redacted runtime log excerpt from pnpm openclaw logs --limit 160 --plain:
    2026-05-25T23:21:44.346Z info gateway agent model: openai/gpt-5.5
    2026-05-25T23:21:47.247Z info channels/telegram [default] starting provider (@Tfungbot)
    2026-05-25T23:21:50.413Z info gateway gateway ready
    2026-05-25T23:33:01.395Z info gateway/channels/telegram/inbound Inbound message telegram:group:<redacted>:topic:1 -> @Tfungbot (group, 41 chars)
    2026-05-25T23:33:15.367Z info gateway/ws res ok message.action channel=telegram
    2026-05-25T23:34:16.778Z info gateway/channels/telegram/inbound Inbound message telegram:group:<redacted>:topic:1 -> @Tfungbot (group, 63 chars)
    2026-05-25T23:34:55.240Z info gateway/ws res ok message.action channel=telegram
    2026-05-25T23:38:04.436Z info gateway/channels/telegram/inbound Inbound message telegram:group:<redacted>:topic:1 -> @Tfungbot (group, 27 chars)
    2026-05-25T23:38:17.677Z info gateway/ws res ok message.action channel=telegram
    2026-05-25T23:39:05.353Z info gateway/ws res ok channels.status
    

Observed result after fix: the bot replied inside the same Telegram topic, and the follow-up answer returned topic first turn persisted, proving the previous accepted user turn was available in the live topic session.

What was not tested: cross-account Telegram routing and non-Telegram channels were not re-run locally. The focused live proof covers the Telegram topic/session path touched by this fix; regression tests cover CLI/direct agent, queued follow-up, fallback suppression, and session JSONL persistence.

@openclaw-barnacle openclaw-barnacle Bot added commands Command implementations agents Agent runtime and tooling size: M triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 25, 2026
@clawsweeper

clawsweeper Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed May 25, 2026, 8:54 PM ET / 00:54 UTC.

Summary
The PR persists accepted CLI user prompts to the session transcript before CLI backend attempts, suppresses duplicate post-run/fallback user writes, and adds regression tests for CLI failure, post-run mirroring, and queued fallback paths.

PR surface: Source +59, Tests +143. Total +202 across 11 files.

Reproducibility: yes. source-reproducible: current main only writes the CLI user prompt in post-run persistence, so a thrown CLI backend exits before the user line is appended. The PR also supplies live Telegram after-fix proof rather than only mock tests.

Review metrics: none identified.

Merge readiness
Overall: 🐚 platinum hermit
Proof: 🦞 diamond lobster ✨ media proof bonus
Patch quality: 🐚 platinum hermit
Result: ready for maintainer review.

Overall follows the weaker of proof and patch quality, so missing proof can cap an otherwise strong patch.

Rank-up moves:

  • Decide whether the linked issue should close with this CLI-scoped fix or stay open for ACP-specific failure persistence.
  • Optionally run independent Telegram Desktop proof before landing if maintainers want a second live transport check.

Mantis proof suggestion
An independent Telegram Desktop run would materially verify the visible topic/session behavior before merge, even though contributor proof is already sufficient. A maintainer can ask Mantis to capture proof by posting a new PR comment that starts with the OpenClaw Mantis account mention, followed by:

telegram desktop proof: verify a follow-up in the same Telegram topic can recall the previous CLI-backed user turn without duplicate transcript user messages.

Risk before merge

  • This PR changes when CLI user turns become durable and how fallback candidates suppress duplicate user writes; a mistake here can create lost or duplicated transcript state across retries.
  • The linked issue explicitly mentions ACP persistence as an affected symbol, while this branch is scoped to CLI-backed turns, so maintainers should decide the closure scope before landing.

Maintainer options:

  1. Land as a CLI-scoped session fix (recommended)
    Merge after maintainer acceptance that early CLI prompt persistence plus duplicate suppression is the intended durable transcript behavior.
  2. Broaden before closing the linked issue
    If maintainers want this PR to fully close the linked issue, add ACP failure-path persistence or adjust the issue closure scope before merge.

Next step before merge
No automated repair is needed; maintainers should review the session-state merge risk and decide the linked issue closure scope.

Security
Cleared: No concrete security or supply-chain concern was found; the diff changes TypeScript runtime/test code and uses the existing transcript append helper rather than adding dependencies or new secret handling.

Review details

Best possible solution:

Land the CLI transcript durability fix once maintainers accept the session-state ordering, and keep or create a narrower ACP follow-up if the linked issue is broader than this patch.

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

Yes, source-reproducible: current main only writes the CLI user prompt in post-run persistence, so a thrown CLI backend exits before the user line is appended. The PR also supplies live Telegram after-fix proof rather than only mock tests.

Is this the best way to solve the issue?

Yes for the CLI path: persisting after before_agent_run approval and before backend/llm_input failure is the narrow durable-transcript fix, with post-run and fallback duplicate suppression. The ACP closure scope is still a maintainer decision, not a code defect in this CLI patch.

AGENTS.md: found and applied where relevant.

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

Label changes

Label justifications:

  • P1: Lost CLI user transcript turns can wedge real agent/channel sessions and block recovery after backend failures.
  • merge-risk: 🚨 session-state: The PR changes durable transcript ordering and fallback duplicate suppression for session state.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🦞 diamond lobster and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Sufficient (screenshot): The PR body and attached Telegram Desktop screenshot show after-fix live topic behavior, with redacted model/channel status and runtime logs supporting the path tested.
  • proof: sufficient: Contributor real behavior proof is sufficient. The PR body and attached Telegram Desktop screenshot show after-fix live topic behavior, with redacted model/channel status and runtime logs supporting the path tested.
  • proof: 📸 screenshot: Contributor real behavior proof includes screenshot evidence. The PR body and attached Telegram Desktop screenshot show after-fix live topic behavior, with redacted model/channel status and runtime logs supporting the path tested.
  • mantis: telegram-visible-proof: Mantis should capture Telegram visible proof. The changed behavior is visible in Telegram topic follow-up memory and can be demonstrated in a short Telegram Desktop recording.
Evidence reviewed

PR surface:

Source +59, Tests +143. Total +202 across 11 files.

View PR surface stats
Area Files Added Removed Net
Source 6 60 1 +59
Tests 5 145 2 +143
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 11 205 3 +202

What I checked:

  • Repository policy applied: Root policy and the scoped src/agents guide were read; session-state compatibility and proof guidance affected this review. (AGENTS.md:16, c9364f03dcbb)
  • Telegram proof standard applied: The Telegram maintainer note calls for real Telegram proof for topic and reply-context behavior, and this PR supplies that kind of proof. (.agents/maintainer-notes/telegram.md:45, c9364f03dcbb)
  • Current main bug surface: Current main runs the CLI llm_input hook and then the CLI attempt without a preceding user transcript append in runPreparedCliAgent. (src/agents/cli-runner.ts:623, c9364f03dcbb)
  • Current main post-run persistence: Current main appends the user prompt inside post-run transcript persistence, so a thrown CLI attempt exits before that user line is written. (src/agents/command/attempt-execution.ts:232, c9364f03dcbb)
  • PR implementation evidence: The PR head adds pre-attempt appendSessionTranscriptMessage handling for CLI turns and skipUserMessage wiring so successful post-run mirroring appends only the assistant after the user was already persisted. (src/agents/cli-runner.ts:110, 052bde7ac426)
  • Fallback duplicate-suppression evidence: The PR threads suppressNextUserMessagePersistence and onUserMessagePersisted through CLI fallback dispatch and adds tests for queued fallback duplicate suppression. (src/auto-reply/reply/agent-runner-execution.ts:1802, 052bde7ac426)

Likely related people:

  • ubehera: Current blame points the agent lifecycle state, transcript persistence parameters, and embedded user-persistence callback plumbing to 48adcb1. (role: introduced/current behavior provenance; confidence: medium; commits: 48adcb162c92; files: src/agents/agent-command.ts, src/agents/command/attempt-execution.ts, src/agents/pi-embedded-runner/run/attempt.ts)
  • tanshanshan: Recent main history shows adjacent auto-reply/session fallback work in the same affected fallback surface. (role: recent adjacent area contributor; confidence: medium; commits: 3a4f2b17fcfc; files: src/auto-reply/reply/agent-runner-execution.ts, src/auto-reply/reply/followup-runner.ts)
What the crustacean ranks mean
  • 🦀 challenger crab: rare, exceptional readiness with strong proof, clean implementation, and convincing validation.
  • 🦞 diamond lobster: very strong readiness with only minor maintainer review expected.
  • 🐚 platinum hermit: good normal PR, likely mergeable with ordinary maintainer review.
  • 🦐 gold shrimp: useful signal, but proof or patch confidence is still limited.
  • 🦪 silver shellfish: thin signal; proof, validation, or implementation needs work.
  • 🧂 unranked krab: not merge-ready because proof is missing/unusable or there are serious correctness or safety concerns.
  • 🌊 off-meta tidepool: rating does not apply to this item.

Shiny media proof means a screenshot, video, or linked artifact directly shows the changed behavior. Runtime, network, CSP, and security claims still need visible diagnostics.

How this review workflow works
  • ClawSweeper keeps one durable marker-backed review comment per issue or PR.
  • Re-runs edit this comment so the latest verdict, findings, and automation markers stay together instead of adding duplicate bot comments.
  • A fresh review can be triggered by eligible @clawsweeper re-review comments, exact-item GitHub events, scheduled/background review runs, or manual workflow dispatch.
  • PR/issue authors and users with repository write access can comment @clawsweeper re-review or @clawsweeper re-run on an open PR or issue to request a fresh review only.
  • Maintainers can also comment @clawsweeper review to request a fresh review only.
  • Fresh-review commands do not start repair, autofix, rebase, CI repair, or automerge.
  • Maintainer-only repair and merge flows require explicit commands such as @clawsweeper autofix, @clawsweeper automerge, @clawsweeper fix ci, or @clawsweeper address review.
  • Maintainers can comment @clawsweeper explain to ask for more context, or @clawsweeper stop to stop active automation.

@clawsweeper clawsweeper Bot added rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. P1 High-priority user-facing bug, regression, or broken workflow. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. labels May 25, 2026
@clawsweeper

clawsweeper Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

ClawSweeper PR egg

✨ Hatched: 🥚 common Brave Proofling

Hatch command

Comment @clawsweeper hatch when this PR is hatchable.

Hatchability rules:

  • Merged PRs are hatchable.
  • Open PRs are hatchable when they are status: 👀 ready for maintainer look, status: 🚀 automerge armed, or labeled clawsweeper:automerge.
  • Closed unmerged PRs are hatchable only when one of those hatchable labels is still present in the durable record.

Rarity: 🥚 common.
Trait: guards the happy path.
Image traits: location CI tidepool; accessory lint brush; palette amber, ink, and glacier blue; mood sleepy but ready; pose pointing at a small proof artifact; shell brushed metal shell; lighting soft underwater shimmer; background subtle branch markers.
Share on X: post this hatch
Copy: My PR egg hatched a 🥚 common Brave Proofling in ClawSweeper.

What is this egg doing here?
  • Eggs appear after the PR passes real-behavior proof. It is here for vibes, not verdicts: it does not change labels, ratings, merge decisions, or automation.
  • The shell reacts to review momentum: open follow-up work warms it up, re-review makes it wobble, and a clean final review lets it hatch.
  • Hatchability usually comes from sufficient real-behavior proof, no blocking P0/P1/P2 findings, no security attention needed, and clean correctness. A merged PR is already final, so merge makes the egg hatchable independently.
  • The hatch is seeded from this repository and PR number, so the same PR keeps the same creature; the reviewed head SHA can only change safe visual details.
  • Rarity is just collectible sparkle: 🥚 common, 🌱 uncommon, 💎 rare, ✨ glimmer, and 🌈 legendary.

@openclaw-barnacle openclaw-barnacle Bot added proof: supplied External PR includes structured after-fix real behavior proof. and removed triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. labels May 25, 2026
@ai-hpc

ai-hpc commented May 25, 2026

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

🦞🧹
ClawSweeper re-review requested.

I asked ClawSweeper to review this item again.
Action: item re-review queued (workflow sweep.yml, event repository_dispatch).
Result: the existing ClawSweeper review comment will be edited in place when the review finishes.

Re-review progress:

@ai-hpc

ai-hpc commented May 25, 2026

Copy link
Copy Markdown
Member Author
Test

@ai-hpc

ai-hpc commented May 25, 2026

Copy link
Copy Markdown
Member Author

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented May 25, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Command router queued. I will update this comment with the next step.

Re-review progress:

@clawsweeper clawsweeper Bot added 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. mantis: telegram-visible-proof Mantis should capture Telegram visible proof. and removed rating: 🦪 silver shellfish Thin PR readiness signal; proof, validation, or implementation needs work. status: 📣 needs proof The PR needs real behavior proof before ClawSweeper can clear the contributor ask. labels May 26, 2026
@BingqingLyu

This comment was marked as spam.

@ai-hpc

ai-hpc commented May 28, 2026

Copy link
Copy Markdown
Member Author

Closing this as superseded by current main. The CLI user-turn durability path is now covered by the newer user-turn transcript recorder flow, including early approved-turn persistence and fallback duplicate-suppression coverage. Keeping this PR open would mostly mean dropping the old patch during conflict resolution rather than fixing a remaining behavior gap.

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

Labels

agents Agent runtime and tooling commands Command implementations mantis: telegram-visible-proof Mantis should capture Telegram visible proof. merge-risk: 🚨 session-state 🚨 May lose, corrupt, stale, or mis-associate session, agent, or context state. P1 High-priority user-facing bug, regression, or broken workflow. proof: 📸 screenshot Contributor real behavior proof includes screenshot evidence. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: M 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.

Inbound user messages are not persisted to session JSONL when the agent attempt throws

2 participants