-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Fix Codex env-key authentication readiness #5807
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
Closed
salman1993
wants to merge
3
commits into
block:main
from
salman1993:smohammed/fix-4755-codex-env-auth
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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
120 changes: 120 additions & 0 deletions
120
desktop/src-tauri/src/commands/agent_discovery/auth_env.rs
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,120 @@ | ||
| use std::collections::BTreeMap; | ||
|
|
||
| use tauri::AppHandle; | ||
|
|
||
| use crate::managed_agents::{ | ||
| baked_build_env, known_acp_runtime, load_global_agent_config, load_managed_agent_configs, | ||
| load_personas, merged_user_env, record_agent_command, resolve_effective_agent_env, | ||
| AgentDefinition, GlobalAgentConfig, ManagedAgentRecord, | ||
| }; | ||
|
|
||
| /// Effective child environments that can supply pre-probe auth evidence. | ||
| /// | ||
| /// Runtime discovery is global rather than agent-specific, so a runtime is | ||
| /// authenticated when the desktop process or any configured child environment | ||
| /// can authenticate it. Readiness still evaluates one exact child environment. | ||
| pub(super) fn load(app: &AppHandle) -> Vec<BTreeMap<String, String>> { | ||
| let personas = load_personas(app).unwrap_or_else(|error| { | ||
| tracing::warn!(%error, "runtime auth discovery could not load personas"); | ||
| vec![] | ||
| }); | ||
| let records = load_managed_agent_configs(app).unwrap_or_else(|error| { | ||
| tracing::warn!(%error, "runtime auth discovery could not load agent configs"); | ||
| vec![] | ||
| }); | ||
| let global = load_global_agent_config(app).unwrap_or_else(|error| { | ||
| tracing::warn!(%error, "runtime auth discovery could not load global config"); | ||
| GlobalAgentConfig::default() | ||
| }); | ||
| configured(&personas, &records, &global) | ||
| } | ||
|
|
||
| fn configured( | ||
| personas: &[AgentDefinition], | ||
| records: &[ManagedAgentRecord], | ||
| global: &GlobalAgentConfig, | ||
| ) -> Vec<BTreeMap<String, String>> { | ||
| let mut base = baked_build_env(); | ||
| base.extend(merged_user_env(&BTreeMap::new(), &global.env_vars)); | ||
|
|
||
| let persona_envs = personas.iter().cloned().map(|persona| { | ||
| let record = persona.into_agent_record(); | ||
| let command = record_agent_command(&record, &[]); | ||
| resolve_effective_agent_env(&record, &[], known_acp_runtime(&command), global).env | ||
| }); | ||
| let agent_envs = records.iter().map(|record| { | ||
| let command = record_agent_command(record, personas); | ||
| resolve_effective_agent_env(record, personas, known_acp_runtime(&command), global).env | ||
| }); | ||
|
|
||
| std::iter::once(base) | ||
| .chain(persona_envs) | ||
| .chain(agent_envs) | ||
| .collect() | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| fn persona(env_vars: BTreeMap<String, String>) -> AgentDefinition { | ||
| AgentDefinition { | ||
| id: "persona".into(), | ||
| display_name: "Persona".into(), | ||
| avatar_url: None, | ||
| system_prompt: "Help".into(), | ||
| runtime: Some("codex".into()), | ||
| model: None, | ||
| provider: None, | ||
| name_pool: vec![], | ||
| is_builtin: false, | ||
| is_active: true, | ||
| shared: false, | ||
| source_team: None, | ||
| source_team_persona_slug: None, | ||
| catalog_source: None, | ||
| env_vars, | ||
| respond_to: None, | ||
| respond_to_allowlist: vec![], | ||
| parallelism: None, | ||
| created_at: String::new(), | ||
| updated_at: String::new(), | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn collects_global_persona_and_agent_env_with_last_wins_precedence() { | ||
| let global = GlobalAgentConfig { | ||
| env_vars: BTreeMap::from([ | ||
| ("GLOBAL_KEY".into(), "global".into()), | ||
| ("SHARED_KEY".into(), "global".into()), | ||
| ]), | ||
| ..Default::default() | ||
| }; | ||
| let persona = persona(BTreeMap::from([ | ||
| ("PERSONA_KEY".into(), "persona".into()), | ||
| ("SHARED_KEY".into(), "persona".into()), | ||
| ])); | ||
| let mut agent = persona.clone().into_agent_record(); | ||
| agent.persona_id = Some(persona.id.clone()); | ||
| agent.env_vars = BTreeMap::from([ | ||
| ("AGENT_KEY".into(), "agent".into()), | ||
| ("SHARED_KEY".into(), String::new()), | ||
| ]); | ||
|
|
||
| let envs = configured(std::slice::from_ref(&persona), &[agent], &global); | ||
|
|
||
| assert!(envs | ||
| .iter() | ||
| .any(|env| env.get("GLOBAL_KEY") == Some(&"global".into()))); | ||
| assert!(envs | ||
| .iter() | ||
| .any(|env| env.get("PERSONA_KEY") == Some(&"persona".into()))); | ||
| let agent_env = envs.last().expect("agent environment"); | ||
| assert_eq!( | ||
| agent_env.get("AGENT_KEY").map(String::as_str), | ||
| Some("agent") | ||
| ); | ||
| assert_eq!(agent_env.get("SHARED_KEY").map(String::as_str), Some("")); | ||
| } | ||
| } |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When Desktop runs on Windows and a saved environment key uses different casing from Codex's
env_key, this exactBTreeMap::getdiverges from the case-insensitive environment seen by the child process. For example,env_key = "CUSTOM_API_KEY"with a savedcustom_api_key=secretworks in Codex but is reported as logged out; conversely, a differently cased empty override can fail to mask an inherited credential and incorrectly report readiness. The newCODEX_HOMEand static Claude-key lookups have the same issue, so these effective-environment lookups need Windows-aware key matching while preserving override precedence.Useful? React with 👍 / 👎.