From 5edf409a6e4441ef2c5a7ab4ad90e31aa931fd6f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:32:30 +0000 Subject: [PATCH 01/13] Fix stale RemoveChild promoting children that were not unlinked. A RemoveChild named against the wrong parent left the child in its real parent's list but still added it to the forest roots, so preorder visited it twice. Only promote to roots when a link was actually cut. Co-authored-by: Tienson Qin --- platform/native/lui_store/lui_store.ml | 79 +++++++++++-------- .../native/lui_store/test/lui_store_test.ml | 27 +++++++ 2 files changed, 72 insertions(+), 34 deletions(-) diff --git a/platform/native/lui_store/lui_store.ml b/platform/native/lui_store/lui_store.ml index 85d25b26..1ba457c8 100644 --- a/platform/native/lui_store/lui_store.ml +++ b/platform/native/lui_store/lui_store.ml @@ -499,40 +499,51 @@ let apply_op t = function | RemoveChild (parent, child) as op -> ( match (Hashtbl.find_opt t.nodes parent, Hashtbl.find_opt t.nodes child) with | Some p, Some c -> - if c.parent = Some parent then ( - unlink p c; - c.parent <- None) - else begin - (* Defensive scan: the child claims a different parent but the - op names this one — walk the list and cut any matching link - so a stale record cannot leave the id listed twice. Only - clear the parent link when a link was actually cut: when the - child is not in this list at all it still sits in its real - parent's list, and dropping [c.parent] would leave it - reachable both there and as a root. *) - let rec scan prev cur = - match cur with - | None -> () - | Some c' -> - if c'.id = child then ( - let next = c'.next_sib in - (match prev with - | Some s -> s.next_sib <- next - | None -> p.first_child <- next); - (match next with - | Some s -> s.prev_sib <- prev - | None -> p.last_child <- prev); - p.child_count <- p.child_count - 1; - c'.prev_sib <- None; - c'.next_sib <- None; - c'.parent <- None) - else scan cur c'.next_sib - in - scan None p.first_child - end; - Hashtbl.replace t.roots child (); - mark_dirty_mask t parent (d_structure lor d_layout lor d_paint); - mark_dirty_mask t child (d_structure lor d_paint) + let unlinked = + if c.parent = Some parent then ( + unlink p c; + c.parent <- None; + true) + else begin + (* Defensive scan: the child claims a different parent but the + op names this one — walk the list and cut any matching link + so a stale record cannot leave the id listed twice. Only + clear the parent link when a link was actually cut: when the + child is not in this list at all it still sits in its real + parent's list, and dropping [c.parent] would leave it + reachable both there and as a root. *) + let cut = ref false in + let rec scan prev cur = + match cur with + | None -> () + | Some c' -> + if c'.id = child then ( + let next = c'.next_sib in + (match prev with + | Some s -> s.next_sib <- next + | None -> p.first_child <- next); + (match next with + | Some s -> s.prev_sib <- prev + | None -> p.last_child <- prev); + p.child_count <- p.child_count - 1; + c'.prev_sib <- None; + c'.next_sib <- None; + c'.parent <- None; + cut := true) + else scan cur c'.next_sib + in + scan None p.first_child; + !cut + end + in + (* H2: a stale RemoveChild that unlinked nothing must not promote + the child to the forest roots — that duplicates it in preorder + while parent(child) still names the real parent. *) + if unlinked then begin + Hashtbl.replace t.roots child (); + mark_dirty_mask t parent (d_structure lor d_layout lor d_paint); + mark_dirty_mask t child (d_structure lor d_paint) + end | _ -> ()); journal_push t op diff --git a/platform/native/lui_store/test/lui_store_test.ml b/platform/native/lui_store/test/lui_store_test.ml index b812e883..ab6380ca 100644 --- a/platform/native/lui_store/test/lui_store_test.ml +++ b/platform/native/lui_store/test/lui_store_test.ml @@ -235,6 +235,31 @@ let test_ops_on_missing_are_ignored () = DropNode 99 ]; check int "still empty" 0 (Lui_store.node_count t) +(* H2: RemoveChild(parent, child) when child is not in that parent's + list must not add the child to the root set. Doing so duplicates it + in preorder (parent(3)=1 still, but 3 is also a root). *) +let bounded_preorder t = + let ids = ref [] in + let n = ref 0 in + (try + Lui_store.iter_preorder t (fun id -> + incr n; + if !n > 32 then failwith "preorder loop"; + ids := id :: !ids) + with Failure _ -> ()); + List.rev !ids + +let test_stale_remove_child_keeps_real_parent () = + let t = Lui_store.create () in + apply t + [ CreateNode (1, Column); CreateNode (2, Column); CreateNode (3, Text); + InsertChild (1, 3, 0) ]; + Lui_store.apply_op t (RemoveChild (2, 3)); + check (option int) "parent of 3 stays 1" (Some 1) (Lui_store.parent t 3); + check (list int) "child stays under 1" [3] (Lui_store.child_ids t 1); + check (list int) "roots do not gain 3" [1; 2] (Lui_store.root_ids t); + check (list int) "preorder visits 3 once" [1; 3; 2] (bounded_preorder t) + (* Recreate must drop the previous subtree: otherwise children of the old node stay in the table with parent=Some id while the replacement starts empty. *) @@ -425,6 +450,8 @@ let () = test_case "recreate drops subtree" `Quick test_create_replaces_drops_subtree; test_case "roots" `Quick test_root_ids; + test_case "stale RemoveChild keeps real parent" `Quick + test_stale_remove_child_keeps_real_parent; test_case "preorder allocation" `Quick test_preorder_allocation; test_case "streamed preorder order" `Quick test_iter_preorder_order; test_case "kind lookup allocation" `Quick test_kind_lookup_allocation; From 4436b24939a911c1e7a707e24aa96cfdc7a95db7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:34:01 +0000 Subject: [PATCH 02/13] Validate native store batches atomically and resync on rejection. Cyclic inserts, stale RemoveChild, and structural ops on missing nodes now fail the whole batch with op context, leaving the tree unchanged. Lui_host.apply_batch returns false so the runtime can resync, matching Web and Android. Property writes to missing ids stay ignored. Co-authored-by: Tienson Qin --- docs/api-migrations.md | 23 +++ platform/native/e2e/test/e2e_test.ml | 22 +-- platform/native/lui_host/lui_host.ml | 59 ++++---- platform/native/lui_host/lui_host.mli | 5 +- .../native/lui_host/test/lui_host_test.ml | 16 ++ platform/native/lui_store/lui_store.ml | 141 ++++++++++++++++++ platform/native/lui_store/lui_store.mli | 6 + .../native/lui_store/test/lui_store_test.ml | 76 ++++++++-- 8 files changed, 299 insertions(+), 49 deletions(-) diff --git a/docs/api-migrations.md b/docs/api-migrations.md index 422c7504..4064c991 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -4060,3 +4060,26 @@ unknown values; they no longer raise `Invalid_argument`. Mounting `split ~sizing_signal` no longer emits a spurious default `ProgressValue 0.5` before the signal's first value. Mode transitions remove only properties belonging to the previous sizing mode. + +## Native store: structural patch validation + +`Lui_store.apply_batch` validates a batch against a structural shadow before +mutating the live tree. Cyclic `InsertChild`/`MoveChild`, `RemoveChild` that +names the wrong parent, and structural ops on missing nodes raise +`Invalid_argument` with op index and a `describe_op` prefix (`patch op N (...): +...`). The store is unchanged so `Lui_host.apply_batch` can return `false` and +the runtime can resync, matching Web and Android. Property ops on missing +nodes are still ignored. `apply_op` stays lenient except that inserting a node +under itself or an ancestor raises to keep `iter_preorder` acyclic. + +```ocaml +(* Before: InsertChild (2, 1) after InsertChild (1, 2) closed a cycle and + hung iter_preorder; RemoveChild (2, 3) while parent(3)=1 duplicated 3 in + the root set. *) +Lui_store.apply_batch store { generation; ops = [ InsertChild (2, 1, 0) ] } + +(* After: the batch raises and the tree is unchanged. Hosts treat the + failure as a rejected batch: + Lui_host.apply_batch host batch (* false *) +*) +``` diff --git a/platform/native/e2e/test/e2e_test.ml b/platform/native/e2e/test/e2e_test.ml index 491b28ad..35d24588 100644 --- a/platform/native/e2e/test/e2e_test.ml +++ b/platform/native/e2e/test/e2e_test.ml @@ -400,17 +400,19 @@ let test_real_layout_positions () = let test_invalid_ops_ignored () = let h = E2e_harness.create () in let store = E2e_harness.store h in - ignore (E2e_harness.apply_batch h - { generation = 1; - ops = - [ SetProp (99, Gap, IntValue 4); - RemoveProp (99, Gap); - InsertChild (1, 99, 0); - RemoveChild (1, 99); - DropNode 99 ] }); + let accepted = + E2e_harness.apply_batch h + { generation = 1; + ops = + [ SetProp (99, Gap, IntValue 4); + RemoveProp (99, Gap); + InsertChild (1, 99, 0); + RemoveChild (1, 99); + DropNode 99 ] } + in + check bool "structural ops on missing ids reject the batch" false accepted; check int "empty store" 0 (Lui_store.node_count store); - (* DropNode on a nonexistent id touches nothing — not even the - dirty set. *) + (* Rejection rolls the batch back — nothing is dirtied. *) check int "nothing dirtied" 0 (List.length (Lui_store.drain_dirty store)) diff --git a/platform/native/lui_host/lui_host.ml b/platform/native/lui_host/lui_host.ml index ebe1856c..61c38e5a 100644 --- a/platform/native/lui_host/lui_host.ml +++ b/platform/native/lui_host/lui_host.ml @@ -323,44 +323,51 @@ let set_reduced_motion h enabled = end let apply_batch ?(animate_resize=true) h (batch : Lui_protocol.patch_batch) = - let previous = Hashtbl.create 4 and created = Hashtbl.create 4 in + let previous = Hashtbl.create 4 and created = Hashtbl.create 4 + and drop_motion = Hashtbl.create 4 in List.iter (function | Lui_protocol.CreateNode (id,_) | CreateExtension (id,_,_) | DropNode id | DetachSubtree id -> Hashtbl.replace created id (); - Hashtbl.remove previous id; - Hashtbl.remove h.transitions id + Hashtbl.replace drop_motion id (); + Hashtbl.remove previous id | RemoveProp (id, (ProgressValue | FirstPaneWidth)) -> - Hashtbl.remove previous id; - Hashtbl.remove h.transitions id + Hashtbl.replace drop_motion id (); + Hashtbl.remove previous id | SetProp (id, ((ProgressValue | FirstPaneWidth) as property), _) when not (Hashtbl.mem created id) && Lui_store.kind h.store id = "split" -> - if not animate_resize then Hashtbl.remove h.transitions id; + if not animate_resize then Hashtbl.replace drop_motion id (); if not (Hashtbl.mem previous id) then (match Lui_store.float_prop h.store id (Lui_wire_schema.property_name property) with | Some value -> Hashtbl.add previous id (property, value) | None -> ()) | _ -> ()) batch.ops; - Lui_store.apply_batch h.store batch; - Hashtbl.filter_map_inplace (fun id motion -> - if Lui_store.mem h.store id then Some motion else None) h.transitions; - Hashtbl.iter (fun id (property, from_value) -> - match h.animation_time, Lui_store.prop h.store id (Lui_wire_schema.property_name property) with - | Some started, Some (Lui_protocol.FloatValue target) - when animate_resize && not h.reduced_motion && from_value <> target -> - let duration = Option.value ~default:0 (Lui_store.int_prop h.store id "resize-duration") in - if duration > 0 then begin - let easing = Option.value ~default:"standard" - (Lui_store.string_prop h.store id "resize-easing") in - (match Hashtbl.find_opt h.transitions id with - | Some motion when motion.property = property && motion.target = target -> () - | _ -> Hashtbl.replace h.transitions id - {property;from_value;target;started;duration=float duration /. 1000.;easing}); - set_transition_value h id property from_value - end else Hashtbl.remove h.transitions id - | _ -> Hashtbl.remove h.transitions id) previous; - h.dirty <- true; - true + match + try Lui_store.apply_batch h.store batch; Ok () + with Invalid_argument _ -> Error () + with + | Error () -> false + | Ok () -> + Hashtbl.iter (fun id () -> Hashtbl.remove h.transitions id) drop_motion; + Hashtbl.filter_map_inplace (fun id motion -> + if Lui_store.mem h.store id then Some motion else None) h.transitions; + Hashtbl.iter (fun id (property, from_value) -> + match h.animation_time, Lui_store.prop h.store id (Lui_wire_schema.property_name property) with + | Some started, Some (Lui_protocol.FloatValue target) + when animate_resize && not h.reduced_motion && from_value <> target -> + let duration = Option.value ~default:0 (Lui_store.int_prop h.store id "resize-duration") in + if duration > 0 then begin + let easing = Option.value ~default:"standard" + (Lui_store.string_prop h.store id "resize-easing") in + (match Hashtbl.find_opt h.transitions id with + | Some motion when motion.property = property && motion.target = target -> () + | _ -> Hashtbl.replace h.transitions id + {property;from_value;target;started;duration=float duration /. 1000.;easing}); + set_transition_value h id property from_value + end else Hashtbl.remove h.transitions id + | _ -> Hashtbl.remove h.transitions id) previous; + h.dirty <- true; + true let resize h ~width ~height ~scale = h.width <- width; diff --git a/platform/native/lui_host/lui_host.mli b/platform/native/lui_host/lui_host.mli index ff866396..12326cf1 100644 --- a/platform/native/lui_host/lui_host.mli +++ b/platform/native/lui_host/lui_host.mli @@ -172,7 +172,10 @@ val invalidate_paint : t -> unit a stable store). *) val apply_batch : ?animate_resize:bool -> t -> Lui_protocol.patch_batch -> bool -(** [animate_resize=false] applies direct manipulation feedback, including +(** Returns [false] when the store rejects the batch (cycles, ops on + missing nodes, wrong parent) so the runtime can resync. The store + is left unchanged on rejection. + [animate_resize=false] applies direct manipulation feedback, including dependent split corrections, without scheduling eased catch-up. *) val resize : t -> width:int -> height:int -> scale:float -> unit diff --git a/platform/native/lui_host/test/lui_host_test.ml b/platform/native/lui_host/test/lui_host_test.ml index 04907030..02aef87b 100644 --- a/platform/native/lui_host/test/lui_host_test.ml +++ b/platform/native/lui_host/test/lui_host_test.ml @@ -34,6 +34,21 @@ let test_apply_marks_dirty () = check bool "dirty after apply" true (Lui_host.wants_repaint h); check int "nodes" 2 (Lui_store.node_count (Lui_host.store h)) +let test_invalid_batch_rejected () = + let h = make () in + check bool "mount" true + (Lui_host.apply_batch h + (batch [ CreateNode (1, Column); CreateNode (2, Text); InsertChild (1, 2, 0) ])); + ignore (Lui_host.repaint h); + check bool "cycle rejected" false + (Lui_host.apply_batch h (batch [ InsertChild (2, 1, 0) ])); + check (option int) "tree unchanged" None + (Lui_store.parent (Lui_host.store h) 1); + check bool "stale remove rejected" false + (Lui_host.apply_batch h (batch [ RemoveChild (2, 1) ])); + check (list int) "child still under 1" [2] + (Lui_store.child_ids (Lui_host.store h) 1) + let test_repaint_clears_dirty () = let h = make () in ignore @@ -600,6 +615,7 @@ let () = ( "dirty", [ test_case "create clean" `Quick test_create_clean; test_case "apply marks dirty" `Quick test_apply_marks_dirty; + test_case "invalid batch rejected" `Quick test_invalid_batch_rejected; test_case "repaint clears" `Quick test_repaint_clears_dirty; test_case "resize marks dirty" `Quick test_resize_marks_dirty; test_case "set_hidden marks dirty" `Quick test_set_hidden_marks_dirty diff --git a/platform/native/lui_store/lui_store.ml b/platform/native/lui_store/lui_store.ml index 1ba457c8..4693a062 100644 --- a/platform/native/lui_store/lui_store.ml +++ b/platform/native/lui_store/lui_store.ml @@ -390,6 +390,18 @@ let rec drop t id = Hashtbl.remove t.roots id; mark_dirty_mask t id (d_structure lor d_paint) +(* Walk [id]'s parent chain looking for [ancestor]. Used to reject + InsertChild/MoveChild ops that would close a cycle and hang + [iter_preorder]. *) +let rec is_ancestor t ancestor id = + match Hashtbl.find_opt t.nodes id with + | Some n -> ( + match n.parent with + | Some p when p = ancestor -> true + | Some p -> is_ancestor t ancestor p + | None -> false) + | None -> false + (* Insert child at index within parent, detaching it from its previous parent first so a move never leaves the id listed twice. @@ -397,6 +409,8 @@ let rec drop t id = the target's children: [c.parent = None] means the id sits in no child list, so the common first-attach path skips any walk. *) let insert_child t parent child index = + if parent = child || is_ancestor t child parent then + invalid_arg "child insertion would create a cycle"; match (Hashtbl.find_opt t.nodes parent, Hashtbl.find_opt t.nodes child) with | Some p, Some c -> (match c.parent with @@ -547,7 +561,134 @@ let apply_op t = function | _ -> ()); journal_push t op +let describe_op = function + | CreateNode (node, _) -> Printf.sprintf "CreateNode %d" node + | CreateExtension (node, identifier, _) -> + Printf.sprintf "CreateExtension %d %s" node identifier + | DropNode node -> Printf.sprintf "DropNode %d" node + | DetachSubtree node -> Printf.sprintf "DetachSubtree node=%d" node + | SetProp (node, _, _) -> Printf.sprintf "SetProp %d" node + | RemoveProp (node, _) -> Printf.sprintf "RemoveProp %d" node + | SetExtensionProp (node, property, _) -> + Printf.sprintf "SetExtensionProp %d %s" node property + | RemoveExtensionProp (node, property) -> + Printf.sprintf "RemoveExtensionProp %d %s" node property + | InsertChild (parent, child, index) -> + Printf.sprintf "InsertChild parent=%d child=%d index=%d" parent child index + | RemoveChild (parent, child) -> + Printf.sprintf "RemoveChild parent=%d child=%d" parent child + | MoveChild (parent, child, index) -> + Printf.sprintf "MoveChild parent=%d child=%d index=%d" parent child index + +(* Lightweight structural shadow used to validate a batch before any + live node is mutated. Property ops on missing ids stay silent (the + web store's rule); structural ops on missing/detached/wrong-parent + nodes and cycle-forming inserts fail the whole batch so the host + can resync. *) +type shadow = { + parent : (int, int option) Hashtbl.t; + children : (int, int list) Hashtbl.t; +} + +let shadow_of t = + let parent = Hashtbl.create (Hashtbl.length t.nodes) in + let children = Hashtbl.create (Hashtbl.length t.nodes) in + Hashtbl.iter + (fun id n -> + Hashtbl.replace parent id n.parent; + Hashtbl.replace children id (map_children (fun c -> c.id) n)) + t.nodes; + { parent; children } + +let shadow_exists sh id = Hashtbl.mem sh.parent id + +let shadow_children sh id = + match Hashtbl.find_opt sh.children id with Some xs -> xs | None -> [] + +let rec shadow_is_ancestor sh ancestor id = + match Hashtbl.find_opt sh.parent id with + | Some (Some p) when p = ancestor -> true + | Some (Some p) -> shadow_is_ancestor sh ancestor p + | _ -> false + +let rec drop_shadow sh id = + if shadow_exists sh id then begin + (match Hashtbl.find sh.parent id with + | Some pid -> + Hashtbl.replace sh.children pid + (List.filter (( <> ) id) (shadow_children sh pid)) + | None -> ()); + List.iter (drop_shadow sh) (shadow_children sh id); + Hashtbl.remove sh.parent id; + Hashtbl.remove sh.children id + end + +let unlink_shadow sh child = + match Hashtbl.find_opt sh.parent child with + | Some (Some pid) -> + Hashtbl.replace sh.children pid + (List.filter (( <> ) child) (shadow_children sh pid)); + Hashtbl.replace sh.parent child None + | _ -> () + +let insert_at_list xs child index = + let xs = List.filter (( <> ) child) xs in + let rec go i = function + | rest when i <= 0 -> child :: rest + | [] -> [ child ] + | x :: rest -> x :: go (i - 1) rest + in + go index xs + +let insert_shadow sh parent child index = + unlink_shadow sh child; + Hashtbl.replace sh.children parent + (insert_at_list (shadow_children sh parent) child index); + Hashtbl.replace sh.parent child (Some parent) + +let fail_op index op msg = + invalid_arg + (Printf.sprintf "patch op %d (%s): %s" index (describe_op op) msg) + +let validate_batch t (batch : patch_batch) = + let sh = shadow_of t in + List.iteri + (fun op_index op -> + match op with + | CreateNode (id, _) | CreateExtension (id, _, _) -> + if shadow_exists sh id then drop_shadow sh id; + Hashtbl.replace sh.parent id None; + Hashtbl.replace sh.children id [] + | DropNode id | DetachSubtree id -> + if not (shadow_exists sh id) then fail_op op_index op "unknown node"; + drop_shadow sh id + | SetProp (id, _, _) + | RemoveProp (id, _) + | SetExtensionProp (id, _, _) + | RemoveExtensionProp (id, _) -> + (* Missing-node property writes are moot; dropping them keeps + one stale write from aborting the rest of the batch. *) + ignore id + | InsertChild (parent, child, at) | MoveChild (parent, child, at) -> + if not (shadow_exists sh parent) then + fail_op op_index op "unknown parent"; + if not (shadow_exists sh child) then + fail_op op_index op "unknown child"; + if parent = child || shadow_is_ancestor sh child parent then + fail_op op_index op "child insertion would create a cycle"; + insert_shadow sh parent child at + | RemoveChild (parent, child) -> + if not (shadow_exists sh parent) then + fail_op op_index op "unknown parent"; + if not (shadow_exists sh child) then + fail_op op_index op "unknown child"; + if not (List.mem child (shadow_children sh parent)) then + fail_op op_index op "child is not attached to parent"; + unlink_shadow sh child) + batch.ops + let apply_batch t (batch : patch_batch) = + validate_batch t batch; t.generation <- batch.generation; List.iter (apply_op t) batch.ops diff --git a/platform/native/lui_store/lui_store.mli b/platform/native/lui_store/lui_store.mli index e769b03f..98139c8a 100644 --- a/platform/native/lui_store/lui_store.mli +++ b/platform/native/lui_store/lui_store.mli @@ -61,6 +61,12 @@ val node_revision : node -> int val node_child_count : node -> int val apply_batch : t -> Lui_protocol.patch_batch -> unit +(** Apply [batch] atomically. Structural ops that name missing nodes, + remove a child from the wrong parent, or would create a parent-cycle + raise [Invalid_argument] with op context and leave the store + unchanged so the host can reject the batch and resync. Property ops + on missing nodes are ignored. *) + val apply_op : t -> Lui_protocol.patch_op -> unit (* Incremental invalidation: ids touched since the last drain, diff --git a/platform/native/lui_store/test/lui_store_test.ml b/platform/native/lui_store/test/lui_store_test.ml index ab6380ca..6bee6f73 100644 --- a/platform/native/lui_store/test/lui_store_test.ml +++ b/platform/native/lui_store/test/lui_store_test.ml @@ -226,18 +226,7 @@ let test_root_ids () = InsertChild (1, 9, 0) ]; check (list int) "roots" [1; 2] (Lui_store.root_ids t) -let test_ops_on_missing_are_ignored () = - let t = Lui_store.create () in - apply t - [ SetProp (99, TextValue, StringValue "x"); - InsertChild (99, 98, 0); - RemoveChild (99, 98); - DropNode 99 ]; - check int "still empty" 0 (Lui_store.node_count t) - -(* H2: RemoveChild(parent, child) when child is not in that parent's - list must not add the child to the root set. Doing so duplicates it - in preorder (parent(3)=1 still, but 3 is also a root). *) +(* Bound preorder so a cycle cannot hang the suite. *) let bounded_preorder t = let ids = ref [] in let n = ref 0 in @@ -249,6 +238,64 @@ let bounded_preorder t = with Failure _ -> ()); List.rev !ids +let test_ops_on_missing_are_ignored () = + let t = Lui_store.create () in + apply t [ SetProp (99, TextValue, StringValue "x"); RemoveProp (99, TextValue) ]; + check int "still empty" 0 (Lui_store.node_count t) + +let test_structural_ops_on_missing_fail_atomically () = + let t = Lui_store.create () in + apply t [ CreateNode (1, Column); CreateNode (2, Text) ]; + check_raises "unknown child" + (Invalid_argument + "patch op 0 (InsertChild parent=1 child=99 index=0): unknown child") + (fun () -> apply t [ InsertChild (1, 99, 0); SetProp (2, TextValue, StringValue "x") ]); + check (option string) "later SetProp not applied" None + (Lui_store.string_prop t 2 "text"); + check (list int) "tree unchanged" [1; 2] (bounded_preorder t); + check_raises "unknown parent" + (Invalid_argument + "patch op 0 (RemoveChild parent=99 child=2): unknown parent") + (fun () -> apply t [ RemoveChild (99, 2) ]); + check_raises "unknown drop" + (Invalid_argument "patch op 0 (DropNode 99): unknown node") + (fun () -> apply t [ DropNode 99; SetProp (2, TextValue, StringValue "y") ]); + check (option string) "drop batch did not apply props" None + (Lui_store.string_prop t 2 "text") + +let test_stale_remove_child_batch_rejected () = + let t = Lui_store.create () in + apply t + [ CreateNode (1, Column); CreateNode (2, Column); CreateNode (3, Text); + InsertChild (1, 3, 0) ]; + check_raises "wrong parent" + (Invalid_argument + "patch op 0 (RemoveChild parent=2 child=3): child is not attached to parent") + (fun () -> + apply t [ RemoveChild (2, 3); SetProp (3, TextValue, StringValue "x") ]); + check (option int) "parent of 3 stays 1" (Some 1) (Lui_store.parent t 3); + check (list int) "roots" [1; 2] (Lui_store.root_ids t); + check (list int) "preorder" [1; 3; 2] (bounded_preorder t); + check (option string) "text not applied" None (Lui_store.string_prop t 3 "text") + +let test_cyclic_insert_rejected () = + let t = Lui_store.create () in + apply t + [ CreateNode (1, Column); CreateNode (2, Column); InsertChild (1, 2, 0) ]; + check_raises "cycle" + (Invalid_argument + "patch op 0 (InsertChild parent=2 child=1 index=0): child insertion would create a cycle") + (fun () -> apply t [ InsertChild (2, 1, 0) ]); + check (option int) "1 remains a root" None (Lui_store.parent t 1); + check (list int) "preorder" [1; 2] (bounded_preorder t); + check_raises "self insert" + (Invalid_argument + "patch op 0 (InsertChild parent=1 child=1 index=0): child insertion would create a cycle") + (fun () -> apply t [ InsertChild (1, 1, 0) ]) + +(* H2: RemoveChild(parent, child) when child is not in that parent's + list must not add the child to the root set. Doing so duplicates it + in preorder (parent(3)=1 still, but 3 is also a root). *) let test_stale_remove_child_keeps_real_parent () = let t = Lui_store.create () in apply t @@ -452,6 +499,11 @@ let () = test_case "roots" `Quick test_root_ids; test_case "stale RemoveChild keeps real parent" `Quick test_stale_remove_child_keeps_real_parent; + test_case "stale RemoveChild batch rejected" `Quick + test_stale_remove_child_batch_rejected; + test_case "cyclic insert rejected" `Quick test_cyclic_insert_rejected; + test_case "structural ops on missing fail atomically" `Quick + test_structural_ops_on_missing_fail_atomically; test_case "preorder allocation" `Quick test_preorder_allocation; test_case "streamed preorder order" `Quick test_iter_preorder_order; test_case "kind lookup allocation" `Quick test_kind_lookup_allocation; From 391ceba18185632d07c4a24ed666afd16554fde8 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:53:36 +0000 Subject: [PATCH 03/13] Keep store batch validation linear and tolerate missing-parent inserts. Shadow child sets replace per-op list filters so wide RemoveChild batches stay O(n). InsertChild/MoveChild whose parent is not a node (including gallery parent=-1) is a no-op, matching apply_op. The incremental layout random test now mutates the leaf ids it created. Co-authored-by: Tienson Qin --- .../native/lui_layout/test/lui_layout_test.ml | 8 +- platform/native/lui_store/lui_store.ml | 90 ++++++++++--------- .../native/lui_store/test/lui_store_test.ml | 12 +++ 3 files changed, 63 insertions(+), 47 deletions(-) diff --git a/platform/native/lui_layout/test/lui_layout_test.ml b/platform/native/lui_layout/test/lui_layout_test.ml index b7f66a9d..a0059cd5 100644 --- a/platform/native/lui_layout/test/lui_layout_test.ml +++ b/platform/native/lui_layout/test/lui_layout_test.ml @@ -1469,10 +1469,10 @@ let test_incremental_random () = InsertChild (1, 2, 0); InsertChild (1, 3, 1) ] @ List.concat_map (fun i -> - [ CreateNode (10 + i, Text); - InsertChild ((if i mod 2 = 0 then 2 else 3), 10 + i, i / 2); - SetProp (10 + i, WidthValue, IntValue (10 + i)); - SetProp (10 + i, HeightValue, IntValue 10) ]) + [ CreateNode (i, Text); + InsertChild ((if i mod 2 = 0 then 2 else 3), i, (i - 10) / 2); + SetProp (i, WidthValue, IntValue i); + SetProp (i, HeightValue, IntValue 10) ]) leaves); let t = Lui_layout.create () in Lui_layout.sync ~width:200. ~height:100. t store; diff --git a/platform/native/lui_store/lui_store.ml b/platform/native/lui_store/lui_store.ml index 4693a062..4027fbe0 100644 --- a/platform/native/lui_store/lui_store.ml +++ b/platform/native/lui_store/lui_store.ml @@ -586,65 +586,66 @@ let describe_op = function nodes and cycle-forming inserts fail the whole batch so the host can resync. *) type shadow = { - parent : (int, int option) Hashtbl.t; - children : (int, int list) Hashtbl.t; + of_parent : (int, int option) Hashtbl.t; + of_children : (int, (int, unit) Hashtbl.t) Hashtbl.t; } +let empty_kids () = Hashtbl.create 4 + let shadow_of t = - let parent = Hashtbl.create (Hashtbl.length t.nodes) in - let children = Hashtbl.create (Hashtbl.length t.nodes) in + let of_parent = Hashtbl.create (Hashtbl.length t.nodes) in + let of_children = Hashtbl.create (Hashtbl.length t.nodes) in Hashtbl.iter (fun id n -> - Hashtbl.replace parent id n.parent; - Hashtbl.replace children id (map_children (fun c -> c.id) n)) + Hashtbl.replace of_parent id n.parent; + let kids = empty_kids () in + List.iter (fun cid -> Hashtbl.replace kids cid ()) + (map_children (fun c -> c.id) n); + Hashtbl.replace of_children id kids) t.nodes; - { parent; children } + { of_parent; of_children } + +let shadow_exists sh id = Hashtbl.mem sh.of_parent id -let shadow_exists sh id = Hashtbl.mem sh.parent id +let shadow_kids sh id = + match Hashtbl.find_opt sh.of_children id with + | Some h -> h + | None -> + let h = empty_kids () in + Hashtbl.replace sh.of_children id h; + h -let shadow_children sh id = - match Hashtbl.find_opt sh.children id with Some xs -> xs | None -> [] +let shadow_has_child sh parent child = Hashtbl.mem (shadow_kids sh parent) child let rec shadow_is_ancestor sh ancestor id = - match Hashtbl.find_opt sh.parent id with + match Hashtbl.find_opt sh.of_parent id with | Some (Some p) when p = ancestor -> true | Some (Some p) -> shadow_is_ancestor sh ancestor p | _ -> false let rec drop_shadow sh id = if shadow_exists sh id then begin - (match Hashtbl.find sh.parent id with - | Some pid -> - Hashtbl.replace sh.children pid - (List.filter (( <> ) id) (shadow_children sh pid)) + (match Hashtbl.find sh.of_parent id with + | Some pid -> Hashtbl.remove (shadow_kids sh pid) id | None -> ()); - List.iter (drop_shadow sh) (shadow_children sh id); - Hashtbl.remove sh.parent id; - Hashtbl.remove sh.children id + let kids = shadow_kids sh id in + let child_ids = Hashtbl.fold (fun cid _ acc -> cid :: acc) kids [] in + List.iter (drop_shadow sh) child_ids; + Hashtbl.remove sh.of_parent id; + Hashtbl.remove sh.of_children id end let unlink_shadow sh child = - match Hashtbl.find_opt sh.parent child with + match Hashtbl.find_opt sh.of_parent child with | Some (Some pid) -> - Hashtbl.replace sh.children pid - (List.filter (( <> ) child) (shadow_children sh pid)); - Hashtbl.replace sh.parent child None + Hashtbl.remove (shadow_kids sh pid) child; + Hashtbl.replace sh.of_parent child None | _ -> () -let insert_at_list xs child index = - let xs = List.filter (( <> ) child) xs in - let rec go i = function - | rest when i <= 0 -> child :: rest - | [] -> [ child ] - | x :: rest -> x :: go (i - 1) rest - in - go index xs - -let insert_shadow sh parent child index = +let insert_shadow sh parent child = unlink_shadow sh child; - Hashtbl.replace sh.children parent - (insert_at_list (shadow_children sh parent) child index); - Hashtbl.replace sh.parent child (Some parent) + Hashtbl.replace (shadow_kids sh parent) child (); + Hashtbl.replace sh.of_parent child (Some parent) let fail_op index op msg = invalid_arg @@ -657,8 +658,8 @@ let validate_batch t (batch : patch_batch) = match op with | CreateNode (id, _) | CreateExtension (id, _, _) -> if shadow_exists sh id then drop_shadow sh id; - Hashtbl.replace sh.parent id None; - Hashtbl.replace sh.children id [] + Hashtbl.replace sh.of_parent id None; + Hashtbl.replace sh.of_children id (empty_kids ()) | DropNode id | DetachSubtree id -> if not (shadow_exists sh id) then fail_op op_index op "unknown node"; drop_shadow sh id @@ -670,19 +671,22 @@ let validate_batch t (batch : patch_batch) = one stale write from aborting the rest of the batch. *) ignore id | InsertChild (parent, child, at) | MoveChild (parent, child, at) -> - if not (shadow_exists sh parent) then - fail_op op_index op "unknown parent"; if not (shadow_exists sh child) then fail_op op_index op "unknown child"; - if parent = child || shadow_is_ancestor sh child parent then - fail_op op_index op "child insertion would create a cycle"; - insert_shadow sh parent child at + (* Missing parent is a no-op, matching apply_op: CreateNode + leaves the id in the root set (gallery uses parent=-1). *) + if shadow_exists sh parent then begin + if parent = child || shadow_is_ancestor sh child parent then + fail_op op_index op "child insertion would create a cycle"; + ignore at; + insert_shadow sh parent child + end | RemoveChild (parent, child) -> if not (shadow_exists sh parent) then fail_op op_index op "unknown parent"; if not (shadow_exists sh child) then fail_op op_index op "unknown child"; - if not (List.mem child (shadow_children sh parent)) then + if not (shadow_has_child sh parent child) then fail_op op_index op "child is not attached to parent"; unlink_shadow sh child) batch.ops diff --git a/platform/native/lui_store/test/lui_store_test.ml b/platform/native/lui_store/test/lui_store_test.ml index 6bee6f73..08033c39 100644 --- a/platform/native/lui_store/test/lui_store_test.ml +++ b/platform/native/lui_store/test/lui_store_test.ml @@ -243,6 +243,16 @@ let test_ops_on_missing_are_ignored () = apply t [ SetProp (99, TextValue, StringValue "x"); RemoveProp (99, TextValue) ]; check int "still empty" 0 (Lui_store.node_count t) +(* Gallery (and the runtime overlay) emit InsertChild(-1, root, 0). The + parent is not a node; apply_op already no-ops, and apply_batch must + too instead of aborting the mount. *) +let test_insert_under_missing_parent_is_noop () = + let t = Lui_store.create () in + apply t [ CreateNode (1, Column); InsertChild (-1, 1, 0) ]; + check (list int) "stays a root" [1] (Lui_store.root_ids t); + check (option int) "no parent" None (Lui_store.parent t 1); + check (list int) "preorder" [1] (bounded_preorder t) + let test_structural_ops_on_missing_fail_atomically () = let t = Lui_store.create () in apply t [ CreateNode (1, Column); CreateNode (2, Text) ]; @@ -504,6 +514,8 @@ let () = test_case "cyclic insert rejected" `Quick test_cyclic_insert_rejected; test_case "structural ops on missing fail atomically" `Quick test_structural_ops_on_missing_fail_atomically; + test_case "insert under missing parent is a no-op" `Quick + test_insert_under_missing_parent_is_noop; test_case "preorder allocation" `Quick test_preorder_allocation; test_case "streamed preorder order" `Quick test_iter_preorder_order; test_case "kind lookup allocation" `Quick test_kind_lookup_allocation; From 3ffef5d514e4795ca53ba0e57acf30c464ec831d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:53:43 +0000 Subject: [PATCH 04/13] Fold clock delays into native wait timeouts and honor toast duration. Lui_window.fold_wait_ms combines the UI input deadline, Lui_clock.next_delay, and an optional host-wants-frame wake. Toast nodes with a duration prop schedule Dismiss from store_changed and wake next_input_deadline, replacing the demo toast_since workaround. Idle store_changed stays allocation-quiet when no toasts are live. Co-authored-by: Tienson Qin --- platform/native/lui_window/lui_window.ml | 61 ++++++++++++++++++- platform/native/lui_window/lui_window.mli | 15 ++++- .../native/lui_window/test/lui_window_test.ml | 50 ++++++++++++++- 3 files changed, 122 insertions(+), 4 deletions(-) diff --git a/platform/native/lui_window/lui_window.ml b/platform/native/lui_window/lui_window.ml index ceba7d16..0412c421 100644 --- a/platform/native/lui_window/lui_window.ml +++ b/platform/native/lui_window/lui_window.ml @@ -91,6 +91,15 @@ let parse_args argv = in loop default_config argv +let fold_wait_ms ~ui_deadline_ms ~clock_delay_ms ~host_wants_frame ~now_ms = + let wait = ui_deadline_ms -. now_ms in + let wait = + match clock_delay_ms with + | None -> wait + | Some delay -> Float.min wait delay + in + if host_wants_frame then Float.min wait 0. else wait + module Input = struct type key = | Return @@ -562,6 +571,8 @@ module Ui = struct mutable scroll : Lui_scroll.state; mutable scroll_clock_ms : float; scrollbar_deadlines : (int, float) Hashtbl.t; + toast_deadlines : (int, float) Hashtbl.t; + mutable toast_rev : int; mutable drag : Lui_drag.state; mutable overlay : Lui_overlay.state; mutable menu_scope : int; @@ -616,6 +627,8 @@ module Ui = struct scroll = Lui_scroll.initial; scroll_clock_ms = 0.; scrollbar_deadlines = Hashtbl.create 8; + toast_deadlines = Hashtbl.create 8; + toast_rev = -1; drag = Lui_drag.initial; overlay = Lui_overlay.initial; menu_scope = 0; menu_typeahead_deadline = 0.; menu_reveal = 0; menu_picker = 0; pending_picker = None; @@ -712,8 +725,11 @@ module Ui = struct let initial = if t.menu_typeahead_deadline > 0. then t.menu_typeahead_deadline else Float.infinity in + let initial = + Hashtbl.fold (fun _ deadline next -> Float.min deadline next) + t.scrollbar_deadlines initial in Hashtbl.fold (fun _ deadline next -> Float.min deadline next) - t.scrollbar_deadlines initial + t.toast_deadlines initial (* Install the next scroll state, recording which containers' offsets moved (a moved offset shifts every descendant rect, so @@ -1077,7 +1093,48 @@ module Ui = struct t.sync_rects_epoch <- t.rects_epoch; take_blur_actions t store @ List.rev !acts - let store_changed t store = sync t store + let schedule_toasts t store = + let live = Hashtbl.create 8 in + Lui_store.iter_preorder store (fun id -> + if Lui_store.kind store id = "toast" then begin + Hashtbl.replace live id (); + if not (Hashtbl.mem t.toast_deadlines id) then + match Lui_store.int_prop store id "duration" with + | Some ms when ms > 0 -> + Hashtbl.replace t.toast_deadlines id + (t.scroll_clock_ms +. float ms) + | _ -> () + end); + Hashtbl.filter_map_inplace + (fun id deadline -> if Hashtbl.mem live id then Some deadline else None) + t.toast_deadlines + + let toast_actions t store = + let now = t.scroll_clock_ms in + let acts = ref [] in + Hashtbl.filter_map_inplace + (fun id deadline -> + if deadline > now then Some deadline + else if + Lui_store.mem store id + && event_supported store id (Dismiss id) + then begin + acts := Dispatch (Dismiss id) :: !acts; + None + end + else None) + t.toast_deadlines; + List.rev !acts + + let store_changed t store = + let acts = sync t store in + let rev = Lui_store.revision store in + if rev <> t.toast_rev then begin + schedule_toasts t store; + t.toast_rev <- rev + end; + if Hashtbl.length t.toast_deadlines = 0 then acts + else acts @ toast_actions t store let resizing_split t = Lui_pointer.resizing t.pointer diff --git a/platform/native/lui_window/lui_window.mli b/platform/native/lui_window/lui_window.mli index daf218fe..85af878f 100644 --- a/platform/native/lui_window/lui_window.mli +++ b/platform/native/lui_window/lui_window.mli @@ -30,6 +30,17 @@ val default_config : config [--drop-file PATH], [--windows N]. *) val parse_args : string list -> (config, string) result +val fold_wait_ms : + ui_deadline_ms:float -> + clock_delay_ms:float option -> + host_wants_frame:bool -> + now_ms:float -> + float +(** Milliseconds the host should block for the next event: the soonest of + the UI deadline, the application-clock delay from {!Lui_clock.next_delay}, + and zero when a frame is due. [clock_delay_ms] of [None] means the clock + has nothing pending. *) + (** Backend-neutral input events. [main.ml] translates SDL events into these; [Ui.handle] maps them to LUI protocol events. Coordinates are in logical (window) pixels. *) @@ -463,7 +474,9 @@ module Ui : sig val scrollbar_visible : t -> int -> bool val next_input_deadline : t -> float - (** True while a container's overlay scrollbar is active. Hidden at rest. *) + (** Soonest of the scrollbar-hide, menu-typeahead and toast-duration + deadlines, or [infinity] when none are pending. Hosts fold this + into their event-wait timeout. *) val set_scroll_cap_x : t -> int -> float -> unit diff --git a/platform/native/lui_window/test/lui_window_test.ml b/platform/native/lui_window/test/lui_window_test.ml index d38dc49a..9543d5cb 100644 --- a/platform/native/lui_window/test/lui_window_test.ml +++ b/platform/native/lui_window/test/lui_window_test.ml @@ -2839,6 +2839,50 @@ let test_file_drop () = | Some (3, "text:hello", 3) -> true | _ -> false) +let test_fold_wait_ms () = + Alcotest.(check (float 0.001)) "ui deadline" + 40. + (fold_wait_ms ~ui_deadline_ms:140. ~clock_delay_ms:None + ~host_wants_frame:false ~now_ms:100.); + Alcotest.(check (float 0.001)) "clock sooner than ui" + 10. + (fold_wait_ms ~ui_deadline_ms:140. ~clock_delay_ms:(Some 10.) + ~host_wants_frame:false ~now_ms:100.); + Alcotest.(check (float 0.001)) "frame due wakes immediately" + 0. + (fold_wait_ms ~ui_deadline_ms:140. ~clock_delay_ms:(Some 10.) + ~host_wants_frame:true ~now_ms:100.); + Alcotest.(check bool) "idle with no timers waits forever" true + (Float.is_infinite + (fold_wait_ms ~ui_deadline_ms:Float.infinity ~clock_delay_ms:None + ~host_wants_frame:false ~now_ms:100.)) + +let test_toast_duration_dismiss () = + let s = + store_of + [ CreateNode (1, Root); + CreateNode (2, Toast); + InsertChild (1, 2, 0); + SetProp (2, DurationValue, IntValue 2500) ] + in + let ui = Ui.create () in + Ui.tick_scrollbars ui ~now_ms:1000.; + let dismiss acts = + List.filter_map + (function Dispatch (Dismiss id) -> Some id | _ -> None) + acts + in + Alcotest.(check (list int)) "no dismiss before duration" + [] (dismiss (Ui.store_changed ui s)); + Alcotest.(check (float 0.001)) "deadline is now + duration" + 3500. (Ui.next_input_deadline ui); + Ui.tick_scrollbars ui ~now_ms:3499.; + Alcotest.(check (list int)) "still open at deadline-1" + [] (dismiss (Ui.store_changed ui s)); + Ui.tick_scrollbars ui ~now_ms:3500.; + Alcotest.(check (list int)) "dismisses when duration elapses" + [ 2 ] (dismiss (Ui.store_changed ui s)) + let test_overlay_close () = let s = store_of @@ -4941,7 +4985,9 @@ let test_nested_split_pointer_resize () = let () = Alcotest.run "lui_window" - [ ("config", [ Alcotest.test_case "parse_args" `Quick test_parse_args ]); + [ ("config", + [ Alcotest.test_case "parse_args" `Quick test_parse_args; + Alcotest.test_case "fold_wait_ms" `Quick test_fold_wait_ms ]); ( "input", [ Alcotest.test_case "tables" `Quick test_input_tables ] ); ( "admission", @@ -5055,6 +5101,8 @@ let () = Alcotest.test_case "picker input ownership" `Quick test_picker_menu_input_ownership; Alcotest.test_case "menu row focus chrome" `Quick test_menu_row_focus_chrome; Alcotest.test_case "pointer scope" `Quick test_modal_pointer_scope; + Alcotest.test_case "toast duration dismiss" `Quick + test_toast_duration_dismiss; Alcotest.test_case "modal+esc" `Quick test_overlay_close; Alcotest.test_case "retained anchor scroll shift" `Quick test_retained_anchor_scroll_shift; Alcotest.test_case "retained popover input" `Quick test_retained_popover_input; From 6166d193d5b98782d2d295728c929267a4fb8140 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:53:51 +0000 Subject: [PATCH 05/13] Recreate only the Vulkan frame on resize and upload damaged rows. Lui_vulkan.resize rebuilds the offscreen image and framebuffer while keeping the instance, device and pipelines. changed_rows lets the Linux host upload a dirty row span instead of the whole readback. Shader fallback compile uses a private mkdtemp directory, cleans up on every path, and releases the OCaml runtime around fork/wait. Window swapchain present remains a follow-up. Co-authored-by: Tienson Qin --- platform/native/lui_vulkan/lui_vulkan.ml | 45 +++++- platform/native/lui_vulkan/lui_vulkan.mli | 13 ++ platform/native/lui_vulkan/lui_vulkan_stubs.c | 132 +++++++++++++++--- .../native/lui_vulkan/test/lui_vulkan_test.ml | 35 +++++ 4 files changed, 201 insertions(+), 24 deletions(-) diff --git a/platform/native/lui_vulkan/lui_vulkan.ml b/platform/native/lui_vulkan/lui_vulkan.ml index b228d11d..d0efbd40 100644 --- a/platform/native/lui_vulkan/lui_vulkan.ml +++ b/platform/native/lui_vulkan/lui_vulkan.ml @@ -18,6 +18,7 @@ type ctx external c_create : int -> int -> ctx = "lui_vk_create" external c_destroy : ctx -> unit = "lui_vk_destroy" +external c_resize : ctx -> int -> int -> unit = "lui_vk_resize" external c_driver : ctx -> string = "lui_vk_driver" external c_dual : ctx -> bool = "lui_vk_dual" external c_max_size : ctx -> int = "lui_vk_max_size" @@ -67,8 +68,8 @@ let texture () = { tex = -1; w = 0; h = 0; gen = 0; ver = -1; last_frame = 0 } type t = { ctx : ctx; - fw : int; (* the frame image's size, which scenes must match *) - fh : int; + mutable fw : int; (* the frame image's size, which scenes must match *) + mutable fh : int; dual : bool; compiler : bool; (* runtime GLSL compile is reachable *) max_size : int; @@ -470,6 +471,46 @@ let read_frame r ~w ~h = if w <= 0 || h <= 0 then Bytes.empty else c_read r.ctx +let resize r ~w ~h = + if w <= 0 || h <= 0 then Stdlib.Error "a size of 0 is not drawable" + else if w = r.fw && h = r.fh then Stdlib.Ok () + else + try + c_resize r.ctx w h; + r.fw <- w; + r.fh <- h; + Stdlib.Ok () + with Failure m -> Stdlib.Error m + +(* First/last changed row of a BGRA frame versus [prev], for uploading + only damaged rows through Lui_blit. [None] means the pixels match. *) +let changed_rows ~prev pix ~w ~h = + let stride = w * 4 in + let expect = stride * h in + if w <= 0 || h <= 0 || Bytes.length pix < expect then None + else if Bytes.length prev <> expect then Some (0, h) + else + let y0 = ref (-1) in + let y1 = ref (-1) in + for y = 0 to h - 1 do + let off = y * stride in + let changed = + let i = ref 0 in + let diff = ref false in + while not !diff && !i < stride do + if Bytes.unsafe_get prev (off + !i) <> Bytes.unsafe_get pix (off + !i) + then diff := true + else incr i + done; + !diff + in + if changed then begin + if !y0 < 0 then y0 := y; + y1 := y + 1 + end + done; + if !y0 < 0 then None else Some (!y0, !y1) + let release r = c_destroy r.ctx; r.effects <- []; diff --git a/platform/native/lui_vulkan/lui_vulkan.mli b/platform/native/lui_vulkan/lui_vulkan.mli index d113e8ff..566239ae 100644 --- a/platform/native/lui_vulkan/lui_vulkan.mli +++ b/platform/native/lui_vulkan/lui_vulkan.mli @@ -45,6 +45,19 @@ val render : t -> Lui_scene.t -> unit val release : t -> unit (** [release r] destroys the renderer's Vulkan objects. *) +val resize : t -> w:int -> h:int -> (unit, string) result +(** [resize r ~w ~h] recreates the offscreen frame image and + framebuffer at [w]×[h], keeping the instance, device and pipelines. + Window swapchain presentation is not implemented: the host still + reads the frame back and uploads it through {!Lui_blit}. *) + +val changed_rows : + prev:bytes -> bytes -> w:int -> h:int -> (int * int) option +(** [changed_rows ~prev pix ~w ~h] is [Some (y0, y1)] covering the + inclusive-exclusive rows of [pix] that differ from [prev], or + [None] when the frames match. A size mismatch treats the whole + frame as damaged. *) + val read_frame : t -> w:int -> h:int -> bytes (** [read_frame r ~w ~h] is the frame image's pixels: premultiplied BGRA rows, top row first. For tests and pixel-parity checks. *) diff --git a/platform/native/lui_vulkan/lui_vulkan_stubs.c b/platform/native/lui_vulkan/lui_vulkan_stubs.c index 3d41f4fa..17ad3f54 100644 --- a/platform/native/lui_vulkan/lui_vulkan_stubs.c +++ b/platform/native/lui_vulkan/lui_vulkan_stubs.c @@ -24,10 +24,14 @@ #include #include #include +#include #include #if defined(__linux__) +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif #include #include #include @@ -1439,6 +1443,54 @@ static int create_ctx(Ctx *c, int w, int h) return 0; } +static void destroy_frame(Ctx *c) +{ + if (!c->dev) + return; + c->vk.DeviceWaitIdle(c->dev); + if (c->frame_fb) + c->vk.DestroyFramebuffer(c->dev, c->frame_fb, NULL); + if (c->frame_view) + c->vk.DestroyImageView(c->dev, c->frame_view, NULL); + if (c->frame_img) + c->vk.DestroyImage(c->dev, c->frame_img, NULL); + if (c->frame_mem) + c->vk.FreeMemory(c->dev, c->frame_mem, NULL); + c->frame_fb = VK_NULL_HANDLE; + c->frame_view = VK_NULL_HANDLE; + c->frame_img = VK_NULL_HANDLE; + c->frame_mem = VK_NULL_HANDLE; +} + +static int create_frame(Ctx *c, int w, int h) +{ + VkCommandBufferBeginInfo bi; + c->frame_img = make_image(c, w, h, FMT_B8G8R8A8_UNORM, + VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | + VK_IMAGE_USAGE_TRANSFER_SRC_BIT); + if (!c->frame_img || image_mem(c, c->frame_img, &c->frame_mem)) + return fail(c, "frame image", -1); + c->frame_view = make_view(c, c->frame_img, FMT_B8G8R8A8_UNORM); + if (!c->frame_view) + return fail(c, "frame view", -1); + c->frame_fb = make_fb(c, c->frame_view, w, h); + if (!c->frame_fb) + return fail(c, "frame framebuffer", -1); + memset(&bi, 0, sizeof bi); + bi.sType = ST_COMMAND_BUFFER_BEGIN_INFO; + c->vk.ResetCommandBuffer(c->cmd, 0); + if (c->vk.BeginCommandBuffer(c->cmd, &bi)) + return fail(c, "vkBeginCommandBuffer", -1); + to_general(c, c->frame_img); + if (c->vk.EndCommandBuffer(c->cmd)) + return fail(c, "vkEndCommandBuffer", -1); + if (submit_and_wait(c, c->cmd)) + return -1; + c->w = w; + c->h = h; + return 0; +} + /* {2 Pipelines} */ static VkHandle make_shader(Ctx *c, const uint32_t *code, size_t len) @@ -1695,59 +1747,77 @@ static int compile_glsl(Ctx *c, const char *src, size_t slen, } } } } - /* a glslangValidator executable */ + /* a glslangValidator executable, in a private mkdtemp directory */ { const char *exe = getenv("LUI_VK_GLSLANG"); - char tmp_src[] = "/tmp/lui_vk_fxXXXXXX"; - char tmp_out[] = "/tmp/lui_vk_fxXXXXXX.spv"; + char dir[] = "/tmp/lui_vk_fxXXXXXX"; + char srcp[80], outp[80]; int fd = -1; - pid_t pid; + pid_t pid = -1; int st = 0; + int ok = 0; + srcp[0] = outp[0] = 0; if (!exe || !*exe) exe = "glslangValidator"; - fd = mkstemp(tmp_src); - if (fd < 0) + if (!mkdtemp(dir)) goto no; + snprintf(srcp, sizeof srcp, "%s/fx.frag", dir); + snprintf(outp, sizeof outp, "%s/fx.spv", dir); + fd = open(srcp, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0600); + if (fd < 0) + goto cleanup; if (write(fd, src, slen) != (ssize_t)slen) { close(fd); - goto no; + fd = -1; + goto cleanup; } close(fd); - memcpy(tmp_out, tmp_src, sizeof tmp_src); - strcat(tmp_out, ".spv"); + fd = -1; + caml_release_runtime_system(); pid = fork(); if (pid == 0) { - int devnull = open("/dev/null", 2); + int devnull = open("/dev/null", O_RDWR | O_CLOEXEC); if (devnull >= 0) { dup2(devnull, 1); dup2(devnull, 2); } - execlp(exe, exe, "-V", "-S", "frag", tmp_src, "-o", - tmp_out, (char *)NULL); + execlp(exe, exe, "-V", "-S", "frag", srcp, "-o", + outp, (char *)NULL); _exit(127); } if (pid < 0 || waitpid(pid, &st, 0) < 0 || !WIFEXITED(st) || - WEXITSTATUS(st) != 0) - goto no; - { FILE *f = fopen(tmp_out, "rb"); + WEXITSTATUS(st) != 0) { + caml_acquire_runtime_system(); + goto cleanup; + } + caml_acquire_runtime_system(); + { FILE *f = fopen(outp, "rb"); long n; unsigned char *res; if (!f) - goto no; + goto cleanup; fseek(f, 0, SEEK_END); n = ftell(f); fseek(f, 0, SEEK_SET); res = malloc((size_t)n); - if (!res || fread(res, 1, (size_t)n, f) != (size_t)n) { + if (!res || n <= 0 || fread(res, 1, (size_t)n, f) != (size_t)n) { fclose(f); free(res); - goto no; + goto cleanup; } fclose(f); *out = res; - *olen = (size_t)n; } - unlink(tmp_src); - unlink(tmp_out); - return 0; + *olen = (size_t)n; + ok = 1; } + cleanup: + if (fd >= 0) + close(fd); + if (srcp[0]) + unlink(srcp); + if (outp[0]) + unlink(outp); + rmdir(dir); + if (ok) + return 0; no: snprintf(c->err, sizeof c->err, "cannot compile effect GLSL: no libshaderc and no " @@ -1815,6 +1885,23 @@ CAMLprim value lui_vk_destroy(value ctx) CAMLreturn(Val_unit); } +CAMLprim value lui_vk_resize(value ctx, value w, value h) +{ + CAMLparam3(ctx, w, h); + Ctx *c = Ctx_val(ctx); + int nw = Int_val(w), nh = Int_val(h); + if (!c) + caml_failwith("lui_vk_resize: destroyed context"); + if (nw <= 0 || nh <= 0) + caml_failwith("lui_vk_resize: a size of 0 is not drawable"); + if (c->w == nw && c->h == nh) + CAMLreturn(Val_unit); + destroy_frame(c); + if (create_frame(c, nw, nh) < 0) + caml_failwith(c->err[0] ? c->err : "lui_vk_resize failed"); + CAMLreturn(Val_unit); +} + CAMLprim value lui_vk_driver(value ctx) { CAMLparam1(ctx); @@ -2307,6 +2394,7 @@ CAMLprim value lui_vk_compile(value ctx, value src) VK_STUB2(lui_vk_create) VK_STUB(lui_vk_destroy) +VK_STUB3(lui_vk_resize) VK_STUB(lui_vk_driver) VK_STUB(lui_vk_dual) VK_STUB(lui_vk_max_size) diff --git a/platform/native/lui_vulkan/test/lui_vulkan_test.ml b/platform/native/lui_vulkan/test/lui_vulkan_test.ml index ce3fc544..de79f48b 100644 --- a/platform/native/lui_vulkan/test/lui_vulkan_test.ml +++ b/platform/native/lui_vulkan/test/lui_vulkan_test.ml @@ -239,6 +239,39 @@ let test_atlas_plan () = | Vk.Nothing -> () | _ -> Alcotest.fail "a caught-up texture does nothing") +let test_changed_rows () = + let w, h = 4, 3 in + let stride = w * 4 in + let a = Bytes.make (stride * h) '\x11' in + Alcotest.(check (option (pair int int))) "size mismatch is whole frame" + (Some (0, h)) + (Vk.changed_rows ~prev:Bytes.empty a ~w ~h); + Alcotest.(check (option (pair int int))) "identical is none" + None + (Vk.changed_rows ~prev:a a ~w ~h); + let b = Bytes.copy a in + Bytes.set b (stride + 1) '\x22'; + Alcotest.(check (option (pair int int))) "middle row" + (Some (1, 2)) + (Vk.changed_rows ~prev:a b ~w ~h); + Bytes.set b (stride * 2) '\x33'; + Alcotest.(check (option (pair int int))) "span of changed rows" + (Some (1, 3)) + (Vk.changed_rows ~prev:a b ~w ~h) + +let test_resize_keeps_device () = + with_vk 16 16 (fun r -> + let name = Vk.driver r in + (match Vk.resize r ~w:24 ~h:20 with + | Ok () -> () + | Error e -> Alcotest.failf "resize: %s" e); + Alcotest.(check string) "device kept" name (Vk.driver r); + (match Vk.resize r ~w:24 ~h:20 with + | Ok () -> () + | Error e -> Alcotest.failf "idempotent resize: %s" e); + Hashtbl.remove renderers (16, 16); + Hashtbl.replace renderers (24, 20) r) + let test_effect_source () = let has_sub sub s = let n = String.length s and m = String.length sub in @@ -270,6 +303,8 @@ let () = [ Alcotest.test_case "instance packing" `Quick test_pack; Alcotest.test_case "scissor clamp" `Quick test_scissor; Alcotest.test_case "atlas plan" `Quick test_atlas_plan; + Alcotest.test_case "changed rows" `Quick test_changed_rows; + Alcotest.test_case "resize keeps device" `Quick test_resize_keeps_device; Alcotest.test_case "effect source" `Quick test_effect_source; Alcotest.test_case "driver" `Quick (fun () -> From 6b0657726e9ee45760016d30e6533251dda2c26d Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:53:58 +0000 Subject: [PATCH 06/13] Bound the resource worker pool and fail requests that cannot spawn. Lui_resource.create defaults to a small Domain pool instead of one spawn per load. request/reload mark the entry Failed when the executor raises, so a domain-cap error cannot leave it stuck Loading. Completions call an optional set_completion_wake so hosts can unblock an idle wait. Hosts that drain every frame land in a later commit. Co-authored-by: Tienson Qin --- platform/native/lui_resource/lui_resource.ml | 75 ++++++++++++++++--- platform/native/lui_resource/lui_resource.mli | 15 +++- .../lui_resource/test/lui_resource_test.ml | 39 +++++++++- platform/native/lui_window/dune | 1 + platform/native/lui_window_linux/dune | 1 + platform/native/lui_window_windows/dune | 3 +- 6 files changed, 118 insertions(+), 16 deletions(-) diff --git a/platform/native/lui_resource/lui_resource.ml b/platform/native/lui_resource/lui_resource.ml index 4c3e91f8..b21f4c51 100644 --- a/platform/native/lui_resource/lui_resource.ml +++ b/platform/native/lui_resource/lui_resource.ml @@ -47,6 +47,50 @@ type executor = (unit -> unit) -> unit let executor_immediate job = job () let executor_domain job = ignore (Domain.spawn job) +(* Completions are published on the foreground via {!drain}; this hook + wakes the host loop so drain runs promptly. Hosts install + [Lui_sdl3.run_on_main_thread] (or equivalent). *) +let completion_wake = ref (fun () -> ()) +let set_completion_wake f = completion_wake := f +let wake_completion () = try !completion_wake () with _ -> () + +let rec pool_worker q m c stop () = + Mutex.lock m; + let rec wait () = + if Atomic.get stop then Mutex.unlock m + else + match Queue.take_opt q with + | Some job -> + Mutex.unlock m; + (try job () with _ -> ()); + pool_worker q m c stop () + | None -> + Condition.wait c m; + wait () + in + wait () + +let executor_pool ?(workers = 4) () : executor = + let workers = max 1 workers in + let q = Queue.create () in + let m = Mutex.create () in + let c = Condition.create () in + let stop = Atomic.make false in + let _ = + Array.init workers (fun _ -> Domain.spawn (pool_worker q m c stop)) + in + fun job -> + Mutex.lock m; + Queue.push job q; + Condition.signal c; + Mutex.unlock m + +let default_pool_workers () = + let n = Domain.recommended_domain_count () in + max 1 (min 4 (max 1 (n - 1))) + +let default_executor = lazy (executor_pool ~workers:(default_pool_workers ()) ()) + let executor_queued () = let q = Queue.create () in let m = Mutex.create () in @@ -158,7 +202,7 @@ let drain c = List.iter (publish c) batch; List.length batch -let create ?(executor = executor_domain) ?(retire = fun _ -> ()) () = +let create ?(executor = Lazy.force default_executor) ?(retire = fun _ -> ()) () = incr last_cache_id; let c = { @@ -188,17 +232,24 @@ let spawn c e load = } in let generation = e.generation in - c.executor (fun () -> - let result = - try load task - with exn -> Error (Printexc.to_string exn) - in - Mutex.lock c.mutex; - Queue.push - { comp_entry = e; comp_generation = generation; comp_result = result } - c.mailbox; - Mutex.unlock c.mutex); - Some task + let run () = + let result = + try load task + with exn -> Error (Printexc.to_string exn) + in + Mutex.lock c.mutex; + Queue.push + { comp_entry = e; comp_generation = generation; comp_result = result } + c.mailbox; + Mutex.unlock c.mutex; + wake_completion () + in + try + c.executor run; + Some task + with exn -> + e.state <- Failed (Printexc.to_string exn); + None let add_watcher e scope = function | None -> () diff --git a/platform/native/lui_resource/lui_resource.mli b/platform/native/lui_resource/lui_resource.mli index eb6d877f..503ba9f6 100644 --- a/platform/native/lui_resource/lui_resource.mli +++ b/platform/native/lui_resource/lui_resource.mli @@ -70,9 +70,20 @@ type executor = (unit -> unit) -> unit val executor_immediate : executor (* Runs each job on a fresh {!Domain}. Loaders' C stubs that release - the runtime system (e.g. libcurl) run truly in parallel. *) + the runtime system (e.g. libcurl) run truly in parallel. Prefer + {!executor_pool} for production hosts: unbounded spawn can raise + past the domain cap. *) val executor_domain : executor +(* [executor_pool ~workers ()] is a bounded worker pool. The default + worker count is [min 4 (recommended_domains - 1)], at least 1. *) +val executor_pool : ?workers:int -> unit -> executor + +(* Hosts install a wake so a worker completion unblocks the event + loop (typically [Lui_sdl3.run_on_main_thread]). Completions still + publish only inside {!drain}. *) +val set_completion_wake : (unit -> unit) -> unit + (* [executor_queued ()] is [(exec, pump)]: [exec] queues jobs and [pump] runs every queued job, returning how many it ran. For tests and single-threaded hosts that want deterministic load timing. *) @@ -81,7 +92,7 @@ val executor_queued : unit -> executor * (unit -> int) type 'value t (* [create ?executor ?retire ()] is an empty cache. [executor] defaults - to {!executor_domain}. [retire v] releases a discarded value on the + to a bounded {!executor_pool}. [retire v] releases a discarded value on the foreground — called for stale/cancelled completions, for a [Ready] value replaced by a newer publish, and for [Ready] values dropped by {!invalidate}/{!clear}/scope dispose; use it to free external diff --git a/platform/native/lui_resource/test/lui_resource_test.ml b/platform/native/lui_resource/test/lui_resource_test.ml index 3b14ab6c..367128ec 100644 --- a/platform/native/lui_resource/test/lui_resource_test.ml +++ b/platform/native/lui_resource/test/lui_resource_test.ml @@ -279,6 +279,40 @@ let test_cooperative_cancel () = (* ---------- real domain executor ---------- *) +let test_executor_raise_marks_failed () = + let exec _ = failwith "domain cap" in + let c = R.create ~executor:exec () in + let sc = scope () in + let e = R.request c ~scope:sc "k" ~load:(fun _ -> Ok "v") in + (match R.state e with + | R.Failed msg -> + check' bool "failed after spawn raise" true + (String.length msg > 0) + | _ -> fail "expected Failed, not stuck Loading"); + check' (option string) "not published" None (R.get c "k"); + Signal.dispose_scope sc; + R.unregister c + +let test_pool_executor () = + let exec = R.executor_pool ~workers:2 () in + let c = R.create ~executor:exec () in + let sc = scope () in + let woke = ref false in + R.set_completion_wake (fun () -> woke := true); + let e = R.request c ~scope:sc "k" ~load:(fun _ -> Ok "pooled") in + let deadline = Unix.gettimeofday () +. 5. in + while R.pending c = 0 && Unix.gettimeofday () < deadline do + Unix.sleepf 0.001 + done; + check' bool "completed" true (R.pending c = 1); + check' bool "wake fired" true !woke; + ignore (R.drain c); + check' (option string) "ready" (Some "pooled") (R.get c "k"); + ignore e; + R.set_completion_wake (fun () -> ()); + Signal.dispose_scope sc; + R.unregister c + let test_domain_executor () = let c = R.create ~executor:R.executor_domain () in let sc = scope () in @@ -400,5 +434,8 @@ let () = test_late_watcher_fires_immediately; ] ); ( "executors", - [ test_case "domain" `Quick test_domain_executor ] ); + [ test_case "domain" `Quick test_domain_executor; + test_case "spawn raise marks failed" `Quick + test_executor_raise_marks_failed; + test_case "bounded pool" `Quick test_pool_executor ] ); ] diff --git a/platform/native/lui_window/dune b/platform/native/lui_window/dune index e1a1833e..2a97cd69 100644 --- a/platform/native/lui_window/dune +++ b/platform/native/lui_window/dune @@ -33,6 +33,7 @@ lui_blit lui_text lui_sdl3 + lui_resource ocaml-signal unix) (preprocess (pps lui_ppx)) diff --git a/platform/native/lui_window_linux/dune b/platform/native/lui_window_linux/dune index d955c342..925d6424 100644 --- a/platform/native/lui_window_linux/dune +++ b/platform/native/lui_window_linux/dune @@ -46,6 +46,7 @@ lui_gl lui_vulkan lui_sdl3 + lui_resource ocaml-signal unix)) diff --git a/platform/native/lui_window_windows/dune b/platform/native/lui_window_windows/dune index e41f2966..517c0838 100644 --- a/platform/native/lui_window_windows/dune +++ b/platform/native/lui_window_windows/dune @@ -41,7 +41,8 @@ lui_shell_windows lui_text_dwrite lui_raster - lui_d3d11)) + lui_d3d11 + lui_resource)) (rule (target c_libs.sexp) From 8619ca90b6ca865e95e2e5186aa78cb49adf8cbe Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:54:05 +0000 Subject: [PATCH 07/13] Make PTY spawn leak-safe and close idempotent. The Unix master fd gets FD_CLOEXEC. The spawn OOM path frees argv, env and the pty record before failing. OCaml close zeros the handle before c_close, and the stub clears mfd so a second close does not double-free. Co-authored-by: Tienson Qin --- .../native/lui_plugin_terminal/terminal_pty.ml | 6 ++++-- .../lui_plugin_terminal/terminal_pty_stubs.c | 17 +++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/platform/native/lui_plugin_terminal/terminal_pty.ml b/platform/native/lui_plugin_terminal/terminal_pty.ml index 05526d84..411d521f 100644 --- a/platform/native/lui_plugin_terminal/terminal_pty.ml +++ b/platform/native/lui_plugin_terminal/terminal_pty.ml @@ -2,7 +2,7 @@ propagation and exit detection. Unix uses forkpty; Windows a real ConPTY; elsewhere spawning raises [Failure]. *) -type t = { h : nativeint; mutable alive : bool } +type t = { mutable h : nativeint; mutable alive : bool } external c_spawn : string array -> string array -> string -> int -> int -> nativeint = "lui_term_pty_spawn" @@ -48,4 +48,6 @@ let kill t = let close t = if t.alive then kill t; - c_close t.h + let h = t.h in + t.h <- 0n; + if h <> 0n then c_close h diff --git a/platform/native/lui_plugin_terminal/terminal_pty_stubs.c b/platform/native/lui_plugin_terminal/terminal_pty_stubs.c index 74d27485..edcba3d4 100644 --- a/platform/native/lui_plugin_terminal/terminal_pty_stubs.c +++ b/platform/native/lui_plugin_terminal/terminal_pty_stubs.c @@ -320,7 +320,12 @@ CAMLprim value lui_term_pty_spawn(value vargv, value venv, value vcwd, if (!p) caml_failwith("pty: out of memory"); argv = strv(vargv); env = strv(venv); - if (!argv || !env) caml_failwith("pty: out of memory"); + if (!argv || !env) { + free(argv); + free(env); + free(p); + caml_failwith("pty: out of memory"); + } memset(&ws, 0, sizeof(ws)); ws.ws_col = (unsigned short)Int_val(vcols); @@ -342,6 +347,11 @@ CAMLprim value lui_term_pty_spawn(value vargv, value venv, value vcwd, p->exited = 0; /* master: nonblocking so the UI loop can poll freely */ fcntl(p->mfd, F_SETFL, fcntl(p->mfd, F_GETFL) | O_NONBLOCK); + { + int fdflags = fcntl(p->mfd, F_GETFD); + if (fdflags >= 0) + fcntl(p->mfd, F_SETFD, fdflags | FD_CLOEXEC); + } CAMLreturn(caml_copy_nativeint((intnat)p)); } @@ -421,7 +431,10 @@ CAMLprim value lui_term_pty_close(value vh) { CAMLparam1(vh); pty_t *p = (pty_t *)(intnat)Nativeint_val(vh); if (p) { - if (p->mfd >= 0) close(p->mfd); + if (p->mfd >= 0) { + close(p->mfd); + p->mfd = -1; + } free(p); } CAMLreturn(Val_unit); From 46ad5a1ccab806d1bfcc1be9ab9998998b0e90c3 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:54:06 +0000 Subject: [PATCH 08/13] Retry short PTY writes and lock terminal plugin tables. pump keeps the unwritten tail of nonblocking writes instead of dropping it. pending is oldest-first. Service tables (terms/next) are mutex protected because resource workers can complete off the main domain. Co-authored-by: Tienson Qin --- .../lui_plugin_terminal.ml | 54 ++++++++++++------- .../lui_plugin_terminal.mli | 5 ++ .../test/lui_plugin_terminal_test.ml | 44 +++++++++++++-- 3 files changed, 80 insertions(+), 23 deletions(-) diff --git a/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml b/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml index c0a79da8..6fc307d3 100644 --- a/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml +++ b/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml @@ -17,7 +17,7 @@ type t = { render : Lui_text.font R.t; mutable pty : P.t option; write_buf : bytes; - mutable pending : string list; (* bytes queued for the pty *) + mutable pending : string list; (* oldest-first bytes queued for the pty *) } let create ?(cols = 80) ?(rows = 24) ?(max_scrollback = 10_000) @@ -36,7 +36,7 @@ let renderer t = t.render (* Apply an action the parser raised. *) let act t = function - | Vt.Reply s -> t.pending <- s :: t.pending + | Vt.Reply s -> t.pending <- t.pending @ [s] | _ -> () let feed t s = @@ -61,15 +61,25 @@ let pty_alive t = let pty_poll t = match t.pty with Some p -> P.poll p | None -> Some (-1) +let unwritten ~wrote s = + if wrote <= 0 then s + else + let n = String.length s in + if wrote >= n then "" else String.sub s wrote (n - wrote) + +let rec flush_pending ~write = function + | [] -> [] + | s :: rest -> + let left = unwritten ~wrote:(write s) s in + if left = "" then flush_pending ~write rest else left :: rest + (* Drain queued input + read available output; call once per frame. Returns the actions raised by newly-fed output. *) let pump t = match t.pty with | None -> [] | Some p -> - List.iter (fun s -> ignore (P.write_string p s)) - (List.rev t.pending); - t.pending <- []; + t.pending <- flush_pending ~write:(P.write_string p) t.pending; let acts = ref [] in (try while true do @@ -82,7 +92,7 @@ let pump t = List.iter (act t) !acts; !acts -let write t s = t.pending <- s :: t.pending +let write t s = if s <> "" then t.pending <- t.pending @ [s] let resize t ~cols ~rows = G.resize (Vt.grid t.vt) ~cols ~rows; @@ -105,7 +115,7 @@ let resize_pixels t ~w ~h = resize t ~cols:(cols_for t w) ~rows:(rows_for t h) (* ---- input -------------------------------------------------------- *) -let enqueue t s = if s <> "" then t.pending <- s :: t.pending +let enqueue t s = write t s (* Pixel position -> (viewport row, col). *) let cell_of t ~x ~y = @@ -204,15 +214,17 @@ let has_selection t = G.has_selection (Vt.grid t.vt) module Service = struct open Yojson.Safe.Util + let lock = Mutex.create () let terms : (int, t) Hashtbl.t = Hashtbl.create 4 let next = ref 0 let find j = match member "id" j with - | `Int n -> ( - match Hashtbl.find_opt terms n with - | Some t -> Ok t - | None -> Error (Printf.sprintf "terminal: no terminal %d" n)) + | `Int n -> + Mutex.protect lock (fun () -> + match Hashtbl.find_opt terms n with + | Some t -> Ok t + | None -> Error (Printf.sprintf "terminal: no terminal %d" n)) | _ -> Error "terminal: \"id\" must be an integer" let member_str j k = @@ -324,9 +336,13 @@ module Service = struct with Failure e -> Error e) with | Error e -> Error e | Ok () -> - let id = !next in - incr next; - Hashtbl.replace terms id t; + let id = + Mutex.protect lock (fun () -> + let id = !next in + incr next; + Hashtbl.replace terms id t; + id) + in ok (`Assoc [ ("id", `Int id) ]) let svc_write = @@ -381,13 +397,15 @@ module Service = struct | Error _ as e -> e | Ok t -> close_pty t; - Hashtbl.remove terms - (match member "id" j with `Int n -> n | _ -> -1); + Mutex.protect lock (fun () -> + Hashtbl.remove terms + (match member "id" j with `Int n -> n | _ -> -1)); ok (obj ()) let reset () = - Hashtbl.iter (fun _ t -> close_pty t) terms; - Hashtbl.clear terms + Mutex.protect lock (fun () -> + Hashtbl.iter (fun _ t -> close_pty t) terms; + Hashtbl.clear terms) end let plugin : Lui_plugin.t = diff --git a/platform/native/lui_plugin_terminal/lui_plugin_terminal.mli b/platform/native/lui_plugin_terminal/lui_plugin_terminal.mli index e5373370..5e351b6e 100644 --- a/platform/native/lui_plugin_terminal/lui_plugin_terminal.mli +++ b/platform/native/lui_plugin_terminal/lui_plugin_terminal.mli @@ -38,6 +38,11 @@ val write : t -> string -> unit (** Queue bytes for the child; flushed by [pump] (or the host's own write loop). *) +val flush_pending : write:(string -> int) -> string list -> string list +(** Write [chunks] oldest-first through [write], keeping the unwritten + tail. [write s] is bytes accepted, 0 for back-pressure, negative + for error (kept unwritten). *) + val resize : t -> cols:int -> rows:int -> unit val resize_pixels : t -> w:int -> h:int -> unit val cols_for : t -> int -> int diff --git a/platform/native/lui_plugin_terminal/test/lui_plugin_terminal_test.ml b/platform/native/lui_plugin_terminal/test/lui_plugin_terminal_test.ml index c4001486..7a76e048 100644 --- a/platform/native/lui_plugin_terminal/test/lui_plugin_terminal_test.ml +++ b/platform/native/lui_plugin_terminal/test/lui_plugin_terminal_test.ml @@ -176,6 +176,37 @@ let test_links () = (* ------------------------------------------------------------ PTY *) +let test_flush_pending () = + let wrote = ref [] in + let write s = + wrote := s :: !wrote; + 2 + in + Alcotest.(check (list string)) "short writes keep the tail" + ["llo"; "world"] + (Lui_plugin_terminal.flush_pending ~write ["hello"; "world"]); + Alcotest.(check (list string)) "first chunk offered" ["hello"] (List.rev !wrote); + let write_all s = String.length s in + Alcotest.(check (list string)) "full writes drain" + [] + (Lui_plugin_terminal.flush_pending ~write:write_all ["ab"; "cd"]); + Alcotest.(check (list string)) "back-pressure keeps all" + ["ab"; "cd"] + (Lui_plugin_terminal.flush_pending ~write:(fun _ -> 0) ["ab"; "cd"]); + Alcotest.(check (list string)) "error keeps all" + ["ab"; "cd"] + (Lui_plugin_terminal.flush_pending ~write:(fun _ -> -1) ["ab"; "cd"]) + +let test_pty_close_twice () = + let true_bin = + if Sys.file_exists "/bin/true" then "/bin/true" else "true" + in + let p = + P.spawn ~argv:[| true_bin |] ~env:[||] ~cwd:"/" ~cols:80 ~rows:24 + in + P.close p; + P.close p + let test_pty_cat () = let cat = if Sys.file_exists "/bin/cat" then "/bin/cat" else "cat" in let p = P.spawn ~argv:[| cat |] ~env:[| "TERM=xterm" |] ~cwd:"/" @@ -319,11 +350,14 @@ let () = "keys", [ Alcotest.test_case "encodings" `Quick test_keys ]; "links", [ Alcotest.test_case "detect" `Quick test_links ]; "pty", - (match Sys.getenv_opt "GITHUB_ACTIONS" with - (* ConPTY under a service session has no console for cat to run in: - the spawn produces no echo and the bounded read times out. *) - | Some _ -> [] - | None -> [ Alcotest.test_case "cat round-trip" `Slow test_pty_cat ]); + [ Alcotest.test_case "flush pending" `Quick test_flush_pending ] + @ (match Sys.getenv_opt "GITHUB_ACTIONS" with + (* ConPTY under a service session has no console for cat to run in: + the spawn produces no echo and the bounded read times out. *) + | Some _ -> [] + | None -> + [ Alcotest.test_case "cat round-trip" `Slow test_pty_cat; + Alcotest.test_case "close twice" `Quick test_pty_close_twice ]); "render", [ Alcotest.test_case "smoke ops" `Quick test_render_smoke; ]; From 7aa989b1a9e0155c78bb2910693650c47933a2d7 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:54:12 +0000 Subject: [PATCH 09/13] Watch DBus fds and release the runtime around blocking calls. Shell and a11y expose unix_fd so the Linux wait loop can select instead of capping idle at 50ms. dbus_wait_cap_ms keeps the cap only when the bus is connected but no fd is available. Blocking send_with_reply_and_block releases the OCaml runtime. Noop renderer frames are zeroed with Bytes.make. Co-authored-by: Tienson Qin --- platform/native/lui_ax_linux/lui_ax_linux.ml | 7 +++++ platform/native/lui_ax_linux/lui_ax_linux.mli | 5 ++++ .../native/lui_ax_linux/lui_ax_linux_stubs.c | 26 +++++++++++++++++++ .../native/lui_shell_linux/lui_shell_linux.ml | 6 +++++ .../lui_shell_linux/lui_shell_linux.mli | 4 +++ .../lui_shell_linux/lui_shell_linux_stubs.c | 19 ++++++++++++++ platform/native/lui_window_linux/lui_linux.ml | 8 +++++- .../test/lui_window_linux_test.ml | 19 +++++++++++++- 8 files changed, 92 insertions(+), 2 deletions(-) diff --git a/platform/native/lui_ax_linux/lui_ax_linux.ml b/platform/native/lui_ax_linux/lui_ax_linux.ml index bf74d2ca..686992b5 100644 --- a/platform/native/lui_ax_linux/lui_ax_linux.ml +++ b/platform/native/lui_ax_linux/lui_ax_linux.ml @@ -654,6 +654,7 @@ external c_dbus_open : string -> int = "lui_ax_dbus_open" external c_dbus_close : int -> unit = "lui_ax_dbus_close" external c_dbus_name : int -> string = "lui_ax_dbus_name" external c_dbus_a11y_address : int -> string = "lui_ax_dbus_a11y_address" +external c_dbus_unix_fd : int -> int = "lui_ax_dbus_unix_fd" external c_dbus_embed : int -> string -> string * string = "lui_ax_dbus_embed" external c_dbus_poll : int -> int -> @@ -1885,6 +1886,12 @@ let set_desktop_ref t r = t.desktop <- Some r let app_id t = t.app_id let transport t = t.trans +let unix_fd t = + if !(t.conn) = 0 then None + else + let fd = c_dbus_unix_fd !(t.conn) in + if fd < 0 then None else Some fd + let session_bus_address () = match Sys.getenv_opt "DBUS_SESSION_BUS_ADDRESS" with | Some a when a <> "" -> a diff --git a/platform/native/lui_ax_linux/lui_ax_linux.mli b/platform/native/lui_ax_linux/lui_ax_linux.mli index c2d2eaed..81d7bc15 100644 --- a/platform/native/lui_ax_linux/lui_ax_linux.mli +++ b/platform/native/lui_ax_linux/lui_ax_linux.mli @@ -292,6 +292,11 @@ val connect : t -> transport emission APIs keep queueing. Requires Linux. *) val transport : t -> transport + +val unix_fd : t -> int option +(** Session or a11y-bus connection fd for the host wait loop, or + [None] when offline. *) + val disconnect : t -> unit val flush : t -> int (** [flush t] sends every queued event over the bus; returns how many diff --git a/platform/native/lui_ax_linux/lui_ax_linux_stubs.c b/platform/native/lui_ax_linux/lui_ax_linux_stubs.c index 412b3af9..cef89d18 100644 --- a/platform/native/lui_ax_linux/lui_ax_linux_stubs.c +++ b/platform/native/lui_ax_linux/lui_ax_linux_stubs.c @@ -25,6 +25,7 @@ #include #include #include +#include /* ---- Minimal libdbus declarations (no headers required) ---- */ @@ -119,6 +120,8 @@ static DBusMessage *(*p_dbus_connection_pop_message)( DBusConnection *) = 0; static DBusMessage *(*p_dbus_connection_send_with_reply_and_block)( DBusConnection *, DBusMessage *, int, DBusError *) = 0; +static dbus_bool_t (*p_dbus_connection_get_unix_fd)( + DBusConnection *, int *) = 0; static DBusMessage *(*p_dbus_message_new_method_call)(const char *, const char *, const char *, @@ -196,6 +199,8 @@ CAMLprim value lui_ax_dbus_probe(value unit) (void **) &p_dbus_connection_pop_message); ok &= sym(h, "dbus_connection_send_with_reply_and_block", (void **) &p_dbus_connection_send_with_reply_and_block); + ok &= sym(h, "dbus_connection_get_unix_fd", + (void **) &p_dbus_connection_get_unix_fd); ok &= sym(h, "dbus_message_new_method_call", (void **) &p_dbus_message_new_method_call); ok &= sym(h, "dbus_message_new_signal", @@ -386,8 +391,10 @@ CAMLprim value lui_ax_dbus_a11y_address(value vc) CAMLreturn(vs); } p_dbus_error_init(&err); + caml_release_runtime_system(); rep = p_dbus_connection_send_with_reply_and_block(c->conn, call, 5000, &err); + caml_acquire_runtime_system(); p_dbus_message_unref(call); if (rep && p_dbus_message_iter_init(rep, &it) && p_dbus_message_iter_get_arg_type(&it) == DBUS_TYPE_STRING) @@ -428,8 +435,10 @@ CAMLprim value lui_ax_dbus_embed(value vc, value vpath) } p_dbus_message_iter_close_container(&it, &sub); p_dbus_error_init(&err); + caml_release_runtime_system(); rep = p_dbus_connection_send_with_reply_and_block(c->conn, call, 5000, &err); + caml_acquire_runtime_system(); if (rep && p_dbus_message_iter_init(rep, &it) && p_dbus_message_iter_get_arg_type(&it) == DBUS_TYPE_STRUCT) { p_dbus_message_iter_recurse(&it, &sub); @@ -453,6 +462,17 @@ CAMLprim value lui_ax_dbus_embed(value vc, value vpath) CAMLreturn(vt); } +CAMLprim value lui_ax_dbus_unix_fd(value vc) +{ + CAMLparam1(vc); + conn_t *c = conn_of(vc); + int fd = -1; + if (p_dbus_connection_get_unix_fd == 0 || c->conn == 0 + || !p_dbus_connection_get_unix_fd(c->conn, &fd)) + CAMLreturn(Val_int(-1)); + CAMLreturn(Val_int(fd)); +} + /* ---- Argument decoding ---- Flattens a message's basic arguments into three arrays: ints (all @@ -1042,6 +1062,12 @@ CAMLprim value lui_ax_dbus_embed(value a, value b) return ax_fail(); } +CAMLprim value lui_ax_dbus_unix_fd(value a) +{ + (void) a; + return Val_int(-1); +} + CAMLprim value lui_ax_dbus_poll(value a, value b) { (void) a; diff --git a/platform/native/lui_shell_linux/lui_shell_linux.ml b/platform/native/lui_shell_linux/lui_shell_linux.ml index 0f25528d..0f395340 100644 --- a/platform/native/lui_shell_linux/lui_shell_linux.ml +++ b/platform/native/lui_shell_linux/lui_shell_linux.ml @@ -13,6 +13,7 @@ external c_probe : string -> int = "lui_shx_probe" external c_gui : unit -> bool = "lui_shx_gui" external c_indicator : unit -> bool = "lui_shx_indicator" external c_pump : int -> (int * int * string) array = "lui_shx_pump" +external c_unix_fd : unit -> int = "lui_shx_unix_fd" (* Well-known session-bus addresses, most specific first: the environment, the systemd per-user socket, then the X11 autolaunch @@ -40,6 +41,11 @@ let transport () = else if !caps land 1 <> 0 then Session_bus else Offline +let unix_fd () = + probe (); + let fd = c_unix_fd () in + if fd < 0 then None else Some fd + let gui_available () = probe (); c_gui () diff --git a/platform/native/lui_shell_linux/lui_shell_linux.mli b/platform/native/lui_shell_linux/lui_shell_linux.mli index 9b7f9649..57d0a23b 100644 --- a/platform/native/lui_shell_linux/lui_shell_linux.mli +++ b/platform/native/lui_shell_linux/lui_shell_linux.mli @@ -39,6 +39,10 @@ val transport : unit -> transport (** Best session tier the transport could bind. Probing is lazy: it runs on the first call that needs it (or on [pump]) and is cached. *) +val unix_fd : unit -> int option +(** Unix fd of the session-bus connection, for folding into the host + wait, or [None] when offline. *) + val gui_available : unit -> bool (** Whether GTK bound and opened a display — clipboard, dialogs and tray menus are possible regardless of the indicator library. *) diff --git a/platform/native/lui_shell_linux/lui_shell_linux_stubs.c b/platform/native/lui_shell_linux/lui_shell_linux_stubs.c index 79329bea..cabc2cb3 100644 --- a/platform/native/lui_shell_linux/lui_shell_linux_stubs.c +++ b/platform/native/lui_shell_linux/lui_shell_linux_stubs.c @@ -44,6 +44,7 @@ #include #include #include +#include /* ---- Minimal libdbus declarations (no headers required) ---- */ @@ -141,6 +142,8 @@ static DBusMessage *(*p_dbus_connection_pop_message)( DBusConnection *) = 0; static DBusMessage *(*p_dbus_connection_send_with_reply_and_block)( DBusConnection *, DBusMessage *, int, DBusError *) = 0; +static dbus_bool_t (*p_dbus_connection_get_unix_fd)( + DBusConnection *, int *) = 0; static dbus_bool_t (*p_dbus_connection_register_object_path)( DBusConnection *, const char *, const DBusObjectPathVTable *, void *) = 0; @@ -348,8 +351,10 @@ static DBusMessage *bus_call(DBusMessage *call, DBusError *err) DBusMessage *rep; if (session == 0 || call == 0) return 0; p_dbus_error_init(err); + caml_release_runtime_system(); rep = p_dbus_connection_send_with_reply_and_block(session, call, 3000, err); + caml_acquire_runtime_system(); p_dbus_message_unref(call); return rep; } @@ -614,6 +619,8 @@ static int probe_dbus(void) (void **) &p_dbus_connection_pop_message); ok &= sym(h, "dbus_connection_send_with_reply_and_block", (void **) &p_dbus_connection_send_with_reply_and_block); + ok &= sym(h, "dbus_connection_get_unix_fd", + (void **) &p_dbus_connection_get_unix_fd); ok &= sym(h, "dbus_connection_register_object_path", (void **) &p_dbus_connection_register_object_path); ok &= sym(h, "dbus_connection_unregister_object_path", @@ -1338,6 +1345,16 @@ CAMLprim value lui_shx_openurl_bus(value vname, value vpath) /* ---- Pump ---- */ +CAMLprim value lui_shx_unix_fd(value unit) +{ + CAMLparam1(unit); + int fd = -1; + if (session == 0 || p_dbus_connection_get_unix_fd == 0 + || !p_dbus_connection_get_unix_fd(session, &fd)) + CAMLreturn(Val_int(-1)); + CAMLreturn(Val_int(fd)); +} + CAMLprim value lui_shx_pump(value vtimeout) { CAMLparam1(vtimeout); @@ -1434,6 +1451,8 @@ CAMLprim value lui_shx_open_url(value a) { (void)a; return unsupported(); } CAMLprim value lui_shx_openurl_bus(value a, value b) { (void)a; (void)b; return unsupported(); } +CAMLprim value lui_shx_unix_fd(value a) { + (void)a; return Val_int(-1); } CAMLprim value lui_shx_pump(value a) { (void)a; return unsupported(); } diff --git a/platform/native/lui_window_linux/lui_linux.ml b/platform/native/lui_window_linux/lui_linux.ml index de06381b..7eae22b7 100644 --- a/platform/native/lui_window_linux/lui_linux.ml +++ b/platform/native/lui_window_linux/lui_linux.ml @@ -31,7 +31,13 @@ let noop_renderer : Lui_host.renderer = { Lui_host.name = "noop"; render = (fun (s : Lui_scene.t) -> - Bytes.create (s.Lui_scene.width * s.Lui_scene.height * 4)) } + Bytes.make (s.Lui_scene.width * s.Lui_scene.height * 4) '\000') } + +let dbus_wait_cap_ms ~connected ~watching next = + if connected && not watching then Float.min next 50. else next + +(* Unix.file_descr is the raw fd on this platform. *) +external fd_of_int : int -> Unix.file_descr = "%identity" (* AT-triggered action -> protocol event, mirroring the pointer path: a screen reader's "press" lands exactly where a click's [Press] diff --git a/platform/native/lui_window_linux/test/lui_window_linux_test.ml b/platform/native/lui_window_linux/test/lui_window_linux_test.ml index 2cd557ea..d6630404 100644 --- a/platform/native/lui_window_linux/test/lui_window_linux_test.ml +++ b/platform/native/lui_window_linux/test/lui_window_linux_test.ml @@ -79,7 +79,23 @@ let test_ladder () = scene.width <- 5; scene.height <- 3; let pix = Lui_linux.noop_renderer.Lui_host.render scene in - Alcotest.(check int) "noop bytes" (5 * 3 * 4) (Bytes.length pix) + Alcotest.(check int) "noop bytes" (5 * 3 * 4) (Bytes.length pix); + Alcotest.(check bool) "noop frame is zeroed" true + (pix = Bytes.make (5 * 3 * 4) '\000') + +let test_dbus_wait_cap () = + Alcotest.(check bool) "idle without bus is unbounded" true + (Float.is_infinite + (Lui_linux.dbus_wait_cap_ms ~connected:false ~watching:false + Float.infinity)); + Alcotest.(check (float 0.001)) "connected without fd watch caps at 50" + 50. + (Lui_linux.dbus_wait_cap_ms ~connected:true ~watching:false + Float.infinity); + Alcotest.(check bool) "watching fds does not cap" true + (Float.is_infinite + (Lui_linux.dbus_wait_cap_ms ~connected:true ~watching:true + Float.infinity)) (* The real walk/demote path the driver uses: injected init failures step down the ladder at startup, and a mid-render device loss @@ -636,6 +652,7 @@ let () = Alcotest.run "lui_window_linux" [ ( "ladder", [ Alcotest.test_case "renderer ladder" `Quick test_ladder; + Alcotest.test_case "dbus wait cap" `Quick test_dbus_wait_cap; Alcotest.test_case "demote path" `Quick test_ladder_demote; Alcotest.test_case "tray icon" `Quick test_tray_icon ] ); ( "a11y", From 7cd2e1e49e2225ebbb09f65ebe83b06924f37e9f Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:54:15 +0000 Subject: [PATCH 10/13] Fail the Pango pkg-config rule instead of swallowing errors. A missing pangocairo/pangoft2/fontconfig used to produce empty cflags and a later missing-header error. The rule now exits so the configure failure is visible. Co-authored-by: Tienson Qin --- platform/native/lui_text_pango/dune | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/native/lui_text_pango/dune b/platform/native/lui_text_pango/dune index e1bc5ca7..d82e19de 100644 --- a/platform/native/lui_text_pango/dune +++ b/platform/native/lui_text_pango/dune @@ -13,11 +13,11 @@ (action (with-stdout-to %{target} (bash - "case '%{system}' in linux) printf '(%s)' \"$(pkg-config --cflags pangocairo pangoft2 fontconfig 2>/dev/null)\";; *) printf '()';; esac")))) + "case '%{system}' in linux) flags=$(pkg-config --cflags pangocairo pangoft2 fontconfig) || exit; printf '(%s)' \"$flags\";; *) printf '()';; esac")))) (rule (target c_libs.sexp) (action (with-stdout-to %{target} (bash - "case '%{system}' in linux) printf '(%s)' \"$(pkg-config --libs pangocairo pangoft2 fontconfig 2>/dev/null)\";; *) printf '()';; esac")))) + "case '%{system}' in linux) flags=$(pkg-config --libs pangocairo pangoft2 fontconfig) || exit; printf '(%s)' \"$flags\";; *) printf '()';; esac")))) From c714534c61df7cf2d941a6ea81c1bd0d31dde68b Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 20:54:20 +0000 Subject: [PATCH 11/13] Tick clocks, drain resources, and present dirty Vulkan rows in host loops. macOS, Linux and Windows window loops call Lui_app.tick_clock and Lui_host.tick every iteration, fold Lui_clock.next_delay into the wait, drain Lui_resource, and wake on completions. Demo toast_since clocks are gone. Linux watches DBus fds instead of a 50ms idle cap, recreates only the Vulkan frame on resize, uploads damaged rows, and treats blit resize failure as a demotion. Swapchain present is documented as a follow-up. Co-authored-by: Tienson Qin --- docs/api-migrations.md | 42 ++++++++- platform/native/lui_window/main.ml | 36 +++---- platform/native/lui_window_linux/main.ml | 105 +++++++++++++++------ platform/native/lui_window_windows/main.ml | 29 +++--- 4 files changed, 152 insertions(+), 60 deletions(-) diff --git a/docs/api-migrations.md b/docs/api-migrations.md index 4064c991..c60d792e 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -4069,8 +4069,10 @@ names the wrong parent, and structural ops on missing nodes raise `Invalid_argument` with op index and a `describe_op` prefix (`patch op N (...): ...`). The store is unchanged so `Lui_host.apply_batch` can return `false` and the runtime can resync, matching Web and Android. Property ops on missing -nodes are still ignored. `apply_op` stays lenient except that inserting a node -under itself or an ancestor raises to keep `iter_preorder` acyclic. +nodes are still ignored. `InsertChild`/`MoveChild` whose parent is not a node +(including the gallery sentinel `parent=-1`) stay a no-op so the child remains +a root, matching `apply_op`. `apply_op` stays lenient except that inserting a +node under itself or an ancestor raises to keep `iter_preorder` acyclic. ```ocaml (* Before: InsertChild (2, 1) after InsertChild (1, 2) closed a cycle and @@ -4083,3 +4085,39 @@ Lui_store.apply_batch store { generation; ops = [ InsertChild (2, 1, 0) ] } Lui_host.apply_batch host batch (* false *) *) ``` + +## Native hosts tick the application clock + +SDL/native window loops (macOS, Linux, Windows) call +`Lui_app.tick_clock ~now` every iteration and fold `Lui_clock.next_delay` +into the event-wait timeout via `Lui_window.fold_wait_ms`. `Lui_ui.after` +and `Lui_ui.every` therefore fire on native windows, matching Apple, +Android and Web. + +Toast `duration` is applied by the native UI layer: `Ui.store_changed` +schedules a `Dismiss` at `now + duration` and `Ui.next_input_deadline` +wakes the host for it. Demo drivers no longer keep a private +`toast_since` clock. + +Native window loops also call `Lui_host.tick ~now` every iteration +(seconds, matching the host animation clock). That starts +`resize-duration` split transitions and advances retained popup motion; +`Lui_host.wants_repaint` already requests frames while those animations +run. + +## Native resource executor and Vulkan frame resize + +`Lui_resource.create` now defaults to a bounded worker pool instead of +one `Domain.spawn` per load. `request`/`reload` mark the entry `Failed` +when the executor raises (for example past the domain cap) instead of +leaving it stuck `Loading`. Hosts call `drain_all` every frame and may +install `set_completion_wake` (SDL hosts use +`Lui_sdl3.run_on_main_thread`) so completions wake an idle wait. + +`Lui_vulkan.resize` recreates only the offscreen frame image and +framebuffer. Window swapchain presentation remains a follow-up: Linux +still reads the frame back and uploads damaged rows through +`Lui_blit`. + +Terminal `pump` retries short PTY writes; `flush_pending` is the +testable helper. `Terminal_pty.close` is idempotent. diff --git a/platform/native/lui_window/main.ml b/platform/native/lui_window/main.ml index cfe2e85f..c2f03d1b 100644 --- a/platform/native/lui_window/main.ml +++ b/platform/native/lui_window/main.ml @@ -129,7 +129,6 @@ type drv = { app : (Demo_app.model, Demo_app.action) Lui_app.reducer_app; a11y : Lui_a11y.t; mutable drop_seen : int; - mutable toast_since : float option; reqs_seen : (string, int) Hashtbl.t; checksum : Checksum.t; mutable frame : int; @@ -355,16 +354,7 @@ let poll_driver (s : surf) = Lui_shell.set_dock_badge (match m.Demo_app.items with | [] -> None - | l -> Some (string_of_int (List.length l))); - (* The toast's [duration] prop is informational on this backend; - the driver owns the clock and dismisses after 2.5 s. *) - (match m.Demo_app.toast_open, d.toast_since with - | true, None -> d.toast_since <- Some (perf_s ()) - | true, Some t0 when perf_s () -. t0 >= 2.5 -> - ignore (Lui_app.send d.app (Demo_app.Close Demo_app.P_toast)); - d.toast_since <- None - | false, _ -> d.toast_since <- None - | _ -> ()) + | l -> Some (string_of_int (List.length l))) end (* One frame-callback for one window: flush queued patches, run @@ -376,6 +366,11 @@ let poll_driver (s : surf) = until the nearest deadline. *) let tick ?(live_resize = false) (s : surf) ~t0 = let d = s.driver in + let now_s = perf_s () in + let now_ms = now_s *. 1000. in + ignore (Lui_app.tick_clock d.app ~now:now_ms); + Lui_host.tick s.host ~now:now_s; + ignore (Lui_resource.drain_all ()); (match d.metal, d.presentations with | Some metal, Some meter when not !headless -> let displayed_frames = Lui_metal.presented_frames metal in @@ -394,6 +389,7 @@ let tick ?(live_resize = false) (s : surf) ~t0 = let allocation_before = if frame_stats then Some (Gc.allocated_bytes (), (Gc.quick_stat ()).minor_collections) else None in let store = Lui_host.store s.host in + Ui.tick_scrollbars s.ui ~now_ms; ignore (Lui_app.flush d.app); apply_actions s (Ui.store_changed s.ui store); (* Effects can dispatch back into the app — flush once more so the @@ -410,7 +406,6 @@ let tick ?(live_resize = false) (s : surf) ~t0 = apply_actions s (Multi.dispatch s (Input.Caret (d.caret_rect ()))); (* Headless proves the loop: force a repaint every iteration so the frame counter and checksum actually run. *) - Ui.tick_scrollbars s.ui ~now_ms:(perf_s () *. 1000.); if !headless || Ui.want_frame s.ui s.host then Frame.request_frame d.sched; let decision = @@ -771,7 +766,7 @@ let open_window ~title ~idx = { sdl = !win; gctx_cell; gl_live_cell; vsync; blit_cell; metal; display_link = false; popups = Lui_blit.Popups.create (); - renderer_cell; app; a11y; drop_seen = 0; toast_since = None; + renderer_cell; app; a11y; drop_seen = 0; reqs_seen = Hashtbl.create 8; checksum = Checksum.create (); frame = 0; stats = { frames = 0; acc_ms = 0.; min_ms = 1e9; max_ms = 0. }; @@ -955,6 +950,8 @@ let () = prefer_metal := not !headless && not !use_cpu && env <> "gl" && env <> "opengl"; sdl_ok (Sdl.init Sdl.Init.(video + events)); + Lui_resource.set_completion_wake (fun () -> + try Sdl.run_on_main_thread ignore with _ -> ()); (* GL context requirements from lui_gl.mli: OpenGL 3.3 core, double-buffered. Attributes are hints — SDL may refuse or substitute, in which case Lui_gl.init fails and the no-op @@ -1016,11 +1013,14 @@ let () = | Frame.Wait_ms ms -> wait_ms := Float.min !wait_ms ms | Frame.Present when Ui.want_frame s.ui s.host -> wait_ms := 0. | Frame.Present | Frame.Idle -> ()); - let deadline = Ui.next_input_deadline s.ui in - let deadline = match s.driver.toast_since with - | None -> deadline - | Some at -> Float.min deadline ((at +. 2.5) *. 1000.) in - wait_ms := Float.min !wait_ms (deadline -. perf_s () *. 1000.)); + let now_ms = perf_s () *. 1000. in + wait_ms := + Float.min !wait_ms + (fold_wait_ms + ~ui_deadline_ms:(Ui.next_input_deadline s.ui) + ~clock_delay_ms:(Lui_clock.next_delay (Lui_app.clock s.driver.app)) + ~host_wants_frame:(Ui.want_frame s.ui s.host) + ~now_ms)); incr iter_no; if not !headless && continue () then begin Lui_metal.set_sdl_waiting true; diff --git a/platform/native/lui_window_linux/main.ml b/platform/native/lui_window_linux/main.ml index 98c76e89..092dcfcd 100644 --- a/platform/native/lui_window_linux/main.ml +++ b/platform/native/lui_window_linux/main.ml @@ -113,6 +113,8 @@ let () = else Lui_linux.ladder ~env in sdl_ok (Sdl.init Sdl.Init.(video + events)); + Lui_resource.set_completion_wake (fun () -> + try Sdl.run_on_main_thread ignore with _ -> ()); (* GL context requirements from lui_gl.mli: OpenGL 3.3 core, double-buffered. Attributes are hints — SDL may refuse or substitute, in which case Lui_gl.init fails and the ladder walks @@ -218,13 +220,14 @@ let () = (Lui_vulkan.driver v) (Lui_vulkan.dual v); vk_cell := Some v; let b = mk_blit () in + let last_pix = ref Bytes.empty in attempt { Lui_host.name = "lui_vulkan+blit"; render = (fun s -> - (* The offscreen frame image is sized at init; a - resized scene forces a re-init. On failure the - rung degrades to a zeroed frame — no crash. *) + (* Offscreen render + readback; only damaged rows are + uploaded through Lui_blit. Window swapchain present + is a follow-up (the renderer has no surface). *) let v = match !vk_cell with | Some v -> Some v @@ -238,27 +241,33 @@ let () = in match v with | None -> - Bytes.create (s.Lui_scene.width * s.Lui_scene.height * 4) + Bytes.make (s.Lui_scene.width * s.Lui_scene.height * 4) '\000' | Some v -> ( try Lui_vulkan.render v s; - let pix = - Lui_vulkan.read_frame v ~w:s.Lui_scene.width - ~h:s.Lui_scene.height - in + let w, h = s.Lui_scene.width, s.Lui_scene.height in + let pix = Lui_vulkan.read_frame v ~w ~h in (match b with | Some b -> - ignore - (Lui_blit.update b ~pix - ~stride:s.Lui_scene.width); + (match + Lui_vulkan.changed_rows ~prev:!last_pix pix ~w ~h + with + | None -> () + | Some (0, rh) when rh = h -> + ignore (Lui_blit.update b ~pix ~stride:w) + | Some (y0, y1) -> + ignore + (Lui_blit.update_region b ~x0:0 ~y0 ~x1:w ~y1 + ~pix ~stride:w)); + last_pix := pix; Lui_blit.present b | None -> ()); pix with | Lui_vulkan.Error m -> warn "lui_vulkan.render: %s" m; - Bytes.create - (s.Lui_scene.width * s.Lui_scene.height * 4))) } + Bytes.make + (s.Lui_scene.width * s.Lui_scene.height * 4) '\000')) } false) | Lui_linux.Gl -> if headless then begin @@ -291,8 +300,9 @@ let () = with | Lui_gl.Error m -> warn "lui_gl.render: %s" m; - Bytes.create - (s.Lui_scene.width * s.Lui_scene.height * 4)) } + Bytes.make + (s.Lui_scene.width * s.Lui_scene.height * 4) + '\000') } true | Error m -> warn "lui_gl.init failed: %s" m; @@ -461,16 +471,22 @@ let () = let dw, dh = drawable () in (match !blit_cell with | Some b -> - if - (match Lui_blit.resize b ~w:dw ~h:dh with - | Ok () -> false - | Error m -> warn "blit resize: %s" m; true) - then (); + (match Lui_blit.resize b ~w:dw ~h:dh with + | Ok () -> () + | Error m -> + warn "blit resize: %s" m; + blit_cell := None) | None -> ()); - (* The Vulkan rung's frame image is sized at init: drop it so - the next render re-inits at the new extent. *) + (* Recreate only the offscreen frame image; keep the Vulkan + instance, device and pipelines. *) (match !vk_state with - | Some v -> Lui_vulkan.release v; vk_state := None + | Some v -> + (match Lui_vulkan.resize v ~w:dw ~h:dh with + | Ok () -> () + | Error m -> + warn "lui_vulkan.resize: %s" m; + Lui_vulkan.release v; + vk_state := None) | None -> ()); scale_cell := float dw /. float (max 1 !win_w); Ui.set_scale ui !scale_cell; @@ -563,6 +579,28 @@ let () = | Lui_shell_linux.Session_bus -> "session-bus" | Lui_shell_linux.Indicator -> "indicator") (Lui_shell_linux.gui_available ()); + let dbus_connected = + Lui_shell_linux.transport () <> Lui_shell_linux.Offline + || ax_transport <> Lui_ax_linux.Offline + in + let dbus_fds = + List.filter_map + (fun fd -> Option.map Lui_linux.fd_of_int fd) + [ Lui_shell_linux.unix_fd (); Lui_ax_linux.unix_fd ax ] + in + let dbus_watching = dbus_fds <> [] in + if dbus_watching && not headless then + ignore + (Domain.spawn (fun () -> + let rec loop () = + match Unix.select dbus_fds [] [] (-1.) with + | [], _, _ -> loop () + | _ -> + (try Sdl.run_on_main_thread ignore with _ -> ()); + Unix.sleepf 0.01; + loop () + in + loop ())); let e = Sdl.Event.create () in let handle_sdl_event () = Ui.tick_scrollbars ui ~now_ms:(perf_s () *. 1000.); @@ -612,7 +650,13 @@ let () = (if headless then " [headless]" else ""); while continue () do let t0 = perf_s () in + let now_s = t0 in + let now_ms = now_s *. 1000. in drain_events (); + ignore (Lui_app.tick_clock app ~now:now_ms); + Lui_host.tick host ~now:now_s; + ignore (Lui_resource.drain_all ()); + Ui.tick_scrollbars ui ~now_ms; (* Patches queued by dispatched events land in the store here; layout must see them before the next paint or hit test. Store-driven effects: Appear lifecycle events, scroll-target @@ -639,7 +683,6 @@ let () = (Assembly.handle asm (Input.Caret (caret_rect ()))); (* Headless proves the loop: force a repaint every iteration so the frame counter and checksum actually run. *) - Ui.tick_scrollbars ui ~now_ms:(perf_s () *. 1000.); let dirty = headless || Ui.want_frame ui host in if dirty then begin (* populate's anchor-drift corrections surface as VisibleRange @@ -659,7 +702,13 @@ let () = if not headless && continue () then begin ignore (Lui_app.flush app); let now = perf_s () *. 1000. in - let next = Ui.next_input_deadline ui -. now in + let next = + fold_wait_ms + ~ui_deadline_ms:(Ui.next_input_deadline ui) + ~clock_delay_ms:(Lui_clock.next_delay (Lui_app.clock app)) + ~host_wants_frame:false + ~now_ms:now + in let next = if Lui_store.revision store <> lifecycle_revision then 0. else if Ui.want_frame ui host then begin @@ -673,9 +722,9 @@ let () = Float.min next (t0 *. 1000. +. 1000. /. hz -. now) end else next in let next = - if Lui_shell_linux.transport () <> Lui_shell_linux.Offline - || ax_transport <> Lui_ax_linux.Offline then Float.min next 50. - else next in + Lui_linux.dbus_wait_cap_ms ~connected:dbus_connected + ~watching:dbus_watching next + in if (if next = Float.infinity then Sdl.wait_event (Some e) else Sdl.wait_event_timeout (Some e) (int_of_float (Float.ceil (Float.max 0. next)))) diff --git a/platform/native/lui_window_windows/main.ml b/platform/native/lui_window_windows/main.ml index e036c10b..f614a876 100644 --- a/platform/native/lui_window_windows/main.ml +++ b/platform/native/lui_window_windows/main.ml @@ -465,7 +465,6 @@ let () = if n > seen then (Hashtbl.replace reqs_seen name n; true) else false in - let toast_since = ref None in let poll_driver () = if not headless then begin let m = Lui_app.model app in @@ -490,15 +489,6 @@ let () = log "a11y: %d nodes — %s" (Lui_a11y.node_count a11y) (String.concat ", " names) end; - (* The toast's [duration] prop is informational on this backend; - the driver owns the clock and dismisses after 2.5 s. *) - (match m.Demo_app.toast_open, !toast_since with - | true, None -> toast_since := Some (perf_s ()) - | true, Some t0 when perf_s () -. t0 > 2.5 -> - ignore (Lui_app.send app (Demo_app.Close Demo_app.P_toast)); - toast_since := None - | false, _ -> toast_since := None - | _ -> ()) end in let continue () = @@ -512,7 +502,12 @@ let () = (if headless then " [headless]" else ""); while continue () do let t0 = perf_s () in + let now_ms = t0 *. 1000. in drain_events (); + ignore (Lui_app.tick_clock app ~now:now_ms); + Lui_host.tick host ~now:t0; + ignore (Lui_resource.drain_all ()); + Ui.tick_scrollbars ui ~now_ms; (* Patches queued by dispatched events land in the store here; the engine re-syncs inside the next repaint. *) ignore (Lui_app.flush app); @@ -570,12 +565,22 @@ let () = iteration (no repaint) also sleeps briefly so the poll loop does not spin at 100% CPU. *) if not headless then begin + let now_ms = perf_s () *. 1000. in + let wait = + fold_wait_ms + ~ui_deadline_ms:(Ui.next_input_deadline ui) + ~clock_delay_ms:(Lui_clock.next_delay (Lui_app.clock app)) + ~host_wants_frame:false + ~now_ms + in if dirty then begin - let remaining = 16.6 -. ((perf_s () -. t0) *. 1000.) in + let remaining = Float.min wait (16.6 -. (now_ms -. t0 *. 1000.)) in if remaining > 0.5 then Lui_win32.delay_ms (int_of_float remaining) end - else Lui_win32.delay_ms 4 + else if wait = Float.infinity then Lui_win32.delay_ms 4 + else if wait > 0.5 then + Lui_win32.delay_ms (int_of_float (Float.ceil wait)) end done; stats_report st (Renderer_ladder.active ladder_t).renderer.Lui_host.name; From 9f2cba780d45fd43d8a33181187ff56c70e498f1 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 21:01:44 +0000 Subject: [PATCH 12/13] Address host-loop review comments from PR 401. Invalidate Vulkan damage after blit resize so window moves re-upload. Keep the blit cell after a resize error so later events can retry. Sample the Linux/Windows clocks after draining events so scrollbar ticks stay monotonic. macOS wait no longer zeros Frame.Wait_ms. Windows idle Sleep is capped at 4ms so input still drains. D-Bus watch uses a C select on raw fds; GTK keeps a 50ms GLib wake. Resource spawn failures notify watchers. Terminal pending writes use a two-list queue. Co-authored-by: Tienson Qin --- docs/api-migrations.md | 3 +- .../lui_plugin_terminal.ml | 35 +++++++++-- platform/native/lui_resource/lui_resource.ml | 1 + .../lui_resource/test/lui_resource_test.ml | 10 ++- platform/native/lui_window/main.ml | 2 +- platform/native/lui_window_linux/dune | 3 + platform/native/lui_window_linux/lui_linux.ml | 13 ++-- .../native/lui_window_linux/lui_linux_stubs.c | 62 +++++++++++++++++++ platform/native/lui_window_linux/main.ml | 32 +++++----- .../test/lui_window_linux_test.ml | 10 ++- platform/native/lui_window_windows/main.ml | 15 +++-- 11 files changed, 151 insertions(+), 35 deletions(-) create mode 100644 platform/native/lui_window_linux/lui_linux_stubs.c diff --git a/docs/api-migrations.md b/docs/api-migrations.md index c60d792e..b0c835ee 100644 --- a/docs/api-migrations.md +++ b/docs/api-migrations.md @@ -4120,4 +4120,5 @@ still reads the frame back and uploads damaged rows through `Lui_blit`. Terminal `pump` retries short PTY writes; `flush_pending` is the -testable helper. `Terminal_pty.close` is idempotent. +testable helper. Incoming chunks enqueue in O(1). `Terminal_pty.close` +is idempotent. diff --git a/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml b/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml index 6fc307d3..f9ca4241 100644 --- a/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml +++ b/platform/native/lui_plugin_terminal/lui_plugin_terminal.ml @@ -17,7 +17,10 @@ type t = { render : Lui_text.font R.t; mutable pty : P.t option; write_buf : bytes; - mutable pending : string list; (* oldest-first bytes queued for the pty *) + (* Two-list queue: [pending_out] next-to-write, [pending_in] reversed + incoming. Enqueue is O(1); flush never copies the whole backlog. *) + mutable pending_out : string list; + mutable pending_in : string list; } let create ?(cols = 80) ?(rows = 24) ?(max_scrollback = 10_000) @@ -29,14 +32,28 @@ let create ?(cols = 80) ?(rows = 24) ?(max_scrollback = 10_000) ~theme () in { vt; render; pty = None; write_buf = Bytes.create 65536; - pending = [] } + pending_out = []; pending_in = [] } let grid t = Vt.grid t.vt let renderer t = t.render +let enqueue_pending t s = + if s <> "" then t.pending_in <- s :: t.pending_in + +let rec take_pending t = + match t.pending_out with + | s :: rest -> t.pending_out <- rest; Some s + | [] -> + match t.pending_in with + | [] -> None + | inn -> + t.pending_out <- List.rev inn; + t.pending_in <- []; + take_pending t + (* Apply an action the parser raised. *) let act t = function - | Vt.Reply s -> t.pending <- t.pending @ [s] + | Vt.Reply s -> enqueue_pending t s | _ -> () let feed t s = @@ -79,7 +96,15 @@ let pump t = match t.pty with | None -> [] | Some p -> - t.pending <- flush_pending ~write:(P.write_string p) t.pending; + let rec flush_self () = + match take_pending t with + | None -> () + | Some s -> + let left = unwritten ~wrote:(P.write_string p s) s in + if left = "" then flush_self () + else t.pending_out <- left :: t.pending_out + in + flush_self (); let acts = ref [] in (try while true do @@ -92,7 +117,7 @@ let pump t = List.iter (act t) !acts; !acts -let write t s = if s <> "" then t.pending <- t.pending @ [s] +let write t s = enqueue_pending t s let resize t ~cols ~rows = G.resize (Vt.grid t.vt) ~cols ~rows; diff --git a/platform/native/lui_resource/lui_resource.ml b/platform/native/lui_resource/lui_resource.ml index b21f4c51..e2eddb0a 100644 --- a/platform/native/lui_resource/lui_resource.ml +++ b/platform/native/lui_resource/lui_resource.ml @@ -249,6 +249,7 @@ let spawn c e load = Some task with exn -> e.state <- Failed (Printexc.to_string exn); + notify e; None let add_watcher e scope = function diff --git a/platform/native/lui_resource/test/lui_resource_test.ml b/platform/native/lui_resource/test/lui_resource_test.ml index 367128ec..fa038c2a 100644 --- a/platform/native/lui_resource/test/lui_resource_test.ml +++ b/platform/native/lui_resource/test/lui_resource_test.ml @@ -283,12 +283,20 @@ let test_executor_raise_marks_failed () = let exec _ = failwith "domain cap" in let c = R.create ~executor:exec () in let sc = scope () in - let e = R.request c ~scope:sc "k" ~load:(fun _ -> Ok "v") in + let seen = ref None in + let e = + R.request c ~scope:sc "k" + ~on_state:(fun e -> seen := Some (R.state e)) + ~load:(fun _ -> Ok "v") + in (match R.state e with | R.Failed msg -> check' bool "failed after spawn raise" true (String.length msg > 0) | _ -> fail "expected Failed, not stuck Loading"); + (match !seen with + | Some (R.Failed _) -> () + | _ -> fail "watcher should see Failed"); check' (option string) "not published" None (R.get c "k"); Signal.dispose_scope sc; R.unregister c diff --git a/platform/native/lui_window/main.ml b/platform/native/lui_window/main.ml index c2f03d1b..916dd2fd 100644 --- a/platform/native/lui_window/main.ml +++ b/platform/native/lui_window/main.ml @@ -1019,7 +1019,7 @@ let () = (fold_wait_ms ~ui_deadline_ms:(Ui.next_input_deadline s.ui) ~clock_delay_ms:(Lui_clock.next_delay (Lui_app.clock s.driver.app)) - ~host_wants_frame:(Ui.want_frame s.ui s.host) + ~host_wants_frame:false ~now_ms)); incr iter_no; if not !headless && continue () then begin diff --git a/platform/native/lui_window_linux/dune b/platform/native/lui_window_linux/dune index 925d6424..ef933917 100644 --- a/platform/native/lui_window_linux/dune +++ b/platform/native/lui_window_linux/dune @@ -21,6 +21,9 @@ lui_sdl3 ocaml-signal unix) + (foreign_stubs + (language c) + (names lui_linux_stubs)) (preprocess (pps lui_ppx)) (wrapped false)) diff --git a/platform/native/lui_window_linux/lui_linux.ml b/platform/native/lui_window_linux/lui_linux.ml index 7eae22b7..bfdba13b 100644 --- a/platform/native/lui_window_linux/lui_linux.ml +++ b/platform/native/lui_window_linux/lui_linux.ml @@ -33,11 +33,16 @@ let noop_renderer : Lui_host.renderer = (fun (s : Lui_scene.t) -> Bytes.make (s.Lui_scene.width * s.Lui_scene.height * 4) '\000') } -let dbus_wait_cap_ms ~connected ~watching next = - if connected && not watching then Float.min next 50. else next +let dbus_wait_cap_ms ~connected ~watching ~gtk next = + (* GTK/AppIndicator run on a GLib context that is not covered by + the two libdbus fds, so a bounded wake remains while GTK is up. + Pure D-Bus (a11y/session) with a watched fd stays idle-quiet. *) + if gtk || (connected && not watching) then Float.min next 50. else next -(* Unix.file_descr is the raw fd on this platform. *) -external fd_of_int : int -> Unix.file_descr = "%identity" +(* [select_readable fds timeout] waits until a descriptor in [fds] is + readable. [timeout] < 0 waits forever. Returns the select count + (0 on timeout / empty set). *) +external select_readable : int list -> float -> int = "lui_linux_select_readable" (* AT-triggered action -> protocol event, mirroring the pointer path: a screen reader's "press" lands exactly where a click's [Press] diff --git a/platform/native/lui_window_linux/lui_linux_stubs.c b/platform/native/lui_window_linux/lui_linux_stubs.c new file mode 100644 index 00000000..902325a9 --- /dev/null +++ b/platform/native/lui_window_linux/lui_linux_stubs.c @@ -0,0 +1,62 @@ +/* Native helpers for the Linux window host: select() on raw ints so + the wait loop can watch D-Bus fds without a magic file_descr cast. */ + +#include +#include +#include + +#if defined(__linux__) + +#include +#include + +CAMLprim value lui_linux_select_readable(value vfds, value vtimeout) +{ + CAMLparam2(vfds, vtimeout); + int fds[FD_SETSIZE]; + int n = 0, i, maxfd = -1, r; + fd_set rfds; + double t; + struct timeval tv, *ptv = NULL; + + for (; vfds != Val_emptylist && n < FD_SETSIZE; vfds = Field(vfds, 1)) { + int fd = Int_val(Field(vfds, 0)); + if (fd >= 0 && fd < FD_SETSIZE) + fds[n++] = fd; + } + t = Double_val(vtimeout); + if (t >= 0.) { + if (t > 1e6) + t = 1e6; + tv.tv_sec = (time_t)t; + tv.tv_usec = (suseconds_t)((t - (double)tv.tv_sec) * 1e6); + if (tv.tv_usec < 0) + tv.tv_usec = 0; + ptv = &tv; + } + + caml_release_runtime_system(); + do { + FD_ZERO(&rfds); + maxfd = -1; + for (i = 0; i < n; i++) { + FD_SET(fds[i], &rfds); + if (fds[i] > maxfd) + maxfd = fds[i]; + } + r = (maxfd < 0) ? 0 : select(maxfd + 1, &rfds, NULL, NULL, ptv); + } while (r < 0 && errno == EINTR); + caml_acquire_runtime_system(); + CAMLreturn(Val_int(r < 0 ? 0 : r)); +} + +#else + +CAMLprim value lui_linux_select_readable(value vfds, value vtimeout) +{ + (void)vfds; + (void)vtimeout; + return Val_int(0); +} + +#endif diff --git a/platform/native/lui_window_linux/main.ml b/platform/native/lui_window_linux/main.ml index 092dcfcd..00459132 100644 --- a/platform/native/lui_window_linux/main.ml +++ b/platform/native/lui_window_linux/main.ml @@ -147,6 +147,7 @@ let () = [Lui_window.Renderer_ladder]; this supplies the per-rung init and the window re-fit the demote path needs. *) let blit_cell = ref None in + let last_pix = ref Bytes.empty in let vk_cell = ref None in let gctx_cell = ref None in let mk_blit () = @@ -220,7 +221,7 @@ let () = (Lui_vulkan.driver v) (Lui_vulkan.dual v); vk_cell := Some v; let b = mk_blit () in - let last_pix = ref Bytes.empty in + last_pix := Bytes.empty; attempt { Lui_host.name = "lui_vulkan+blit"; render = @@ -327,7 +328,8 @@ let () = (match !vk_cell with | Some v -> Lui_vulkan.release v; vk_cell := None | None -> ()); - blit_cell := None) + blit_cell := None; + last_pix := Bytes.empty) ~before_walk:(fun rest -> (* Re-flag the window for the rung about to be tried. *) Lui_blit.Popups.clear popups; @@ -473,9 +475,11 @@ let () = | Some b -> (match Lui_blit.resize b ~w:dw ~h:dh with | Ok () -> () - | Error m -> - warn "blit resize: %s" m; - blit_cell := None) + | Error m -> warn "blit resize: %s" m); + (* Same-size moves still replace the SDL texture; drop the + damage baseline so the next present re-uploads. Keep the + cell so a later resize can retry. *) + last_pix := Bytes.empty | None -> ()); (* Recreate only the offscreen frame image; keep the Vulkan instance, device and pipelines. *) @@ -584,21 +588,19 @@ let () = || ax_transport <> Lui_ax_linux.Offline in let dbus_fds = - List.filter_map - (fun fd -> Option.map Lui_linux.fd_of_int fd) + List.filter_map Fun.id [ Lui_shell_linux.unix_fd (); Lui_ax_linux.unix_fd ax ] in let dbus_watching = dbus_fds <> [] in + let gtk_pump = Lui_shell_linux.gui_available () in if dbus_watching && not headless then ignore (Domain.spawn (fun () -> let rec loop () = - match Unix.select dbus_fds [] [] (-1.) with - | [], _, _ -> loop () - | _ -> + if Lui_linux.select_readable dbus_fds (-1.) > 0 then (try Sdl.run_on_main_thread ignore with _ -> ()); - Unix.sleepf 0.01; - loop () + Unix.sleepf 0.01; + loop () in loop ())); let e = Sdl.Event.create () in @@ -650,9 +652,9 @@ let () = (if headless then " [headless]" else ""); while continue () do let t0 = perf_s () in - let now_s = t0 in - let now_ms = now_s *. 1000. in drain_events (); + let now_s = perf_s () in + let now_ms = now_s *. 1000. in ignore (Lui_app.tick_clock app ~now:now_ms); Lui_host.tick host ~now:now_s; ignore (Lui_resource.drain_all ()); @@ -723,7 +725,7 @@ let () = end else next in let next = Lui_linux.dbus_wait_cap_ms ~connected:dbus_connected - ~watching:dbus_watching next + ~watching:dbus_watching ~gtk:gtk_pump next in if (if next = Float.infinity then Sdl.wait_event (Some e) else Sdl.wait_event_timeout (Some e) diff --git a/platform/native/lui_window_linux/test/lui_window_linux_test.ml b/platform/native/lui_window_linux/test/lui_window_linux_test.ml index d6630404..e7db33b4 100644 --- a/platform/native/lui_window_linux/test/lui_window_linux_test.ml +++ b/platform/native/lui_window_linux/test/lui_window_linux_test.ml @@ -87,15 +87,19 @@ let test_dbus_wait_cap () = Alcotest.(check bool) "idle without bus is unbounded" true (Float.is_infinite (Lui_linux.dbus_wait_cap_ms ~connected:false ~watching:false - Float.infinity)); + ~gtk:false Float.infinity)); Alcotest.(check (float 0.001)) "connected without fd watch caps at 50" 50. (Lui_linux.dbus_wait_cap_ms ~connected:true ~watching:false - Float.infinity); + ~gtk:false Float.infinity); Alcotest.(check bool) "watching fds does not cap" true (Float.is_infinite (Lui_linux.dbus_wait_cap_ms ~connected:true ~watching:true - Float.infinity)) + ~gtk:false Float.infinity)); + Alcotest.(check (float 0.001)) "GTK keeps a bounded wake for GLib" + 50. + (Lui_linux.dbus_wait_cap_ms ~connected:true ~watching:true + ~gtk:true Float.infinity) (* The real walk/demote path the driver uses: injected init failures step down the ladder at startup, and a mid-render device loss diff --git a/platform/native/lui_window_windows/main.ml b/platform/native/lui_window_windows/main.ml index f614a876..f8c32d15 100644 --- a/platform/native/lui_window_windows/main.ml +++ b/platform/native/lui_window_windows/main.ml @@ -502,10 +502,11 @@ let () = (if headless then " [headless]" else ""); while continue () do let t0 = perf_s () in - let now_ms = t0 *. 1000. in drain_events (); + let now_s = perf_s () in + let now_ms = now_s *. 1000. in ignore (Lui_app.tick_clock app ~now:now_ms); - Lui_host.tick host ~now:t0; + Lui_host.tick host ~now:now_s; ignore (Lui_resource.drain_all ()); Ui.tick_scrollbars ui ~now_ms; (* Patches queued by dispatched events land in the store here; @@ -578,9 +579,13 @@ let () = if remaining > 0.5 then Lui_win32.delay_ms (int_of_float remaining) end - else if wait = Float.infinity then Lui_win32.delay_ms 4 - else if wait > 0.5 then - Lui_win32.delay_ms (int_of_float (Float.ceil wait)) + else begin + (* delay_ms is Sleep: cap idle so window messages still drain + while folding timer deadlines down to an immediate wake. *) + let cap = if wait = Float.infinity then 4. else Float.min wait 4. in + if cap > 0.5 then + Lui_win32.delay_ms (int_of_float (Float.ceil cap)) + end end done; stats_report st (Renderer_ladder.active ladder_t).renderer.Lui_host.name; From 1783f78a82ff62078106c60881955bfe7f7a3894 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Fri, 20 Nov 2026 21:04:43 +0000 Subject: [PATCH 13/13] Assign resource tasks before notifying spawn-failure watchers. A Failed on_state callback can reload immediately. Notifying from spawn before request/reload stored the result overwrote the retry's task with None, so scope disposal could not cancel it. Co-authored-by: Tienson Qin --- platform/native/lui_resource/lui_resource.ml | 10 ++++++- .../lui_resource/test/lui_resource_test.ml | 29 +++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/platform/native/lui_resource/lui_resource.ml b/platform/native/lui_resource/lui_resource.ml index e2eddb0a..c48b0fca 100644 --- a/platform/native/lui_resource/lui_resource.ml +++ b/platform/native/lui_resource/lui_resource.ml @@ -249,9 +249,15 @@ let spawn c e load = Some task with exn -> e.state <- Failed (Printexc.to_string exn); - notify e; None +(* Watchers may reload from Failed; assign [e.task] before notifying + so a retry's handle is not overwritten by this spawn's [None]. *) +let notify_if_spawn_failed e = + match e.state, e.task with + | Failed _, None -> notify e + | _ -> () + let add_watcher e scope = function | None -> () | Some f -> @@ -291,6 +297,7 @@ let request c ~scope ?on_state key ~load = attach c e scope; add_watcher e scope on_state; e.task <- spawn c e load; + notify_if_spawn_failed e; e let reload c ~scope ?on_state key ~load = @@ -311,6 +318,7 @@ let reload c ~scope ?on_state key ~load = attach c e scope; add_watcher e scope on_state; e.task <- spawn c e load; + notify_if_spawn_failed e; e let watch ~scope e f = diff --git a/platform/native/lui_resource/test/lui_resource_test.ml b/platform/native/lui_resource/test/lui_resource_test.ml index fa038c2a..df11a5ca 100644 --- a/platform/native/lui_resource/test/lui_resource_test.ml +++ b/platform/native/lui_resource/test/lui_resource_test.ml @@ -301,6 +301,33 @@ let test_executor_raise_marks_failed () = Signal.dispose_scope sc; R.unregister c +let test_executor_raise_retry_keeps_task () = + let n = ref 0 in + let exec job = + incr n; + if !n = 1 then failwith "domain cap" else job () + in + let c = R.create ~executor:exec () in + let sc = scope () in + let rec on_state e = + match R.state e with + | R.Failed _ when !n = 1 -> + ignore (R.reload c ~scope:sc ~on_state "k" ~load:(fun _ -> Ok "retry")) + | _ -> () + in + let e = R.request c ~scope:sc ~on_state "k" ~load:(fun _ -> Ok "v") in + check' bool "retry has a task" true (Option.is_some (R.task e)); + (match R.state e with + | R.Failed _ -> fail "retry should not stay Failed" + | R.Loading | R.Ready _ -> ()); + let cancelled = ref false in + (match R.task e with + | Some t -> R.task_set_on_cancel t (fun () -> cancelled := true) + | None -> fail "missing retry task"); + Signal.dispose_scope sc; + check' bool "dispose cancelled the retry" true !cancelled; + R.unregister c + let test_pool_executor () = let exec = R.executor_pool ~workers:2 () in let c = R.create ~executor:exec () in @@ -445,5 +472,7 @@ let () = [ test_case "domain" `Quick test_domain_executor; test_case "spawn raise marks failed" `Quick test_executor_raise_marks_failed; + test_case "spawn raise retry keeps task" `Quick + test_executor_raise_retry_keeps_task; test_case "bounded pool" `Quick test_pool_executor ] ); ]