open Camlet_domain let ( let* ) = Result.bind module String_set = Set.Make (String) let protocol_error ~code ~message = App_error.make ~category:Protocol ~code ~message let consistency_error ~code ~message = App_error.make ~category:Consistency ~code ~message let issue_author issue_uri = match Did.of_string (At_uri.authority issue_uri) with | Ok value -> Ok value | Error _ -> Error (App_error.make ~category:Usage ~code:"issue.view_uri_invalid" ~message: "The issue locator must be a did:plc sh.tangled.repo.issue AT URI") let ensure_issue_locator issue_uri = match (At_uri.collection issue_uri, At_uri.record_key issue_uri) with | Some "sh.tangled.repo.issue", Some _ -> issue_author issue_uri | _ -> Error (App_error.make ~category:Usage ~code:"issue.view_uri_invalid" ~message: "The issue locator must be a did:plc sh.tangled.repo.issue AT URI") let validate_coverage before after = if Int64.compare after.Issue_list.events_processed before.Issue_list.events_processed < 0 || Int64.compare after.last_cursor before.last_cursor < 0 || (before.ready && not after.ready) then Error (consistency_error ~code:"issue.coverage_regressed" ~message:"Bobbin coverage regressed during issue observation") else Ok () let paginate_states ~ports ~bobbin ~issue_uri = let rec loop ~page_count ~item_count ~cursors ~seen ~reversed ~cursor = if page_count >= 100 then Error (protocol_error ~code:"issue.view_state_page_limit" ~message:"Bobbin exceeded the issue-state page limit") else let* page = ports.Issue_view_ports.list_states bobbin issue_uri ~cursor in let item_count = item_count + List.length page.states in if item_count > 1_000 then Error (protocol_error ~code:"issue.view_state_item_limit" ~message:"Bobbin exceeded the issue-state item limit") else let rec add seen reversed = function | [] -> Ok (seen, reversed) | (state : Issue_view.state_record) :: rest -> let uri = At_uri.to_string state.Issue_view.uri in if String_set.mem uri seen then Error (consistency_error ~code:"issue.view_state_duplicate_uri" ~message: "Bobbin returned the same issue-state URI more than once") else add (String_set.add uri seen) (state :: reversed) rest in let* seen, reversed = add seen reversed page.states in match page.cursor with | None -> Ok (List.rev reversed) | Some next when String_set.mem next cursors -> Error (protocol_error ~code:"issue.view_state_cursor_repeated" ~message:"Bobbin repeated an issue-state page cursor") | Some next -> loop ~page_count:(page_count + 1) ~item_count ~cursors:(String_set.add next cursors) ~seen ~reversed ~cursor:(Some next) in loop ~page_count:0 ~item_count:0 ~cursors:String_set.empty ~seen:String_set.empty ~reversed:[] ~cursor:None let paginate_comments ~ports ~bobbin ~issue_uri = let rec loop ~page_count ~item_count ~cursors ~seen ~reversed ~cursor = if page_count >= 100 then Error (protocol_error ~code:"issue.view_comment_page_limit" ~message:"Bobbin exceeded the comment page limit") else let* page = ports.Issue_view_ports.list_comments bobbin issue_uri ~cursor in let item_count = item_count + List.length page.comments in if item_count > 10_000 then Error (protocol_error ~code:"issue.view_comment_item_limit" ~message:"Bobbin exceeded the comment item limit") else let rec add seen reversed = function | [] -> Ok (seen, reversed) | (comment : Issue_view.comment) :: rest -> let uri = At_uri.to_string comment.Issue_view.uri in if String_set.mem uri seen then Error (consistency_error ~code:"issue.view_comment_duplicate_uri" ~message: "Bobbin returned the same comment URI more than once") else add (String_set.add uri seen) (comment :: reversed) rest in let* seen, reversed = add seen reversed page.comments in match page.cursor with | None -> Ok (List.rev reversed) | Some next when String_set.mem next cursors -> Error (protocol_error ~code:"issue.view_comment_cursor_repeated" ~message:"Bobbin repeated a comment page cursor") | Some next -> loop ~page_count:(page_count + 1) ~item_count ~cursors:(String_set.add next cursors) ~seen ~reversed ~cursor:(Some next) in loop ~page_count:0 ~item_count:0 ~cursors:String_set.empty ~seen:String_set.empty ~reversed:[] ~cursor:None let same_uri left right = String.equal (At_uri.to_string left) (At_uri.to_string right) let resolve_record ports cache author = let key = Did.to_string author in match List.assoc_opt key cache with | Some origin -> Ok (origin, cache) | None -> let* origin = ports.Issue_view_ports.resolve_did author in Ok (origin, (key, origin) :: cache) let verify_states ~ports ~issue_uri ~issue_author ~repository_owner states = let accepted (state : Issue_view.state_record) = Did.equal state.Issue_view.author_did issue_author || Did.equal state.author_did repository_owner in let rec loop cache reversed = function | [] -> Ok (List.rev reversed) | (state : Issue_view.state_record) :: rest when not (accepted state) -> loop cache reversed rest | (state : Issue_view.state_record) :: rest -> if not (same_uri state.issue_uri issue_uri) then Error (consistency_error ~code:"issue.view_state_subject_mismatch" ~message: "Bobbin returned an accepted state for a different issue") else let* pds, cache = resolve_record ports cache state.author_did in let* source = ports.read_state pds state.uri in if not (Issue_view.equal_state_record state source) then Error (consistency_error ~code:"issue.view_state_source_mismatch" ~message: "Bobbin and the state-author PDS disagree on a state record") else loop cache (source :: reversed) rest in loop [] [] states let verify_comments ~ports ~issue_uri comments = let rec loop cache reversed = function | [] -> Ok (List.rev reversed) | (comment : Issue_view.comment) :: rest -> if not (same_uri comment.Issue_view.subject.uri issue_uri) then Error (consistency_error ~code:"issue.view_comment_subject_mismatch" ~message:"Bobbin returned a comment for a different issue") else let* pds, cache = resolve_record ports cache comment.author_did in let* source = ports.read_comment pds comment.uri in if not (Issue_view.equal_comment comment source) then Error (consistency_error ~code:"issue.view_comment_source_mismatch" ~message: "Bobbin and the comment-author PDS disagree on a comment") else loop cache (source :: reversed) rest in loop [] [] comments let compare_record (left : Issue_view.state_record) (right : Issue_view.state_record) = let timestamp = Timestamp.compare left.Issue_view.created_at right.created_at in if timestamp <> 0 then timestamp else String.compare (At_uri.to_string left.uri) (At_uri.to_string right.uri) let latest_state states = match List.sort compare_record states |> List.rev with | [] -> (Issue_list.Open, None) | latest :: _ -> (latest.Issue_view.state, Some latest) let compare_comment (left : Issue_view.comment) (right : Issue_view.comment) = let timestamp = Timestamp.compare left.Issue_view.created_at right.created_at in if timestamp <> 0 then timestamp else String.compare (At_uri.to_string left.uri) (At_uri.to_string right.uri) let run ~ports ~issue_uri ~bobbin = let* author = ensure_issue_locator issue_uri in let* author_pds = ports.Issue_view_ports.resolve_did author in let* issue = ports.read_issue author_pds issue_uri in if (not (same_uri issue.uri issue_uri)) || not (Did.equal issue.author_did author) then Error (consistency_error ~code:"issue.view_base_identity_mismatch" ~message:"The issue-author PDS returned a different issue identity") else let* repository = ports.view_repository issue.repository_did in if not (Repository_did.equal repository.repository_did issue.repository_did) then Error (consistency_error ~code:"issue.view_repository_mismatch" ~message:"Repository verification returned a different identity") else let* coverage_before = ports.read_coverage bobbin in let* observed_states = paginate_states ~ports ~bobbin ~issue_uri in let* observed_comments = paginate_comments ~ports ~bobbin ~issue_uri in let* states = verify_states ~ports ~issue_uri ~issue_author:author ~repository_owner:repository.owner_did observed_states in let* comments = verify_comments ~ports ~issue_uri observed_comments in let* coverage_after = ports.read_coverage bobbin in let* () = validate_coverage coverage_before coverage_after in let* observed_at = ports.now () in let state, state_record = latest_state states in Ok Issue_view. { repository ; issue ; state ; state_record ; comments = List.sort compare_comment comments ; bobbin ; coverage_before ; coverage_after ; observed_at }