From 79ea5f79450f24f249f95a06341728f2ebc4d6cf Mon Sep 17 00:00:00 2001 From: Guilhem Lemouel Date: Mon, 26 Oct 2026 12:25:03 -0700 Subject: [PATCH] fix: read both ends of a script's runs when opening a run history group Co-Authored-By: Claude Opus 5.5 (1M context) --- backend/windmill-api-inputs/src/history.rs | 36 +++++++---- .../tests/inputs.rs | 60 +++++++++++++++++++ 2 files changed, 84 insertions(+), 12 deletions(-) diff --git a/backend/windmill-api-inputs/src/history.rs b/backend/windmill-api-inputs/src/history.rs index 47fcf06cc6..ffecb8484f 100644 --- a/backend/windmill-api-inputs/src/history.rs +++ b/backend/windmill-api-inputs/src/history.rs @@ -727,8 +727,8 @@ struct InsideRow { /// The runs of the runnable inside the top-level run `top_run`, in execution order, with their /// args (or `"WINDMILL_TOO_BIG"`), since an opened group shows them all at once. These are the -/// runs [`list`] groups under `top_run`: among the runnable's runs since `top_run` started (at -/// most [`CAP`] of them), those the walk up from ends there. +/// runs [`list`] groups under `top_run`: among the runnable's runs since `top_run` started, those +/// the walk up from ends there. /// /// Walking down from `top_run` instead would stop at a loop container or subflow the viewer /// cannot read, and its cost would grow with every job of the run, not only this runnable's. @@ -740,21 +740,33 @@ pub(crate) async fn list_inside( let categories: Vec<&str> = q.started_by.iter().map(|c| c.as_str()).collect(); // A run inside another is created after it starts. The queue and completed rows have no // row-level security, so they give that time for a top-level run the viewer cannot read. + // Both ends are read, at most CAP runs each: the latest hold every run the list's window + // can count, the first the retries and handlers of an older run. let sql = format!( - "WITH runs AS (\ + "WITH since AS (\ + SELECT COALESCE(\ + (SELECT created_at FROM v2_job WHERE id = $4 AND workspace_id = $2), \ + (SELECT created_at FROM v2_job_queue WHERE id = $4 AND workspace_id = $2), \ + (SELECT COALESCE(started_at, completed_at) FROM v2_job_completed \ + WHERE id = $4 AND workspace_id = $2)) AS t\ + ), \ + ids AS (\ + (SELECT id FROM v2_job \ + WHERE workspace_id = $2 AND runnable_path = $1 AND parent_job IS NOT NULL \ + AND kind = {inside_kind} AND created_at >= (SELECT t FROM since) \ + ORDER BY created_at LIMIT $7) \ + UNION \ + (SELECT id FROM v2_job \ + WHERE workspace_id = $2 AND runnable_path = $1 AND parent_job IS NOT NULL \ + AND kind = {inside_kind} AND created_at >= (SELECT t FROM since) \ + ORDER BY created_at DESC LIMIT $7)\ + ), \ + runs AS (\ SELECT id, created_at, created_by, trigger_kind, trigger, parent_job, root_job, \ flow_innermost_root_job, flow_step_id, script_entrypoint_override, args, \ {own_category} AS own_category \ FROM v2_job \ - WHERE workspace_id = $2 AND runnable_path = $1 AND parent_job IS NOT NULL \ - AND kind = {inside_kind} \ - AND created_at >= (SELECT COALESCE(\ - (SELECT created_at FROM v2_job WHERE id = $4 AND workspace_id = $2), \ - (SELECT created_at FROM v2_job_queue WHERE id = $4 AND workspace_id = $2), \ - (SELECT COALESCE(started_at, completed_at) FROM v2_job_completed \ - WHERE id = $4 AND workspace_id = $2))) \ - ORDER BY created_at \ - LIMIT $7\ + WHERE id IN (SELECT id FROM ids) AND workspace_id = $2\ ), \ tops AS (\ SELECT f.first, COALESCE(a4.id, a3.id, a2.id, a1.id, f.first) AS top \ diff --git a/backend/windmill-api-integration-tests/tests/inputs.rs b/backend/windmill-api-integration-tests/tests/inputs.rs index 10a6a6dc0e..d47072f2f2 100644 --- a/backend/windmill-api-integration-tests/tests/inputs.rs +++ b/backend/windmill-api-integration-tests/tests/inputs.rs @@ -353,6 +353,53 @@ async fn test_input_history_started_by(db: Pool) -> anyhow::Result<()> ) .await?; + // A long flow calls f/test/busy only after another run has called it 5,000 times: opening its + // group must still find the calls the list counts. + let long_flow = insert_run( + &db, + Run { kind: "flow", path: "f/test/long_flow", at: 22, ..Run::default() }, + ) + .await?; + let busy_flow = insert_run( + &db, + Run { kind: "flow", path: "f/test/busy_flow", at: 23, ..Run::default() }, + ) + .await?; + sqlx::query( + "WITH jobs AS (\ + INSERT INTO v2_job (id, workspace_id, tag, created_by, permissioned_as, \ + permissioned_as_email, kind, runnable_path, parent_job, flow_innermost_root_job, \ + flow_step_id, created_at, args, same_worker, visible_to_owner) \ + SELECT gen_random_uuid(), 'test-workspace', 'deno', 'test-user', 'u/test-user', \ + 'test@windmill.dev', 'script', 'f/test/busy', $1, $1, 'a', \ + now() - interval '1 hour' + make_interval(secs => 23 + i / 1000.0), '{}'::jsonb, \ + false, true \ + FROM generate_series(1, 5000) i \ + RETURNING id, workspace_id, created_at) \ + INSERT INTO v2_job_completed (id, workspace_id, duration_ms, deleted, status, \ + started_at, completed_at) \ + SELECT id, workspace_id, 1, false, 'success', created_at, created_at FROM jobs", + ) + .bind(busy_flow) + .execute(&db) + .await?; + // Without statistics on these rows, the planner picks nested loops that take seconds. + sqlx::query("ANALYZE v2_job, v2_job_completed") + .execute(&db) + .await?; + let mut late_calls = vec![]; + for at in [30, 31] { + let run = Run { + path: "f/test/busy", + parent_job: Some(long_flow), + innermost_root: Some(long_flow), + flow_step_id: Some("b"), + at, + ..Run::default() + }; + late_calls.push(insert_run(&db, run).await?); + } + let server = ApiServer::start(db.clone()).await?; let port = server.addr.port(); let fetch = |token: &'static str, query: String| { @@ -439,6 +486,19 @@ async fn test_input_history_started_by(db: Pool) -> anyhow::Result<()> [own_step, in_loop[0], in_loop[1]].map(|id| id.to_string()) ); + let busy = "&runnable_id=f/test/busy&started_by=flow_step"; + let listed = fetch("SECRET_TOKEN", busy.to_string()).await?; + assert_eq!(listed[0]["top_run"]["id"], long_flow.to_string()); + assert_eq!(listed[0]["group"]["runs"], 2); + let opened = fetch("SECRET_TOKEN", format!("{busy}&top_run={long_flow}")).await?; + assert_eq!( + ids(&opened), + late_calls + .iter() + .map(|id| id.to_string()) + .collect::>() + ); + Ok(()) }