Skip to content

Flush the runtime object cache before each STDIO message - #413

Open
softglazee wants to merge 2 commits into
WordPress:trunkfrom
softglazee:fix/409-stdio-object-cache
Open

softglazee wants to merge 2 commits into
WordPress:trunkfrom
softglazee:fix/409-stdio-object-cache

Conversation

@softglazee

@softglazee softglazee commented Oct 23, 2026 •

Copy link
Copy Markdown

What?

Closes #409

The STDIO server now flushes the runtime object cache before it handles each message, so tools read current data.

Why?

wp mcp-adapter serve handles the whole session in one WP-CLI process, so the object cache lives as long as the session. Tools kept returning what they read first and missed changes made in WP Admin, cron or REST requests. An ability that checks current data before a write could check an old copy. The HTTP transport is not affected, since each call is its own request.

How?

StdioServerBridge::handle_request() calls wp_cache_flush_runtime() before it decodes each message, when wp_cache_supports( 'flush_runtime' ). That clears the in-memory copy only, so a persistent cache such as Redis keeps its data and the next read gets the current value from it. A persistent cache that does not support a runtime flush is left alone, because wp_cache_flush() would empty the shared cache for the whole site.

The flush runs in handle_request() rather than in the read loop, so it covers every message and the existing tests that call handle_request() directly go through it too.

New tests in StdioServerBridgeTest:

  • test_tool_call_reads_changes_made_by_another_process: calls a tool that returns blogname, changes the title directly in the database (as another process would), and calls the tool again. On trunk it returns the old title.
  • test_each_message_flushes_the_runtime_cache: a cached value is gone after one ping.

Testing Instructions

  1. Start the STDIO server, for example with the MCP Inspector: wp mcp-adapter serve --server=mcp-adapter-default-server --user=admin.
  2. Call mcp-adapter-execute-ability with {"ability_name": "core/get-site-info", "parameters": {"fields": ["name"]}}. It returns the site title.
  3. Change the site title in Settings > General.
  4. Call the same tool again. On trunk it returns the old title; with this PR it returns the new one.

Results on PHP 8.3 with WordPress trunk:

  • Full suite: 545 tests, OK (1 skipped, as on trunk).
  • Both new tests fail against the trunk version of StdioServerBridge.php.
  • PHPStan and PHPCS on the changed files: no errors.

Changelog Entry

Fixed - The STDIO server flushes the runtime object cache before each message, so tools see changes made outside the session.

Open WordPress Playground Preview

The STDIO server handles a whole session in one WP-CLI process, so the object cache kept what earlier messages read and tools returned stale data after changes made in WP Admin, cron or REST requests. Flush the runtime cache before each message. Persistent caches without runtime flush support are left alone, since wp_cache_flush() would empty the shared cache for the whole site.

Fixes WordPress#409.
Copilot AI balanced review requested due to automatic review settings October 23, 2026 19:32
@softglazee
softglazee requested a review from a team as a code owner October 23, 2026 19:32
@github-actions

github-actions Bot commented Oct 23, 2026 •

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: softglazee <softglaze@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.

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

The focused change matches the intended behavior, protects shared persistent caches, and includes relevant regression coverage.

No open issues 🎉

What changed in this PR

Addresses #409 by flushing the runtime object cache before each STDIO message, allowing the long-running server to read current data when runtime flushing is supported.

Changes:

  • Flushes the runtime cache before decoding each message.
  • Leaves caches without runtime-flush support untouched to protect shared persistent data.
  • Adds regression tests for stale site-title reads and per-message cache clearing.
File Description
tests/​phpunit/​Unit/​Cli/​StdioServerBridgeTest.php Adds regression coverage for fresh reads and cache clearing.
includes/​Cli/​StdioServerBridge.php Adds a capability-guarded runtime cache flush before message handling.

Review effort: Balanced

@codecov

codecov Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.13%. Comparing base (f6bb4b7) to head (99ed048).

Additional details and impacted files
@@            Coverage Diff            @@
##              trunk     #413   +/-   ##
=========================================
  Coverage     93.12%   93.13%           
- Complexity     1382     1383    +1     
=========================================
  Files            59       59           
  Lines          4016     4018    +2     
=========================================
+ Hits           3740     3742    +2     
  Misses          276      276           
Flag Coverage Δ
unit 93.13% <100.00%> (+<0.01%) ⬆️

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.

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

2 participants