fix: bound a run history group's read by its top-level run's creation

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Guilhem Lemouel
2026-10-26 13:27:32 -07:00
co-authored by Claude Opus 5.5
parent e5aea51101
commit eb40b5fe07
2 changed files with 24 additions and 19 deletions
+14 -8
View File
@@ -261,10 +261,12 @@ fn inline_args(alias: &str) -> String {
)
}
/// When the run `{id}` started, or NULL before it does. Every run inside it is created after
/// that, but a scheduled run is created a whole period before it starts, so its creation time
/// is no bound. A queued workflow-as-code run's `started_at` is its current segment's; its first
/// start is in `extras`. The queue and completed rows carry no row-level security: this also
/// When the run `{id}` started, or NULL before it does: a group's count is exact when its
/// top-level run started after the oldest run the list's window read. Its creation time would
/// leave a scheduled run's group inexact, since that run is created a whole period before it
/// starts. A queued workflow-as-code run's `started_at` is its current segment's; its first
/// start is in `extras`. After a zombie recovery it is the latest pull's, which can mark a
/// group exact too early. The queue and completed rows carry no row-level security: this also
/// works for a run the viewer cannot read.
fn run_start(id: &str) -> String {
format!(
@@ -790,7 +792,11 @@ pub(crate) async fn list_inside(
top_run: Uuid,
) -> Result<Vec<Input>> {
let categories: Vec<&str> = q.started_by.iter().map(|c| c.as_str()).collect();
// The latest CAP runs since `top_run` started hold every run the list's window can count.
// The latest CAP runs since `top_run` was created hold every run the list's window can
// count. Its creation time is the bound that holds whatever happened since: its start is
// stamped again after a zombie recovery and on each workflow-as-code segment. The queue row
// has no row-level security, for a queued run the viewer cannot read; a completed one leaves
// the read unbounded, which the LIMIT still caps.
// Only a script run has native retries, and a flow can have a loop's worth of children.
let retry_gate = match q.runnable_type {
RunnableType::FlowPath => "AND false",
@@ -801,9 +807,10 @@ pub(crate) async fn list_inside(
(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 >= COALESCE({start}, \
AND created_at >= 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 created_at FROM v2_job_queue WHERE id = $4 AND workspace_id = $2), \
'-infinity') \
ORDER BY created_at DESC LIMIT $7) \
UNION \
SELECT x.id FROM v2_job x JOIN native_retry_attempt n ON n.job_id = x.id \
@@ -848,7 +855,6 @@ pub(crate) async fn list_inside(
classify = classify_inside(q.runnable_type),
state = run_state("s"),
args = inline_args("s"),
start = run_start("$4"),
);
let rows = sqlx::query_as::<_, InsideRow>(&sql)
.bind(q.path)
@@ -427,23 +427,22 @@ async fn test_input_history_started_by(db: Pool<Postgres>) -> anyhow::Result<()>
sqlx::query("ANALYZE v2_job, v2_job_completed")
.execute(&db)
.await?;
// A workflow-as-code run in a later segment: its queue row's start is the segment's, and its
// first start is in extras. Opening its group must still find an earlier segment's task.
let workflow = insert_run(
// A run whose start was stamped again, as the pull after a zombie recovery does: opening its
// group must still find the call it made before.
let restarted = insert_run(
&db,
Run { path: "f/test/workflow", running: true, at: 24, ..Run::default() },
Run { path: "f/test/restarted", running: true, at: 24, ..Run::default() },
)
.await?;
let task = insert_run(
let early_call = insert_run(
&db,
Run { path: "f/test/task", parent_job: Some(workflow), at: 25, ..Run::default() },
Run { path: "f/test/task", parent_job: Some(restarted), at: 25, ..Run::default() },
)
.await?;
sqlx::query(
"UPDATE v2_job_queue SET started_at = started_at + interval '5 seconds', \
extras = jsonb_build_object('wac_first_started_at', started_at) WHERE id = $1",
"UPDATE v2_job_queue SET started_at = started_at + interval '5 seconds' WHERE id = $1",
)
.bind(workflow)
.bind(restarted)
.execute(&db)
.await?;
let mut late_calls = vec![];
@@ -560,9 +559,9 @@ async fn test_input_history_started_by(db: Pool<Postgres>) -> anyhow::Result<()>
assert_eq!(retried[0]["group"]["retries"], 1);
let opened = fetch("SECRET_TOKEN", format!("{manual}&top_run={failed_call}")).await?;
assert_eq!(ids(&opened), [retried_call.to_string()]);
let calls = format!("&runnable_id=f/test/task&started_by=script_call&top_run={workflow}");
let calls = format!("&runnable_id=f/test/task&started_by=script_call&top_run={restarted}");
let opened = fetch("SECRET_TOKEN", calls).await?;
assert_eq!(ids(&opened), [task.to_string()]);
assert_eq!(ids(&opened), [early_call.to_string()]);
let opened = fetch("SECRET_TOKEN", format!("{busy}&top_run={long_flow}")).await?;
assert_eq!(
ids(&opened),