fix(aws): build the gateway sidecar payload only when preparing a task
Attaching to a running task no longer needs the provider key or gateway models; the payload is built lazily on the prepare path only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
34f8a4161d
commit
f87edbc5df
2 files changed
+50
-7
No files matched your search
@@ -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<GatewayPayload<'a>>,
|
||||
/// ([`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<Option<SidecarSecrets>, 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<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
(task, token, None, None, false)
|
||||
} else {
|
||||
progress(PROGRESS_PREPARING);
|
||||
let sidecar = (inp.gateway)()?;
|
||||
let prepared = prepare_workspace(
|
||||
work,
|
||||
inp.who,
|
||||
@@ -526,7 +528,7 @@ pub async fn start_core<T: TaskApi, W: WorkspaceApi, R: RuntimeApi, C: Connector
|
||||
inp.files,
|
||||
inp.custom_env_keys,
|
||||
inp.image,
|
||||
inp.gateway,
|
||||
sidecar.as_ref().map(SidecarSecrets::payload),
|
||||
)
|
||||
.await
|
||||
.map_err(s)?;
|
||||
@@ -660,7 +662,7 @@ pub async fn ensure_started_with(
|
||||
settings: &AppSettings,
|
||||
store: &ProjectsStore,
|
||||
progress: &(dyn Fn(&str) + Send + Sync),
|
||||
read_gateway_key: impl FnOnce() -> Result<Option<String>, String>,
|
||||
read_gateway_key: impl FnOnce() -> Result<Option<String>, String> + Send,
|
||||
) -> Result<ContainerTarget, String> {
|
||||
// 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,
|
||||
|
||||
@@ -262,6 +262,10 @@ impl Connector for Fake {
|
||||
}
|
||||
}
|
||||
|
||||
fn no_gateway() -> Result<Option<SidecarSecrets>, 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<Option<SidecarSecrets>, 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);
|
||||
|
||||
Reference in new issue
Block a user