Skip to content

[AI-assisted] fix(agents): invalidate context engine cache#78163

Merged
jalehman merged 2 commits intoopenclaw:mainfrom
brokemac79:fix-context-engine-cache-invalidation
May 6, 2026
Merged

[AI-assisted] fix(agents): invalidate context engine cache#78163
jalehman merged 2 commits intoopenclaw:mainfrom
brokemac79:fix-context-engine-cache-invalidation

Conversation

@brokemac79
Copy link
Copy Markdown
Contributor

Summary

  • Clear the context-engine loop hook's cached assembled view when the source message history shrinks.
  • Clear that cache when context-engine assembly fails, so fallback uses the current source messages.
  • Add focused regressions for both stale-cache paths.

Fixes #77968.

Real behavior proof

Behavior or issue addressed: installContextEngineLoopHook could return a previously cached assembled context view after the live source history shrank or after a later assemble() failure, allowing stale pre-reset context to be reused.

Real environment tested: Local Windows checkout of this PR branch at commit 2663018ee2, using Node/pnpm from the repository toolchain.

Exact steps or command run after this patch: Inspected the patched hook and ran git diff --check, corepack pnpm exec oxfmt --check --threads=1 src/agents/pi-embedded-runner/tool-result-context-guard.ts src/agents/pi-embedded-runner/tool-result-context-guard.test.ts CHANGELOG.md, corepack pnpm test src/agents/pi-embedded-runner/tool-result-context-guard.test.ts, and corepack pnpm check:changed.

Evidence after fix: Focused regression tests now prove the cache invalidates in both reported failure modes:

clears the cached assembled view when the source history shrinks
clears the cached assembled view when assemble fails

The focused test shard passed:

Test Files  1 passed (1)
Tests       34 passed (34)

Observed result after fix: When source history shrinks, the hook clears the cached assembled view and returns the fresh source messages. When assemble() throws after a previous successful cached view, the hook clears that cache and subsequent unchanged iterations no longer reuse stale assembled messages.

What was not tested: I did not run a live Gateway session reset with a real context-engine plugin. This proof uses the core hook's focused regression tests and source-level behavior.

Validation

  • git diff --check
  • corepack pnpm exec oxfmt --check --threads=1 src/agents/pi-embedded-runner/tool-result-context-guard.ts src/agents/pi-embedded-runner/tool-result-context-guard.test.ts CHANGELOG.md
  • corepack pnpm test src/agents/pi-embedded-runner/tool-result-context-guard.test.ts
  • corepack pnpm check:changed

@openclaw-barnacle openclaw-barnacle Bot added agents Agent runtime and tooling size: S proof: supplied External PR includes structured after-fix real behavior proof. labels May 6, 2026
@clawsweeper
Copy link
Copy Markdown
Contributor

clawsweeper Bot commented May 6, 2026

Codex review: needs real behavior proof before merge.

Summary
The PR invalidates installContextEngineLoopHook's cached assembled context view on source-history shrink or assembly failure, adds focused regression tests, and updates the changelog.

Reproducibility: yes. source-level: current main keeps lastAssembledView, returns it on unchanged calls, and leaves it intact after assembly failure. I did not run a live Gateway/lossless-claw reset path in this read-only review.

Real behavior proof
Needs real behavior proof before merge: The PR supplies focused test/check output and says no live Gateway session reset with a real context-engine plugin was run, so it still needs after-fix real behavior proof before merge. After adding proof, update the PR body; ClawSweeper should re-review automatically. If it does not, ask a maintainer to comment @clawsweeper re-review.

Next step before merge
Contributor live runtime proof and maintainer check-gate judgment remain the blockers; there is no narrow code repair for automation to make on this PR branch.

Security
Cleared: The diff only changes agent cache logic, focused tests, and the changelog; no concrete security or supply-chain concern was found in the patch.

Review details

Best possible solution:

Land the narrow cache invalidation after live Gateway/context-engine proof shows reset and assemble-failure paths no longer reuse stale pre-reset messages, and after required checks are green or explicitly deemed unrelated.

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

Yes, source-level: current main keeps lastAssembledView, returns it on unchanged calls, and leaves it intact after assembly failure. I did not run a live Gateway/lossless-claw reset path in this read-only review.

Is this the best way to solve the issue?

Yes, the code direction is the narrow maintainable fix: track the source length that produced the cached assembled view and clear the cache on shrink or engine failure. The remaining blocker is proof and gate readiness, not a definite implementation defect.

What I checked:

Likely related people:

  • Bikkies: Commit fecd4fcc5568 introduced the per-iteration context-engine ingest/assemble hook and the focused hook tests where this cache lives. (role: introduced behavior; confidence: high; commits: fecd4fcc5568; files: src/agents/pi-embedded-runner/tool-result-context-guard.ts, src/agents/pi-embedded-runner/tool-result-context-guard.test.ts, src/agents/pi-embedded-runner/run/attempt.ts)
  • jalehman: The introducing commit lists jalehman as co-author/reviewer, later context-engine prompt-cache work touched the same lifecycle boundary, and this PR is currently assigned to jalehman with a changelog attribution commit. (role: adjacent context-engine owner/reviewer; confidence: high; commits: fecd4fcc5568, a327b6750dbe, 66d59ba08fca; files: src/agents/pi-embedded-runner/tool-result-context-guard.ts, src/agents/pi-embedded-runner/run/attempt.ts, CHANGELOG.md)

Remaining risk / open question:

  • Contributor-supplied after-fix proof is still test/source-level only; no live Gateway reset or real context-engine plugin run is shown on the PR branch.
  • Latest head CI was not fully green at inspection time, with aggregate security failures and multiple queued broad checks needing maintainer interpretation or rerun.

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

@brokemac79 brokemac79 force-pushed the fix-context-engine-cache-invalidation branch from 2663018 to ec9e105 Compare May 6, 2026 06:28
@brokemac79
Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current openclaw/main and resolved the CHANGELOG.md conflict by keeping both upstream's plugin/Teams entries and this PR's Agents/context engine entry.

New head: ec9e1054e1

Focused validation after the rebase:

  • corepack pnpm test src/agents/pi-embedded-runner/tool-result-context-guard.test.ts
  • corepack pnpm exec oxfmt --check CHANGELOG.md src/agents/pi-embedded-runner/tool-result-context-guard.test.ts src/agents/pi-embedded-runner/tool-result-context-guard.ts
  • corepack pnpm check:changed
  • corepack pnpm check:changelog-attributions
  • git diff --check origin/main...HEAD

@brokemac79 brokemac79 force-pushed the fix-context-engine-cache-invalidation branch from ec9e105 to c575501 Compare May 6, 2026 07:38
@brokemac79
Copy link
Copy Markdown
Contributor Author

Rebased this branch onto current openclaw/main after upstream moved again, resolving the CHANGELOG.md conflict by keeping both upstream's Gateway/sessions entry and this PR's Agents/context engine entry.

New head: c575501fdd

Focused validation after the rebase:

  • corepack pnpm test src/agents/pi-embedded-runner/tool-result-context-guard.test.ts
  • corepack pnpm exec oxfmt --check CHANGELOG.md src/agents/pi-embedded-runner/tool-result-context-guard.test.ts src/agents/pi-embedded-runner/tool-result-context-guard.ts
  • corepack pnpm check:changed
  • corepack pnpm check:changelog-attributions
  • git diff --check origin/main...HEAD

@brokemac79
Copy link
Copy Markdown
Contributor Author

Fresh CI on c575501fdd has Real behavior proof, OpenGrep, changed-path scanning, and the focused/security checks passing. The remaining red checks are outside this PR's diff:

  • check-lint: src/gateway/server.cron.test.ts:194 — unnecessary spread operator
  • check-test-types: src/gateway/server.cron.test.ts:444Property 'run' does not exist on type '{ start: () => Promise<void>; stop: () => void; }'

This PR only changes:

  • CHANGELOG.md
  • src/agents/pi-embedded-runner/tool-result-context-guard.ts
  • src/agents/pi-embedded-runner/tool-result-context-guard.test.ts

I am not folding unrelated gateway cron test fixes into this context-engine PR; leaving this for upstream/main or maintainer rerun.

@jalehman jalehman self-assigned this May 6, 2026
@jalehman jalehman force-pushed the fix-context-engine-cache-invalidation branch 4 times, most recently from f1a4693 to 546fced Compare May 6, 2026 21:31
@jalehman jalehman force-pushed the fix-context-engine-cache-invalidation branch from 546fced to 6ed3add Compare May 6, 2026 21:33
@jalehman jalehman merged commit 609a5d7 into openclaw:main May 6, 2026
19 checks passed
@jalehman
Copy link
Copy Markdown
Contributor

jalehman commented May 6, 2026

Merged via squash.

Thanks @brokemac79!

rogerdigital pushed a commit to rogerdigital/openclaw that referenced this pull request May 7, 2026
…78163)

Merged via squash.

Prepared head SHA: 6ed3add
Co-authored-by: brokemac79 <255583030+brokemac79@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
steipete pushed a commit that referenced this pull request May 7, 2026
Merged via squash.

Prepared head SHA: 6ed3add
Co-authored-by: brokemac79 <255583030+brokemac79@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman

(cherry picked from commit 609a5d7)
github-actions Bot pushed a commit to Desicool/openclaw that referenced this pull request May 9, 2026
…78163)

Merged via squash.

Prepared head SHA: 6ed3add
Co-authored-by: brokemac79 <255583030+brokemac79@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
rogerdigital pushed a commit to rogerdigital/openclaw that referenced this pull request May 9, 2026
…78163)

Merged via squash.

Prepared head SHA: 6ed3add
Co-authored-by: brokemac79 <255583030+brokemac79@users.noreply.github.com>
Co-authored-by: jalehman <550978+jalehman@users.noreply.github.com>
Reviewed-by: @jalehman
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

agents Agent runtime and tooling 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.

installContextEngineLoopHook caches lastAssembledView indefinitely without invalidation

2 participants