Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 122 additions & 9 deletions pkgs/fleet/crates/fleet-queue/src/queue_broker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,15 +150,35 @@ impl HostedQueueDoor {
/// up the envelope.
const SHADOW_DIFF_SAMPLE: usize = 25;

/// The stable base-projection fields the shadow comparison checks
/// for content drift on rows both planes carry (#14987 CF-2b):
/// exactly the shared `/list` base projection (LIST_BASE_KEYS in
/// the hosted worker, mirrored by the native rich projection since
/// #14317), minus the `id` the rows are matched on. Volatile rich
/// fields (lease_until, claimed_at, progress, …) move under live
/// traffic by design and are never compared; a field one side does
/// not carry is a projection gap, not row drift, so only fields
/// present on both rows are compared.
const SHADOW_CONTENT_KEYS: [&str; 8] = [
"queue",
"title",
"priority",
"status",
"dedupe_key",
"created_at",
"not_before",
"platform",
];

/// Compare the two `/list` envelopes a shadow read returns —
/// local Turso plane first, hosted door second — by task id
/// (#14987 CF-2b shadow window). Reports the counts each plane
/// answered, the ids present on exactly one side, and whether the
/// planes diverge; `complete` is false when either window is
/// truncated or a row carries no numeric id, so a partial read
/// can never certify equality. Rows are compared by id only: a
/// status/payload drift inside the same id set is migration
/// follow-up, not a cutover blocker at this layer.
/// local Turso plane first, hosted door second — by task id and,
/// for ids both planes carry, by row content (#14987 CF-2b shadow
/// window). Reports the counts each plane answered, the ids
/// present on exactly one side, the shared rows whose stable
/// base-projection content drifted (with per-field counts), and
/// whether the planes diverge; `complete` is false when either
/// window is truncated or a row carries no numeric id, so a
/// partial read can never certify equality.
fn shadow_diff(local: &Value, door: &Value) -> Result<Value, String> {
let local_rows =
crate::client::list_rows(local).map_err(|e| format!("local /list unreadable: {e}"))?;
Expand All @@ -178,6 +198,39 @@ fn shadow_diff(local: &Value, door: &Value) -> Result<Value, String> {
let mut only_in_door: Vec<i64> = door_ids.difference(&local_ids).copied().collect();
only_in_turso.truncate(SHADOW_DIFF_SAMPLE);
only_in_door.truncate(SHADOW_DIFF_SAMPLE);
// Content layer: for ids both planes carry, compare the stable
// base-projection fields both rows carry. The CF-2 evidence
// baseline was rows agreeing, not just ids present — an id-set
// match with a drifted status would otherwise certify parity
// the planes do not have.
fn by_id<'a>(rows: &'a [Value]) -> BTreeMap<i64, &'a Value> {
rows.iter()
.filter_map(|row| row.get("id")?.as_i64().map(|id| (id, row)))
.collect()
}
let local_by_id = by_id(&local_rows);
let door_by_id = by_id(&door_rows);
let mut drifted: Vec<i64> = Vec::new();
let mut drift_fields: BTreeMap<&str, usize> = BTreeMap::new();
for id in local_ids.intersection(&door_ids) {
let (Some(local_row), Some(door_row)) = (local_by_id.get(id), door_by_id.get(id)) else {
continue;
};
let mut row_drifted = false;
for key in SHADOW_CONTENT_KEYS {
if let (Some(a), Some(b)) = (local_row.get(key), door_row.get(key)) {
if a != b {
row_drifted = true;
*drift_fields.entry(key).or_insert(0) += 1;
}
}
}
if row_drifted {
drifted.push(*id);
}
}
let drifted_rows = drifted.len();
drifted.truncate(SHADOW_DIFF_SAMPLE);
let truncated = |resp: &Value| {
resp.get("truncated")
.and_then(Value::as_bool)
Expand All @@ -186,10 +239,13 @@ fn shadow_diff(local: &Value, door: &Value) -> Result<Value, String> {
let complete = !truncated(local) && !truncated(door) && unkeyed_local == 0 && unkeyed_door == 0;
// Unkeyed rows cannot be matched across planes, so they count
// as divergence: the evidence is insufficient to cut over on.
// A shared row whose content drifted is divergence of the same
// kind: the planes do not agree on the row.
let divergent = !only_in_turso.is_empty()
|| !only_in_door.is_empty()
|| unkeyed_local > 0
|| unkeyed_door > 0;
|| unkeyed_door > 0
|| drifted_rows > 0;
Ok(json!({
"divergent": divergent,
"complete": complete,
Expand All @@ -201,6 +257,9 @@ fn shadow_diff(local: &Value, door: &Value) -> Result<Value, String> {
"only_in_door": only_in_door,
"unkeyed_turso_rows": unkeyed_local,
"unkeyed_door_rows": unkeyed_door,
"drifted_rows": drifted_rows,
"drifted": drifted,
"drift_fields": drift_fields,
}))
}

Expand Down Expand Up @@ -2423,6 +2482,60 @@ mod tests {
assert!(shadow_diff(&same, &json!({"ok": true})).is_err());
}

#[test]
fn shadow_diff_reports_row_content_drift() {
// Oracle (#14987 CF-2b shadow window, content layer): the
// id-set comparison alone certifies membership, not parity —
// two planes can carry the same ids while a shared row's
// content has drifted (the CF-2 evidence baseline was rows
// agreeing, not just ids present). A shared row whose stable
// base-projection fields differ is reported as drift, with
// the per-field counts the cutover evidence needs.
let local = json!({"ok": true, "rows": [
{"id": 1, "queue": "q", "title": "a", "priority": "high", "status": "queued",
"dedupe_key": "k1", "created_at": "2026-11-25T00:00:00Z", "not_before": null,
"platform": "fleet"},
{"id": 2, "queue": "q", "title": "b", "priority": "normal", "status": "done",
"dedupe_key": "k2", "created_at": "2026-11-25T00:00:00Z", "not_before": null,
"platform": "fleet"}
], "total": 2, "truncated": false});
let door = json!({"ok": true, "rows": [
{"id": 1, "queue": "q", "title": "a", "priority": "high", "status": "claimed",
"dedupe_key": "k1", "created_at": "2026-11-25T00:00:00Z", "not_before": null,
"platform": "fleet"},
{"id": 2, "queue": "q", "title": "b", "priority": "normal", "status": "done",
"dedupe_key": "k2", "created_at": "2026-11-25T00:00:00Z", "not_before": null,
"platform": "fleet"}
], "total": 2, "truncated": false});
let diff = shadow_diff(&local, &door).unwrap();
assert_eq!(diff["divergent"], json!(true));
assert_eq!(diff["complete"], json!(true));
assert_eq!(diff["only_in_turso"], json!([]));
assert_eq!(diff["only_in_door"], json!([]));
assert_eq!(diff["drifted_rows"], json!(1));
assert_eq!(diff["drifted"], json!([1]));
assert_eq!(diff["drift_fields"], json!({"status": 1}));

// Identical content on both planes: no drift, no divergence.
let diff = shadow_diff(&local, &local).unwrap();
assert_eq!(diff["divergent"], json!(false));
assert_eq!(diff["drifted_rows"], json!(0));
assert_eq!(diff["drifted"], json!([]));
assert_eq!(diff["drift_fields"], json!({}));

// A field present on only one side is a projection gap, not
// row drift: the comparison covers the fields both rows
// carry. Volatile rich fields (lease_until) move under live
// traffic by design and are never compared.
let sparse = json!({"ok": true, "rows": [
{"id": 1, "status": "queued", "lease_until": "2026-11-29T00:00:00Z"},
{"id": 2, "status": "done", "lease_until": "2026-11-29T00:00:00Z"}
], "total": 2, "truncated": false});
let diff = shadow_diff(&sparse, &local).unwrap();
assert_eq!(diff["drifted_rows"], json!(0));
assert_eq!(diff["divergent"], json!(false));
}

#[test]
fn door_shadow_without_door_is_config_error() {
// Oracle: POST /door-shadow without QUEUE_BROKER_DOOR_URL
Expand Down
Loading