Skip to content

Commit cee0e41

Browse files
committed
fix(desktop): keep selected Intelligence project authoritative
1 parent bc3af1f commit cee0e41

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

‎desktop/src-tauri/src/env.rs‎

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -538,10 +538,11 @@ pub fn compose(
538538
"INTELLIGENCE_GATEWAY_WS_URL".into(),
539539
intelligence.gateway_ws_url.trim().to_string(),
540540
);
541-
env.insert(
542-
"INTELLIGENCE_API_KEY".into(),
543-
intelligence.api_key.trim().to_string(),
544-
);
541+
// Explicit desktop selection must override a CLI key left in the checkout's .env.
542+
// Supply both names for consumers that still read the legacy variable directly.
543+
for key in ["CPK_INTELLIGENCE_API_KEY", "INTELLIGENCE_API_KEY"] {
544+
env.insert(key.into(), intelligence.api_key.trim().to_string());
545+
}
545546

546547
/*
547548
* MINTED ONCE PER DEPLOYMENT, NOT ONCE PER START.
@@ -1344,6 +1345,42 @@ HTTPS_PROXY=http://proxy:8080
13441345
assert_eq!(env["OPENAI_API_KEY"], "sk-model");
13451346
}
13461347

1348+
#[test]
1349+
fn desktop_project_selection_replaces_and_purges_a_previous_cli_key() {
1350+
let dir = temp_root("env-cli-project-selection");
1351+
std::fs::create_dir_all(&dir).unwrap();
1352+
let path = dir.join(".env");
1353+
std::fs::write(
1354+
&path,
1355+
"CPK_INTELLIGENCE_API_KEY=cpk-old-cli-project\nINTELLIGENCE_API_KEY=cpk-old-legacy\nCUSTOM=keep-me\n",
1356+
)
1357+
.unwrap();
1358+
let selected = Intelligence {
1359+
api_url: "https://selected.example".into(),
1360+
gateway_ws_url: "wss://selected.example".into(),
1361+
api_key: " cpk-desktop-project ".into(),
1362+
};
1363+
let (settings, secrets) = crate::vault::split(compose(
1364+
&selected,
1365+
&Model::default(),
1366+
&engine_status(None),
1367+
&Ports::default(),
1368+
&pinned(),
1369+
None,
1370+
&BTreeMap::new(),
1371+
));
1372+
for key in ["CPK_INTELLIGENCE_API_KEY", "INTELLIGENCE_API_KEY"] {
1373+
assert_eq!(secrets[key], "cpk-desktop-project");
1374+
assert!(!settings.contains_key(key));
1375+
}
1376+
write(&path, &settings, &secrets).unwrap();
1377+
let saved = std::fs::read_to_string(&path).unwrap();
1378+
assert!(!saved.contains("INTELLIGENCE_API_KEY="));
1379+
assert!(saved.contains("CUSTOM=keep-me"));
1380+
assert!(saved.contains("INTELLIGENCE_API_URL=https://selected.example"));
1381+
std::fs::remove_dir_all(dir).unwrap();
1382+
}
1383+
13471384
#[test]
13481385
fn a_value_with_nothing_around_it_is_untouched() {
13491386
let env = compose(

‎desktop/src-tauri/src/vault.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub fn is_secret(key: &str) -> bool {
3131
key,
3232
// Somebody's own credentials, pasted or signed in for.
3333
"INTELLIGENCE_API_KEY"
34+
| "CPK_INTELLIGENCE_API_KEY"
3435
| "OPENAI_API_KEY"
3536
| "ANTHROPIC_API_KEY"
3637
| "CLAUDE_CODE_OAUTH_TOKEN"
@@ -1451,6 +1452,7 @@ mod tests {
14511452
fn every_credential_is_named_and_nothing_else_is() {
14521453
for key in [
14531454
"INTELLIGENCE_API_KEY",
1455+
"CPK_INTELLIGENCE_API_KEY",
14541456
"OPENAI_API_KEY",
14551457
"ANTHROPIC_API_KEY",
14561458
"CLAUDE_CODE_OAUTH_TOKEN",

0 commit comments

Comments
 (0)