fix: compare script modules and real step metadata in the ci test rule

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
hugocasa
2026-11-23 17:18:23 +01:00
co-authored by Claude Opus 5.5
parent d80b5b5b37
commit 34a01ac644
9 changed files with 152 additions and 101 deletions
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT value AS \"value: sqlx::types::Json<serde_json::Value>\", edited_at FROM resource WHERE workspace_id = $1 AND path = $2",
"query": "SELECT value AS \"value: sqlx::types::Json<serde_json::Value>\", resource_type, edited_at FROM resource WHERE workspace_id = $1 AND path = $2",
"describe": {
"columns": [
{
@@ -10,6 +10,11 @@
},
{
"ordinal": 1,
"name": "resource_type",
"type_info": "Varchar"
},
{
"ordinal": 2,
"name": "edited_at",
"type_info": "Timestamptz"
}
@@ -22,8 +27,9 @@
},
"nullable": [
true,
false,
true
]
},
"hash": "4be62c01b78e3bbccdf905b96934faa950827a51990aea4b0a5a0cab46f4bbe5"
"hash": "35089c287fa018bc7f181fafa11054ee686cb7e3c7f4285ea1a28b427490bcb2"
}
@@ -1,6 +1,6 @@
{
"db_name": "PostgreSQL",
"query": "SELECT content, created_at FROM script WHERE workspace_id = $1 AND path = $2 AND deleted = false AND archived = false ORDER BY created_at DESC LIMIT 1",
"query": "SELECT content, modules, created_at FROM script WHERE workspace_id = $1 AND path = $2 AND deleted = false AND archived = false ORDER BY created_at DESC LIMIT 1",
"describe": {
"columns": [
{
@@ -10,6 +10,11 @@
},
{
"ordinal": 1,
"name": "modules",
"type_info": "Jsonb"
},
{
"ordinal": 2,
"name": "created_at",
"type_info": "Timestamptz"
}
@@ -22,8 +27,9 @@
},
"nullable": [
false,
true,
false
]
},
"hash": "af6179af83d71da36480328217306422db7507fcf073ae306db3ca2a916e3f7c"
"hash": "8d3bf3ac9f130748fdda52ff82fb391f7ae18d1d7e5c946b2991ed3ac7f853e1"
}
@@ -0,0 +1,37 @@
{
"db_name": "PostgreSQL",
"query": "SELECT j.created_at, jc.status::text AS status, (s.content IS NOT DISTINCT FROM t.content AND s.modules IS NOT DISTINCT FROM t.modules) AS \"same_test!\" FROM v2_job j LEFT JOIN v2_job_completed jc ON jc.id = j.id LEFT JOIN script s ON s.workspace_id = j.workspace_id AND s.hash = j.runnable_id LEFT JOIN LATERAL ( SELECT content, modules FROM script WHERE workspace_id = $4 AND path = $2 AND deleted = false AND archived = false ORDER BY created_at DESC LIMIT 1 ) t ON true WHERE j.workspace_id = $1 AND j.runnable_path = $2 AND j.trigger_kind = 'ci_test' AND j.trigger = $3 ORDER BY j.created_at DESC LIMIT 1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 1,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "same_test!",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text",
"Text"
]
},
"nullable": [
false,
null,
null
]
},
"hash": "c6afbcded8b6705675e910a93b44518a7b67b808f869d596a52b2d02579e9b91"
}
@@ -1,37 +0,0 @@
{
"db_name": "PostgreSQL",
"query": "SELECT j.created_at, jc.status::text AS status, (s.content IS NOT DISTINCT FROM t.content) AS \"same_test!\" FROM v2_job j LEFT JOIN v2_job_completed jc ON jc.id = j.id LEFT JOIN script s ON s.workspace_id = j.workspace_id AND s.hash = j.runnable_id LEFT JOIN LATERAL ( SELECT content FROM script WHERE workspace_id = $4 AND path = $2 AND deleted = false AND archived = false ORDER BY created_at DESC LIMIT 1 ) t ON true WHERE j.workspace_id = $1 AND j.runnable_path = $2 AND j.trigger_kind = 'ci_test' AND j.trigger = $3 ORDER BY j.created_at DESC LIMIT 1",
"describe": {
"columns": [
{
"ordinal": 0,
"name": "created_at",
"type_info": "Timestamptz"
},
{
"ordinal": 1,
"name": "status",
"type_info": "Text"
},
{
"ordinal": 2,
"name": "same_test!",
"type_info": "Bool"
}
],
"parameters": {
"Left": [
"Text",
"Text",
"Text",
"Text"
]
},
"nullable": [
false,
null,
null
]
},
"hash": "fa53459440564be4a82cc9306482a93142379b318263f1b4ee054663aaadb810"
}
+7 -1
View File
@@ -1385,7 +1385,13 @@ async fn create_script_internal<'c>(
&db,
&w_id,
&ns.path,
windmill_dep_map::ci_tests::DeployedContent::Script(&ns.content),
windmill_dep_map::ci_tests::DeployedContent::Script {
content: &ns.content,
modules: ns
.modules
.as_ref()
.and_then(|m| serde_json::to_value(m).ok()),
},
&authed,
&authed.groups,
authed.is_admin,
+59 -29
View File
@@ -199,15 +199,19 @@ pub async fn trigger_all_ci_tests(
/// What a deploy writes at its path, as `RequirePassingCiTests` compares it with a fork's.
pub enum DeployedContent<'a> {
Script(&'a str),
Script {
content: &'a str,
modules: Option<serde_json::Value>,
},
Flow(&'a serde_json::value::RawValue),
Resource(&'a serde_json::value::RawValue),
/// `None` when the write clears the value.
Resource(Option<&'a serde_json::value::RawValue>),
}
impl DeployedContent<'_> {
fn kind(&self) -> &'static str {
match self {
DeployedContent::Script(_) => "script",
DeployedContent::Script { .. } => "script",
DeployedContent::Flow(_) => "flow",
DeployedContent::Resource(_) => "resource",
}
@@ -308,13 +312,19 @@ async fn ci_tests_failure(
)
.fetch_all(db)
.await?;
let value = match content {
DeployedContent::Script(_) => None,
DeployedContent::Flow(v) | DeployedContent::Resource(v) => Some(
serde_json::from_str::<serde_json::Value>(v.get())
.map_err(|e| error::Error::BadRequest(format!("invalid {kind} value: {e}")))?,
),
let raw = match content {
DeployedContent::Script { .. } => None,
DeployedContent::Flow(v) => Some(Some(*v)),
DeployedContent::Resource(v) => Some(*v),
};
let value = raw
.map(|v| {
v.map_or(Ok(serde_json::Value::Null), |v| {
serde_json::from_str(v.get())
})
.map_err(|e| error::Error::BadRequest(format!("invalid {kind} value: {e}")))
})
.transpose()?;
let mut failure = format!("No fork of {w_id} holds this version of {kind} {path}");
for fork in forks {
let Some(deployed_at) = fork_deployed_at(db, &fork, path, content, value.as_ref()).await?
@@ -339,8 +349,8 @@ async fn fork_deployed_at(
value: Option<&serde_json::Value>,
) -> error::Result<Option<chrono::DateTime<chrono::Utc>>> {
Ok(match (content, value) {
(DeployedContent::Script(c), _) => sqlx::query!(
"SELECT content, created_at FROM script \
(DeployedContent::Script { content: c, modules }, _) => sqlx::query!(
"SELECT content, modules, created_at FROM script \
WHERE workspace_id = $1 AND path = $2 AND deleted = false AND archived = false \
ORDER BY created_at DESC LIMIT 1",
fork,
@@ -348,7 +358,7 @@ async fn fork_deployed_at(
)
.fetch_optional(db)
.await?
.filter(|s| s.content == *c)
.filter(|s| s.content == *c && s.modules == *modules)
.map(|s| s.created_at),
(DeployedContent::Flow(_), Some(v)) => sqlx::query!(
"SELECT f.value AS \"value: sqlx::types::Json<serde_json::Value>\", \
@@ -363,14 +373,18 @@ async fn fork_deployed_at(
.filter(|f| same_flow(&f.value.0, v))
.and_then(|f| f.deployed_at),
(DeployedContent::Resource(_), Some(v)) => sqlx::query!(
"SELECT value AS \"value: sqlx::types::Json<serde_json::Value>\", edited_at \
"SELECT value AS \"value: sqlx::types::Json<serde_json::Value>\", resource_type, \
edited_at \
FROM resource WHERE workspace_id = $1 AND path = $2",
fork,
path
)
.fetch_optional(db)
.await?
.filter(|r| same_resource(r.value.as_ref().map(|v| &v.0), v))
.filter(|r| {
let fork_value = r.value.as_ref().map_or(&serde_json::Value::Null, |v| &v.0);
same_resource(&r.resource_type, fork_value, v)
})
.and_then(|r| r.edited_at),
_ => None,
})
@@ -391,12 +405,13 @@ async fn fork_tests_failure(
for test in tests {
let run = sqlx::query!(
"SELECT j.created_at, jc.status::text AS status, \
(s.content IS NOT DISTINCT FROM t.content) AS \"same_test!\" \
(s.content IS NOT DISTINCT FROM t.content \
AND s.modules IS NOT DISTINCT FROM t.modules) AS \"same_test!\" \
FROM v2_job j \
LEFT JOIN v2_job_completed jc ON jc.id = j.id \
LEFT JOIN script s ON s.workspace_id = j.workspace_id AND s.hash = j.runnable_id \
LEFT JOIN LATERAL ( \
SELECT content FROM script \
SELECT content, modules FROM script \
WHERE workspace_id = $4 AND path = $2 AND deleted = false AND archived = false \
ORDER BY created_at DESC LIMIT 1 \
) t ON true \
@@ -428,7 +443,8 @@ async fn fork_tests_failure(
}
/// A flow carries over to its parent without the hashes of its script steps, and its locks
/// are recomputed per workspace: neither tells two versions of its logic apart.
/// are recomputed per workspace: neither tells two versions of its logic apart. Step arguments
/// are user data, compared as they are even where they look like a step.
fn same_flow(a: &serde_json::Value, b: &serde_json::Value) -> bool {
fn strip(v: &serde_json::Value) -> serde_json::Value {
match v {
@@ -440,7 +456,14 @@ fn same_flow(a: &serde_json::Value, b: &serde_json::Value) -> bool {
serde_json::Value::Object(
m.iter()
.filter(|(k, _)| !(step && matches!(k.as_str(), "hash" | "lock")))
.map(|(k, v)| (k.clone(), strip(v)))
.map(|(k, v)| {
let v = if k == "input_transforms" {
v.clone()
} else {
strip(v)
};
(k.clone(), v)
})
.collect(),
)
}
@@ -452,15 +475,18 @@ fn same_flow(a: &serde_json::Value, b: &serde_json::Value) -> bool {
}
/// An agent carries over to its parent with the principal it runs as replaced.
fn same_resource(a: Option<&serde_json::Value>, b: &serde_json::Value) -> bool {
fn strip(v: &serde_json::Value) -> serde_json::Value {
fn same_resource(fork_type: &str, a: &serde_json::Value, b: &serde_json::Value) -> bool {
if fork_type != "ai_agent" {
return a == b;
}
let strip = |v: &serde_json::Value| {
let mut v = v.clone();
if let Some(m) = v.as_object_mut() {
m.remove("on_behalf_of");
}
v
}
a.map(strip) == Some(strip(b))
};
strip(a) == strip(b)
}
#[cfg(test)]
@@ -480,19 +506,23 @@ mod tests {
let mut changed_arg = merged.clone();
changed_arg["modules"][0]["value"]["input_transforms"]["lock"]["value"] = json!(2);
assert!(!same_flow(&fork, &changed_arg));
let mut changed_code = merged;
let mut changed_code = merged.clone();
changed_code["modules"][0]["value"]["content"] = json!("y");
assert!(!same_flow(&fork, &changed_code));
let (mut fork_arg, mut merged_arg) = (fork.clone(), merged);
fork_arg["modules"][0]["value"]["input_transforms"]["lock"]["value"] =
json!({"type": "script", "hash": "v1"});
merged_arg["modules"][0]["value"]["input_transforms"]["lock"]["value"] =
json!({"type": "script", "hash": "v2"});
assert!(!same_flow(&fork_arg, &merged_arg));
}
#[test]
fn same_resource_ignores_agent_principal() {
let fork = json!({"model": "m", "on_behalf_of": "u/a"});
assert!(same_resource(
Some(&fork),
&json!({"model": "m", "on_behalf_of": "u/b"})
));
assert!(!same_resource(Some(&fork), &json!({"model": "n"})));
assert!(!same_resource(None, &json!({"model": "m"})));
let other_principal = json!({"model": "m", "on_behalf_of": "u/b"});
assert!(same_resource("ai_agent", &fork, &other_principal));
assert!(!same_resource("custom", &fork, &other_principal));
assert!(!same_resource("ai_agent", &fork, &json!({"model": "n"})));
}
}
+26 -27
View File
@@ -1315,18 +1315,16 @@ async fn create_resource(
.as_ref(),
)
.await?;
if let Some(value) = resource.value.as_deref() {
windmill_dep_map::ci_tests::check_ci_tests_rule(
&db,
&w_id,
&resource.path,
windmill_dep_map::ci_tests::DeployedContent::Resource(value),
&authed,
&authed.groups,
authed.is_admin,
)
.await?;
}
windmill_dep_map::ci_tests::check_ci_tests_rule(
&db,
&w_id,
&resource.path,
windmill_dep_map::ci_tests::DeployedContent::Resource(resource.value.as_deref()),
&authed,
&authed.groups,
authed.is_admin,
)
.await?;
let mut tx = user_db.begin(&authed).await?;
@@ -2221,7 +2219,7 @@ async fn update_resource(
&db,
&w_id,
ns.path.as_deref().unwrap_or(path),
windmill_dep_map::ci_tests::DeployedContent::Resource(value),
windmill_dep_map::ci_tests::DeployedContent::Resource(Some(value)),
&authed,
&authed.groups,
authed.is_admin,
@@ -2672,20 +2670,21 @@ async fn set_resource_value(
return Err(Error::PermissionDenied(msg));
}
authorize_azure_devops_reference(authed, db, user_db, w_id, value.as_ref()).await?;
if let Some(value) = value.as_ref() {
let raw = serde_json::value::to_raw_value(value)
.map_err(|e| Error::internal_err(e.to_string()))?;
windmill_dep_map::ci_tests::check_ci_tests_rule(
db,
w_id,
path,
windmill_dep_map::ci_tests::DeployedContent::Resource(&raw),
authed,
&authed.groups,
authed.is_admin,
)
.await?;
}
let raw = value
.as_ref()
.map(serde_json::value::to_raw_value)
.transpose()
.map_err(|e| Error::internal_err(e.to_string()))?;
windmill_dep_map::ci_tests::check_ci_tests_rule(
db,
w_id,
path,
windmill_dep_map::ci_tests::DeployedContent::Resource(raw.as_deref()),
authed,
&authed.groups,
authed.is_admin,
)
.await?;
let mut tx = user_db.clone().begin(authed).await?;
@@ -1668,7 +1668,11 @@
ciBlockedItems={selectedDiffs
.filter((d) => {
const status = getCiTestStatus(d)
return status === 'fail' || status === 'running'
return (
status === 'fail' ||
status === 'running' ||
(ciTestResults[`${d.kind}:${d.path}`] ?? []).some((r) => !r.job_id)
)
})
.map((d) => d.path)}
onUpdateCanDeploy={(canDeploy) => {
@@ -21,7 +21,7 @@
/** How to name the target in the message, when its id is not what a reader would recognize
* — a remote deploy target is addressed through a proxy path. */
displayName?: string
/** Selected items whose CI tests are failing or still running in the source workspace. */
/** Selected items whose CI tests are failing, running or not yet run in the source workspace. */
ciBlockedItems?: string[]
onUpdateCanDeploy?: (value: boolean) => void
} = $props()
@@ -115,7 +115,7 @@
<p>
The workspace {displayName ?? parentWorkspaceId} requires passing CI tests (<b
>{ciRulesets.map((r) => r.name).join(', ')}</b
>). These selected items have tests that are failing or still running:
>). These selected items have tests that are failing, still running or not yet run:
<b>{ciBlockedItems.join(', ')}</b>.
</p>
{#if canBypassCi}