Skip to content

fix(agents): preserve prompt cache boundary keys - #126380

Merged
steipete merged 1 commit into
mainfrom
codex/autoqa23-prompt-cache-key
Aug 19, 2026
Merged

steipete merged 1 commit into
mainfrom
codex/autoqa23-prompt-cache-key

Conversation

@steipete

Copy link
Copy Markdown
Contributor

What Problem This Solves

Fixes an issue where OpenAI-backed sessions with long internally derived session IDs could reuse the same prompt-cache key after a reset or compaction. The lifecycle boundary suffix was appended after the session ID and then truncated away at the 64-code-point provider limit, so distinct boundaries collided.

Why This Change Was Made

Derived keys now reserve space for :<boundaryCount> inside the canonical OpenAI prompt-cache-key limit and truncate only the session ID using Unicode code-point semantics. Explicit caller-provided keys, short derived keys, and non-OpenAI APIs keep their existing behavior.

This is the canonical derivation-owner repair: no fallback or downstream guard was added. Production LOC is +5/-5 (net zero); tests are +22/-13.

User Impact

Reset and compaction boundaries retain distinct prompt-cache affinity even for long internal session IDs, preventing stale boundary collisions while keeping normal short-session behavior unchanged.

Release-note context: OpenAI prompt-cache keys now preserve reset and compaction boundaries for long session IDs.

Evidence

  • Exact head: 31a1dca82acee7304a76b6dab73191d460555949
  • Rebased base: e474d23e653c91fb3364976f6ecd8fb61b4cf0f5
  • Before: the previous append-then-clamp derivation produced one distinct key for boundary counts 0, 1, and 2; each key was 64 Unicode code points and had lost its suffix.
  • After: node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts — 1 file, 3/3 tests passed.
  • Changed gates: node scripts/check-changed.mjs — core and coreTests passed.
  • Formatting: node_modules/.bin/oxfmt --check src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.ts src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts — passed.
  • Diff integrity: git diff --check origin/main...HEAD — passed.
  • Fresh mandatory autoreview: clean, with no accepted or actionable findings; correctness assessment 0.99.
  • Live-provider gap: no provider call was made because this is deterministic local key derivation with no external API behavior change; the focused owner regression and exact-head CI cover the changed contract.
  • AI-assisted: yes. The maintainer reviewed the patch, affected owner/caller/provider paths, tests, and evidence.

@clawsweeper

clawsweeper Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

🦞👀
ClawSweeper picked this up.

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

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: XS labels Aug 19, 2026
@steipete
steipete marked this pull request as ready for review August 19, 2026 17:40
@openclaw-barnacle openclaw-barnacle Bot added the maintainer Maintainer-authored PR label Aug 19, 2026
@clawsweeper clawsweeper Bot added P2 Normal backlog priority with limited blast radius. 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. labels Aug 19, 2026
@clawsweeper

clawsweeper Bot commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

Codex review: needs maintainer review before merge. Reviewed August 19, 2026, 1:46 PM ET / 17:46 UTC.

ClawSweeper review

What this changes

The PR preserves reset and compaction boundary suffixes when long session IDs are converted into OpenAI-compatible prompt-cache keys.

Regression provenance

Possible regression — suspected (reviewed change). No predecessor PR is attributed.

Merge readiness

⚠️ Ready for maintainer review - 1 item remains

Keep open. This maintainer-authored, maintainer-labeled PR fixes a current-main collision at the derivation owner; no blocking patch defect was found.

Priority: P2
Reviewed head: 31a1dca82acee7304a76b6dab73191d460555949

Review scores

Measure Result What it means
Overall readiness 🐚 platinum hermit (4/6) A focused net-neutral owner-boundary repair with a regression test that directly protects the reported collision.
Proof confidence 🌊 off-meta tidepool Not applicable: This is a maintainer-authored deterministic helper repair; the external-contributor real-behavior-proof gate does not apply, and the PR body records focused after-fix test evidence.
Patch quality 🐚 platinum hermit (4/6) No actionable review findings were identified.

Verification

Check Result Evidence
Real behavior Not applicable Not applicable: This is a maintainer-authored deterministic helper repair; the external-contributor real-behavior-proof gate does not apply, and the PR body records focused after-fix test evidence.
Evidence reviewed 6 items Current-main failure path: Current main concatenates the session ID and boundary suffix, then clamps the complete string; a suffix beyond the first 64 code points is lost.
Canonical owner repair: The PR reserves suffix space before Unicode code-point truncation, preserving lifecycle separation without altering explicit caller keys.
Provider contract: The shared provider contract defines a 64-code-point maximum, and the OpenAI Responses request builder still applies the same clamp at egress.
Findings None None.
Security None None.

Live Verification

Command: node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts

Result: FAIL (partial) — step 2 expect_output keeps long Unicode derived keys distinct across boundaries within the 64-char limit: expected terminal output was not visible within 30 seconds: "keeps long Unicode derived keys distinct across boundaries within the 64-char limit"

runner@runnervmzvulz:/tmp/clawsweeper-live-proof-126380-u6pFak/target$ node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-
cache-key.test.ts
[test] starting test/vitest/vitest.unit-fast.config.ts
node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-126380-u6pFak/target

 ✓  unit-fast  src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts (3 tests) 3ms

 Test Files  1 passed (1)
      Tests  3 passed (3)
   Start at  17:47:21
   Duration  299ms (transform 145ms, setup 77ms, import 100ms, tests 3ms, environment 0ms)

[test] passed 1 Vitest shard in 8.50s
runner@runnervmzvulz:/tmp/clawsweeper-live-proof-126380-u6pFak/target$ node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-
cache-key.test.ts
[test] starting test/vitest/vitest.unit-fast.config.ts

 RUN  v4.1.10 /tmp/clawsweeper-live-proof-126380-u6pFak/target

 ✓  unit-fast  src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts (3 tests) 3ms

 Test Files  1 passed (1)
      Tests  3 passed (3)
   Start at  17:47:30
   Duration  198ms (transform 43ms, setup 26ms, import 49ms, tests 3ms, environment 0ms)

[test] passed 1 Vitest shard in 8.30s
runner@runnervmzvulz:/tmp/clawsweeper-live-proof-126380-u6pFak/target$





















Assertions:

  • FAIL expect_output: keeps long Unicode derived keys distinct across boundaries within the 64-char limit

How this fits together

The embedded agent runner derives a cache key from a session ID and lifecycle boundary, then passes it through provider request builders. OpenAI-compatible builders cap that key at 64 Unicode code points before sending it upstream.

flowchart LR
A[Session ID] --> C[Cache-key derivation]
B[Reset or compaction count] --> C
C --> D[OpenAI-compatible key]
D --> E[Provider request builder]
E --> F[Length-limited upstream key]
Loading

Before merge

  • Complete next step (P2) - No repair lane is needed: the PR is already a narrow canonical fix with no actionable review finding.
Agent review details

Security

None.

PR surface

Source 0, Tests +9. Total +9 across 2 files.

View PR surface stats
Area Files Added Removed Net
Source 1 5 5 0
Tests 1 22 13 +9
Docs 0 0 0 0
Config 0 0 0 0
Generated 0 0 0 0
Other 0 0 0 0
Total 2 27 18 +9

Review metrics

Metric Value Why it matters
Production vs test delta production +5/-5 (net 0); tests +22/-13 (net +9) The repair remains net-neutral in production and adds focused regression coverage at the derivation owner.

Stored data model

Persistent data-model change detected: serialized state: src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts. Confirm migration or upgrade compatibility proof before merge.

Technical review

Best possible solution:

Retain this derivation-owner repair so long internal session IDs preserve distinct reset and compaction cache boundaries without adding a second provider-side policy.

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

Yes — source inspection provides a high-confidence deterministic path: use a session ID longer than 64 Unicode code points and boundary counts 0, 1, and 2; current main truncates all suffixes away.

Is this the best way to solve the issue?

Yes — reserving the suffix within the derivation owner’s existing provider limit is narrower and safer than relying on downstream request builders after the lifecycle distinction has already been lost.

AGENTS.md: found and applied where relevant.

Codex review notes: model internal, reasoning high; reviewed against e474d23e653c.

Labels

Label changes:

  • add P2: The PR repairs bounded prompt-cache affinity for long OpenAI-compatible session IDs without evidence of data loss, security bypass, or core unavailability.
  • add rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • add status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored deterministic helper repair; the external-contributor real-behavior-proof gate does not apply, and the PR body records focused after-fix test evidence.

Label justifications:

  • P2: The PR repairs bounded prompt-cache affinity for long OpenAI-compatible session IDs without evidence of data loss, security bypass, or core unavailability.
  • rating: 🐚 platinum hermit: Overall readiness is 🐚 platinum hermit; proof is 🌊 off-meta tidepool and patch quality is 🐚 platinum hermit.
  • status: 👀 ready for maintainer look: ClawSweeper has no concrete contributor-facing blocker left for this PR. Not applicable: This is a maintainer-authored deterministic helper repair; the external-contributor real-behavior-proof gate does not apply, and the PR body records focused after-fix test evidence.

Evidence

What I checked:

Likely related people:

  • Ayaan Zaidi: Git blame attributes the original cache-key derivation and shared 64-code-point helper to this area’s prior session-tools work. (role: introduced behavior; confidence: high; commits: 16c87e69a633; files: src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.ts, packages/ai/src/providers/openai-prompt-cache.ts)
  • steipete: Authored this repair and appears in prior session-feature history for the affected embedded-agent surface. (role: recent area contributor; confidence: medium; commits: 31a1dca82ace, a95486cefc67; files: src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.ts, src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts)

Rating scale

Score Internal tier Crab rank Meaning
6/6 S 🦀 challenger crab Exceptional readiness
5/6 A 🦞 diamond lobster Very strong readiness
4/6 B 🐚 platinum hermit Good normal PR; ordinary maintainer review
3/6 C 🦐 gold shrimp Useful, but confidence is limited
2/6 D 🦪 silver shellfish Proof or implementation needs work
1/6 F 🧂 unranked krab Not merge-ready
N/A NA 🌊 off-meta tidepool Rating does not apply

Overall follows the weaker of proof and patch quality.
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.

Workflow

  • 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.

@steipete

Copy link
Copy Markdown
Contributor Author

Land-ready proof for 31a1dca82acee7304a76b6dab73191d460555949:

  • Root cause: the derivation owner appended :<boundaryCount> and then clamped the complete key, so long session IDs truncated away the lifecycle suffix. The repair reserves suffix space inside the canonical 64-code-point limit and truncates only the session ID.
  • Focused regression: node scripts/run-vitest.mjs src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts — 1 file, 3/3 tests passed.
  • Changed gates: node scripts/check-changed.mjs — core and coreTests passed.
  • Formatting: node_modules/.bin/oxfmt --check src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.ts src/agents/embedded-agent-runner/run/session-boundary-prompt-cache-key.test.ts — passed.
  • Diff integrity: git diff --check origin/main...HEAD — passed.
  • CI: run 32282943650, attempt 2, completed successfully on the exact head: https://github.com/openclaw/openclaw/actions/runs/32282943650. Attempt 1 had one unrelated doctor-config-preflight.process.test.ts process-fixture shard time out under load; the failed-job rerun passed unchanged, with no repair edits.
  • LOC: production +5/-5 (net 0); tests +22/-13 (net +9).
  • Review: fresh mandatory autoreview was clean with no accepted/actionable findings. ClawSweeper found no actionable finding and published no repair rank-up move. Its partial expect_output result is a harness-display mismatch: the captured output shows the focused command passing twice at 3/3, but the reporter looked for an individual test title that Vitest's default output does not print. Its stored-data classification is also inapplicable: the matched path is a test file, with no schema, persistence, or migration change.
  • Proof gap: no live provider call was made. This change is deterministic local key derivation, introduces no external API behavior, and is covered at the derivation owner plus exact-head CI.

Explicit caller keys, short derived keys, and non-OpenAI APIs remain unchanged. No compatibility path, fallback, schema change, protocol change, or removed runtime path is involved.

@steipete
steipete merged commit 03a48b5 into main Aug 19, 2026
462 of 465 checks passed
@steipete
steipete deleted the codex/autoqa23-prompt-cache-key branch August 19, 2026 18:10
@steipete

Copy link
Copy Markdown
Contributor Author

Merged via squash.

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

Labels

agents Agent runtime and tooling maintainer Maintainer-authored PR P2 Normal backlog priority with limited blast radius. rating: 🐚 platinum hermit Good normal PR readiness with ordinary maintainer review expected. size: XS 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.

1 participant