fix: define a flow's run once, through completed jobs only

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-10-27 01:20:39 -07:00
co-authored by Claude Opus 5.5
parent 38a64903c4
commit 7806ed2b45
6 changed files with 96 additions and 78 deletions
@@ -1,28 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "WITH RECURSIVE run AS (\n SELECT id FROM v2_job WHERE parent_job = $1\n UNION ALL\n SELECT j.id FROM v2_job j JOIN run r ON j.parent_job = r.id\n )\n SELECT j.id AS \"id!\", j.flow_step_id\n FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id!",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "flow_step_id",
"type_info": "Varchar"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
false,
true
]
},
"hash": "3674f638d7fc89ec11c4b8beb94c9e8cec2b12402bca723aa1d4a3762b540090"
}
@@ -0,0 +1,34 @@
{
"db_name": "PostgreSQL",
"query": "WITH RECURSIVE run AS (\n SELECT j.id, j.flow_step_id, j.kind = 'flow' AS is_flow, false AS in_subflow\n FROM v2_job j JOIN v2_job_completed c ON c.id = j.id\n WHERE j.parent_job = $1\n UNION ALL\n SELECT j.id, j.flow_step_id, j.kind = 'flow', r.in_subflow OR r.is_flow\n FROM v2_job j JOIN v2_job_completed c ON c.id = j.id JOIN run r ON j.parent_job = r.id\n )\n SELECT id AS \"id!\", flow_step_id, in_subflow AS \"in_subflow!\" FROM run",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "id!",
"type_info": "Uuid"
},
{
"ordinal": 1,
"name": "flow_step_id",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "in_subflow!",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Uuid"
]
},
"nullable": [
null,
null,
null
]
},
"hash": "4493fcace6d3bcc293d871bb1fdc2da2d42e36530a7c5deef42923600e9907be"
}
@@ -1,15 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "WITH RECURSIVE run AS (\n SELECT id, flow_step_id FROM v2_job WHERE parent_job = $1\n UNION ALL\n SELECT j.id, j.flow_step_id FROM v2_job j JOIN run r ON j.parent_job = r.id\n )\n UPDATE v2_job_completed SET result = '{}'::jsonb\n WHERE id IN (SELECT id FROM run WHERE flow_step_id = $2)",
"describe": {
"columns": [],
"parameters": {
"Left": [
"Uuid",
"Text"
]
},
"nullable": []
},
"hash": "f64307bc2dd518e554461911f83abf1465d961326a18cbcf07cb8bb49d5a2486"
}
+11 -9
View File
@@ -870,16 +870,18 @@ pub async fn delete_flow_result_after_read(db: &DB, job_uuid: Uuid) -> Result<()
{
delete_job_metadata_after_use(db, job_uuid).await?;
if let Some(early_return) = flow_value.early_return.as_deref() {
let early_return_ids: Vec<Uuid> =
windmill_common::jobs::completed_flow_run_jobs(db, job_uuid)
.await?
.into_iter()
.filter(|j| {
!j.in_subflow && j.flow_step_id.as_deref() == Some(early_return)
})
.map(|j| j.id)
.collect();
sqlx::query!(
"WITH RECURSIVE run AS (
SELECT id, flow_step_id FROM v2_job WHERE parent_job = $1
UNION ALL
SELECT j.id, j.flow_step_id FROM v2_job j JOIN run r ON j.parent_job = r.id
)
UPDATE v2_job_completed SET result = '{}'::jsonb
WHERE id IN (SELECT id FROM run WHERE flow_step_id = $2)",
job_uuid,
early_return,
"UPDATE v2_job_completed SET result = '{}'::jsonb WHERE id = ANY($1)",
&early_return_ids,
)
.execute(db)
.await?;
+41 -3
View File
@@ -107,6 +107,40 @@ pub fn get_has_preprocessor_from_content_and_lang(
Ok(has_preprocessor)
}
/// A job of a completed flow's run, as returned by [`completed_flow_run_jobs`].
pub struct FlowRunJob {
pub id: uuid::Uuid,
pub flow_step_id: Option<String>,
/// Inside a nested flow, whose step ids are its own rather than the run's.
pub in_subflow: bool,
}
/// The jobs of a completed flow's run, at any depth. Walks `parent_job`, because `root_job`
/// is stored NULL whenever it equals the job's parent or innermost root, which is the case
/// for most steps. Descends through completed jobs only: once the flow has completed, a job
/// still queued or running below it was launched by a step through the API, and that job and
/// everything under it are a separate run.
pub async fn completed_flow_run_jobs(
db: &DB,
flow_id: uuid::Uuid,
) -> crate::error::Result<Vec<FlowRunJob>> {
Ok(sqlx::query_as!(
FlowRunJob,
r#"WITH RECURSIVE run AS (
SELECT j.id, j.flow_step_id, j.kind = 'flow' AS is_flow, false AS in_subflow
FROM v2_job j JOIN v2_job_completed c ON c.id = j.id
WHERE j.parent_job = $1
UNION ALL
SELECT j.id, j.flow_step_id, j.kind = 'flow', r.in_subflow OR r.is_flow
FROM v2_job j JOIN v2_job_completed c ON c.id = j.id JOIN run r ON j.parent_job = r.id
)
SELECT id AS "id!", flow_step_id, in_subflow AS "in_subflow!" FROM run"#,
flow_id
)
.fetch_all(db)
.await?)
}
pub async fn schedule_job_deletion(
db: &DB,
job_id: uuid::Uuid,
@@ -270,8 +304,9 @@ pub async fn get_payload_tag_from_prefixed_path(
None,
)
} else {
let FlowVersionInfo { dedicated_worker, tag, version, labels, job_token_scopes, .. } =
get_latest_flow_version_info_for_path(None, &db, w_id, &path, true).await?;
let FlowVersionInfo {
dedicated_worker, tag, version, labels, job_token_scopes, ..
} = get_latest_flow_version_info_for_path(None, &db, w_id, &path, true).await?;
(
JobPayload::Flow {
path,
@@ -554,7 +589,10 @@ pub struct RunInlinePreviewScriptFnParams {
pub enum InlineScriptTarget {
/// A script addressed by path, pinned to the version the caller resolved and checked.
Path { path: String, hash: i64 },
Path {
path: String,
hash: i64,
},
Hash(i64),
}
+10 -23
View File
@@ -2187,35 +2187,22 @@ async fn delete_flow_run_data(
secs: i32,
early_return: Option<&str>,
) -> error::Result<()> {
// Walks parent_job rather than reading root_job: root_job is stored NULL whenever it
// equals the job's parent or innermost root, which is the case for most steps of a run.
// Only completed jobs: a step can leave a job it spawned through the SDK queued for later,
// and that job still needs its args.
let run = sqlx::query!(
r#"WITH RECURSIVE run AS (
SELECT id FROM v2_job WHERE parent_job = $1
UNION ALL
SELECT j.id FROM v2_job j JOIN run r ON j.parent_job = r.id
)
SELECT j.id AS "id!", j.flow_step_id
FROM run JOIN v2_job j USING (id) JOIN v2_job_completed c USING (id)"#,
flow_job.id
)
.fetch_all(db)
.await
.map_err(|e| {
Error::internal_err(format!(
"error fetching child jobs for flow-level deletion: {e:#}"
))
})?;
let run = windmill_common::jobs::completed_flow_run_jobs(db, flow_job.id)
.await
.map_err(|e| {
Error::internal_err(format!(
"error fetching child jobs for flow-level deletion: {e:#}"
))
})?;
let mut all_ids: Vec<Uuid> = run.iter().map(|j| j.id).collect();
all_ids.push(flow_job.id);
// A synchronous caller of an early-return flow reads the early-return node's result,
// possibly after the flow has completed; it clears that result once read, like the flow's.
// The node can sit at any depth, inside a branch for instance.
let result_ids: Vec<Uuid> = run
.iter()
.filter(|j| early_return.is_none() || j.flow_step_id.as_deref() != early_return)
.filter(|j| {
j.in_subflow || early_return.is_none() || j.flow_step_id.as_deref() != early_return
})
.map(|j| j.id)
.collect();