diff --git a/pkgs/fleet/crates/fleet-queue/src/client.rs b/pkgs/fleet/crates/fleet-queue/src/client.rs index 2e284c3417..11f5d33e6c 100644 --- a/pkgs/fleet/crates/fleet-queue/src/client.rs +++ b/pkgs/fleet/crates/fleet-queue/src/client.rs @@ -1020,6 +1020,18 @@ fn validate_via_broker(packet: &Value, quiet: bool) -> Vec { // Subcommands // --------------------------------------------------------------------------- +/// #8097 Rule 2: a --platform value is a coarse FAMILY selector, +/// never a lane name: exactly "openchamber" or "muse". Shared by +/// the enqueue and claim verbs so both spell the rule once. +fn platform_flag(v: &str) -> Result { + match v { + "openchamber" | "muse" => Ok(v.to_string()), + other => Err(format!( + "invalid --platform {other:?} (expected \"openchamber\" or \"muse\")" + )), + } +} + fn cmd_validate(rest: &[String]) -> i32 { let args = parse_args(rest, &[], &[]); reject_extra_pos("validate", &args, 1); @@ -1070,7 +1082,7 @@ fn resolve_enqueue_queue( } fn cmd_enqueue(rest: &[String]) -> i32 { - let args = parse_args(rest, &["queue"], &["dry-run", "shadow-intent"]); + let args = parse_args(rest, &["queue", "platform"], &["dry-run", "shadow-intent"]); reject_extra_pos("enqueue", &args, 1); let path = args .pos @@ -1089,6 +1101,15 @@ fn cmd_enqueue(rest: &[String]) -> i32 { Ok(q) => q, Err(e) => die(2, &e), }; + // #8097 Rule 2: the platform selector is optional; when given + // it restricts the item to that family. Absent = unset. + let platform = match args.get("platform") { + None => None, + Some(p) => match platform_flag(&p) { + Ok(v) => Some(v), + Err(e) => die(2, &e), + }, + }; let shadow_intent = args.has("shadow-intent"); if queue.ends_with("-shadow") && !shadow_intent { die( @@ -1104,7 +1125,7 @@ fn cmd_enqueue(rest: &[String]) -> i32 { "/enqueue", &json!({ "packet": packet, "queue": queue, "dry_run": dry_run, - "shadow_intent": shadow_intent, + "shadow_intent": shadow_intent, "platform": platform, }), ); let resp = unwrap_ok(resp); @@ -1247,7 +1268,11 @@ fn cmd_list(rest: &[String]) -> i32 { } fn cmd_claim(rest: &[String]) -> i32 { - let args = parse_args(rest, &["queue", "worker", "lease-seconds", "lane", "task"], &[]); + let args = parse_args( + rest, + &["queue", "worker", "lease-seconds", "lane", "task", "platform"], + &["handoff"], + ); reject_extra_pos("claim", &args, 0); let worker = opt_worker(&args); let mut body = json!({ @@ -1258,6 +1283,23 @@ fn cmd_claim(rest: &[String]) -> i32 { if let Some(lane) = args.get("lane") { body["lane"] = json!(lane); } + // --platform : declare this claim's family (#8097 + // Rule 2). The broker only hands this claim items whose + // platform selector is unset or equal to it. A claim that + // declares no family keeps its pre-#8097 reach (unset items). + if let Some(p) = args.get("platform") { + match platform_flag(&p) { + Ok(v) => body["platform"] = json!(v), + Err(e) => die(2, &e), + } + } + // --handoff: the no-id collection of the next parked recorded + // opencode handoff (#8097 Rule 1) — the claimant's default + // parked-handoff path. The BROKER picks the row; the caller + // names no task id. (--task remains the special-case claim.) + if args.has("handoff") { + body["pick"] = json!("parked_handoff"); + } // --task : by-id claim — take exactly that row when the // broker's by-id predicate allows it (a queued row under the // ordinary predicate, or a parked recorded opencode handoff). @@ -1912,6 +1954,16 @@ mod tests { use super::*; use proptest::prelude::*; + #[test] + fn platform_flag_accepts_only_the_two_families() { + // #8097 Rule 2: the selector is a coarse family, never a + // lane name — a lane id as --platform is a usage error. + assert_eq!(platform_flag("openchamber"), Ok("openchamber".to_string())); + assert_eq!(platform_flag("muse"), Ok("muse".to_string())); + assert!(platform_flag("code-a").is_err()); + assert!(platform_flag("").is_err()); + } + #[test] fn fallback_switch_json_shape() { let v = fallback_switch_json("/claim", 4, "store down"); diff --git a/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs b/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs index 783eb69bd7..fa90413148 100644 --- a/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs +++ b/pkgs/fleet/crates/fleet-queue/src/turso_queue.rs @@ -59,7 +59,7 @@ pub const DEFAULT_TURSO_URL: &str = "https://fleet-queue-dezren39.aws-us-east-1. pub const TURSO_TOKEN_SECRET_NAME: &str = "turso.fleet-queue-rw-token"; const TASK_COLS: &str = "id, queue, title, body, payload, dedupe_key, priority, status, \ - not_before, worker_id, claimed_at, lease_until, lane_id, attempts, max_attempts, \ + not_before, worker_id, claimed_at, lease_until, lane_id, platform, attempts, max_attempts, \ progress, result, last_error, park_kind, wait_kind, ready_condition, \ last_checked_at, check_outcome, created_at, updated_at, completed_at"; const LEDGER_COLS: &str = "id, queue, task_id, assigned_lane, executed_lane, \ @@ -350,6 +350,21 @@ impl TursoBackend { pub fn ensure_schema(&self) -> Result<(), TErr> { let stmts: Vec<(&str, Vec)> = SCHEMA.iter().map(|s| (*s, Vec::new())).collect(); self.pipeline(&stmts)?; + // #8097 Rule 2: tasks.platform (the optional coarse platform + // selector) — databases created before the column landed + // gain it here. Existing rows read NULL (unset: either + // family may claim them); no backfill, nothing destructive. + let cols = self.query_objs( + "SELECT name FROM pragma_table_info('tasks')", + &[], + &[], + )?; + let has_platform = cols + .iter() + .any(|c| c.get("name").and_then(|v| v.as_str()) == Some("platform")); + if !has_platform { + self.exec("ALTER TABLE tasks ADD COLUMN platform TEXT", &[])?; + } Ok(()) } } @@ -373,7 +388,7 @@ const SCHEMA: [&str; 12] = [ payload TEXT NOT NULL DEFAULT '{}', dedupe_key TEXT, \ priority INTEGER, status TEXT NOT NULL DEFAULT 'queued', \ not_before TEXT, worker_id TEXT, claimed_at TEXT, lease_until TEXT, \ - lane_id TEXT, attempts INTEGER NOT NULL DEFAULT 0, \ + lane_id TEXT, platform TEXT, attempts INTEGER NOT NULL DEFAULT 0, \ max_attempts INTEGER NOT NULL DEFAULT 4, \ progress TEXT NOT NULL DEFAULT '{}', result TEXT NOT NULL DEFAULT '{}', \ last_error TEXT, park_kind TEXT, wait_kind TEXT, ready_condition TEXT, \ @@ -1308,7 +1323,22 @@ fn verb_enqueue(t: &TursoBackend, req: &Value) -> Value { ), ); } - let task = task_from_packet(&packet, &queue); + let mut task = task_from_packet(&packet, &queue); + // #8097 Rule 2: the optional coarse platform selector. The + // request field (CLI --platform) wins over the packet's own + // "platform"; absent both, the row stores NULL (unset — either + // family may claim it). The selector names a family, never a + // lane, and is never required. + let platform = match parse_platform( + req_str(req, "platform") + .or_else(|| packet.get("platform").and_then(|v| v.as_str())), + ) { + Ok(p) => p, + Err(e) => return fail(2, format!("enqueue: {e}")), + }; + if let Some(obj) = task.as_object_mut() { + obj.insert("platform".to_string(), json!(platform)); + } if req.get("dry_run").and_then(|d| d.as_bool()).unwrap_or(false) { return ok(json!({"dry_run": true, "task": task, "warnings": warnings})); } @@ -1333,8 +1363,8 @@ fn verb_enqueue(t: &TursoBackend, req: &Value) -> Value { } let ins = t.exec( "INSERT INTO tasks (queue, title, body, payload, dedupe_key, priority, \ - status, not_before, max_attempts, progress, result, created_at, updated_at) \ - VALUES (?, ?, ?, ?, ?, ?, 'queued', ?, ?, '{}', '{}', ?, ?)", + status, not_before, max_attempts, platform, progress, result, created_at, updated_at) \ + VALUES (?, ?, ?, ?, ?, ?, 'queued', ?, ?, ?, '{}', '{}', ?, ?)", &[ Arg::Text(queue.clone()), arg_str(task.get("title").and_then(|v| v.as_str())), @@ -1352,6 +1382,7 @@ fn verb_enqueue(t: &TursoBackend, req: &Value) -> Value { .and_then(|v| v.as_i64()) .map(Arg::Int) .unwrap_or(Arg::Int(4)), + arg_str(platform.as_deref()), Arg::Text(now.clone()), Arg::Text(now), ], @@ -1597,6 +1628,54 @@ fn claim_lease_until(claimed_at_unix: i64, lease_seconds: i64) -> String { iso(claimed_at_unix + lease_seconds) } +/// The coarse platform families a queue item may be restricted to +/// (#8097 Rule 2): a task's `platform` is NULL (unset — either +/// family may claim it) or exactly one of these. A platform names +/// a FAMILY, never a specific lane. +pub const PLATFORM_FAMILIES: [&str; 2] = ["openchamber", "muse"]; + +/// Parse an optional platform selector (an enqueue request field, +/// a packet field, or a claim's declared family). Missing/empty +/// means unset; anything that is not one of PLATFORM_FAMILIES is +/// an error, so a typo can never silently widen or narrow who may +/// take an item. +fn parse_platform(raw: Option<&str>) -> Result, String> { + match raw.map(str::trim) { + None | Some("") => Ok(None), + Some(p) if PLATFORM_FAMILIES.contains(&p) => Ok(Some(p.to_string())), + Some(p) => Err(format!( + "invalid platform {p:?} (expected \"openchamber\" or \"muse\")" + )), + } +} + +/// Whether a claim declaring `family` may take a row whose +/// platform selector is `row_platform`. Unset rows are open to +/// every claim; a set row only to a claim declaring the same +/// family. A claim that declares NO family therefore only ever +/// takes unset rows, so pre-#8097 claimants keep exactly their +/// old reach and selector items wait for their own family. +fn platform_allows(row_platform: Option<&str>, family: Option<&str>) -> bool { + match row_platform { + None => true, + Some(p) => family == Some(p), + } +} + +/// The SQL predicate mirroring is_parked_handoff, for the broker's +/// no-id handoff pick (#8097 Rule 1): a parked recorded opencode +/// handoff — progress lane opencode, a yield stage (the top-level +/// stage, else the last checkpoint's), and no session binding. +/// The park verb's default not_before timer is the handoff sink's +/// bookkeeping, not a genuine wait, so the predicate deliberately +/// ignores not_before. +const PARKED_HANDOFF_SQL: &str = "status = 'parked' \ + AND json_extract(progress, '$.lane') = 'opencode' \ + AND json_extract(progress, '$.session_id') IS NULL \ + AND COALESCE(json_extract(progress, '$.stage'), \ + json_extract(progress, '$.last_checkpoint.stage')) \ + IN ('handed_off', 'launch_failed')"; + /// The yield stage a task's progress records: the top-level `stage` /// when present, else the last checkpoint's stage (the checkpoint /// verb records it under progress.last_checkpoint.stage, #7706 — the @@ -1653,7 +1732,12 @@ fn is_parked_handoff(row: &Value) -> bool { /// so it does not gate the claimant collecting its own handoff. /// Anything else (claimed, blocked, a genuine wait park, a queued /// row parked into the future) is not claimable by id. -fn by_id_claimable(row: &Value, now: &str) -> bool { +fn by_id_claimable(row: &Value, now: &str, family: Option<&str>) -> bool { + // #8097 Rule 2: even the special-case by-id claim respects the + // item's platform selector. + if !platform_allows(row.get("platform").and_then(|v| v.as_str()), family) { + return false; + } match row.get("status").and_then(|v| v.as_str()) { Some("queued") => { let attempts = row.get("attempts").and_then(|v| v.as_i64()).unwrap_or(0); @@ -1680,6 +1764,7 @@ fn claim_by_id( worker: &str, lane: &Option, lease_seconds: i64, + family: Option<&str>, ) -> Result, TErr> { let Some(row) = get_task(t, task_id)? else { return Ok(None); @@ -1698,7 +1783,7 @@ fn claim_by_id( .to_string(); let now_unix = now_unix(); let now = iso(now_unix); - if !by_id_claimable(&row, &now) { + if !by_id_claimable(&row, &now, family) { return Ok(None); } let lease_until = claim_lease_until(now_unix, lease_seconds); @@ -1767,7 +1852,7 @@ fn claim_by_id( &json!({ "lease_seconds": lease_seconds, "via": "claim_repo_task_turso", "cleared_session_id": cleared, "lane_id": lane, - "by_id": true, "task_id": task_id, + "by_id": true, "task_id": task_id, "platform": family, }), )?; Ok(Some(row)) @@ -1778,6 +1863,24 @@ fn verb_claim(t: &TursoBackend, req: &Value) -> Value { let queue = req_str(req, "queue").unwrap_or("").to_string(); let lease_seconds = req_i64(req, "lease_seconds").unwrap_or(CLAIM_DEFAULT_LEASE_SECONDS); let lane = req_str(req, "lane").map(str::to_string); + // #8097 Rule 2: the claim's declared family. Only items whose + // platform selector is unset or equal to it may be claimed. + let family = match parse_platform(req_str(req, "platform")) { + Ok(f) => f, + Err(e) => return fail(2, format!("claim_repo_task: {e}")), + }; + // #8097 Rule 1: pick mode. The default pick is the queue's next + // suitable item. "parked_handoff" restricts the pick to parked + // recorded opencode handoffs — the broker chooses the row, the + // caller names NO task id. Naming a task (task_id, below) stays + // the explicit special case. + let handoff_pick = match req_str(req, "pick") { + None => false, + Some("parked_handoff") => true, + Some(other) => { + return fail(2, format!("claim_repo_task: unknown pick mode {other:?}")); + } + }; let work = (|| -> Result, TErr> { if let Some(lane_id) = &lane { let lanes = t.query_objs( @@ -1806,12 +1909,22 @@ fn verb_claim(t: &TursoBackend, req: &Value) -> Value { } expire_leases(t)?; quarantine_payload_missing(t, &queue)?; - // By-id claim: when the request names a task, take exactly - // that row (claim_by_id) instead of the pick. This is how - // the opencode claimant collects a recorded handoff out of - // the parked sink the handing-off lane left it in. + // By-id claim — the SPECIAL CASE (#8097 Rule 1): when the + // request names a task, take exactly that row (claim_by_id) + // instead of the pick. Reserved for one-off revivals of a + // specific row (e.g. a single parked handoff); the default + // flow never names a task — the claimant collects parked + // handoffs through the pick="parked_handoff" mode above. if let Some(want_id) = req_i64(req, "task_id") { - return claim_by_id(t, want_id, &queue, &worker, &lane, lease_seconds); + return claim_by_id( + t, + want_id, + &queue, + &worker, + &lane, + lease_seconds, + family.as_deref(), + ); } let now_unix = now_unix(); let now = iso(now_unix); @@ -1844,45 +1957,67 @@ fn verb_claim(t: &TursoBackend, req: &Value) -> Value { // instant, so if a competing claim takes the picked row // first — or it is parked into the future in between — this // UPDATE affects 0 rows and the loop re-picks. - let pick = t.query_objs( - "SELECT id FROM tasks WHERE queue = ? \ - AND payload IS NOT NULL AND payload <> '' AND payload <> '{}' AND ( \ - (status = 'queued' AND attempts < max_attempts \ + // #8097: the pick predicate is built per pick mode. Both + // modes carry the Rule 2 platform gate — an undeclared + // family is the empty string here, which no stored + // selector equals, so such claims only ever pick unset + // items. The default mode keeps the ordinary predicate + // (queued rows due; parked rows whose timer has passed). + // Handoff mode picks the next parked recorded opencode + // handoff (PARKED_HANDOFF_SQL) with no caller-named id + // and no not_before gating — Rule 1's default collection + // path for the claimant. + let pick_predicate = if handoff_pick { + PARKED_HANDOFF_SQL.to_string() + } else { + "(status = 'queued' AND attempts < max_attempts \ AND (not_before IS NULL OR not_before <= ?)) \ - OR (status = 'parked' AND not_before IS NOT NULL AND not_before <= ?)) \ - ORDER BY (priority IS NULL), priority ASC, created_at ASC LIMIT 1", - &[ - Arg::Text(queue.clone()), - Arg::Text(now.clone()), - Arg::Text(now.clone()), - ], - &[], - )?; + OR (status = 'parked' AND not_before IS NOT NULL AND not_before <= ?)" + .to_string() + }; + let pick_sql = format!( + "SELECT id FROM tasks WHERE queue = ? \ + AND payload IS NOT NULL AND payload <> '' AND payload <> '{{}}' \ + AND (platform IS NULL OR platform = ?) \ + AND ( {pick_predicate} ) \ + ORDER BY (priority IS NULL), priority ASC, created_at ASC LIMIT 1" + ); + let mut pick_args: Vec = vec![ + Arg::Text(queue.clone()), + Arg::Text(family.as_deref().unwrap_or("").to_string()), + ]; + if !handoff_pick { + pick_args.push(Arg::Text(now.clone())); + pick_args.push(Arg::Text(now.clone())); + } + let pick = t.query_objs(&pick_sql, &pick_args, &[])?; let Some(picked) = pick.into_iter().next() else { return Ok(None); }; let pick_id = picked.get("id").and_then(|v| v.as_i64()).unwrap_or(0); - let upd = t.exec( + let upd_sql = format!( "UPDATE tasks SET status = 'claimed', worker_id = ?, claimed_at = ?, \ lease_until = ?, \ attempts = attempts + (CASE WHEN status = 'parked' THEN 0 ELSE 1 END), \ lane_id = ?, park_kind = NULL, wait_kind = NULL, not_before = NULL, \ updated_at = ? \ - WHERE id = ? AND ( \ - (status = 'queued' AND attempts < max_attempts \ - AND (not_before IS NULL OR not_before <= ?)) \ - OR (status = 'parked' AND not_before IS NOT NULL AND not_before <= ?))", - &[ - Arg::Text(worker.clone()), - Arg::Text(now.clone()), - Arg::Text(lease_until.clone()), - arg_str(lane.as_deref()), - Arg::Text(now.clone()), - Arg::Int(pick_id), - Arg::Text(now.clone()), - Arg::Text(now.clone()), - ], - )?; + WHERE id = ? AND (platform IS NULL OR platform = ?) \ + AND ( {pick_predicate} )" + ); + let mut upd_args: Vec = vec![ + Arg::Text(worker.clone()), + Arg::Text(now.clone()), + Arg::Text(lease_until.clone()), + arg_str(lane.as_deref()), + Arg::Text(now.clone()), + Arg::Int(pick_id), + Arg::Text(family.as_deref().unwrap_or("").to_string()), + ]; + if !handoff_pick { + upd_args.push(Arg::Text(now.clone())); + upd_args.push(Arg::Text(now.clone())); + } + let upd = t.exec(&upd_sql, &upd_args)?; if upd.affected == 0 { // Lost the race for the picked row; re-pick. skipped += 1; @@ -1949,6 +2084,8 @@ fn verb_claim(t: &TursoBackend, req: &Value) -> Value { &json!({ "lease_seconds": lease_seconds, "via": "claim_repo_task_turso", "cleared_session_id": cleared, "lane_id": lane, + "platform": family, + "pick": if handoff_pick { "parked_handoff" } else { "default" }, }), )?; Ok(Some(row)) @@ -2851,9 +2988,9 @@ fn verb_list(t: &TursoBackend, req: &Value) -> Value { let rich = req.get("rich").and_then(|r| r.as_bool()).unwrap_or(false); let select = if rich { "id, queue, title, priority, status, dedupe_key, created_at, not_before, \ - worker_id, lease_until, lane_id, attempts, max_attempts, progress, payload" + worker_id, lease_until, lane_id, platform, attempts, max_attempts, progress, payload" } else { - "id, queue, title, priority, status, dedupe_key, created_at, not_before" + "id, queue, title, priority, status, dedupe_key, created_at, not_before, platform" }; let mut sql = format!( "SELECT {select} FROM tasks WHERE queue = ?" @@ -3478,14 +3615,14 @@ mod tests { }), ); assert!(is_parked_handoff(&row)); - assert!(by_id_claimable(&row, "2026-11-09T23:20:00Z")); + assert!(by_id_claimable(&row, "2026-11-09T23:20:00Z", None)); // Top-level stage and the launch_failed recovery yield are // the same recorded handoff in their other spellings. let row2 = handoff_row( "parked", json!({"lane": "opencode", "stage": "launch_failed"}), ); - assert!(by_id_claimable(&row2, "2026-11-09T23:20:00Z")); + assert!(by_id_claimable(&row2, "2026-11-09T23:20:00Z", None)); } #[test] @@ -3496,13 +3633,13 @@ mod tests { "parked", json!({"lane": "opencode", "stage": "parked", "gate": "#3801 OPEN"}), ); - assert!(!by_id_claimable(&gated, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&gated, "2026-11-09T23:20:00Z", None)); // A direct-lane yield is the direct lane's own business. let direct = handoff_row( "parked", json!({"lane": "direct", "stage": "handed_off"}), ); - assert!(!by_id_claimable(&direct, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&direct, "2026-11-09T23:20:00Z", None)); // A bound session id means a launch already happened; the // binding is supervision's, never a fresh claim's. let bound = handoff_row( @@ -3512,13 +3649,61 @@ mod tests { "session_id": "ses_abc", }), ); - assert!(!by_id_claimable(&bound, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&bound, "2026-11-09T23:20:00Z", None)); // A claimed row is owned; by-id never steals a live claim. let claimed = handoff_row( "claimed", json!({"lane": "opencode", "stage": "handed_off"}), ); - assert!(!by_id_claimable(&claimed, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&claimed, "2026-11-09T23:20:00Z", None)); + } + + #[test] + fn platform_parse_accepts_only_the_two_families() { + // #8097 Rule 2: unset is absence; the only set values are + // the two coarse families; anything else errors. + assert_eq!(parse_platform(None), Ok(None)); + assert_eq!(parse_platform(Some("")), Ok(None)); + assert_eq!( + parse_platform(Some("openchamber")), + Ok(Some("openchamber".to_string())) + ); + assert_eq!( + parse_platform(Some("muse")), + Ok(Some("muse".to_string())) + ); + assert!(parse_platform(Some("code-a")).is_err()); + assert!(parse_platform(Some("OpenChamber")).is_err()); + } + + #[test] + fn platform_allows_matrix() { + // Unset rows are open to every claim, family declared or not. + assert!(platform_allows(None, None)); + assert!(platform_allows(None, Some("muse"))); + assert!(platform_allows(None, Some("openchamber"))); + // A set row answers only to its own family; an undeclared + // claim never takes a selector item. + assert!(platform_allows(Some("muse"), Some("muse"))); + assert!(!platform_allows(Some("muse"), Some("openchamber"))); + assert!(!platform_allows(Some("muse"), None)); + assert!(platform_allows(Some("openchamber"), Some("openchamber"))); + assert!(!platform_allows(Some("openchamber"), Some("muse"))); + assert!(!platform_allows(Some("openchamber"), None)); + } + + #[test] + fn by_id_claim_respects_the_platform_selector() { + // #8097 Rule 2: the special-case by-id claim honors the + // selector too — a muse item is not claimable by an + // openchamber (or undeclared) by-id claim, and vice versa. + let mut row = handoff_row("queued", json!({})); + row["not_before"] = json!("2026-11-09T00:00:00Z"); + row["platform"] = json!("muse"); + let now = "2026-11-09T23:20:00Z"; + assert!(by_id_claimable(&row, now, Some("muse"))); + assert!(!by_id_claimable(&row, now, Some("openchamber"))); + assert!(!by_id_claimable(&row, now, None)); } #[test] @@ -3526,15 +3711,15 @@ mod tests { // Queued + attempts left + not_before passed: claimable. let mut row = handoff_row("queued", json!({})); row["not_before"] = json!("2026-11-09T00:00:00Z"); - assert!(by_id_claimable(&row, "2026-11-09T23:20:00Z")); + assert!(by_id_claimable(&row, "2026-11-09T23:20:00Z", None)); // Queued but parked into the future: not claimable by id. let future = handoff_row("queued", json!({})); - assert!(!by_id_claimable(&future, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&future, "2026-11-09T23:20:00Z", None)); // Queued at the attempts cap: not claimable by id. let mut capped = handoff_row("queued", json!({})); capped["not_before"] = json!("2026-11-09T00:00:00Z"); capped["attempts"] = json!(4); - assert!(!by_id_claimable(&capped, "2026-11-09T23:20:00Z")); + assert!(!by_id_claimable(&capped, "2026-11-09T23:20:00Z", None)); } #[test] diff --git a/pm-stack/opencode-claimant/claimant.py b/pm-stack/opencode-claimant/claimant.py index 979ae49c5b..f6150f3fcc 100755 --- a/pm-stack/opencode-claimant/claimant.py +++ b/pm-stack/opencode-claimant/claimant.py @@ -440,72 +440,57 @@ def _send_prompt(session_id: str, prompt: str) -> bool: CLAIMANT_WORKER = "opencode-claimant" -def _claim_parked_candidate(candidate: Dict) -> Optional[str]: - """Collect a parked handoff candidate: claim exactly that task - by id, as the claimant, so the bind below has a live claim to - attach to. Returns None on success (the candidate is updated in - place: status claimed, worker_id the claimant), else a skip - reason. +def _collect_parked_handoffs(queue: str, want: int) -> Dict[int, Dict]: + """Collect up to `want` parked recorded handoffs from `queue` + WITHOUT naming a task id (#8097 Rule 1): each + `repo-queue claim --handoff` lets the BROKER pick the next + parked handoff (its ordinary pick order), claimed as the + claimant, so the launch flow below has a live claim to bind + against. Returns {task_id: claimed row}. The handing-off lane parks the task as its handoff sink, so - without this step the task is never in the claimed state the + without collection the task is never in the claimed state the launch flow binds against — it waited out the park timer, was re-claimed by the next shift wave, handed off, and parked again - (#7907/#6533). The by-id claim (claim --task) is the broker's - recorded-handoff path: it takes a parked row only when its - progress is an unbound opencode yield, and never steals a live - claim or a genuine wait park. A broker build without by-id - support (the PG fallback) ignores the task id and claims the - queue's top row instead; that row is parked straight back and - the mismatch is reported, never launched over.""" - task_id = candidate["id"] - try: - result = subprocess.run( - [ - str(REPO_QUEUE_CMD), - "claim", - "--queue", candidate["queue"], - "--worker", CLAIMANT_WORKER, - "--task", str(task_id), - ], - capture_output=True, - text=True, - timeout=15, - ) - data = json.loads(result.stdout) if result.stdout.strip() else None - except Exception as e: - return f"parked_claim_error: {str(e)}" - # cmd_claim prints the claimed row itself ({"task": null} when - # nothing was claimed). - row = data if isinstance(data, dict) else None - claimed_id = row.get("id") if isinstance(row, dict) else None - if claimed_id == task_id: - candidate["status"] = "claimed" - candidate["worker_id"] = CLAIMANT_WORKER - return None - if claimed_id is not None: - # Wrong row claimed: this broker ignored --task. Restore the - # row to parked at once and report the mismatch; launching - # anything on a guessed claim is how double-execution starts. + (#7907/#6533). Collection used to name the task + (`claim --task `); that by-id claim is now the special-case + verb (one-off revivals), and this default path does not depend + on it — the broker's handoff pick only ever returns a parked + recorded opencode handoff (unbound yield progress), never a + live claim or a genuine wait park. + + The claim declares the claimant's family, --platform + openchamber (#8097 Rule 2): a parked handoff whose item + carries the `muse` selector is not the opencode claimant's + to collect. + """ + collected: Dict[int, Dict] = {} + for _ in range(max(want, 0)): try: - subprocess.run( + result = subprocess.run( [ str(REPO_QUEUE_CMD), - "park", str(claimed_id), + "claim", + "--queue", queue, "--worker", CLAIMANT_WORKER, - "--delay-seconds", "60", - "--reason", - "claimant by-id claim unsupported on this broker " - "build; wrong-row claim restored to parked", + "--platform", "openchamber", + "--handoff", ], capture_output=True, text=True, - timeout=10, + timeout=15, ) + data = json.loads(result.stdout) if result.stdout.strip() else None except Exception: - pass - return "parked_claim_mismatch_restored" - return "parked_claim_failed" + break + # cmd_claim prints the claimed row itself ({"task": null} + # when nothing was claimed). + row = data if isinstance(data, dict) else None + claimed_id = row.get("id") if isinstance(row, dict) else None + if claimed_id is None: + break + collected[claimed_id] = row + return collected def launch_step(mode: str, cap_room: int) -> List[Dict]: @@ -563,6 +548,22 @@ def launch_step(mode: str, cap_room: int) -> List[Dict]: and c.get("stage") in ("handed_off", "launch_failed") ] + # Parked handoff sink (#8097 Rule 1): collect this fire's + # parked candidates' claims up front, grouped by queue, + # without naming any task id — one broker-picked handoff + # claim per discovered parked candidate. A parked + # candidate whose id is not in `collected` when its turn + # comes was not collected (its pick lost a race or the + # broker had nothing claimable) and is skipped below — + # the binding is never improvised. + parked_by_queue: Dict[str, int] = {} + for c in unbound_opencode: + if c.get("status") == "parked": + parked_by_queue[c["queue"]] = parked_by_queue.get(c["queue"], 0) + 1 + collected: Dict[int, Dict] = {} + for queue_name, want in parked_by_queue.items(): + collected.update(_collect_parked_handoffs(queue_name, want)) + for candidate in unbound_opencode: if cap_room <= 0: skips.append({ @@ -578,20 +579,21 @@ def launch_step(mode: str, cap_room: int) -> List[Dict]: # Parked handoff sink: the candidate was handed off and # then parked by its claiming lane, so no live claim - # exists to bind against. Collect the claim by id first - # (as the claimant; the launched session then works - # under the claimant's worker identity). A failed or - # mismatched collection skips the candidate — the - # binding is never improvised. + # existed to bind against until the collection above + # claimed it (as the claimant; the launched session + # then works under the claimant's worker identity). A + # candidate the collection did not claim is skipped — + # the binding is never improvised. if candidate.get("status") == "parked": - claim_skip = _claim_parked_candidate(candidate) - if claim_skip is not None: + if task_id not in collected: skips.append({ "task_id": task_id, - "reason": claim_skip + "reason": "parked_claim_failed" }) continue - worker_id = candidate.get("worker_id", CLAIMANT_WORKER) + candidate["status"] = "claimed" + candidate["worker_id"] = CLAIMANT_WORKER + worker_id = CLAIMANT_WORKER # Step 4a: Re-verify task state try: diff --git a/pm-stack/opencode-claimant/prompt.md b/pm-stack/opencode-claimant/prompt.md index 8a592a6e67..89514fb83a 100644 --- a/pm-stack/opencode-claimant/prompt.md +++ b/pm-stack/opencode-claimant/prompt.md @@ -74,14 +74,21 @@ outage is recoverable, a double-launch's damage is not. parks the task as its handoff sink (the park clears its claim and stamps a default timer), so claimed-only discovery never saw them and they looped claim → handoff → park → re-claim (#7907/#6533). - For a parked candidate, claimant.py first collects the claim by id - (`repo-queue claim --task `, as worker `opencode-claimant`; - the broker takes a parked row by id only when it is a recorded - unbound opencode handoff), then launches exactly as for a claimed - candidate, with the session working under `--worker - opencode-claimant`. A parked candidate whose by-id claim fails or - lands on a different task is skipped with the reason logged — - never launched over a guessed claim. + For parked candidates, claimant.py first collects their claims + WITHOUT naming any task id (#8097 Rule 1): one + `repo-queue claim --handoff --platform openchamber` per parked + candidate, as worker `opencode-claimant`, lets the BROKER pick + the next parked recorded opencode handoff (the pick only ever + returns an unbound yield, never a live claim or a genuine wait + park; the `openchamber` family declaration keeps a + `muse`-selected item out of this claimant's hands, #8097 + Rule 2). It then launches exactly as for a claimed candidate, + with the session working under `--worker opencode-claimant`. + A parked candidate the collection did not claim is skipped + with the reason logged — never launched over a guessed claim. + (The by-id claim, `claim --task `, remains only as the + special-case verb for one-off revivals; no default flow uses + it.) Retain the candidate list from the start of each fire; a task recovered during supervision is eligible on the next fire, not this one. - Concurrency cap: **36** active root sessions across the fleet. Count diff --git a/pm-stack/opencode-claimant/test_claimant.py b/pm-stack/opencode-claimant/test_claimant.py index fb8598d6c6..45d53bcda4 100644 --- a/pm-stack/opencode-claimant/test_claimant.py +++ b/pm-stack/opencode-claimant/test_claimant.py @@ -152,6 +152,59 @@ def test_valid_candidate_launches_and_binds(self): self.assertIn("777", bind_argv) self.assertIn("ses_new1", bind_argv) + def test_parked_candidate_is_collected_without_naming_its_id(self): + # #8097 Rule 1: a parked handoff candidate is collected via + # the broker-picked handoff claim — no argv ever names the + # task id — and then launches exactly like a claimed one. + binds = [] + claims = [] + base_fake = make_fake_run(binds) + + def fake_run(cmd, **kwargs): + argv = [str(part) for part in cmd] + if "repo-queue" in argv[0] and "claim" in argv: + claims.append(argv) + return Result(0, json.dumps({"id": 888, "status": "claimed"})) + return base_fake(cmd, **kwargs) + + parked = dict(CANDIDATE, id=888, status="parked", + queue=claimant.QUEUES[0]) + with patch.object(claimant, "run_poll", + return_value={"candidates": [dict(parked)]}), \ + patch.object(claimant.subprocess, "run", fake_run): + launched, skips = claimant.launch_step("live", 3) + self.assertEqual(skips, []) + self.assertEqual(len(launched), 1) + self.assertEqual(launched[0]["task_id"], 888) + self.assertEqual(len(claims), 1) + claim_argv = claims[0] + self.assertIn("--handoff", claim_argv) + self.assertIn("--platform", claim_argv) + self.assertIn("openchamber", claim_argv) + self.assertNotIn("--task", claim_argv) + self.assertNotIn("888", claim_argv) + + def test_collect_parked_handoffs_drains_until_the_broker_has_none(self): + # The collection loop claims broker-picked handoffs until + # the broker answers {"task": null}; every claim is a + # no-id handoff claim under the openchamber family. + rows = [Result(0, json.dumps({"id": 501, "status": "claimed"})), + Result(0, json.dumps({"id": 502, "status": "claimed"})), + Result(0, json.dumps({"task": None}))] + argvs = [] + + def fake_run(cmd, **kwargs): + argvs.append([str(part) for part in cmd]) + return rows[len(argvs) - 1] + + with patch.object(claimant.subprocess, "run", fake_run): + collected = claimant._collect_parked_handoffs("q1", 5) + self.assertEqual(sorted(collected), [501, 502]) + self.assertEqual(len(argvs), 3) + for argv in argvs: + self.assertIn("--handoff", argv) + self.assertNotIn("--task", argv) + def test_vanished_candidate_is_a_recorded_skip(self): calls = {"n": 0}