Skip to content

Gate P6 — Versioned transactions, branches and WHAT_IF

Status: G0 passed (2026-10-23); implementation done. G1–G5, G7 and G8 pass (2026-10-26), rerun on the reviewed tree 6eab9bd for G1, G3, G4 and G7. The independent implementation review is done: 8 majors and 6 minors fixed, 3 minors accepted (below). The phase gate passed on 2026-11-02, after P4, P5a and P5b (SPEC A11, v1.6.12).

Implementation milestones:

  1. Read sets (ExpectHeads) in the single-branch engine and the oracle: done.
  2. Multi-branch kernel: branch table, fork, commit routing, global ids and key namespace (ADR §3.1): done.
  3. Visibility and cross-branch dedup imports (contract 5, ADR §11a): done.
  4. WHAT_IF and IMPACT{Exact} on an undo journal (ADR §5), in the database and the kernel: done.
  5. Merge: sync points, merge base by containment, three-way classification, Conflict artifacts, cycle recheck, certification, read sets, observer domain and anchor_unobserved (ADR §6.3a): done.
  6. Conflict gate in the kernel and the oracle (ADR §6.4): done.
  7. Independent branch oracle and PROP13 at 10,000 seeds (ADR §11): done. It found two defects, now fixed: the canonical order on branches and A5 on merges (ADR r0.5).
  8. Store format 3: the kernel in the store, the result trailer, kernel snapshots, migration 2→3 with changed results reported, branch-keyed projections (schema 2), EventLog v1 (ADR §8): done.
  9. Protocol 4:
  10. 9a, branch-aware reads with global ids, BRANCH_ONLY (S13), per-branch certificates and BranchMismatch (T62): done.
  11. 9b, sessions and the observer on branches (protocol 4 session and anchor fields, D1 resolution, durable binding): done.
  12. 9c, protocol 4 requests (Commit with expect_cert_valid, WhatIf, Branch*, Merge, OnBranch), MCP tools, CLI commands and hook claims: done.
  13. 9d, the remaining end-to-end checks (below): done.
  14. Gate runs (G1, G3 mutation, G4, G5, G7, G8): done (below). They found four defects, all fixed: the branch oracle depended on blake3 (G1), and G4's branch stress found a cubic merge-base search and sync points that retained whole databases (ADR r0.6).

ADR 0013 r0.4 was accepted after:

  • an independent review (4 blockers, 12 majors, 6 minors);
  • a closure check (4 new majors);
  • a re-check (R1–R4);
  • a final check.

SPEC v1.6.8 mirrors it. Owner decisions:

  • D1: branches are created explicitly; a session binds to the branch named like its git branch, otherwise main.
  • D2: agents may merge, including into main; require_certified defaults to true.
  • D3: the SD17 Conflict gate moves into P6.

P6 depends on P5b (SPEC §5). P5b's gate in turn waits on P5a and P4 (A11), so P6's gate cannot pass before them. Implementation may proceed meanwhile. The live dogfood stores (protocol 3, op_log format 2) are upgraded only after the P4 dogfood report (closed 2026-11-02).

Scope

SPEC SD19 and its v1.6.8 supplement, SD17 (the P6 gate), SD13, SD14b (anchor_unobserved), §5 P6, §6.1 contracts 12–13, §6.2–§6.4 and §7. Out of scope: branch deletion, automatic git-merge detection, P7 conflict producers, ACLs.

Layer Requirement Status
G0 ADR 0013, SPEC v1.6.8, independent review Passed (2026-10-23)
G1 fmt, Clippy, tests, deny, dependency policy; loom on publication and replica paths Pass: bash scripts/check.sh on macOS and on the remote Linux host (Ubuntu 24.04) for 7f9be3a, and again for 6eab9bd after the review fixes (both hosts) (plus uncommitted documentation; no source change during the run), loom publication/reclaim models included. The first run failed the dependency policy: contextdb-oracle depended on blake3 for the merge Conflict payload hash; the caller now injects the hash (f23f600)
G2 T14, T19 (variants), T20a, T20b, T47, T48, T51, T62 (cross-branch), T72, T73, T75, S13 BRANCH_ONLY and CONFLICTED; visibility, merge-base, classification, observer-domain and rule-dedup goldens Pass: coverage table below. Criss-cross on artifact_open is covered only by PROP13
G3 PROP13 against an independent oracle branch model (10,000 seeds); PROP7 for branch ops; PROP8/I1, PROP10, PROP11 per branch; oracle Conflict gate; mutation of visibility, merge, ExpectHeads, undo journal and gate Pass: p6-g3-properties.txt (remote, scripts/g3-p6.sh): PROP13 10,000 seeds (206,327 commits, 28,484 merges, 640 conflicts); PROP7 for CreateBranch, Merge, ExpectHeads and observer batches on disjoint resources, 10,000 seeds, plus ExpectHeads vs a move of a listed head (1,163 distinguished); PROP11 per branch 10,000 seeds against the oracle over each branch's history at global epochs; PROP8 and I1 per branch in p6_branch_fuzz; PROP10 per branch through the store (2,000 seeds, with and without snapshots); the oracle gate in PROP13 and p6_conflict_gate. Mutation: every survivor closed by a new test (12 tests in p6_kernel_edges) or argued equivalent. Rerun on 6eab9bd: PROP13 10,000 seeds (206,310 commits, 28,466 merges, 627 conflicts; more merges now refused under the corrected require_certified), PROP7/PROP11/PROP10 pass; kernel mutation 352 caught, 27 missed: 24 known equivalents, 3 in the review fixes now killed
G4 Concurrent create/commit/merge stress; crash injection around the new ops; ctx migrate 2→3, interrupted and resumed Pass: p6-g4-stress.txt (scripts/stress-p6.sh, M1 Pro). ctxd-branch-stress at 1/8/32 clients × 10 min: 48,801–58,826 epochs and 15,116–29,043 merges each; coupled heads agree on every branch at every epoch, every successful CAS is linearizable in the replayed log, racing creates succeed once, replay equals the store. Crash injection before the WAL write, between write and sync, and after sync before publication with a fork, a merge or a branch commit in flight: 1,000 fired per point. ctx migrate 2→3 failed at every disk step and crashed (3,552 runs, 256 between the renames), then rerun: equal to the clean migration. Rerun on 6eab9bd: 46,824–59,531 epochs and 14,491–29,222 merges, 0 breaks, crash and migration counts unchanged. The first stress runs timed out and then ran out of memory: two merge defects, fixed in ADR r0.6 (e16de3a, e37e864)
G5 Fork O(1) at 10³–10⁶ objects; WHAT_IF ≤ 2× a commit; branch commit ≤ 1.2× main; memory and sync-point bytes; actor validation latency Pass (report): fork 0.29–0.42 µs and 212 B at every size; WHAT_IF 1.31–1.60×; branch vs main interleaved in one kernel 0.88–1.05 in 11 of 12 cells, one unreproduced 4.09 (and 1.26 in one cell of the earlier two-kernel measurement), both disclosed; expect_cert_valid adds no measurable latency over fsync. The first write after a fork or merge copies the branch: 51–252 ms at 10⁶ revisions
G6 None —
G7 P0–P5b regressions at gate counts Pass: p6-g7.txt (remote, scripts/gate-regression.sh, now with the P5b suites; rerun on 6eab9bd): paranoid testkit, exhaustive depth 6 (770 states) and 8 (1,711), store/daemon crash injection at 1,000 per point and PROP10 at 10,000 seeds, P4, P5a and P5b suites in release; 0 failures
G8 README, CHANGELOG, status Pass: README P6 section (branches, ctx merge, transactions, WHAT_IF, the protocol 4 / format 3 upgrade), CHANGELOG, IMPLEMENTATION_STATUS.md

G2 coverage (as of 9d)

Tests are in crates/contextdb-testkit/tests/p6_*.rs (kernel) and in crates/contextdb-daemon/tests/{branches,p6_protocol}.rs and crates/contextdb-cli/tests/{daemon_cli,p6_observer_cli}.rs (protocol).

Item Test
T14 p6_what_if::t14_what_if_changes_nothing, plus the 1,600-trace dry-run property
T19 raw ExpectHeads p6_read_set::t19_write_skew_second_plan_fails
T19 expect_cert_valid + head move branches::t19_expect_cert_valid_first_committer_wins
T19 base retraction / rule disable / ValidationRecord alone p6_protocol::t19_window_changes_other_than_head_moves
T19 watermark change alone anchors::t19_expect_cert_valid_with_only_a_watermark_change_in_the_window (a Git commit that moves only the position: refused for lag before the scan, then StrictChange for a strict plan; a non-strict plan commits)
T19 two plans in one group p6_protocol::t19_concurrent_plans_one_commits (20 concurrent rounds)
T19 read-then-supersede p6_read_set::read_then_supersede_in_one_batch_commits; the first plan in the expect_cert_valid test
T19 cross-branch p6_protocol::t19_plans_on_other_branches_do_not_conflict
T19 idempotent retry branches::t19_expect_cert_valid_first_committer_wins
T20a, T20b p6_merge::t20_merge_and_certification
T47, T48 p6_branches::t47_branch_isolation, p6_visibility::t48_multi_head_atomicity_on_a_branch
T51 p6_conflict_gate::t51_open_conflict_blocks_certified_only
T62 across branches p6_views::t62_certificates_are_per_branch
T72 p6_branches::t72_cycle_graphs_are_per_branch, p6_merge::t72_merge_rechecks_the_cycle_graph
T73 (+ rule variant) p6_visibility::t73_cross_branch_revision_dedup, cross_branch_rule_dedup
T75 p6_branches::t75_global_key_namespace
S13 BRANCH_ONLY p6_views::s13_branch_only_on_main
S13 CONFLICTED p6_protocol::s13_conflicted_after_a_merge
Visibility goldens p6_visibility (fork boundary, inheritance, dedup imports, archived), p6_merge::merge_takes_source_changes_and_keeps_target_changes (merge, then fork, then reference)
Merge-base goldens p6_merge (repeated merges both ways, revert through a third branch, criss-cross, R1 retention)
dirs intersection, empty result p6_merge::policy_and_metadata
Anchors newly visible / watermark clearing p6_merge::observer_domain
Criss-cross on base, enabled, policy (R3) p6_merge::criss_cross_fails_closed, r3_criss_cross_on_enabled_and_policy, r3_criss_cross_policy_refuses; artifact_open only randomly (PROP13)
Metadata conflicts listed (R4) p6_merge::policy_and_metadata
Historical reads never BRANCH_ONLY p6_views::s13_branch_only_on_main
Every classification row PROP13 (10,000 seeds) against the oracle
Observer domain, worktree branch p6_merge::observer_domain, p6_observer_cli::observer_follows_the_worktree_branch

Independent implementation review (2026-10-26)

Four non-implementing reviewers, one per area (kernel, merge and WHAT_IF, service and protocol, store and migration), given ADR 0013, the SPEC and the code only, looking for counterexamples. No blockers; 8 majors, 9 minors. Each finding below was checked by reading the code, and each fix has a regression test that fails on the code before it (checked by restoring the old file). ADR 0013 r0.7 and SPEC v1.6.9 record the semantic ones.

Majors (all fixed):

  • Merge M1: a default require_certified merge passed while it opened a Conflict on a revision other than the head (a base, a rule's conclusion), because the check read the head's status. The kernel and the oracle agreed. The same counterexample showed the ADR's R3 claim ("booleans never conflict with a single base") false through cross-branch dedup. A logical the merge opened a Conflict on is now CONFLICTED for the check; R3 corrected. p6_kernel_edges::require_certified_refuses_conflicts_off_the_head (kernel = oracle).
  • Kernel K1: an ExpectHeads after a Merge with an invalid read set was mapped to global ids twice: a wrong logical, or an index panic in the actor (admin Raw only). late_invalid_read_set_in_a_merge_names_the_global_logical.
  • Kernel K2: branch digests left out the branch entry and received sync points that §3 requires, so replica checks could miss a divergent sync point, name or archive flag. branch_digests_cover_the_branch_table_and_sync_points.
  • Service S1: a strict full check on a branch session issued its certificate from main's database: NotCertified, or a panic once main renumbered. anchors::strict_full_check_on_a_branch_issues_from_the_branch.
  • Service S2: the ctx_commit idempotency hash ignored branch, channel and principal, so another session's identical ops under the same client_txn_id received a result committed on a different branch. p6_protocol::commit_txn_ids_are_bound_to_branch_and_principal.
  • Service S3: the hash included the session's cert id, so a retry of a durable commit got TxnIdReuse after a ctx_check re-issued the certificate, also after a restart (T18b). commit_retry_survives_a_reissued_certificate.
  • Store T1: a group commit wrote projection rows read after the whole group (posts, base history, rule enabled), so they differed from a rebuild (PROP10); a historical read served by the projection after a restart returned a different answer. Older than P6 for posts and base; rows are now captured after each batch. p6_review::group_commit_projections_equal_a_rebuild.
  • Store T2: ctx migrate interrupted after staging (or failing its last directory sync) lost the T68 list of changed epochs. Resume and rerun now derive it from the backup. interrupted_migration_still_reports_changed_epochs.
  • Store T3: a rollback through log.v2.bak failed once a format-3 binary had opened the store, because the old binary could not write the schema-2 projection tables. Format 3 keeps its projections in projections.v3.sqlite. Verified by reading the old open path; not run with an old binary.

Minors:

  • Fixed: MergeNotCertified named the first failing logical in certify order, not by id (merge_not_certified_names_the_first_failing_logical_by_id); ValidationRecord conflict payloads did not identify the curvec (payload now starts with it; validation_records_conflict_or_converge); ConflictingVersions left with a branch-local id (conflicting_versions_names_the_global_logical); the 2→3 listing compared against a format-0 store's acked_result and listed every epoch (format0_descended_stores_list_only_real_changes); an unknown merge source was reported before the archived target (§9); an expect_cert_valid commit with no certificate returned an uncatalogued NoCertificate (now Refused with Empty); ctx_branch and ctx_merge took no client_txn_id (SPEC §6.5); ctx_commit writes were not counted as session writes (branch_requests_take_txn_ids_and_errors_follow_the_order, ctx_commit_is_a_session_write).
  • Accepted, documented in ADR §4: a client-sent ExpectHeads{source: Certificate} is refused by the daemon before the kernel's §9 checks.
  • Accepted: branch names also refuse a trailing / and // (stricter than §2, as git); LogicalRef::New in a merge batch's ExpectHeads is UnknownRef under the §9 order; a format-2 log with a torn tail must be reopened by the old binary once before ctx migrate (ChainBroken otherwise; no data lost).

Areas reviewed and found sound (the reviewers' own tests and a 400-seed fuzz of what_if = commit and snapshot import = live): recursive visibility, the id map and import retry, cross-branch dedup, failed and dry-run batches leaving no trace, BRANCH_ONLY, per-branch certificates, merge-base equivalence with the naive search and pruning soundness, classification and the gate, the WHAT_IF journal (merges included), validate-then-append with no gap, session binding, the observer per branch, protocol lockstep, format-3 rows, idempotency persistence, snapshots with pruned states.