diff --git a/app/src-tauri/src/host/aws_host.rs b/app/src-tauri/src/host/aws_host.rs index 5e5b605..2969fd8 100644 --- a/app/src-tauri/src/host/aws_host.rs +++ b/app/src-tauri/src/host/aws_host.rs @@ -406,8 +406,9 @@ pub struct StartInputs<'a> { pub custom_env_keys: &'a [String], pub image: &'a str, /// The gateway sidecar's payload, when the project runs it - /// ([`gateway_sidecar`]). - pub gateway: Option>, + /// ([`gateway_sidecar`]). Called only when the start prepares a new task: + /// attaching to a running one never needs the provider key or models. + pub gateway: &'a (dyn Fn() -> Result, String> + Send + Sync), pub wait: WaitTiming, pub access_point_wait: PollTiming, /// Called as soon as the project has a task (found, or RunTask @@ -517,6 +518,7 @@ pub async fn start_core Result, String>, + read_gateway_key: impl FnOnce() -> Result, String> + Send, ) -> Result { // A background re-attach (after a restart) may be opening a tunnel: // cancel it, this start attaches itself. Its tunnel, if it gets one, is @@ -685,7 +687,15 @@ pub async fn ensure_started_with( let files = workspace_files(ssh, ca.as_ref())?; let custom_env_keys = crate::docker::workspace_env::custom_env_keys(project, &settings.global_custom_env_vars); - let sidecar = gateway_sidecar(project, settings, read_gateway_key)?; + // Read lazily: only a start that prepares a new task needs it (an attach + // must work with the provider key gone from the keychain). + let read_gateway_key = std::sync::Mutex::new(Some(read_gateway_key)); + let sidecar = || { + gateway_sidecar(project, settings, || match read_gateway_key.lock().unwrap().take() { + Some(read) => read(), + None => Ok(None), + }) + }; let (connection_id, region, sdk) = project_sdk(project, settings).await?; let who = caller_identity(&sdk).await.map_err(s)?; @@ -702,7 +712,7 @@ pub async fn ensure_started_with( files: &files, custom_env_keys: &custom_env_keys, image: &image, - gateway: sidecar.as_ref().map(SidecarSecrets::payload), + gateway: &sidecar, wait: WaitTiming::default(), access_point_wait: PollTiming::default(), on_task: &on_task, diff --git a/app/src-tauri/src/host/aws_host/tests.rs b/app/src-tauri/src/host/aws_host/tests.rs index 908c6dd..48c19f0 100644 --- a/app/src-tauri/src/host/aws_host/tests.rs +++ b/app/src-tauri/src/host/aws_host/tests.rs @@ -262,6 +262,10 @@ impl Connector for Fake { } } +fn no_gateway() -> Result, String> { + Ok(None) +} + fn inputs<'a>( p: &'a Project, who: &'a CallerIdentity, @@ -275,7 +279,7 @@ fn inputs<'a>( files: &[], custom_env_keys: &[], image: REGISTRY_IMAGE, - gateway: None, + gateway: &no_gateway, wait: WaitTiming { interval: Duration::from_millis(1), timeout: Duration::from_millis(30), @@ -414,6 +418,35 @@ async fn a_running_task_is_attached_with_its_own_token_and_nothing_is_rewritten( assert_eq!(steps, vec![PROGRESS_READING, PROGRESS_WAITING, PROGRESS_CONNECTING]); } +/// A gateway payload that cannot be built: the provider key is gone. +fn missing_gateway_key() -> Result, String> { + Err("The LLM gateway sidecar needs the provider API key".into()) +} + +#[tokio::test] +async fn attaching_to_a_running_task_never_builds_the_gateway_payload() { + let mut p = aws_project(StorageMode::Hybrid); + p.aws.as_mut().unwrap().gateway_sidecar = true; + let live = task(&p, "live", "RUNNING", true); + let fake = Fake { + listed: Mutex::new(vec![vec![live.arn.clone()]]), + ..Default::default() + } + .with_task(live); + let w = who(); + let on_task = |_: Option<&TaskRecord>| {}; + let inp = StartInputs { gateway: &missing_gateway_key, ..inputs(&p, &w, &on_task) }; + let started = start_core(&fake, &fake, &fake, &fake, &inp, &|_| {}).await.unwrap(); + assert!(!started.created); + + // A start that prepares a new task does need it, and fails before RunTask. + let fake = Fake::default(); + let inp = StartInputs { gateway: &missing_gateway_key, ..inputs(&p, &w, &on_task) }; + let e = start_core(&fake, &fake, &fake, &fake, &inp, &|_| {}).await.err().unwrap(); + assert!(e.contains("provider API key"), "{e}"); + assert!(!fake.called("run")); +} + #[tokio::test] async fn losing_the_run_race_is_a_clear_error_and_nothing_is_stopped() { let p = aws_project(StorageMode::Ephemeral);