Skip to content

fix(agents): keep silent replies in replay so the session prefix stays append-only - #198183

Closed
obviyus wants to merge 3 commits into
mainfrom
openclaw/cool-claw
Closed

obviyus wants to merge 3 commits into
mainfrom
openclaw/cool-claw

Conversation

@obviyus

@obviyus obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Problem

A deliberate silent reply (NO_REPLY) broke the prompt cache prefix on role-alternating transports.

  • Replay dropped the silent assistant row, so the user turn before it and the next user turn became adjacent.
  • OpenAI Chat Completions and Bedrock Converse merge adjacent user turns. The merge rewrote an already-sent user message, and every later request missed the cache from that point.
  • Anthropic Messages, OpenAI Responses and Google kept the turns separate, so they did not break.

Fix

One invariant across five transports

New fake servers (test/helpers/fake-model-transports.ts) speak the five wire formats: anthropic-messages, openai-responses, openai-completions, google-generative-ai and bedrock-converse-stream (Bedrock uses a hand-built AWS event stream). Each transport has its own e2e file that runs one Gateway scenario:

first turn → background exec → subagent spawn and completion → inter-session send → skill install → workspace AGENTS.md edit → silent turn → next turn → compaction → turn after compaction.

The test canonicalizes each request (cache markers removed) and asserts exactly one prefix break, at compaction.

Transport main This PR
Anthropic Messages pass pass
OpenAI Responses pass pass
OpenAI Chat Completions break at silent turn pass
Google Generative AI pass pass
Bedrock Converse break at silent turn pass

Design references

  • Hermes treats prompt caching as an invariant, with compression as the only exception (AGENTS.md:18-21), and asserts breaks with prefix_breaks (tests/e2e/core/history/_helpers.py:214). The e2e assertion here copies that shape: count breaks and allow exactly one, at compaction.
  • Pi keeps system prompt and tools as the leading log entry, and sends later system changes as deltas (packages/agent/src/agent.ts:78-87, packages/ai/src/types.ts). OpenClaw already sends in-history system updates and retained runtime context this way; the fake matrix shows that path holds on all five transports.
  • Not copied: Pi's per-transport collapseSystemMessages rebuild (packages/ai/src/utils/transcript.ts). Rebuilding the system block on transports without mid-conversation system messages would change the prefix.

Evidence

  • Unit: src/agents/embedded-agent-runner/replay-history.test.ts 60/60.
  • E2E (fake servers): all five transport files pass on this head. On main, Chat Completions and Bedrock fail at the silent turn.
  • Real provider routes were not measured for this change. Proof is request-byte stability against the fakes, not provider cacheRead numbers.

Not in this PR

  • Google managed explicit caching still opts out of retained runtime context (buildGoogleGeminiReplayPolicy). Removing that needs a live Gemini cache measurement.
  • Remote worker turns still render the system prompt each turn. Pinning them needs a worker protocol change.

obviyus and others added 3 commits November 13, 2026 13:45
…s append-only

Dropping a NO_REPLY row left two user turns adjacent. Chat Completions and Bedrock merge them, which rewrote an already-sent turn and broke the prompt cache. The row is now dropped only when another assistant turn follows (#99620).

Co-authored-by: obviyus <[email protected]>
…s append-only

OpenClaw-Publication: 0d81101e-150c-8ae4-acbb-e22613e64f78
@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

Pull request received. I will update this pull request when review starts.

ClawSweeper review complete

ClawSweeper finished reviewing this revision. The review result is being finalized.

View the workflow run.

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: L labels Nov 13, 2026
@clawsweeper clawsweeper Bot added clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action. and removed clawsweeper:intake:bug Preliminary submission classification; full source review pending. clawsweeper:intake:needs-environment Preliminary submission classification; full source review pending. clawsweeper:intake:needs-repro Preliminary submission classification; full source review pending. labels Nov 13, 2026
@clawsweeper

clawsweeper Bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Codex review: needs changes before merge.

What this changes

Preserve silent assistant replies during replay to prevent adjacent user turns from changing an already-sent prompt prefix.

Example: A user sends “Check deployment,” the agent returns NO_REPLY, and the user then sends “Any update?”

  • Before: Replay removes NO_REPLY, allowing role-alternating transports to merge the two user messages and change the earlier request prefix.
  • After: The silent assistant row separates the user messages, preserving the earlier prefix.

Review scores

Measure Result What it means
Overall readiness 🧂 unranked krab (1/6) A useful shared-boundary repair with meaningful transport coverage, but the identified defects and fake-only behavior evidence prevent a pass.
Proof confidence 🧂 unranked krab (1/6) Real behavior proof is not required for maintainer- or bot-authored pull requests.
Patch quality 🦪 silver shellfish (2/6) 2 actionable review findings remain.

Product

Kind: Bug fix · Worth it: Yes · Fix scope: Complete
User problem: A deliberate silent reply can cause later requests to reprocess an otherwise reusable conversation prefix.
Reason: The change restores the documented session-prefix invariant, and the collaborator-authored direction supplies the owner decision.

Merge readiness

⛔ Needs changes before merge - 2 items remain

Keep open: this maintainer-authored PR addresses a current replay defect, but introduces quadratic history copying and leaves a consumer test failing.

Priority: P2
Reviewed head: d4d1a71da2f1593e441c95b7055faf4c40f745ec

Before merge

  • [P2] Find the next row without copying the remaining history (P2) - Every silent row copies the entire remaining messages array before find can stop at its immediate successor. A 2,000-row history alternating user and NO_REPLY rows copies 999,000 references per normalization, even with no custom rows to skip. This adds quadratic allocation work to prompt preparation and the LLM boundary, which both invoke this normalizer. Walk forward by index without materializing the suffix.
  • [P2] Update the consumer fixture that expects silence to be removed (P2) - The existing shrink-history test uses [user, NO_REPLY, user] and still expects prePromptMessageCount=2 plus replacement with the two user rows. The new behavior correctly preserves all three rows, so checks-node-changed-compact-large-24 fails at line 568 on this head. Use an actually removable failed or empty assistant row for this fixture and preserve its count-rebasing assertions.

Findings

  • [P2] [P2] Find the next row without copying the remaining history — src/agents/embedded-agent-runner/replay-history.ts:324
  • [P2] [P2] Update the consumer fixture that expects silence to be removed — src/agents/embedded-agent-runner/run/attempt-prompt-context.test.ts:557-560

Tests

  • Missing end-to-end proof: The PR owner should supply real-Gateway traces for NO_REPLY followed by another turn on Chat Completions and Bedrock, showing stable earlier messages and successful continuation, with keys, IPs, phone numbers, and private endpoints redacted.
Agent review details

How this fits together

The embedded agent runner projects stored conversation rows into model history before provider adapters construct outgoing requests.

flowchart LR
  A[Stored conversation] --> B[Replay normalization]
  B --> C[Prompt preparation]
  C --> D[Provider adapter]
  D --> E[Model request]
  E --> F[Provider response]
Loading

Technical review

Best possible solution:

Keep the existing replay owner, replace suffix copying with indexed lookahead, and make the shrink-history fixture use a genuinely removable row.

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

Current-main source removes the silent separator, and the preceding merged PR records the resulting Chat Completions prefix break; the supplied fake-provider journeys report base failures and head passes, but no target code was executed during this read-only review.

Is this the best way to solve the issue?

Retaining the separator at the shared replay boundary addresses the cause across transports without introducing another policy owner; the expanded wire-format fixture provides distinct transport coverage.

Full review comments:

  • [P2] [P2] Find the next row without copying the remaining history — src/agents/embedded-agent-runner/replay-history.ts:324
    Every silent row copies the entire remaining messages array before find can stop at its immediate successor. A 2,000-row history alternating user and NO_REPLY rows copies 999,000 references per normalization, even with no custom rows to skip. This adds quadratic allocation work to prompt preparation and the LLM boundary, which both invoke this normalizer. Walk forward by index without materializing the suffix.
    Confidence: 0.99
  • [P2] [P2] Update the consumer fixture that expects silence to be removed — src/agents/embedded-agent-runner/run/attempt-prompt-context.test.ts:557-560
    The existing shrink-history test uses [user, NO_REPLY, user] and still expects prePromptMessageCount=2 plus replacement with the two user rows. The new behavior correctly preserves all three rows, so checks-node-changed-compact-large-24 fails at line 568 on this head. Use an actually removable failed or empty assistant row for this fixture and preserve its count-rebasing assertions.
    Confidence: 1

Overall correctness: patch is incorrect
Overall confidence: 0.96

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning medium; reviewed against e60207216d61.

Provenance checked

  • Silent assistant-row removal changes intended behavior with a stated reason (bd5f760: Centralized whole-row silence classification while preserving hidden model-context rows and avoiding fabricated replies.)
  • Immutable replay-normalization memo keeps the original intent (3ec819f: Avoid repeatedly normalizing immutable messages while keeping history-dependent decisions outside the message-local memo.)
  • Provider prefix-stability journey keeps the original intent (bf0b800: Exercise mid-session changes through the Gateway and permit a prefix rebuild only at explicit compaction.)

Testing

Proof path: in-process harness.

Security

None.

Evidence

Acceptance criteria:

  • pnpm test src/agents/embedded-agent-runner/replay-history.test.ts
  • pnpm test src/agents/embedded-agent-runner/run/attempt-prompt-context.test.ts
  • pnpm test src/agents/agent-tools.prefix-stability.test.ts
  • pnpm test:e2e:gateway test/prompt-cache-prefix-stability.anthropic.e2e.test.ts test/prompt-cache-prefix-stability.openai-completions.e2e.test.ts test/prompt-cache-prefix-stability.openai-responses.e2e.test.ts test/prompt-cache-prefix-stability.google.e2e.test.ts test/prompt-cache-prefix-stability.bedrock.e2e.test.ts
  • pnpm check:changed

What I checked:

Likely related people:

  • Ayaan Zaidi: Raw commit bf0b800 adds test/prompt-cache-prefix-stability.test-support.ts:1 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: bf0b8002c2b9; files: test/prompt-cache-prefix-stability.test-support.ts)
  • Peter Steinberger: Raw commit 3ec819f adds src/agents/embedded-agent-runner/replay-history.ts:97 relative to its recorded parents. This identifies author metadata, not feature responsibility or a PR merger. (role: source-line author; confidence: high; commits: 3ec819f8c11c; files: src/agents/embedded-agent-runner/replay-history.ts)

PR surface

Source +14, Tests +296. Total +310 across 7 files. Added test files: 4.

View PR surface stats
Area Files Added Removed Net
Source 1 17 3 +14
Tests 6 451 155 +296
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 7 468 158 +310

Preflight source

Source unknown. Source comparison at the observed PR snapshot only. Completion is self-reported; body, policy, tests, execution, and report bytes are not verified.

Labels

Label changes:

No label changes.

Label justifications:

  • P2: Silent-turn replay can reduce prompt-cache reuse and increase model cost and latency, without evidence of an urgent availability regression.
  • rating: 🧂 unranked krab: Overall readiness is 🧂 unranked krab; proof is 🧂 unranked krab and patch quality is 🦪 silver shellfish.
  • status: ⏳ waiting on author: ClawSweeper has contributor-facing work open and is waiting for author action.

Rating scale

6/6 🦀 challenger crab · 5/6 🦞 diamond lobster · 4/6 🐚 platinum hermit · 3/6 🦐 gold shrimp · 2/6 🦪 silver shellfish · 1/6 🧂 unranked krab. Overall follows the weaker of proof and patch quality; ✨ marks media proof (a screenshot, video, or linked artifact) that directly shows the changed behavior.

Workflow

ClawSweeper edits this one comment on every review. Comment @clawsweeper re-review for a fresh review only. Repair and merge execution lanes are retired.

Reviewed November 13, 2026, 3:34 AM ET / 08:34 UTC.

@obviyus
obviyus marked this pull request as draft November 13, 2026 08:37
@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

Converting to draft. This is held until the redesign work reproduces the prefix-cache causes end to end from captured request bytes and proves the fix on real traffic; it may be superseded by that design.

@obviyus

obviyus commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator Author

Superseded by #198410. It carries this fix and its tests unchanged (replay-history silent-reply rule and the five-transport prefix e2e), extends the fake transports for seeded randomized journeys on all five transports, and adds the session prefix owner. Closing in favor of #198410.

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

Labels

agents Agent runtime and tooling P2 Normal backlog priority with limited blast radius. rating: 🧂 unranked krab Not merge-ready due to missing proof or serious correctness/safety concerns. size: L status: ⏳ waiting on author ClawSweeper has contributor-facing work open and is waiting for author action.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant