-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Using cached connector directory for discoverable tools list #21497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mzeng-openai
merged 9 commits into
main
from
dev/mzeng/cached-tool-suggest-discoverables
May 8, 2026
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bea2ba0
Avoid fetching connector directory for tool suggestions
mzeng-openai 9720388
update
mzeng-openai 86745f4
update
mzeng-openai f56d10c
update
mzeng-openai 31573e1
update
mzeng-openai 5759281
update
mzeng-openai 3748aed
update
mzeng-openai 1bdbc91
update
mzeng-openai ec44562
update
mzeng-openai File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use codex_app_server_protocol::AppInfo; | ||
| use serde::Deserialize; | ||
| use serde::Serialize; | ||
| use sha1::Digest; | ||
| use sha1::Sha1; | ||
| use tracing::warn; | ||
|
|
||
| use crate::ConnectorDirectoryCacheKey; | ||
|
|
||
| pub(crate) const CONNECTOR_DIRECTORY_DISK_CACHE_SCHEMA_VERSION: u8 = 1; | ||
| const CONNECTOR_DIRECTORY_DISK_CACHE_DIR: &str = "cache/codex_app_directory"; | ||
|
|
||
| #[derive(Clone)] | ||
| pub struct ConnectorDirectoryCacheContext { | ||
| pub(crate) codex_home: PathBuf, | ||
| pub(crate) cache_key: ConnectorDirectoryCacheKey, | ||
| } | ||
|
|
||
| impl ConnectorDirectoryCacheContext { | ||
| pub fn new(codex_home: PathBuf, cache_key: ConnectorDirectoryCacheKey) -> Self { | ||
| Self { | ||
| codex_home, | ||
| cache_key, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn cache_path(&self) -> PathBuf { | ||
| let cache_key_json = serde_json::to_string(&self.cache_key).unwrap_or_default(); | ||
| let cache_key_hash = sha1_hex(&cache_key_json); | ||
| self.codex_home | ||
| .join(CONNECTOR_DIRECTORY_DISK_CACHE_DIR) | ||
| .join(format!("{cache_key_hash}.json")) | ||
| } | ||
| } | ||
|
|
||
| pub(crate) enum CachedConnectorDirectoryDiskLoad { | ||
| Hit { connectors: Vec<AppInfo> }, | ||
| Missing, | ||
| Invalid, | ||
| } | ||
|
|
||
| pub(crate) fn load_cached_directory_connectors_from_disk( | ||
| cache_context: &ConnectorDirectoryCacheContext, | ||
| ) -> CachedConnectorDirectoryDiskLoad { | ||
| let cache_path = cache_context.cache_path(); | ||
| let bytes = match std::fs::read(&cache_path) { | ||
| Ok(bytes) => bytes, | ||
| Err(err) if err.kind() == std::io::ErrorKind::NotFound => { | ||
| return CachedConnectorDirectoryDiskLoad::Missing; | ||
| } | ||
| Err(err) => { | ||
| warn!( | ||
| cache_path = %cache_path.display(), | ||
| "failed to read connector directory disk cache: {err}" | ||
| ); | ||
| return CachedConnectorDirectoryDiskLoad::Invalid; | ||
| } | ||
| }; | ||
| let cache: ConnectorDirectoryDiskCache = match serde_json::from_slice(&bytes) { | ||
| Ok(cache) => cache, | ||
| Err(err) => { | ||
| warn!( | ||
| cache_path = %cache_path.display(), | ||
| "failed to parse connector directory disk cache: {err}" | ||
| ); | ||
| let _ = std::fs::remove_file(cache_path); | ||
| return CachedConnectorDirectoryDiskLoad::Invalid; | ||
| } | ||
| }; | ||
| if cache.schema_version != CONNECTOR_DIRECTORY_DISK_CACHE_SCHEMA_VERSION { | ||
| let _ = std::fs::remove_file(cache_path); | ||
| return CachedConnectorDirectoryDiskLoad::Invalid; | ||
| } | ||
|
|
||
| CachedConnectorDirectoryDiskLoad::Hit { | ||
| connectors: cache.connectors, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn write_cached_directory_connectors_to_disk( | ||
| cache_context: &ConnectorDirectoryCacheContext, | ||
| connectors: &[AppInfo], | ||
| ) { | ||
| let cache_path = cache_context.cache_path(); | ||
| if let Some(parent) = cache_path.parent() | ||
| && std::fs::create_dir_all(parent).is_err() | ||
| { | ||
| return; | ||
| } | ||
| let Ok(bytes) = serde_json::to_vec_pretty(&ConnectorDirectoryDiskCache { | ||
| schema_version: CONNECTOR_DIRECTORY_DISK_CACHE_SCHEMA_VERSION, | ||
| connectors: connectors.to_vec(), | ||
| }) else { | ||
| return; | ||
| }; | ||
| let _ = std::fs::write(cache_path, bytes); | ||
| } | ||
|
|
||
| #[derive(Debug, Clone, Serialize, Deserialize)] | ||
| struct ConnectorDirectoryDiskCache { | ||
| schema_version: u8, | ||
| connectors: Vec<AppInfo>, | ||
| } | ||
|
|
||
| fn sha1_hex(value: &str) -> String { | ||
| let mut hasher = Sha1::new(); | ||
| hasher.update(value.as_bytes()); | ||
| let sha1 = hasher.finalize(); | ||
| format!("{sha1:x}") | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.