From 3feb1b33ec4e72d71c31ea2603615a91c3986d89 Mon Sep 17 00:00:00 2001 From: LHaghfoss Date: Wed, 25 Nov 2026 23:22:35 +0100 Subject: [PATCH] fix(engine): resume a workflow run through the app that asked --- docs/workflows.md | 4 + rustcode/engine/src/acp/extension_dispatch.rs | 12 ++- rustcode/engine/src/acp/workflows.rs | 12 ++- rustcode/engine/src/workflow.rs | 73 +++++++++++++++++-- 4 files changed, 91 insertions(+), 10 deletions(-) diff --git a/docs/workflows.md b/docs/workflows.md index 325181c52..7431bde5f 100644 --- a/docs/workflows.md +++ b/docs/workflows.md @@ -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 diff --git a/rustcode/engine/src/acp/extension_dispatch.rs b/rustcode/engine/src/acp/extension_dispatch.rs index 053f19b48..f46fc1d80 100644 --- a/rustcode/engine/src/acp/extension_dispatch.rs +++ b/rustcode/engine/src/acp/extension_dispatch.rs @@ -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)?; diff --git a/rustcode/engine/src/acp/workflows.rs b/rustcode/engine/src/acp/workflows.rs index 0945269a4..fd6381b79 100644 --- a/rustcode/engine/src/acp/workflows.rs +++ b/rustcode/engine/src/acp/workflows.rs @@ -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; @@ -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, 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 } }), } diff --git a/rustcode/engine/src/workflow.rs b/rustcode/engine/src/workflow.rs index fb74ee30b..c21760829 100644 --- a/rustcode/engine/src/workflow.rs +++ b/rustcode/engine/src/workflow.rs @@ -430,18 +430,42 @@ fn resume_in(store: &Store, runner: &Arc) -> 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 { +/// 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>, + version: u8, +) -> Result { 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, +) -> Result { + 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")) } @@ -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();