sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
Deterministic workflow engine on cabal
Install
dune-project
Dependency
Authors
Maintainers
Sources
cabal-workflow-runner-v0.23.5.tar.gz
sha256=846019b2ad922fcc94cd2bba6021b400c6784fcaebfac3bfcfc1461cd39b1ec9
doc/src/cabal_workflow_runner.runner/agent_control_fd.ml.html
Source file agent_control_fd.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289open Cabal_workflow_runner type binding = { run_id : string; workflow_digest : Agent_control.digest; session_nonce : string; host_binding_digest : Agent_control.digest; control_policy_digest : Agent_control.digest; control_protocol_version : int; } type t = { fd : Unix.file_descr; clock : float Eio.Time.clock_ty Eio.Resource.t; deadline_at : float; mutable next_id : int; mutable failed : bool; } let max_frame_bytes = 1024 * 1024 let ( let* ) = Result.bind let fail channel message = channel.failed <- true; Error message let wait channel read = let remaining = channel.deadline_at -. Eio.Time.now channel.clock in if remaining <= 0. then fail channel "control transport deadline expired" else try Eio.Time.with_timeout_exn channel.clock remaining (fun () -> if read then Eio_unix.await_readable channel.fd else Eio_unix.await_writable channel.fd); Ok () with | Eio.Time.Timeout -> fail channel "control transport deadline expired" | Eio.Cancel.Cancelled _ as exn -> channel.failed <- true; raise exn | Unix.Unix_error _ -> fail channel "control transport polling failed" let read_exact channel length = let bytes = Bytes.create length in let rec loop offset = if offset = length then Ok bytes else let* () = wait channel true in try let count = Unix.read channel.fd bytes offset (length - offset) in if count = 0 then fail channel "control transport closed" else loop (offset + count) with | Unix.Unix_error (Unix.EINTR, _, _) -> loop offset | Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _) -> loop offset | Unix.Unix_error _ -> fail channel "control transport read failed" in loop 0 let write_exact channel bytes = let length = Bytes.length bytes in let rec loop offset = if offset = length then Ok () else let* () = wait channel false in try let count = Unix.write channel.fd bytes offset (length - offset) in if count = 0 then fail channel "control transport closed" else loop (offset + count) with | Unix.Unix_error (Unix.EINTR, _, _) -> loop offset | Unix.Unix_error ((Unix.EAGAIN | Unix.EWOULDBLOCK), _, _) -> loop offset | Unix.Unix_error _ -> fail channel "control transport write failed" in loop 0 let int_of_header bytes = let byte index = Char.code (Bytes.get bytes index) in byte 0 lsl 24 lor byte 1 lsl 16 lor byte 2 lsl 8 lor byte 3 let header_of_int length = Bytes.init 4 (function | 0 -> Char.chr ((length lsr 24) land 255) | 1 -> Char.chr ((length lsr 16) land 255) | 2 -> Char.chr ((length lsr 8) land 255) | _ -> Char.chr (length land 255)) let read_frame channel = let* header = read_exact channel 4 in let length = int_of_header header in if length <= 0 || length > max_frame_bytes then fail channel "control frame length is invalid" else let* bytes = read_exact channel length in let raw = Bytes.to_string bytes in try let json = Yojson.Safe.from_string raw in let* canonical = Canonical_json.to_string json in if canonical <> raw then fail channel "control frame is not canonical JSON" else Ok json with Yojson.Json_error _ -> fail channel "control frame is invalid JSON" let write_frame channel json = let* canonical = Canonical_json.to_string json in let length = String.length canonical in if length <= 0 || length > max_frame_bytes then fail channel "control frame exceeds 1 MiB" else let* () = write_exact channel (header_of_int length) in write_exact channel (Bytes.of_string canonical) let exact_fields expected = function | `Assoc fields -> let actual = List.map fst fields |> List.sort String.compare and expected = List.sort String.compare expected in if actual = expected then Ok fields else Error "control reply has missing, duplicate, or unknown fields" | _ -> Error "control reply must be an object" let field name fields = match List.assoc_opt name fields with | Some value -> Ok value | None -> Error ("control reply lacks " ^ name) let decode_frame channel = function | Ok value -> Ok value | Error message -> fail channel message let call channel ~kind ~body = if channel.failed then Error "control transport has failed" else let id = channel.next_id in channel.next_id <- id + 1; let request = `Assoc [ "body", body; "id", `Int id; "kind", `String kind; "v", `Int 1 ] in let* () = write_frame channel request in let* reply = read_frame channel in match reply with | `Assoc fields -> let ok = List.assoc_opt "ok" fields in let expected = match ok with Some (`Bool true) -> [ "body"; "id"; "ok"; "v" ] | _ -> [ "error_code"; "id"; "ok"; "v" ] in let* fields = decode_frame channel (exact_fields expected reply) in let* version = decode_frame channel (field "v" fields) in let* reply_id = decode_frame channel (field "id" fields) in if version <> `Int 1 then fail channel "control reply version mismatch" else if reply_id <> `Int id then fail channel "control reply id mismatch" else (match ok with | Some (`Bool true) -> decode_frame channel (field "body" fields) | Some (`Bool false) -> let* code = decode_frame channel (field "error_code" fields) in (match code with | `String value when value <> "" && String.length value <= 128 -> Error ("controller refused request: " ^ value) | _ -> fail channel "control error code is invalid") | _ -> fail channel "control reply ok field is invalid") | _ -> fail channel "control reply must be an object" let binding_to_json binding = `Assoc [ "control_policy_digest", `String binding.control_policy_digest; "control_protocol_version", `Int binding.control_protocol_version; "host_binding_digest", `String binding.host_binding_digest; "run_id", `String binding.run_id; "session_nonce", `String binding.session_nonce; "workflow_digest", `String binding.workflow_digest ] let private_connected_socket fd = try let stats = Unix.fstat fd in let local = Unix.getsockname fd and peer = Unix.getpeername fd in stats.Unix.st_kind = Unix.S_SOCK && local = Unix.ADDR_UNIX "" && peer = Unix.ADDR_UNIX "" with Unix.Unix_error _ -> false let valid_id value = value <> "" && String.length value <= 512 && not (String.contains value '\000') && String.is_valid_utf_8 value let validate_binding binding = if not (valid_id binding.run_id) then Error "control run id is invalid" else if not (valid_id binding.session_nonce) then Error "control session nonce is invalid" else let* () = Agent_control.validate_digest "control workflow digest" binding.workflow_digest in let* () = Agent_control.validate_digest "control host binding digest" binding.host_binding_digest in let* () = Agent_control.validate_digest "control policy digest" binding.control_policy_digest in if binding.control_protocol_version < 1 then Error "control protocol version is invalid" else Ok () let create ~sw ~clock ~fd ~binding ~deadline_at = let* () = validate_binding binding in if not (Float.is_finite deadline_at) || deadline_at <= Unix.gettimeofday () then Error "control deadline must be finite and in the future" else if not (private_connected_socket fd) then Error "control FD must be an inherited connected unnamed Unix socket" else ( ignore (Eio_unix.Net.import_socket_stream ~sw ~close_unix:false fd); let channel = { fd; clock; deadline_at; next_id = 0; failed = false } in Unix.set_nonblock fd; match call channel ~kind:"hello" ~body:(binding_to_json binding) with | Ok (`Assoc []) -> Ok channel | Ok _ -> let error = fail channel "control hello reply must have an empty body" in (try Unix.close channel.fd with Unix.Unix_error _ -> ()); error | Error _ as error -> (try Unix.close channel.fd with Unix.Unix_error _ -> ()); error) let close channel = try Unix.close channel.fd with Unix.Unix_error _ -> () let decode channel name decoder body = match decoder body with | Ok value -> Ok value | Error _ -> fail channel (name ^ " reply body is invalid") let option_string_json = function None -> `Null | Some value -> `String value let run_request_public (request : Run_control.request) = `Assoc [ "executable_digest", option_string_json request.executable_digest; "request_ref", `String request.request_digest; "selected_input_digest", option_string_json request.selected_input_digest ] let run_effect_public (run_effect : Run_control.run_effect) = `Assoc [ "effect_ref", `String run_effect.effect_id; "profile_ref", `String run_effect.profile.profile_id; "request_ref", `String run_effect.request.request_digest ] let decode_public_receipt channel run_effect body = let* fields = decode_frame channel (exact_fields [ "executable"; "result" ] body) in let* result_json = decode_frame channel (field "result" fields) in let* executable_json = decode_frame channel (field "executable" fields) in let* result = decode channel "Run result" Run_control.run_result_of_json result_json in let* executable = decode channel "Run executable" Run_control.executable_identity_of_json executable_json in decode channel "Run receipt" Fun.id (Run_control.make_receipt ~run_effect ~result ~executable) let decode_public_reconciliation channel run_effect body = let* fields = decode_frame channel (match body with | `Assoc fields -> Ok fields | _ -> Error "Run reconciliation reply must be an object") in let* status = decode_frame channel (field "status" fields) in match status with | `String "pending" | `String "unknown" as status -> let* _ = decode_frame channel (exact_fields [ "status" ] body) in Ok (if status = `String "pending" then Run_control.Pending else Unknown) | `String "recovered" -> let* fields = decode_frame channel (exact_fields [ "executable"; "result"; "status" ] body) in let public_receipt = `Assoc [ "executable", List.assoc "executable" fields; "result", List.assoc "result" fields ] in let* receipt = decode_public_receipt channel run_effect public_receipt in Ok (Run_control.Recovered receipt) | _ -> fail channel "Run reconciliation status is invalid" let agent_control channel = Agent_control. { admit = (fun request -> let* body = call channel ~kind:"agent.admit" ~body:(Agent_control.admit_request_to_json request) in decode channel "agent admission" Agent_control.admission_of_json body); record = (fun completion -> let* body = call channel ~kind:"agent.record" ~body:(Agent_control.completion_to_json completion) in decode channel "agent receipt" Agent_control.terminal_receipt_of_json body); reconcile = (fun attempt -> let* body = call channel ~kind:"agent.reconcile" ~body:(Agent_control.attempt_ref_to_json attempt) in decode channel "agent reconciliation" Agent_control.reconciliation_of_json body); check_stop = (fun () -> let* body = call channel ~kind:"agent.check_stop" ~body:(`Assoc []) in decode channel "agent STOP" Agent_control.stop_of_json body) } let run_control channel = Run_control. { resolve = (fun request -> let* body = call channel ~kind:"run.resolve" ~body:(run_request_public request) in decode channel "Run profile" Run_control.profile_of_json body); execute = (fun run_effect -> let* body = call channel ~kind:"run.execute" ~body:(run_effect_public run_effect) in decode_public_receipt channel run_effect body); reconcile = (fun run_effect -> let* body = call channel ~kind:"run.reconcile" ~body:(run_effect_public run_effect) in decode_public_reconciliation channel run_effect body) }
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>