diff --git a/logseq_overlay_db/lib/database.ml b/logseq_overlay_db/lib/database.ml index 19d05751..60410b12 100644 --- a/logseq_overlay_db/lib/database.ml +++ b/logseq_overlay_db/lib/database.ml @@ -1487,6 +1487,7 @@ type hydration_cache = ; block_presence : (string, bool) Hashtbl.t ; append_nodes : (string, (Types.append_node * Graph.Uuid.t) Uuid_map.t) Hashtbl.t ; insert_orders : (string, string Uuid_map.t) Hashtbl.t + ; task_tag_targets : (string, unit Uuid_map.t) Hashtbl.t ; property_class : int option ; property_definitions : (string, Graph.property_summary option) Hashtbl.t ; ident_entities : (string, int) Hashtbl.t @@ -1509,6 +1510,7 @@ let hydration_cache ?(index_idents = true) database = ; block_presence = Hashtbl.create 8 ; append_nodes = Hashtbl.create 4 ; insert_orders = Hashtbl.create 4 + ; task_tag_targets = Hashtbl.create 4 ; property_class = (if index_idents then Hashtbl.find_opt ident_entities "logseq.class/Property" @@ -1837,6 +1839,12 @@ let status_entity database = function | Legacy_status_ident ident -> entity_of_ident database ident ;; +let task_class_ident = "logseq.class/Task" + +let task_class_uuid database = + Option.bind (entity_of_ident database task_class_ident) (uuid_of_entity database) +;; + let task_status_of_datoms database datoms = match one_in_datoms datoms "logseq.property/status" with | Some (Datascript.Ref entity) -> @@ -2525,6 +2533,40 @@ let cached_insert_orders cache (record:outbox_record) = memoized cache.insert_orders (Graph.Uuid.to_string record.mutation_id)(fun()->assigned_orders record) ;; +let insert_temp_id uuid = "overlay-insert:" ^ Graph.Uuid.to_string uuid + +let record_changes_task_tag ?cache ?(submitted = false) (record : outbox_record) = + let targets () = + let module J = Transit_core.Json in + let source = + if submitted then Option.value record.protected_transaction ~default:record.normalized_transaction + else record.normalized_transaction + in + match Transit_native.Transit.Json.of_string source with + | J.Array operations -> + List.fold_left + (fun targets -> function + | J.Array [J.Keyword "db/add"; J.String target; J.Keyword "block/tags"; J.Keyword tag] + | J.Array [J.Keyword "db/retract"; J.Array [J.Keyword "block/uuid"; J.Uuid target]; + J.Keyword "block/tags"; J.Keyword tag] + when String.equal tag task_class_ident -> Uuid_map.add target () targets + | _ -> targets) + Uuid_map.empty operations + | _ -> invalid_arg "invalid outbox transaction shape" + in + let targets = lazy (match cache with + | None -> targets () + | Some cache -> memoized cache.task_tag_targets (Graph.Uuid.to_string record.mutation_id) targets) + in + fun uuid -> + let target = + match record.mutation with + | Types.Append_journal _ -> insert_temp_id uuid + | _ -> Graph.Uuid.to_string uuid + in + Uuid_map.mem target (Lazy.force targets) +;; + let cached_append_node cache (record:outbox_record) root uuid = let nodes=memoized cache.append_nodes (Graph.Uuid.to_string record.mutation_id)(fun()-> let rec index parent found (node:Types.append_node) = @@ -2949,7 +2991,15 @@ let logical_block_at ?cache ?initial (snapshot : snapshot) uuid = |Some status->(match status_entity authoritative (Types.Status_uuid status) with None->inserted.block.properties |Some entity->set_property_values ?cache authoritative "logseq.property/status" [Datascript.Ref entity] inserted.block.properties) in - Some{inserted with block={inserted.block with properties};task_status=Option.map(fun id->Types.Status_uuid id)node.task_status}) + let tags = + if not (record_changes_task_tag ?cache ~submitted:true record uuid) + then inserted.block.tags + else + (match task_class_uuid authoritative with + | None -> inserted.block.tags + | Some task -> List.sort_uniq Graph.Uuid.compare (task :: inserted.block.tags)) + in + Some{inserted with block={inserted.block with properties; tags};task_status=Option.map(fun id->Types.Status_uuid id)node.task_status}) | Insert_blocks { parent; tree; asset; _ } -> let page = match record.effect_footprint.page_uuids with @@ -3152,6 +3202,13 @@ let logical_block_at ?cache ?initial (snapshot : snapshot) uuid = { value.block with updated_at_ms = record.intent_time_ms ; properties + ; tags = + (if not (record_changes_task_tag ?cache ~submitted:true record uuid) + then value.block.tags + else match task_class_uuid authoritative with + | None -> value.block.tags + | Some task -> + List.filter (fun tag -> not (Graph.Uuid.equal tag task)) value.block.tags) } ; task_status = None }) @@ -5229,6 +5286,7 @@ let published_asset_matches descriptor version = ;; let rec local_operations + ?(changes_task_tag = fun _ -> true) ~append_create ~fingerprint:_ ~intent_time_ms @@ -5264,13 +5322,19 @@ let rec local_operations ~intent_time_ms ~next_tx ~orders ~effect_footprint ~delete_artifacts:None mutation in let creation=if append_create then run(Types.Create_journal_page{mutation_id;page;title;journal_day}) else [] in let rec add_tree parent_ref (node:Types.append_node) = - let node_ref=uuid_temp "overlay-insert" node.uuid in + let node_ref=Datascript.Temp_id (insert_temp_id node.uuid) in let fields=Datascript.[ Add(node_ref,"block/uuid",Uuid(Graph.Uuid.to_string node.uuid));Add(node_ref,"block/title",String node.title); Add(node_ref,"block/parent",Ref_to parent_ref);Add(node_ref,"block/page",Ref_to(uuid_lookup page)); Add(node_ref,"block/order",String(assigned_order orders node.uuid)); Add(node_ref,"block/created-at",Int64 intent_time_ms);Add(node_ref,"block/updated-at",Int64 intent_time_ms); Add(node_ref,"block/tx-id",Int64(Int64.of_int next_tx))] in + let fields = match node.task_status with + | None -> fields + | Some _ when changes_task_tag node.uuid -> + Datascript.Add (node_ref, "block/tags", Ref_to (Ident task_class_ident)) :: fields + | Some _ -> fields + in Seq.append(List.to_seq fields)(Rrbvec.to_seq node.children |> Seq.flat_map(add_tree node_ref)) in let insertion=add_tree(uuid_lookup page)root |> List.of_seq in let statuses=Overlay_planner.append_nodes root |> Rrbvec.to_seq @@ -5401,11 +5465,28 @@ let rec local_operations ; Add (uuid_lookup block, "block/updated-at", Int64 (Int64.of_int now)) ; Add (uuid_lookup block, "block/tx-id", Int64 (Int64.of_int next_tx)) ] + @ (if changes_task_tag block + then [Retract (uuid_lookup block, "block/tags", Some (Ref_to (Ident task_class_ident)))] + else []) in operations ;; +let record_operations (record : outbox_record) = + local_operations + ~changes_task_tag:(record_changes_task_tag record) + ~append_create:record.append_create + ~fingerprint:record.fingerprint + ~intent_time_ms:record.intent_time_ms + ~next_tx:record.planned_tx + ~orders:(assigned_orders record) + ~effect_footprint:record.effect_footprint + ~delete_artifacts:record.delete_artifacts + record.mutation +;; + let normalized_transaction_in + ?changes_task_tag ~append_create authoritative_database ~fingerprint @@ -5417,6 +5498,7 @@ let normalized_transaction_in mutation = local_operations + ?changes_task_tag ~append_create ~fingerprint ~intent_time_ms @@ -6037,10 +6119,12 @@ let planned_effect database context ~now = function | Types.Append_journal{page;root;journal_day;_}-> let resolved=resolve_append_page context.read_snapshot ~candidate:page ~day:journal_day in let nodes=Overlay_planner.append_nodes root in + let has_task_class = Option.is_some (task_class_uuid (authoritative_database database)) in let invalid=Rrbvec.find_map(fun(node:Types.append_node)-> if append_identity_present context.read_snapshot node.uuid then Some "An append block UUID is already present." else match node.task_status with |Some status when task_status_at context.read_snapshot status=None ->Some "Append status is missing or recycled." + |Some _ when not has_task_class -> Some "Built-in Task class is missing or has no UUID." |_->None) nodes in (match invalid,read_page context resolved with | Some message,_->Error(Types.Invalid_local_mutation message) @@ -6074,6 +6158,8 @@ let planned_effect database context ~now = function "Status is missing, recycled or belongs to another property") | Some _ -> Ok ({ block_uuids = [ block ]; page_uuids = []; structure_interests = [] }, None))) + | Clear_task_status _ when Option.is_none (task_class_uuid (authoritative_database database)) -> + Error (Types.Invalid_local_mutation "Built-in Task class is missing or has no UUID.") | Types.Save_block { block; _ } | Clear_task_status { block; _ } -> Ok ({ block_uuids = [ block ]; page_uuids = []; structure_interests = [] }, None) | Insert_blocks { tree; parent; asset; _ } -> @@ -7385,11 +7471,23 @@ let authoritative_import_matches | _ -> false) ;; -let authoritative_satisfies_mutation_in database = function +let promised_task_tag_satisfied database changes_task_tag ~present (value : Types.block_record) = + not (changes_task_tag value.block.uuid) + || (match task_class_uuid database with + | Some task -> List.exists (Graph.Uuid.equal task) value.block.tags = present + | None -> not present) +;; + +let authoritative_satisfies_record database (record : outbox_record) = + let tag_satisfied = + promised_task_tag_satisfied database (record_changes_task_tag ~submitted:true record) + in + match record.mutation with | Types.Append_journal{root;journal_day;_}-> let rec matches parent (node:Types.append_node)=match block_of_database database node.uuid with | Some value->(match parent with None->true|Some parent->value.block.parent=parent) && value.block.title=node.title && value.task_status=Option.map(fun id->Types.Status_uuid id)node.task_status + && tag_satisfied ~present:true value && Rrbvec.for_all(matches(Some node.uuid))node.children | None->false in matches None root && (match block_of_database database root.uuid with @@ -7433,7 +7531,7 @@ let authoritative_satisfies_mutation_in database = function | Clear_task_status { block; _ } -> block_of_database database block |> option_satisfies (fun (record : Types.block_record) -> - Option.is_none record.task_status) + Option.is_none record.task_status && tag_satisfied ~present:false record) ;; let mutation_supersedes earlier later = @@ -7473,7 +7571,14 @@ let accepted_record_satisfied database records (record : outbox_record) = List.exists (fun (later : outbox_record) -> mutation_supersedes record.mutation later.mutation) later - then true + then ( + match record.mutation with + | Types.Clear_task_status { block; _ } -> + let changes_task_tag = record_changes_task_tag ~submitted:true record in + not (changes_task_tag block) + || (block_of_database database block + |> option_satisfies (promised_task_tag_satisfied database changes_task_tag ~present:false)) + | _ -> true) else ( match record.mutation with | Types.Insert_blocks { parent; tree; asset; _ } -> @@ -7494,7 +7599,7 @@ let accepted_record_satisfied database records (record : outbox_record) = && Option.is_some metadata.replace_reference | _ -> false) later) - | mutation -> authoritative_satisfies_mutation_in database mutation) + | _ -> authoritative_satisfies_record database record) ;; let begin_outbox_transition database ~expected transition = @@ -7569,15 +7674,7 @@ let begin_outbox_transition database ~expected transition = let items = records |> List.concat_map (fun (record : outbox_record) -> - local_operations - ~append_create:record.append_create - ~fingerprint:record.fingerprint - ~intent_time_ms:record.intent_time_ms - ~next_tx:record.planned_tx - ~orders:(assigned_orders record) - ~effect_footprint:record.effect_footprint - ~delete_artifacts:record.delete_artifacts - record.mutation + record_operations record |> Sync_tx_codec.protected_plaintexts |> Result.get_ok |> List.map (fun value -> record.mutation_id, value)) @@ -7791,32 +7888,14 @@ let make_batch preparation encrypted = then List.rev reversed else invalid_arg "encrypted value count exceeds normalized transaction" | (record : outbox_record) :: rest -> + let operations = record_operations record in let expected = - local_operations - ~append_create:record.append_create - ~fingerprint:record.fingerprint - ~intent_time_ms:record.intent_time_ms - ~next_tx:record.planned_tx - ~orders:(assigned_orders record) - ~effect_footprint:record.effect_footprint - ~delete_artifacts:record.delete_artifacts - record.mutation + operations |> Sync_tx_codec.protected_plaintexts |> Result.get_ok |> List.length in let encrypted, remaining = take_prefix expected [] remaining in - let operations = - local_operations - ~append_create:record.append_create - ~fingerprint:record.fingerprint - ~intent_time_ms:record.intent_time_ms - ~next_tx:record.planned_tx - ~orders:(assigned_orders record) - ~effect_footprint:record.effect_footprint - ~delete_artifacts:record.delete_artifacts - record.mutation - in let protected_transaction = Sync_tx_codec.encode_protected ~db:(authoritative_database preparation.transition_owner) @@ -8100,15 +8179,7 @@ let split_prefix count values = ;; let synchronized_record_operations (record : outbox_record) = - local_operations - ~append_create:record.append_create - ~fingerprint:record.fingerprint - ~intent_time_ms:record.intent_time_ms - ~next_tx:record.planned_tx - ~orders:(assigned_orders record) - ~effect_footprint:record.effect_footprint - ~delete_artifacts:record.delete_artifacts - record.mutation + record_operations record |> Sync_tx_codec.synchronized_operations ;; @@ -8146,15 +8217,38 @@ let simulation_failure_context exn = [ "exception", authoritative_exception_kind exn; "simulationCause", cause ] ;; +let expected_record_operations database (record : outbox_record) = + match record.mutation, record.protected_transaction with + | (Append_journal _ | Clear_task_status _), Some submitted -> + let decode_error source = + Option.iter + (fun detail -> + report_warning + ("journal-integrity: frozen submission decode: " + ^ Authoritative_diagnostic.encode detail ^ "\n")) + (Authoritative_diagnostic.decode source); + [ "simulationCause", "unknown" ] + in + Result.bind + (Sync_tx_codec.protected_values record.normalized_transaction + |> Result.map_error decode_error) + (fun decrypted_values -> + Sync_tx_codec.decode ~db:database ~decrypted_values submitted + |> Result.map_error decode_error) + | _ -> Ok (synchronized_record_operations record) +;; + let expected_record_transaction_data database record = try - Ok - (Datascript.transact - ~tx_meta:[ "skip-store?", Datascript.Bool true ] - database - (synchronized_record_operations record) - : Datascript.tx_report) - .tx_data + Result.map + (fun operations -> + (Datascript.transact + ~tx_meta:[ "skip-store?", Datascript.Bool true ] + database + operations + : Datascript.tx_report) + .tx_data) + (expected_record_operations database record) with | exn -> Error (simulation_failure_context exn) ;; @@ -9167,11 +9261,17 @@ let rec logical_tree_is_present snapshot ~parent (tree : Types.block_tree) = && List.for_all (logical_tree_is_present snapshot ~parent:tree.uuid) tree.children ;; -let logical_snapshot_satisfies_mutation snapshot = function +let logical_snapshot_satisfies_record snapshot (record : outbox_record) = + let tag_satisfied = + promised_task_tag_satisfied + (Option.get snapshot.authoritative_database) (record_changes_task_tag record) + in + match record.mutation with | Types.Append_journal{root;journal_day;_}-> let rec matches parent (node:Types.append_node)=match logical_block_at snapshot node.uuid with | Some value->(match parent with None->true|Some parent->value.block.parent=parent) && value.block.title=node.title && value.task_status=Option.map(fun id->Types.Status_uuid id)node.task_status + && tag_satisfied ~present:true value && Rrbvec.for_all(matches(Some node.uuid))node.children | None->false in matches None root && (match logical_block_at snapshot root.uuid with @@ -9222,7 +9322,7 @@ let logical_snapshot_satisfies_mutation snapshot = function | Clear_task_status { block; _ } -> logical_block_at snapshot block |> option_satisfies (fun (record : Types.block_record) -> - Option.is_none record.task_status) + Option.is_none record.task_status && tag_satisfied ~present:false record) ;; let ordinary_effect_footprint ?append_owner snapshot = function @@ -9484,7 +9584,7 @@ let replan_queued_ordinary database authoritative_database records = let append_owner=match mutation with | Types.Append_journal{page;journal_day;_}->Some(append_owner snapshot page journal_day) | _->None in - if logical_snapshot_satisfies_mutation snapshot mutation + if logical_snapshot_satisfies_record snapshot record then loop accumulated @@ -9541,6 +9641,7 @@ let replan_queued_ordinary database authoritative_database records = { record with normalized_transaction = normalized_transaction_in + ~changes_task_tag:(record_changes_task_tag record) authoritative_database ~append_create ~fingerprint:record.fingerprint diff --git a/logseq_overlay_db/test/test_overlay_sync.ml b/logseq_overlay_db/test/test_overlay_sync.ml index d14c4aa3..af1ebac6 100644 --- a/logseq_overlay_db/test/test_overlay_sync.ml +++ b/logseq_overlay_db/test/test_overlay_sync.ml @@ -1235,8 +1235,22 @@ let atomic_append_submits_and_replays_complete_tree ?(remote_day=false) database let behavior="atomic append submission and authoritative echo" in T.seed_task_statuses database; let page=T.mutation_uuid 7790 and root_uuid=T.mutation_uuid 7791 and child_uuid=T.mutation_uuid 7792 in + let task = T.uuid "00000002-1282-1814-5700-000000000000" in + let plain_uuid = T.mutation_uuid 7796 in let root:Types.append_node={uuid=root_uuid;title="Atomic wire root";task_status=Some(T.status_uuid 2); - children=Rrbvec.singleton{uuid=child_uuid;title="Atomic wire child";task_status=Some(T.status_uuid 3);children=Rrbvec.empty}} in + children=Rrbvec.of_list + [{uuid=child_uuid;title="Atomic wire child";task_status=Some(T.status_uuid 3);children=Rrbvec.empty}; + {uuid=plain_uuid;title="Plain child";task_status=None;children=Rrbvec.empty}]} in + let check_tags stage = + let snapshot = Database.current_snapshot database |> T.require_ok ~behavior in + Fun.protect ~finally:(fun () -> Database.release_snapshot snapshot) (fun () -> + match Database.get_outline_blocks snapshot [root_uuid; child_uuid; plain_uuid] + |> T.require_ok ~behavior with + | [Present_block {value=root;_}; Present_block {value=child;_}; Present_block {value=plain;_}] -> + T.require (root.block.tags = [task] && child.block.tags = [task] && plain.block.tags = []) + "%s: only status-bearing capture nodes must have the built-in Task tag" stage + | _ -> Alcotest.fail (stage ^ ": capture nodes missing")) + in let mutation=Types.Append_journal{mutation_id=T.mutation_uuid 7793;page;title="2026-11-02";journal_day=20261102;root} in let expected=Database.write_precondition ~blocks:[] ~pages:[] ~scopes:[] |> T.require_ok ~behavior in let intent=Database.persist_local_intent database ~namespace:(String.make 64 'f') ~payload:"complete append wire" @@ -1269,6 +1283,7 @@ let atomic_append_submits_and_replays_complete_tree ?(remote_day=false) database match Database.get_pages snapshot [resolved_page] |> T.require_ok ~behavior with |[Present_page{value;_}]->value|_->Alcotest.fail "atomic page disappeared") in let before_submit=page_value() in + if not remote_day then check_tags "local commit"; let submitted=submit_one database local behavior in T.require(page_value()=before_submit) "transport submission changed complete append page DTO"; T.require(List.length(submission_batch_wires submitted)=1) "append split one atomic action into separate outbox transactions"; @@ -1287,6 +1302,7 @@ let atomic_append_submits_and_replays_complete_tree ?(remote_day=false) database let decrypted=Option.map(fun request->decrypt_submitted_request request behavior)crypto in (match Database.apply_authoritative database prepared ~decrypted |> T.require_ok ~behavior with | Authoritative_applied _->()|_->Alcotest.fail "atomic append echo was deferred"); + if not remote_day then check_tags "authoritative echo"; let snapshot=Database.current_snapshot database |> T.require_ok ~behavior in Fun.protect ~finally:(fun()->Database.release_snapshot snapshot)(fun()-> (match Database.get_pages snapshot [resolved_page] |> T.require_ok ~behavior with @@ -3889,6 +3905,210 @@ let stale_commit database mutation behavior = | Local_existing _ -> Alcotest.fail "Stale mutation fixture already exists" ;; +let clear_removes_only_builtin_task ?(stale = false) database = + let behavior = "clear status and only the built-in Task tag" in + let module J = Transit_core.Json in + let block = T.authoritative_block_uuid in + let task = T.uuid "00000002-1282-1814-5700-000000000000" in + let other = T.mutation_uuid 7797 in + let other_entity = J.String "other-task-tag" in + T.apply_test_transaction database ~cursor:1 + [ T.status_add other_entity "block/uuid" (J.Uuid (Graph.Uuid.to_string other)) + ; T.status_add other_entity "block/title" (J.String "Task") + ; T.status_add (T.status_lookup block) "block/tags" other_entity + ; T.status_add (T.status_lookup block) "block/tags" (J.Keyword "logseq.class/Task") + ; T.status_add (T.status_lookup block) "logseq.property/status" + (J.Keyword "logseq.property/status.todo") + ]; + let read () = + let snapshot = Database.current_snapshot database |> T.require_ok ~behavior in + Fun.protect ~finally:(fun () -> Database.release_snapshot snapshot) (fun () -> + match Database.get_outline_blocks snapshot [block] |> T.require_ok ~behavior with + | [Present_block {value;_}] -> value + | _ -> Alcotest.fail "clear target missing") + in + let before = read () in + T.require (List.mem task before.block.tags && List.mem other before.block.tags + && Option.is_some before.task_status) "clear fixture missing status or tags"; + let check stage = + let after = read () in + T.require (after.task_status = None && after.block.title = before.block.title + && after.block.tags = List.filter (fun tag -> not (Graph.Uuid.equal tag task)) before.block.tags) + "%s: clear must remove status and built-in Task without changing title or other tags" stage + in + let local = stale_commit database (Clear_task_status {mutation_id=T.mutation_uuid 7798;block}) behavior in + check "local commit"; + let submitted = submit_one database local behavior in + let submitted, cursor = + if not stale then submitted, 2 + else ( + ignore (reject_stale database submitted 2 behavior); + T.apply_test_transaction database ~cursor:2 + [J.Array [J.Keyword "db.fn/retractAttribute"; T.status_lookup block; + J.Keyword "logseq.property/status"]]; + check "after remote removed only status"; + submit_one database local behavior, 3) + in + T.require (List.length (submission_batch_wires submitted) = 1) "clear split its transaction"; + let wire = server_transaction ~mutation_id:local.mutation_id ~operation:"clear-task-status" submitted in + ignore (stale_apply_wires database cursor [wire] behavior); + check "authoritative echo" +;; + +let legacy_task_tag_submission ?(recovery = `Echo) ~append () = + let behavior = if append then "legacy append" else "legacy clear" in + let module J = Transit_core.Json in + let module Codec = Transit_native.Transit.Json in + let task = T.uuid "00000002-1282-1814-5700-000000000000" in + let block = if append then T.mutation_uuid 7821 else T.authoritative_block_uuid in + let mutation_id = T.mutation_uuid 7820 in + let title = "Legacy capture" in + let without_task_operation source = + match Codec.of_string source with + | J.Array operations -> + J.Array + (List.filter + (function + | J.Array [_; _; J.Keyword "block/tags"; J.Keyword "logseq.class/Task"] -> false + | _ -> true) + operations) + |> Codec.to_string ~mode:Codec.Verbose + | _ -> Alcotest.fail "expected a submitted Transit transaction" + in + T.with_temp_directory "overlay-legacy-task-" (fun support -> + let path = T.seed_mirror support in + let dependencies = T.dependencies ~behavior in + Eio_main.run (fun _ -> Eio.Switch.run (fun sw -> + let open_database () = + Database.inspect_mirror ~application_support_directory:support ~graph_id:T.graph_uuid + |> T.require_ok ~behavior + |> fun inspection -> Database.open_ ~sw dependencies inspection ~graph_name:"oracle-graph" + |> T.require_ok ~behavior + in + let first = open_database () in + T.seed_task_statuses first; + let baseline = sync_view_checkpoint (stale_view first behavior) |> Server_cursor.to_int64 in + let baseline = Int64.to_int baseline in + let before_title, baseline = + if append then title, baseline + else ( + T.apply_test_transaction first ~cursor:(baseline + 1) + [ T.status_add (T.status_lookup block) "block/tags" (J.Keyword "logseq.class/Task") + ; T.status_add (T.status_lookup block) "logseq.property/status" + (J.Keyword "logseq.property/status.todo") + ]; + let snapshot = Database.current_snapshot first |> T.require_ok ~behavior in + let outline = Database.get_outline_blocks snapshot [block] |> T.require_ok ~behavior in + Database.release_snapshot snapshot; + match outline with + | [Present_block {value; _}] -> value.block.title, baseline + 1 + | _ -> Alcotest.fail "legacy clear target missing") + in + let mutation = + if append then + Append_journal + { mutation_id; page = T.mutation_uuid 7822; title = "Legacy day"; journal_day = 20261102 + ; root = {uuid = block; title; task_status = Some (T.status_uuid 2); children = Rrbvec.empty} + } + else Clear_task_status {mutation_id; block} + in + let local = stale_commit first mutation behavior in + let submitted = + if recovery = `Queued then None else Some (submit_one first local behavior) + in + let protected_wire = Option.map submitted_transaction submitted in + Database.close first |> T.require_ok ~behavior; + let legacy_wire = Option.map without_task_operation protected_wire in + let sqlite = Sqlite3.db_open ~mode:`NO_CREATE path in + Fun.protect ~finally:(fun () -> T.require (Sqlite3.db_close sqlite) "close legacy fixture") + (fun () -> + let normalized = ref None in + Sqlite3.Rc.check + (Sqlite3.exec sqlite ~cb:(fun row _ -> normalized := row.(0)) + "SELECT json_extract(record, '$.normalized_transaction') FROM sync_outbox"); + let old_normalized = without_task_operation (Option.get !normalized) in + let stmt = Sqlite3.prepare sqlite + "UPDATE sync_outbox SET record = json_set(record, '$.normalized_transaction', ?, '$.protected_transaction', ?)" in + Fun.protect ~finally:(fun () -> Sqlite3.Rc.check (Sqlite3.finalize stmt)) (fun () -> + Sqlite3.Rc.check (Sqlite3.bind_values stmt [TEXT old_normalized; (match legacy_wire with None -> NULL | Some wire -> TEXT wire)]); + T.require (Sqlite3.step stmt = DONE && Sqlite3.changes sqlite = 1) + "legacy fixture did not rewrite exactly one submitted record")); + let database = open_database () in + Fun.protect ~finally:(fun () -> Database.close database |> T.require_ok ~behavior) (fun () -> + let check stage = + let snapshot = Database.current_snapshot database |> T.require_ok ~behavior in + let outline = Database.get_outline_blocks snapshot [block] |> T.require_ok ~behavior in + Database.release_snapshot snapshot; + match outline with + | [Present_block {value; _}] -> + T.require (value.block.title = before_title + && value.task_status = (if append then Some (Status_uuid (T.status_uuid 2)) else None)) + "%s: legacy title or status changed" stage; + T.require (List.mem task value.block.tags = not append) + "%s: projection must respect legacy Task tag semantics" stage + | _ -> Alcotest.failf "%s: legacy block missing" stage + in + check "after reopen"; + (match recovery with + | `Stale -> + ignore (reject_stale database (Option.get submitted) (baseline + 1) behavior); + ignore (stale_apply_wires database (baseline + 1) + [stale_remote_wire T.page_uuid "block/updated-at" (J.Int 99)] behavior); + check "after Stale replan" + | `Queued | `Echo -> ()); + if recovery <> `Echo then ( + let fresh = submit_one database local behavior in + let cursor = baseline + (if recovery = `Stale then 2 else 1) in + let barrier = { through = server_cursor cursor; checksum = checksum "1111111111111111" } in + let accepted, _ = Database.begin_outbox_transition database + ~expected:(sync_view_token (stale_view database behavior)) + (Accept_group {batch_id = submission_batch_id fresh; barrier}) |> T.require_ok ~behavior in + ignore (Database.apply_outbox_transition database accepted ~encrypted:None |> T.require_ok ~behavior); + ignore (stale_apply_wires database cursor + [server_transaction ~mutation_id ~operation:behavior fresh] behavior); + check "after recovered record echo"; + T.require (sync_view_submissions (stale_view database behavior) = []) + "recovered record did not retire") + else ( + let submitted = Option.get submitted in + let protected_wire = Option.get protected_wire in + let legacy_wire = Option.get legacy_wire in + let barrier = { through = server_cursor (baseline + 1); checksum = checksum "1111111111111111" } in + let accepted, _ = Database.begin_outbox_transition database + ~expected:(sync_view_token (stale_view database behavior)) + (Accept_group {batch_id = submission_batch_id submitted; barrier}) |> T.require_ok ~behavior in + ignore (Database.apply_outbox_transition database accepted ~encrypted:None |> T.require_ok ~behavior); + let apply wire = + let cursor = server_cursor (baseline + 1) in + let transaction = server_normalized_transaction ~mutation_id ~operation:behavior wire in + let batch = authoritative_batch ~maximum_count:1 ~maximum_bytes:16384 + ~transactions:[authoritative_transaction ~cursor ~transaction:(encoded ~maximum_bytes:16384 transaction)] + ~through:cursor ~checksum:None |> T.require_ok ~behavior in + let preparation, request = Database.begin_authoritative database + ~expected:(sync_view_token (stale_view database behavior)) batch |> T.require_ok ~behavior in + Database.apply_authoritative database preparation + ~decrypted:(Option.map (fun request -> decrypt_submitted_request request behavior) request) + in + (match apply protected_wire with + | Error (Authoritative_integrity_failure source) -> + let module D = Logseq_db_types.Authoritative_diagnostic in + (match D.decode source with + | Some diagnostic -> + T.require (List.mem "reason=payload-mismatch" (String.split_on_char ';' (D.message diagnostic) + |> List.map String.trim)) + "legacy negative control failed outside the payload guard" + | None -> Alcotest.fail "legacy negative control returned no diagnostic") + | Error _ -> Alcotest.fail "legacy negative control returned the wrong error" + | Ok _ -> Alcotest.fail "legacy submission accepted a mismatched Task tag operation"); + check "after mismatched echo"; + (match apply legacy_wire |> T.require_ok ~behavior with + | Authoritative_applied _ -> () + | Authoritative_deferred _ -> Alcotest.fail "matching legacy echo deferred"); + check "after matching echo"; + T.require (sync_view_submissions (stale_view database behavior) = []) + "matching legacy echo did not retire the outbox record"))))) +;; + let stale_mutation kind = let mutation_id = T.mutation_uuid 901 in match kind with @@ -4778,6 +4998,16 @@ let () = T.database_case "complete protected transaction echoes atomically" (atomic_append_submits_and_replays_complete_tree ~remote_day:false); T.database_case "queued append adopts remote day and follows its tail" (atomic_append_submits_and_replays_complete_tree ~remote_day:true); T.database_case "journal catalog keeps submitted page shadow" submitted_append_page_shadow_stays_in_journal_index] + ; "task tags", + [ T.database_case "clear preserves title and unrelated tags" (clear_removes_only_builtin_task ~stale:false) + ; T.database_case "stale clear still removes Task when only status was cleared remotely" (clear_removes_only_builtin_task ~stale:true) + ; Alcotest.test_case "legacy append keeps tags across reopen and echo" `Quick (legacy_task_tag_submission ~append:true) + ; Alcotest.test_case "legacy clear keeps tags across reopen and echo" `Quick (legacy_task_tag_submission ~append:false) + ; Alcotest.test_case "legacy queued append keeps tags" `Quick (legacy_task_tag_submission ~recovery:`Queued ~append:true) + ; Alcotest.test_case "legacy queued clear keeps tags" `Quick (legacy_task_tag_submission ~recovery:`Queued ~append:false) + ; Alcotest.test_case "legacy stale append keeps tags" `Quick (legacy_task_tag_submission ~recovery:`Stale ~append:true) + ; Alcotest.test_case "legacy stale clear keeps tags" `Quick (legacy_task_tag_submission ~recovery:`Stale ~append:false) + ] ; "empty authoritative compatibility", [ T.database_case "empty array advances only sync state" (empty_authoritative_preserves_logical_state "[]") ; T.database_case "empty Transit list advances only sync state" (empty_authoritative_preserves_logical_state