Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 46 additions & 7 deletions app/src/ai/agent_sdk/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ pub(crate) mod attachments;
pub(crate) mod cloud_provider;
pub(crate) mod environment;
mod error_classification;
pub(crate) mod git_credentials;
pub(crate) mod harness;
pub(super) mod output;
mod snapshot;
Expand Down Expand Up @@ -1454,17 +1455,42 @@ impl AgentDriver {
}
}

// Run the harness with a prompt
// Fetch task_id and AI client once for the refresh loop. Both are needed to call
// `taskGitCredentials` periodically to keep credential files fresh.
let (task_id_for_refresh, ai_client_for_refresh) = foreground
.spawn(|me, ctx| {
let task_id = me.task_id.map(|id| id.to_string());
let ai_client = ServerApiProvider::as_ref(ctx).get_ai_client().clone();
(task_id, ai_client)
})
.await?;

// Run the harness with a prompt, racing it against an infinite git-credentials
// refresh loop. The refresh future never resolves on its own — it is dropped
// automatically when `select!` resolves on the harness result.
match task.harness {
HarnessKind::Oz => {
let conversation_status = foreground
let status_rx = foreground
.spawn(move |me, ctx| me.execute_run(task.prompt, ctx))
.await?
.await
.map_err(|_| {
.await?;

let conversation_status = if let Some(task_id) = task_id_for_refresh {
let refresh =
git_credentials::refresh_loop(task_id, ai_client_for_refresh).fuse();
futures::pin_mut!(refresh);
futures::select! {
result = status_rx.fuse() => result.map_err(|_| {
log::error!("Subscription dropped before agent finished");
AgentDriverError::InvalidRuntimeState
})?,
_ = refresh => unreachable!("git credentials refresh loop resolved unexpectedly"),
}
} else {
status_rx.await.map_err(|_| {
log::error!("Subscription dropped before agent finished");
AgentDriverError::InvalidRuntimeState
})?;
})?
};

// Pause before returning to make sure that all conversation events are transmitted before the session is closed.
// TODO: This is a bit of a bandaid fix, and it would be better if we explicitly waited for the session to end before terminating.
Expand All @@ -1480,7 +1506,20 @@ impl AgentDriver {
let harness_exit_rx = Self::setup_harness(harness.as_ref(), &foreground).await?;
let runner =
Self::prepare_harness(&task.prompt, harness.as_ref(), &foreground).await?;
Self::run_harness(runner, &foreground, harness_exit_rx).await

if let Some(task_id) = task_id_for_refresh {
let harness_fut =
Self::run_harness(runner, &foreground, harness_exit_rx).fuse();
let refresh =
git_credentials::refresh_loop(task_id, ai_client_for_refresh).fuse();
futures::pin_mut!(harness_fut, refresh);
futures::select! {
result = harness_fut => result,
_ = refresh => unreachable!("git credentials refresh loop resolved unexpectedly"),
}
} else {
Self::run_harness(runner, &foreground, harness_exit_rx).await
}
}
HarnessKind::Unsupported(harness) => Err(AgentDriverError::HarnessSetupFailed {
harness: harness.to_string(),
Expand Down
17 changes: 14 additions & 3 deletions app/src/ai/agent_sdk/driver/error_classification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ pub fn classify_driver_error(error: &AgentDriverError) -> (AgentTaskState, TaskS
PlatformErrorCode::InternalError,
),
),
AgentDriverError::ConversationHarnessMismatch { conversation_id, expected, got } => (
AgentDriverError::ConversationHarnessMismatch {
conversation_id,
expected,
got,
} => (
AgentTaskState::Failed,
TaskStatusUpdate::with_error_code(
format!(
Expand All @@ -258,7 +262,11 @@ pub fn classify_driver_error(error: &AgentDriverError) -> (AgentTaskState, TaskS
PlatformErrorCode::EnvironmentSetupFailed,
),
),
AgentDriverError::TaskHarnessMismatch { task_id, expected, got } => (
AgentDriverError::TaskHarnessMismatch {
task_id,
expected,
got,
} => (
AgentTaskState::Failed,
TaskStatusUpdate::with_error_code(
format!(
Expand All @@ -268,7 +276,10 @@ pub fn classify_driver_error(error: &AgentDriverError) -> (AgentTaskState, TaskS
PlatformErrorCode::EnvironmentSetupFailed,
),
),
AgentDriverError::ConversationResumeStateMissing { harness, conversation_id } => (
AgentDriverError::ConversationResumeStateMissing {
harness,
conversation_id,
} => (
AgentTaskState::Failed,
TaskStatusUpdate::with_error_code(
format!(
Expand Down
Loading
Loading