Skip to content

[faster-connectors] (1/4) Extract apps cache logic into ConnectorRuntimeManager - #31471

Open
mzeng-openai wants to merge 9 commits into
mainfrom
dev/mzeng/apps-runtime-state-manager
Open

[faster-connectors] (1/4) Extract apps cache logic into ConnectorRuntimeManager#31471
mzeng-openai wants to merge 9 commits into
mainfrom
dev/mzeng/apps-runtime-state-manager

Conversation

@mzeng-openai

Copy link
Copy Markdown
Contributor

What

  • Extract the existing Codex Apps tools cache behind ConnectorRuntimeManager, ConnectorRuntimeContext, and an immutable tools-plus-refresh-time snapshot.
  • Scope the active runtime context by account, ChatGPT user, workspace-account mode, and Codex home; discard stale contexts on context changes.
  • Keep the existing schema-v4 tools-only cache wire format, while adding a 32 MiB bounded read, file-mtime cold-load bookkeeping, and atomic replacement.
  • Retain compatibility aliases so existing app/list callers and startup behavior do not change in this slice.
  • Prevent discarded contexts from advertising or executing their old Apps tools, and serialize accepted snapshot persistence so disk cannot regress behind memory.

Why

This is stack PR 1 of 4 for the connector runtime state refactor. It establishes one owner for committed connector runtime state before later PRs add explicit refresh serialization, request-stable consumption, and the v2 app/installed API.

The snapshot intentionally contains runtime facts only: the raw /ps/mcp tools/list ToolInfo set and refreshed_at. Connector directory metadata remains owned by app/read / ConnectorMetadataStore.

Compatibility and impact

  • No public app-server API change.
  • No app/list behavior change.
  • No _meta.installedApps dependency.
  • No connector-directory fetch during runtime load or publication.
  • The persisted schema remains { "schema_version": 4, "tools": [...] }, so existing caches remain readable.

Checks

  • just test -p codex-mcp (112 passed)
  • just fmt

@mzeng-openai
mzeng-openai marked this pull request as ready for review July 7, 2026 22:30

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9f4775b492

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/codex-mcp/src/rmcp_client.rs Outdated
Comment thread codex-rs/codex-mcp/src/connection_manager.rs Outdated
@mzeng-openai

Copy link
Copy Markdown
Contributor Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 95f76cd3e0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread codex-rs/codex-mcp/src/connector_runtime.rs Outdated
Comment thread codex-rs/codex-mcp/src/rmcp_client.rs Outdated
Comment thread codex-rs/codex-mcp/src/rmcp_client.rs Outdated
@mzeng-openai mzeng-openai changed the title codex-mcp: add connector runtime snapshot manager Extract apps cache logic into ConnectorRuntimeManager Jul 8, 2026
@mzeng-openai mzeng-openai changed the title Extract apps cache logic into ConnectorRuntimeManager [faster-connectors][1/4] Extract apps cache logic into ConnectorRuntimeManager Jul 8, 2026
@mzeng-openai mzeng-openai changed the title [faster-connectors][1/4] Extract apps cache logic into ConnectorRuntimeManager [faster-connectors] (1/4) Extract apps cache logic into ConnectorRuntimeManager Jul 8, 2026
@mzeng-openai
mzeng-openai requested a review from a team as a code owner July 10, 2026 04:05
@glook9001

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4f6fb1bbf1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

use super::emit_duration;

const MCP_TOOLS_CACHE_WRITE_DURATION_METRIC: &str = "codex.mcp.tools.cache_write.duration_ms";
pub(crate) const CODEX_APPS_TOOLS_CACHE_MAX_BYTES: u64 = 32 * 1024 * 1024;

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.

P0 Badge Cap cached tool payloads below context limits

This 32 MiB cache limit is still accepted as a valid Codex Apps tool snapshot, and cached ToolInfo values flow through McpConnectionManager::list_all_tools() into model-visible tool declarations. A single cached tool description/schema under this byte limit can far exceed the 10K-token item cap, so oversized cache entries should be dropped or trimmed with a model-context-sized hard cap before exposure.

AGENTS.md reference: AGENTS.md:L97-L99

Useful? React with 👍 / 👎.

/// Activating a different context discards the prior in-memory entry. Handles
/// to a discarded context can no longer read or publish its snapshot, which
/// prevents account A state from bleeding into account B.
pub struct ConnectorRuntimeManager<T: ConnectorRuntimePayload> {

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.

P2 Badge Split the runtime extraction into smaller stages

This commit changes 16 files with 1,612 insertions and 509 deletions while introducing new ConnectorRuntimeManager semantics, active-context discard/cancellation, bounded/atomic persistence, call-site rewrites, and extensive test changes, so it is not a mechanical move and exceeds the 800-line / 500-line complex-logic size guidance. A smaller coherent first stage would be to move the existing codex_apps_cache implementation/tests into codex-connectors behind compatibility aliases with unchanged behavior, then layer the runtime-context and persistence hardening changes separately.

AGENTS.md reference: AGENTS.md:L125-L131

Useful? React with 👍 / 👎.

Comment on lines +476 to +478
if self.routing_cancellation_token.is_cancelled() {
return Err(StartupOutcomeError::Cancelled);
}

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.

P2 Badge Add integration coverage for Apps context discard

This new routing-cancellation branch changes agent-visible behavior because client() now makes stale Codex Apps managers fail before tool listing/calls reach core, but the commit adds only crate-level unit tests and no core/tests/suite TestCodex coverage for an account/workspace context switch. Add an integration test that proves stale Apps tools are not sent in the model request and stale Apps tool calls are rejected after the runtime context changes.

AGENTS.md reference: AGENTS.md:L114-L118

Useful? React with 👍 / 👎.

mod persistence;

#[cfg(test)]
mod tests;

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.

P3 Badge Put new connector-runtime tests in a named test file

This introduces a new unit-test module through the implicit connector_runtime/tests.rs path, while the repo guidance asks new test modules to use an explicit #[path = "..._tests.rs"] with a descriptive filename. Rename the file and declare it with an explicit path so the connector-runtime tests follow the expected layout.

AGENTS.md reference: AGENTS.md:L169-L178

Useful? React with 👍 / 👎.

Comment on lines +222 to +223
#[cfg(test)]
pub(crate) fn write_cached_codex_apps_tools_for_test<T>(

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.

P3 Badge Move test-only cache helpers out of the implementation

These #[cfg(test)] helper APIs are only consumed by connector_runtime/tests.rs, so the production persistence module now carries test-only setup surface instead of keeping that fixture code beside the tests or exercising the production persistence entry points directly. Move these helpers into test code/test support to keep the implementation free of test-only functions.

AGENTS.md reference: AGENTS.md:L120-L121

Useful? React with 👍 / 👎.

@glook9001

Copy link
Copy Markdown

@codex review

2 similar comments
@glook9001

Copy link
Copy Markdown

@codex review

@glook9001

Copy link
Copy Markdown

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 4f6fb1bbf1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +547 to +549
if self.routing_cancellation_token.is_cancelled() {
return None;
}

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.

P1 Badge Revalidate Apps tools before returning the aggregate

When a Codex Apps client passes this guard, McpConnectionManager::list_all_tools() can still keep those tools in its local tools vector while awaiting later MCP servers; if another account/workspace activates the shared runtime during that later await, the discarded Apps tools are still normalized and returned for the next model-visible tool list. Please recheck the routing context at the aggregate boundary, or carry a guarded Apps snapshot through aggregation, so tools from a discarded context cannot be advertised.

AGENTS.md reference: AGENTS.md:L91-L93

Useful? React with 👍 / 👎.


[dependencies]
anyhow = { workspace = true }
arc-swap = { workspace = true }

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.

P2 Badge Refresh the Bazel lockfile

This commit adds normal dependencies to codex-connectors and updates Cargo.lock, but the diff does not include MODULE.bazel.lock. The repo requires running just bazel-lock-update for Rust dependency changes and notes that CI verifies lockfile drift, so Bazel-based checks will fail until the corresponding lockfile update is committed.

AGENTS.md reference: AGENTS.md:L37-L39

Useful? React with 👍 / 👎.

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.

2 participants