Skip to content
Merged
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
4 changes: 4 additions & 0 deletions docs/workflows.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ opened or started on a harness with the orchestration tools). Until then
`workflow_status` reports the run as `running` with a note that it is not being
driven. The workflow card and status row offer **Resume** while the run is undriven.
Replay reads finished calls from disk and continues with the unfinished work.
A run resumed from **Resume** starts its workers through the app that pressed
it, with approval cards like any delegate the thread starts (#4380); the
engine answers the calls itself only when no app hosts the thread's
orchestration, and then only a thread at Full access starts workers.

## Limits

Expand Down
12 changes: 11 additions & 1 deletion rustcode/engine/src/acp/extension_dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1877,8 +1877,18 @@ pub(super) async fn dispatch_request(
}
"_rustcode/workflows/resume" => {
let request: workflows::WorkflowResumeRequest = parse_params(params)?;
let asker = server.clients.info(client_id).orchestration;
let host = (asker >= 2)
.then(|| {
super::preview::orchestration_client_for(
true,
&server.clients.session(&request.session_id),
)
})
.flatten()
.map(|client| (client, asker));

responder.respond(workflows::resume(request))
responder.respond(workflows::resume(request, host))
}
"_rustcode/workflows/wake" => {
let request: workflows::WorkflowWakeRequest = parse_params(params)?;
Expand Down
12 changes: 10 additions & 2 deletions rustcode/engine/src/acp/workflows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! a run, and claims the one wake that tells the starting thread the run ended.

use std::collections::HashSet;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

Expand Down Expand Up @@ -54,8 +55,15 @@ pub(crate) struct WorkflowResumeRequest {
pub run_id: String,
}

pub(crate) fn resume(request: WorkflowResumeRequest) -> Value {
match crate::workflow::resume_run(&request.session_id, &request.run_id) {
pub(crate) fn resume(
request: WorkflowResumeRequest,
host: Option<(Arc<dyn crate::preview::PreviewClient>, u8)>,
) -> Value {
let (client, version) = match host {
Some((client, version)) => (Some(client), version),
None => (None, 0),
};
match crate::workflow::resume_run(&request.session_id, &request.run_id, client, version) {
Ok(run) => json!({ "run": run }),
Err(message) => json!({ "error": { "code": "failed", "message": message } }),
}
Expand Down
73 changes: 66 additions & 7 deletions rustcode/engine/src/workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,18 +430,42 @@ fn resume_in(store: &Store, runner: &Arc<dyn AgentRunner>) -> usize {
// Serialize the transition between a saved run and a live driver.
static DRIVER_TRANSITION: Mutex<()> = Mutex::new(());

pub(crate) fn resume_run(thread: &str, run_id: &str) -> Result<Value, String> {
/// Resume one saved run for a client that asked: its workers go through
/// `client` (the app that pressed Resume) when it hosts orchestration, and
/// through the engine's own answer only when no client does.
pub(crate) fn resume_run(
thread: &str,
run_id: &str,
client: Option<Arc<dyn crate::preview::PreviewClient>>,
version: u8,
) -> Result<Value, String> {
validate(STOP_TOOL_NAME, &json!({ "run_id": run_id }))?;
let store = Store::for_thread(thread)
.ok_or_else(|| "this thread has no folder that keeps runs".to_owned())?;
resume_one(
&store,
run_id,
ThreadRunner::new(thread, None, crate::orchestration::VERSION),
)?;
let version = resume_version(client.is_some(), version);
resume_run_in(&store, run_id, ThreadRunner::new(thread, client, version))
}

/// The orchestration version a resumed run's workers are started with: the
/// asking client's, never above the engine's, or the engine's own when the
/// engine answers the calls itself.
fn resume_version(hosted_by_client: bool, client_version: u8) -> u8 {
if hosted_by_client {
client_version.min(crate::orchestration::VERSION)
} else {
crate::orchestration::VERSION
}
}

fn resume_run_in(
store: &Store,
run_id: &str,
runner: Arc<dyn AgentRunner>,
) -> Result<Value, String> {
resume_one(store, run_id, runner)?;
store
.load(run_id)
.map(|record| summary(&store, &record))
.map(|record| summary(store, &record))
.ok_or_else(|| format!("no run {run_id} in this thread"))
}

Expand Down Expand Up @@ -1020,6 +1044,41 @@ mod tests {
assert_eq!(resume_in(&store, &runner), 0);
}

#[tokio::test]
async fn a_resume_the_app_asks_for_drives_the_run_with_the_runner_it_was_given() {
let dir = tempfile::tempdir().unwrap();
let store = Store::at(dir.path().join("workflows"));
let (echo, runner) = echo();
let record = new_record(
"1784626803679",
None,
Value::Null,
Limits {
concurrency: 1,
call_budget: 5,
timeout_secs: 30,
},
);
store
.create(&record, r#"return await agent("one");"#)
.unwrap();

let summary = resume_run_in(&store, &record.id, runner).unwrap();
assert_eq!(summary["run_id"], json!(record.id));
let finished = settled(&store, &record.id).await;
assert_eq!(finished.status, RunStatus::Done, "{:?}", finished.error);
assert_eq!(finished.result, Some(json!("done by session-1")));
assert_eq!(echo.started.load(Ordering::SeqCst), 1);
}

#[test]
fn a_resumed_run_uses_the_asking_clients_version_and_the_engines_without_one() {
let engine = crate::orchestration::VERSION;
assert_eq!(resume_version(true, 2), 2);
assert_eq!(resume_version(true, engine.saturating_add(5)), engine);
assert_eq!(resume_version(false, 0), engine);
}

#[tokio::test]
async fn status_waits_for_a_run_to_end_and_answers_at_the_deadline_otherwise() {
let dir = tempfile::tempdir().unwrap();
Expand Down
Loading