Skip to content

Reset runtime object cache between STDIO requests - #419

Closed
AshishJii wants to merge 1 commit into
WordPress:trunkfrom
AshishJii:feat/stdio-cache-fix
Closed

AshishJii wants to merge 1 commit into
WordPress:trunkfrom
AshishJii:feat/stdio-cache-fix

Conversation

@AshishJii

@AshishJii AshishJii commented Oct 26, 2026 •

Copy link
Copy Markdown

What?

Closes #409

Resets the WordPress runtime object cache before dispatching each valid JSON-RPC message in the STDIO server bridge, ensuring subsequent tool and prompt executions observe fresh state without evicting shared persistent caches.

Why?

wp mcp-adapter serve runs as a single long-lived CLI process reading from STDIN. Because WordPress's in-memory object cache persists across the entire process lifetime, changes made by external processes (WP Admin, REST API, cron, or another CLI session) never reach the STDIO server.

This causes:

  • Tools, resources, and prompts to return stale cached data.
  • Permission callbacks to evaluate authorization against outdated options or capabilities.
  • Write preconditions to accept changes that current data would reject, or overwrite newer concurrent writes.

How?

  • Clears the process-local cache in StdioServerBridge before dispatching each valid request (wp_cache_flush() for the default cache; wp_cache_flush_runtime() when supported by an external cache).
  • Never falls back to flushing shared persistent storage; warns once on STDERR if an external backend lacks runtime-flush support.
  • Extracted basic envelope inspection into JsonRpcRequestDecoder::inspect_envelope() so malformed envelopes retain error precedence, cache failures preserve request IDs, and notifications remain silent.
  • Added an opt-out filter (mcp_adapter_stdio_flush_runtime_cache), updated docs and added tests for live reads, permissions, etc.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini 3.8 Flash
Used for: Issue root-cause analysis, implementation plan drafting, creating tests and verifying implementation.

Testing Instructions

npm run test:php -- tests/phpunit/Integration/StdioCacheInvalidationTest.php
npm run test:php
npm run wp-env:test -- run cli --env-cwd=wp-content/plugins/mcp-adapter/ composer run-script phpstan
npm run wp-env:test -- run cli --env-cwd=wp-content/plugins/mcp-adapter/ composer run-script lint

Changelog Entry

Fixed - Reset runtime object cache between STDIO requests to prevent serving stale data during long-running sessions.

Open WordPress Playground Preview

@AshishJii
AshishJii requested a review from a team as a code owner October 26, 2026 04:43
Copilot AI balanced review requested due to automatic review settings October 26, 2026 04:43
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: AshishJii <ashishjii@git.wordpress.org>
Co-authored-by: zaguiini <zaguiini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@codecov

codecov Bot commented Oct 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 10 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.95%. Comparing base (f6bb4b7) to head (8811782).

Files with missing lines Patch % Lines
includes/Cli/StdioServerBridge.php 52.38% 10 Missing ⚠ïļ
Additional details and impacted files
@@             Coverage Diff              @@
##              trunk     #419      +/-   ##
============================================
- Coverage     93.12%   92.95%   -0.17%     
- Complexity     1382     1393      +11     
============================================
  Files            59       59              
  Lines          4016     4062      +46     
============================================
+ Hits           3740     3776      +36     
- Misses          276      286      +10     
Flag Coverage Δ
unit 92.95% <81.81%> (-0.17%) ⮇ïļ

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
ðŸ“Ē Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄ïļ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • ðŸ“Ķ JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ðŸŸĒ Approval recommended

Static review found no blocking issues in cache isolation or protocol handling, though tests were not executed.

No open issues 🎉

What changed in this PR

Addresses #409 by resetting the process-local WordPress object cache between valid STDIO messages, allowing long-running connections to observe updated data without flushing shared persistent storage.

Changes:

  • Adds runtime cache resets, an opt-out filter, and safe handling of unsupported or failing cache backends.
  • Centralizes envelope inspection to preserve protocol errors, request IDs, and notification silence.
  • Adds regression coverage and documents cache freshness behavior and limitations.
File Description
tests/​phpunit/​Integration/​StdioCacheInvalidationTest.php Tests fresh reads, permission checks, write preconditions, and backend handling.
tests/​phpunit/​Fixtures/​StdioCacheProcess.php Provides isolated cache doubles and real STDIO stream coverage.
includes/​Transport/​Infrastructure/​McpWireOrchestrator.php Reuses shared envelope inspection during dispatch.
includes/​Transport/​Infrastructure/​JsonRpcRequestDecoder.php Adds shared request and notification envelope inspection.
includes/​Cli/​StdioServerBridge.php Resets runtime caches before dispatch with persistent-cache safeguards.
docs/​guides/​cli-usage.md Documents reset behavior, opt-out, failures, and freshness limits.

Review effort: Balanced

@galatanovidiu

Copy link
Copy Markdown
Contributor

I'm going with #413 for #409 and closing this PR.

#413 fixes the bug with a small guarded flush in StdioServerBridge. This PR adds a new filter and changes request handling shared with HTTP, and when the flush returns false, valid STDIO requests fail with an internal error.

The one-time STDERR warning when a drop-in does not support flush_runtime is useful. That could be a small follow-up on its own.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

STDIO server keeps the object cache between tool calls, so tools read stale data

3 participants