Skip to content

Fix stale Discord final answer payload - #82645

Closed
compoodment wants to merge 6 commits into
openclaw:mainfrom
compoodment:fix-discord-final-answer-payload
Closed

compoodment wants to merge 6 commits into
openclaw:mainfrom
compoodment:fix-discord-final-answer-payload

Conversation

@compoodment

@compoodment compoodment commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Problem: Discord final-send payloads could keep a stale accumulated streamed text snapshot even when the assistant message contained a complete final_answer.
  • Why it matters: users can see an incomplete/truncated final Discord reply after the model already produced the canonical final answer.
  • What changed: when there is exactly one accumulated assistant text and a phase-tagged canonical final_answer is available, the payload builder now sends the canonical final answer; the incomplete-prefix heuristic remains as a fallback for unphased final text.
  • What did NOT change (scope boundary): this does not change tool-output suppression, media reply-directive handling, multi-text final-answer precedence, or channel delivery APIs.

AI-assisted: yes, implemented with Codex and manually inspected/verified.

Change Type (select all)

  • Bug fix
  • Feature
  • Refactor required for the fix
  • Docs
  • Security hardening
  • Chore/infra

Scope (select all touched areas)

  • Gateway / orchestration
  • Skills / tool execution
  • Auth / tokens
  • Memory / storage
  • Integrations
  • API / contracts
  • UI / DX
  • CI/CD / infra

Linked Issue/PR

Real behavior proof (required for external PRs)

External proof status: partial only. I verified the production payload-selection behavior locally with a focused regression test, but I did not run this patched checkout as the live Discord bot/gateway runtime.

  • Behavior or issue addressed: a final Discord reply can use an older accumulated streamed snapshot instead of the completed final_answer.
  • Real environment tested: OpenClaw source checkout on Ubuntu Linux VPS, Node v24.15.0.
  • Exact steps or command run after this patch: node scripts/test-projects.mjs src/agents/pi-embedded-runner/run/payloads.test.ts
  • Evidence after fix (screenshot, recording, terminal capture, console output, redacted runtime log, linked artifact, or copied live output):
[test] starting test/vitest/vitest.unit-fast.config.ts

 RUN  v4.1.6 /tmp/openclaw-src

 Test Files  1 passed (1)
      Tests  29 passed (29)
   Start at  19:08:46
   Duration  2.02s (transform 1.03s, setup 0ms, import 1.69s, tests 46ms, environment 0ms)

[test] passed 1 Vitest shard in 11.09s
  • Observed result after fix: the regression cases return the complete canonical final_answer when the single accumulated text is an incomplete prefix or contains commentary plus final text, and preserve the accumulated text when there is no explicit final answer.
  • What was not tested: full live patched Discord delivery through a running gateway/channel runtime.
  • Before evidence (optional but encouraged): the added regression test models the observed failure shape where the accumulated text ends early while lastAssistant contains a longer final_answer.

Additional live PR-build Discord delivery proof

Test setup:

  • Isolated PR sidecar build, not the normal beta install.
  • Git head included a933dc0d94fcbdbd9ef0fd4de8cde9518a94106c.
  • Built runtime reported OpenClaw 2026.5.17 (a933dc0).
  • Discord plugin loaded and the PR sidecar gateway reached ready state.
  • Built dist contained the new fix symbols:
    • resolveExplicitFinalAnswerText
    • isLikelyIncompleteAssistantTextSnapshot

Discord delivery stress results:

  • PR82645-STRESS-1: delivered final marker END-PR82645-STRESS-1.
  • PR82645-STRESS-2: delivered full 8-bullet response ending END-PR82645-STRESS-2.
  • PR82645-STRESS-3: delivered two-paragraph response ending END-PR82645-STRESS-3.

Outcome:

No truncation reproduced, no commentary leakage reproduced, and all three PR-sidecar Discord deliveries reached the expected final markers.

Caveat:

Prompts were driven through the isolated PR sidecar with Discord delivery enabled, rather than inbound Discord DMs, because the normal beta gateway was still active. This still exercises PR-build Discord delivery from code containing a933dc0d94.

Root Cause (if applicable)

  • Root cause: buildEmbeddedRunPayloads only preferred the canonical final answer when there were multiple accumulated assistant text payloads. A single accumulated snapshot was always treated as the payload source, even when phase metadata made the canonical final answer available.
  • Missing detection / guardrail: there was no regression test for a single incomplete accumulated text snapshot competing with a complete final_answer.
  • Contributing context (if known): streamed/status text can be captured before the final canonical assistant item is available, so the stale snapshot can outlive the completed answer unless the final payload builder detects it.

Regression Test Plan (if applicable)

  • Coverage level that should have caught this:
    • Unit test
    • Seam / integration test
    • End-to-end test
    • Existing coverage already sufficient
  • Target test or file: src/agents/pi-embedded-runner/run/payloads.test.ts
  • Scenario the test should lock in: a single accumulated assistant text should send the canonical answer when it has an explicit phase-tagged final_answer, including the commentary-plus-final one-entry shape; the incomplete-prefix fallback remains covered too.
  • Why this is the smallest reliable guardrail: the bug is in pure payload selection logic before the channel send layer, so a focused unit test catches the branch without needing Discord credentials or a running gateway.
  • Existing test that already covers this (if any): none.
  • If no new test is added, why not: N/A, this PR adds coverage.

User-visible / Behavior Changes

Discord final replies should show the completed assistant answer instead of an older incomplete streamed snapshot in this edge case. No config or default behavior changes.

Diagram (if applicable)

Before:
[stream snapshot captured early] -> [final_answer completes] -> [Discord final payload uses early snapshot]

After:
[stream snapshot captured early] -> [final_answer completes] -> [payload builder detects incomplete prefix] -> [Discord final payload uses final_answer]

Security Impact (required)

  • New permissions/capabilities? No
  • Secrets/tokens handling changed? No
  • New/changed network calls? No
  • Command/tool execution surface changed? No
  • Data access scope changed? No
  • If any Yes, explain risk + mitigation: N/A

Repro + Verification

Environment

  • OS: Linux dicky 6.8.0-106-generic x86_64
  • Runtime/container: local source checkout, Node v24.15.0
  • Model/provider: N/A for the focused payload-builder test
  • Integration/channel (if any): Discord final payload path
  • Relevant config (redacted): N/A

Steps

  1. Build payloads with one accumulated assistant text that ends before the canonical final_answer.
  2. Include lastAssistant text with textSignature.phase = "final_answer".
  3. Verify the generated final payload text equals the canonical final answer.
  4. Verify the commentary-plus-final one-entry shape sends only the canonical final answer.
  5. Verify a different single accumulated text is still preserved when there is no explicit final answer.

Expected

  • Incomplete accumulated prefix: payload uses canonical final_answer.
  • Commentary-plus-final single text with explicit final phase: payload uses canonical final_answer.
  • Single accumulated text without explicit final phase: payload preserves the accumulated text.

Actual

  • Matches expected in the added regression tests.

Evidence

Attach at least one:

  • Failing test/log before + passing after
  • Trace/log snippets
  • Screenshot/recording
  • Perf numbers (if relevant)

Copied verification output is included in the Real behavior proof section above.

Human Verification (required)

What you personally verified (not just CI), and how:

  • Verified scenarios: focused payload regression test for incomplete-prefix replacement, commentary-plus-final single-snapshot replacement, and no-explicit-final preservation.
  • Edge cases checked: generated trailing ellipsis is stripped before prefix comparison; a phase-tagged final answer overrides a single commentary-plus-final snapshot; a different unphased fallback does not replace a single accumulated text.
  • What you did not verify: live patched Discord send behavior, full pnpm build && pnpm check && pnpm test, and local codex review --base origin/main because the codex CLI is not installed in this VPS environment.

Review Conversations

  • I replied to or resolved every bot review conversation I addressed in this PR.
  • I left unresolved only the conversations that still need reviewer or maintainer judgment.

No bot review conversations have been addressed so far.

Compatibility / Migration

  • Backward compatible? Yes
  • Config/env changes? No
  • Migration needed? No
  • If yes, exact upgrade steps: N/A

Risks and Mitigations

  • Risk: the prefix heuristic could choose final_answer when a single accumulated text is intentionally shorter but still a prefix of the final answer.
    • Mitigation: the heuristic only applies when the canonical final answer is longer and starts with the normalized accumulated text; the PR also adds a guard test showing unrelated single accumulated text is preserved.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S triage: needs-real-behavior-proof Candidate: external PR needs after-fix proof from a real setup. 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 16, 2026
@clawsweeper

clawsweeper Bot commented May 16, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge.

Summary
The branch updates buildEmbeddedRunPayloads to prefer a phase-tagged canonical final_answer over a stale single accumulated assistant-text snapshot and adds focused regression coverage.

Reproducibility: yes. at source level: on current main, buildEmbeddedRunPayloads with exactly one raw accumulated assistant text and a phase-tagged lastAssistant still returns the raw text because canonical fallback is gated on more than one accumulated text. The PR adds focused regression cases for that path.

Real behavior proof
Sufficient (live_output): The PR body includes after-fix PR-sidecar Discord delivery evidence with the patched runtime head and expected final markers, plus focused regression test output.

Next step before merge
No ClawSweeper repair job is needed because the PR already contains the focused implementation, regression coverage, sufficient proof, and no blocking review findings.

Security
Cleared: The diff only changes pure payload-selection logic and tests; it does not alter dependencies, scripts, permissions, secrets, network calls, or package metadata.

Review details

Best possible solution:

Land this focused payload-builder fix with its regression tests, while leaving the restart/abort recovery class to the existing dedicated issues.

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

Yes, at source level: on current main, buildEmbeddedRunPayloads with exactly one raw accumulated assistant text and a phase-tagged lastAssistant still returns the raw text because canonical fallback is gated on more than one accumulated text. The PR adds focused regression cases for that path.

Is this the best way to solve the issue?

Yes. Preferring the already phase-filtered canonical final answer when explicit final-answer metadata exists is the narrow maintainable fix, and the PR preserves the unphased fallback boundary with coverage.

What I checked:

Likely related people:

  • steipete: Authored the current canonical embedded reply payload behavior and earlier commentary fallback suppression in the same payload/phase extraction surface. (role: recent area contributor; confidence: high; commits: 83a108080de7, 9eed092baa50, 98ce1c29027a; files: src/agents/pi-embedded-runner/run/payloads.ts, src/agents/pi-embedded-runner/run/payloads.test.ts, src/agents/pi-embedded-utils.ts)
  • Sanjays2402: Added the blank streamed-text recovery path in buildEmbeddedRunPayloads, directly adjacent to this final-answer fallback behavior. (role: adjacent behavior contributor; confidence: medium; commits: 1f06dbd04ced; files: src/agents/pi-embedded-runner/run/payloads.ts, src/agents/pi-embedded-runner/run/payloads.test.ts)
  • BunsDev: Recently changed aborted assistant output suppression in the same payload helper and nearby regression tests. (role: recent area contributor; confidence: medium; commits: dd4c68b5252d; files: src/agents/pi-embedded-runner/run/payloads.ts, src/agents/pi-embedded-runner/run/payloads.errors.test.ts, src/agents/pi-embedded-runner/run/payloads.test.ts)
  • vincentkoc: Current checkout blame and path history show recent maintenance across the payload builder and agent delivery helper area, including warning-suppression work in this file. (role: recent adjacent contributor; confidence: medium; commits: 5911b5bf2d4c, 8c886e943848; files: src/agents/pi-embedded-runner/run/payloads.ts, src/agents/pi-embedded-runner/run/payloads.test.ts)

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

@clawsweeper clawsweeper Bot added the P2 Normal backlog priority with limited blast radius. label May 16, 2026

Copy link
Copy Markdown
Contributor Author

Addressed the ClawSweeper P2 finding in a933dc0.

What changed:

  • Single accumulated assistant text now prefers an explicit phase-tagged final_answer when present.
  • The incomplete-prefix heuristic remains as fallback for unphased final text.
  • Added regression coverage for the commentary-plus-final one-entry shape: Need inspect.\n\nDone. -> Done.
  • Kept coverage that a single accumulated text is preserved when there is no explicit final answer.

Verification:

  • node scripts/test-projects.mjs src/agents/pi-embedded-runner/run/payloads.test.ts on dicky: 29 passed.
  • Touched-file format check: passed.
  • Touched-file oxlint: passed.
  • pnpm check:changed passed through tests/format/typecheck but failed on unrelated existing lint in src/commands/gateway-readiness.ts.

@clawsweeper re-review

@clawsweeper

clawsweeper Bot commented May 16, 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:

rteoo commented May 16, 2026

Copy link
Copy Markdown

I tested this branch while investigating a related Discord "progress/status happened, but no final done reply" incident on OpenClaw 2026.5.12 (f066dd2).

Local test results

  • PR Fix stale Discord final answer payload #82645 branch direct payload suite:
    • pnpm exec vitest run src/agents/pi-embedded-runner/run/payloads.test.ts
    • passed: 1 file, 29 tests
  • PR Fix stale Discord final answer payload #82645 branch full pi-embedded suite:
    • pnpm exec vitest run --config test/vitest/vitest.agents-pi-embedded.config.ts
    • 1011/1012 passed
    • 1 failure looked unrelated: model forward-compat expectation for anthropic/claude-sonnet-4-5
  • origin/main direct payload suite:
    • pnpm exec vitest run src/agents/pi-embedded-runner/run/payloads.test.ts
    • passed: 1 file, 26 tests

My read: this PR looks correct for the stale/truncated final payload-selection bug described in the PR body. It does not fix the separate restart/abort/no-final-lifecycle class I was investigating, where the Discord turn never reaches model.completed / session.ended and no assistant final is written before a restart, abort, or timeout. That gap still matches #69249 / #78380 more closely.

Copy link
Copy Markdown
Contributor Author

Added sanitized live PR-build Discord delivery evidence to the PR body.

The test used an isolated PR sidecar build containing a933dc0d94, verified the built runtime and fix symbols, and delivered three Discord stress replies with expected final markers and no truncation/commentary leakage.

@clawsweeper re-review

@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 16, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@clawsweeper clawsweeper Bot added the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@openclaw-barnacle openclaw-barnacle Bot removed the proof: sufficient ClawSweeper judged the real behavior proof convincing. label May 17, 2026
@clawsweeper clawsweeper Bot added proof: sufficient ClawSweeper judged the real behavior proof convincing. impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. labels May 17, 2026
@steipete

Copy link
Copy Markdown
Contributor

Thanks for the focused fix and the extra Discord proof.

I'm closing this as superseded by #82850, which has already landed on main as 7d13176. That merged change covers the same stale streamed-preview/final-answer payload class with a broader embedded-runner fix: it passes the current assistant into payload assembly, prefers the completed structured final answer, keeps stale transcript assistants from overriding current one-chunk replies, and includes focused payload regressions.

I also verified current main locally with:

OPENCLAW_VITEST_MAX_WORKERS=1 node scripts/run-vitest.mjs src/agents/pi-embedded-runner/run/payloads.test.ts

Result: 1 file passed, 28 tests passed.

Leaving this open would now duplicate the already-landed fix and conflict with the current payload helper shape. If we still want the exact commentary-plus-final one-entry regression from this branch, we can add that as a tiny follow-up test on top of main.

@steipete steipete closed this May 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling impact:message-loss Channel message delivery can be lost, duplicated, or misrouted. P2 Normal backlog priority with limited blast radius. proof: sufficient ClawSweeper judged the real behavior proof convincing. proof: supplied External PR includes structured after-fix real behavior proof. size: S

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants