diff --git a/README.md b/README.md index ea58a50..d223c5e 100644 --- a/README.md +++ b/README.md @@ -39,6 +39,7 @@ GitHub keeps, at most, a read-only mirror of this one repository so that people | cuda-crack, a copy of its tables and Omega tools for this gate (its home stays its own Radicle repository, `rad:z631r2JJiVT8it4tST7RK24hpowi`): its Omega table generator, built by the seed-born compilers, regenerates all 471 sm_121 instruction forms. Its Omega reencoder rebuilds the 70 kernels that ran on the GB10 (13,520 instructions) from those tables alone, to the chip receipt's sha256. Its four planted bugs, now made by Omega (`check/mutate.om`, byte for byte the same as the original awk and od), are each caught. | HOST_TESTED, 2026-11-07, AArch64 and native x86-64; the kernels themselves ran on the chip on 2026-10-27 | step 8, [`gpu/cuda-crack/PROVENANCE.md`](gpu/cuda-crack/PROVENANCE.md) | | The fork-push generator, [`tools/forkpush.om`](tools/forkpush.om), writes the same script on both targets from [`stack/FORKS.tsv`](stack/FORKS.tsv). It refuses 18 malformed manifests, and a branch marked private appears only as a comment, never as a push. | HOST_TESTED, 2026-11-07 | step 9 | | The checked bulk word `count A off n v`, added to omc ([`seed/PROVENANCE.md`](seed/PROVENANCE.md#local-changes)): its 16 examples in [`tests/omc/`](tests/omc/) pass on both targets, and a compiler with its slice check deleted fails both trap-3 examples. Built with it, the stack ledger runs 6.1 times faster on native x86-64. | HOST_TESTED, 2026-11-07; the speed-up was measured over the 44 stack checkouts, outside the gate | steps 10 and 11; speed-up in [`seed/PROVENANCE.md`](seed/PROVENANCE.md#local-changes) | +| The Omega merge queue, [`tools/flow.om`](tools/flow.om), which replaces GitHub's Actions, required checks and merge queue for Radicle patches ([`tools/FLOW.md`](tools/FLOW.md)): built for both targets, it writes the same plan from the same facts, reproduces its two fixtures byte for byte and refuses 21 malformed inputs and 3 malformed arguments. Run by [`tools/flow-cycle.sh`](tools/flow-cycle.sh) against a real Radicle repository, offline, it gates each distinct tree once with the default branch's `build.sh`, never with the delegate's key in its environment (a patch that rewrites its own `build.sh` to pass still fails), posts every verdict on its patch, and fast-forwards the default branch only to a tree with a pass receipt, one patch per cycle; a plan made before the branch moved refuses to run. It plans 4,096 patches against 16,384 receipts in 0.06 s on native x86-64. The Lean 4 model of its decision rules, [`tests/flow/Flow.lean`](tests/flow/Flow.lean), is written but not yet machine-checked. | HOST_TESTED, 2026-11-08, AArch64 under qemu-aarch64 8.2.2 and native x86-64, radicle-cli 0.23.0 and radicle-node 0.21.1 | step 12; step 12b, [`tests/flow/e2e.sh`](tests/flow/e2e.sh), runs when `rad` is installed | | omega-mv's whole FR-9 x86 corpus ran with its x86-64 programs native for the first time: 16,179 rows, 15,499 PASS, 232 FAIL, 32 NOT_RUN, 30 MISSING_IMPLEMENTATION, 75 BLOCKED, 311 EXCLUDED. No FAIL is an x86 miscompile. 150 need om2 candidate compilers whose commits are not published; 46 are omega-bridge refusing the compiler's hash (all 49 bridge rows pass when that one pin is moved); 36 pin the AArch64 bytes of an older omc. The run also found a cross-target bug in omc, fixed in [`seed/LOCAL.patch`](seed/LOCAL.patch). | HOST_TESTED, 2026-11-07, native x86-64; the AArch64 side under qemu-aarch64 8.2.2 | [`evidence/x86-native/`](evidence/x86-native/) | The whole gate takes 19.3 s on a 4-vCPU x86-64 cloud machine. There, omc-x86 compiles the 337,225-byte compiler in 0.10 s natively. @@ -54,9 +55,9 @@ The only hardware result above is the cuda-crack kernels' chip run of 2026-10-27 | [`seed/`](seed/) | the compiler's sources, from the hand-written `seed0.hex` to `omc/omc.om`, copied from omega-mv with [provenance](seed/PROVENANCE.md) | | [`build.sh`](build.sh) | the one build and gate | | [`compiler/`](compiler/) | `si.om`, the front-door generator | -| [`tools/`](tools/) | `ledger.om`, the stack ledger; `forkpush.om`, which writes the script that sends my forks' branches to Radicle | +| [`tools/`](tools/) | `ledger.om`, the stack ledger; `forkpush.om`, which writes the script that sends my forks' branches to Radicle; `flow.om` and `flow-cycle.sh`, the merge queue that gates and merges Radicle patches ([`FLOW.md`](tools/FLOW.md)) | | [`harness/`](harness/) | the Omega harness: agent skills for writing correct Omega; `.claude/skills` points at it | -| [`tests/`](tests/) | the ledger and fork-push fixtures, the example runner, the compiler regressions and the examples of the words this repository adds to the compiler | +| [`tests/`](tests/) | the ledger, fork-push and merge-queue fixtures (with the queue's end-to-end Radicle test and Lean model), the example runner, the compiler regressions and the examples of the words this repository adds to the compiler | | [`gpu/cuda-crack/`](gpu/cuda-crack/) | a copy of cuda-crack's sm_121 encoding tables, Omega encoder and reencoder, and chip receipts, so the gate can rebuild the chip kernels; [provenance](gpu/cuda-crack/PROVENANCE.md). cuda-crack itself lives in its own Radicle repository | | [`docs/`](docs/) | writing and sites from my personal account that have no Radicle project: the AIEN constitution, drakestapleton.com's source, and the old site as an archive | | [`evidence/`](evidence/) | receipts of runs made from this repository, starting with the native x86-64 corpus run | diff --git a/build.sh b/build.sh index 0903811..fb4bac0 100644 --- a/build.sh +++ b/build.sh @@ -35,6 +35,10 @@ # 10. the examples of compiler words beyond the imported harness (tests/omc), on both targets # 11. control: omc.om with count's slice check deleted must build a compiler that fails both trap-3 # examples of count on every target that runs, while count_word still passes +# 12. tools/flow.om, the merge queue's planner, built for both targets, must print +# tests/flow/expected.sh and expected-empty.sh and refuse every tests/flow/bad/*.tsv and three +# malformed arguments; 12b. with rad and git-remote-rad installed, tests/flow/e2e.sh runs the +# whole queue against a throwaway Radicle repository with each build, else NOT_RUN # Last line: RC=0 when every step passed. set -eu R=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) @@ -351,4 +355,58 @@ for t in a64 x86; do done pass "control: count without its slice check fails count_trap3 and count_trap3_wrap; count_word still passes" +# 12. tools/flow.om, the Omega merge queue's planner (tools/flow-cycle.sh runs it), on both targets. +fb=b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0 +fg=9a7e777777777777777777777777777777777777 +for t in a64 x86; do + if [ $t = x86 ] && [ "$X86" = none ]; then note "NOT_RUN flow on x86-64"; continue; fi + if [ $t = a64 ]; then + a64 $O/omc < tools/flow.om > $O/flow-$t + else + { printf 'target x86linux\n'; cat tools/flow.om; } | x86 $O/omc-x86 > $O/flow-$t + fi + mk $O/flow-$t + for f in state:expected empty:expected-empty; do + i=tests/flow/${f%%:*}.tsv e=tests/flow/${f#*:}.sh + $t $O/flow-$t $fb $fg $i tests/flow/receipts.tsv $O/fl-$t.sh || fail "flow ($t) refused $i" + cmp -s $O/fl-$t.sh $e || fail "flow ($t) differs from $e" + sh -n $O/fl-$t.sh || fail "flow ($t): $e does not parse" + done + nbad=0 + for b in tests/flow/bad/*.tsv; do + case ${b##*/} in + state-*) st=$b rf=tests/flow/receipts.tsv ;; + rec-*) st=tests/flow/state.tsv rf=$b ;; + *) fail "flow: $b is neither state-* nor rec-*" ;; + esac + rm -f $O/fl-bad.sh + if $t $O/flow-$t $fb $fg $st $rf $O/fl-bad.sh 2> $O/fl-bad.err; then fail "flow ($t) accepted $b"; fi + [ ! -e $O/fl-bad.sh ] || fail "flow ($t) wrote output for $b" + [ "$(wc -l < $O/fl-bad.err)" -eq 1 ] && grep -q '^flow: ' $O/fl-bad.err || fail "flow ($t) refused $b without one flow: line" + nbad=$((nbad + 1)) + done + for a in "${fb%b0}b $fg" "$(printf %s $fb | tr b B) $fg" "$fb ${fg%7}g"; do + rm -f $O/fl-bad.sh + if $t $O/flow-$t $a tests/flow/state.tsv tests/flow/receipts.tsv $O/fl-bad.sh 2> /dev/null; then fail "flow ($t) accepted the arguments $a"; fi + [ ! -e $O/fl-bad.sh ] || fail "flow ($t) wrote output for the arguments $a" + done + rc=0; $t $O/flow-$t $fb $fg tests/flow/state.tsv $O/fl-bad.sh 2> /dev/null || rc=$? + [ $rc -eq 2 ] || fail "flow ($t) exited $rc, not 2, for four arguments" + pass "flow ($t) prints tests/flow/expected.sh and expected-empty.sh, refuses all $nbad bad inputs and 3 bad arguments" +done +# 12b. The merge queue against a real Radicle repository, offline, when rad is installed. +if command -v rad > /dev/null 2>&1 && command -v git-remote-rad > /dev/null 2>&1; then + for t in a64 x86; do + [ -x $O/flow-$t ] || { note "NOT_RUN flow e2e on $t"; continue; } + if [ $t = a64 ]; then q=$A64; else q=$X86; fi + printf '#!/bin/sh\nexec %s "%s" "$@"\n' "$q" "$R/$O/flow-$t" > $O/flow-$t.run + mk $O/flow-$t.run + sh tests/flow/e2e.sh $O/flow-$t.run > $O/flow-e2e-$t.log 2>&1 || fail "flow e2e ($t): $(grep '^FAIL' $O/flow-e2e-$t.log | head -1)" + tail -1 $O/flow-e2e-$t.log | grep -qx 'RC=0' || fail "flow e2e ($t) did not finish" + pass "flow e2e ($t): $(grep -c '^PASS' $O/flow-e2e-$t.log) checks against a real Radicle repository" + done +else + note "NOT_RUN flow e2e: needs rad and git-remote-rad (https://radicle.xyz)" +fi + printf 'RC=0\n' diff --git a/tests/flow/Flow.lean b/tests/flow/Flow.lean new file mode 100644 index 0000000..ba1ff17 --- /dev/null +++ b/tests/flow/Flow.lean @@ -0,0 +1,193 @@ +/- +tests/flow/Flow.lean: the decision rules of tools/flow.om, the Omega merge queue's planner, as a +Lean 4 model, and the safety properties the queue depends on, proved over that model. + +The model is the "Decision for each STATE row" section of tools/flow.om's header. Commits and +trees are opaque (Nat stands for a 40-hex object id). `Receipts` is the receipt ledger already +restricted to the plan's gate: what the gate said about a tree, if anything. + +Proved, for every input: + plan_length every patch gets exactly one action + merge_safe a merge is planned only for a fast-forward whose exact tree has a pass + receipt from the default branch's gate + merge_at_most_one a plan holds at most one merge + gate_needs_none a tree is gated only when it has no receipt for this gate + gate_once no tree is gated twice in one plan + fail_never_merges a tree with a fail receipt is never merged +Together with flow.sh's checks (rad/DEFAULT is still the planned branch, HEAD's tree is the +planned tree, the push is a fast-forward), merge_safe gives the queue's invariant: the default +branch only ever moves to a tree that passed the gate guarding it. + +What this does not prove: that tools/flow.om implements this model (the fixtures in tests/flow +and the end-to-end run in tests/flow/e2e.sh test that, they do not prove it), nor anything +about git, Radicle or the gate itself. + +Check: lean tests/flow/Flow.lean (core Lean 4, no Mathlib; checked with leanprover/lean4:v4.15.0). +-/ +namespace Flow + +inductive Rel | ff | div | inB + deriving DecidableEq, Repr + +inductive Verdict | pass | fail + deriving DecidableEq, Repr + +inductive Action | merged | merge | queued | rebase | fail | gate | same + deriving DecidableEq, Repr + +structure Row where + patch : Nat + head : Nat + tree : Nat + rel : Rel + +/-- The receipt ledger, restricted to the plan's gate. -/ +abbrev Receipts := Nat → Option Verdict + +/-- One row's action. `m`: an earlier row was given merge. `seen`: the trees of earlier rows. -/ +def decide1 (r : Receipts) (m : Bool) (seen : List Nat) (x : Row) : Action := + match x.rel, r x.tree with + | .inB, _ => .merged + | .ff, some .pass => if m then .queued else .merge + | .div, some .pass => .rebase + | .ff, some .fail => .fail + | .div, some .fail => .fail + | .ff, none => if x.tree ∈ seen then .same else .gate + | .div, none => if x.tree ∈ seen then .same else .gate + +/-- The plan: rows in input order, each decided with what the rows before it were given. -/ +def plan (r : Receipts) : Bool → List Nat → List Row → List Action + | _, _, [] => [] + | m, seen, x :: xs => + let a := decide1 r m seen x + a :: plan r (m || decide (a = .merge)) (x.tree :: seen) xs + +/-- The plan flow.om makes from a STATE: nothing merged yet, no tree seen yet. -/ +def flow (r : Receipts) (xs : List Row) : List Action := plan r false [] xs + +theorem decide1_merge {r : Receipts} {m : Bool} {seen : List Nat} {x : Row} + (h : decide1 r m seen x = .merge) : r x.tree = some .pass ∧ x.rel = .ff ∧ m = false := by + unfold decide1 at h + split at h <;> (try split at h) <;> simp_all + +theorem decide1_gate {r : Receipts} {m : Bool} {seen : List Nat} {x : Row} + (h : decide1 r m seen x = .gate) : r x.tree = none ∧ x.tree ∉ seen := by + unfold decide1 at h + split at h <;> (try split at h) <;> simp_all + +theorem plan_length (r : Receipts) : + ∀ (xs : List Row) (m : Bool) (seen : List Nat), (plan r m seen xs).length = xs.length := by + intro xs + induction xs with + | nil => intro m seen; rfl + | cons y ys ih => intro m seen; simp [plan, ih] + +theorem merge_safe_gen (r : Receipts) : + ∀ (xs : List Row) (m : Bool) (seen : List Nat) (x : Row), + (x, Action.merge) ∈ xs.zip (plan r m seen xs) → r x.tree = some .pass ∧ x.rel = .ff := by + intro xs + induction xs with + | nil => intro m seen x h; simp [plan] at h + | cons y ys ih => + intro m seen x h + simp only [plan, List.zip_cons_cons, List.mem_cons] at h + rcases h with h | h + · have hx : x = y := (Prod.mk.inj h).1 + have ha : decide1 r m seen y = .merge := (Prod.mk.inj h).2.symm + subst hx + exact ⟨(decide1_merge ha).1, (decide1_merge ha).2.1⟩ + · exact ih _ _ x h + +/-- A merge is planned only for a fast-forward whose exact tree passed this gate. -/ +theorem merge_safe (r : Receipts) (xs : List Row) (x : Row) + (h : (x, Action.merge) ∈ xs.zip (flow r xs)) : r x.tree = some .pass ∧ x.rel = .ff := + merge_safe_gen r xs false [] x h + +theorem no_merge_after_merge (r : Receipts) : + ∀ (xs : List Row) (seen : List Nat), Action.merge ∉ plan r true seen xs := by + intro xs + induction xs with + | nil => intro seen; simp [plan] + | cons y ys ih => + intro seen h + simp only [plan, Bool.true_or, List.mem_cons] at h + rcases h with h | h + · have := (decide1_merge h.symm).2.2 + simp at this + · exact ih _ h + +theorem merge_count_le (r : Receipts) : + ∀ (xs : List Row) (seen : List Nat), (plan r false seen xs).count .merge ≤ 1 := by + intro xs + induction xs with + | nil => intro seen; simp [plan] + | cons y ys ih => + intro seen + simp only [plan, Bool.false_or] + by_cases ha : decide1 r false seen y = .merge + · have h0 : (plan r true (y.tree :: seen) ys).count .merge = 0 := + List.count_eq_zero.mpr (no_merge_after_merge r ys _) + simp [ha, h0] + · have := ih (y.tree :: seen) + simp [ha, List.count_cons, this] + +/-- A plan holds at most one merge, so each step moves the default branch at most once. -/ +theorem merge_at_most_one (r : Receipts) (xs : List Row) : (flow r xs).count .merge ≤ 1 := + merge_count_le r xs [] + +/-- The trees of the rows a plan gates, in order. -/ +def gateTrees : List (Row × Action) → List Nat + | [] => [] + | (x, a) :: rest => if a = .gate then x.tree :: gateTrees rest else gateTrees rest + +theorem gate_gen (r : Receipts) : + ∀ (xs : List Row) (m : Bool) (seen : List Nat), + (gateTrees (xs.zip (plan r m seen xs))).Nodup ∧ + (∀ t ∈ gateTrees (xs.zip (plan r m seen xs)), t ∉ seen ∧ r t = none) := by + intro xs + induction xs with + | nil => intro m seen; simp [plan, gateTrees] + | cons y ys ih => + intro m seen + obtain ⟨hn, hs⟩ := ih (m || decide (decide1 r m seen y = .merge)) (y.tree :: seen) + simp only [plan, List.zip_cons_cons, gateTrees] + split + · rename_i hg + obtain ⟨hr, hy⟩ := decide1_gate hg + refine ⟨List.nodup_cons.mpr ⟨fun hm => (hs _ hm).1 (List.mem_cons_self _ _), hn⟩, ?_⟩ + intro t ht + rcases List.mem_cons.mp ht with rfl | ht + · exact ⟨hy, hr⟩ + · exact ⟨fun h => (hs t ht).1 (List.mem_cons_of_mem _ h), (hs t ht).2⟩ + · exact ⟨hn, fun t ht => ⟨fun h => (hs t ht).1 (List.mem_cons_of_mem _ h), (hs t ht).2⟩⟩ + +/-- No tree is gated twice in one plan. -/ +theorem gate_once (r : Receipts) (xs : List Row) : (gateTrees (xs.zip (flow r xs))).Nodup := + (gate_gen r xs false []).1 + +/-- A tree is gated only when this gate has no receipt for it. -/ +theorem gate_needs_none (r : Receipts) (xs : List Row) : + ∀ t ∈ gateTrees (xs.zip (flow r xs)), r t = none := + fun t ht => ((gate_gen r xs false []).2 t ht).2 + +/-- A tree with a fail receipt is never merged. -/ +theorem fail_never_merges (r : Receipts) (xs : List Row) (x : Row) + (hf : r x.tree = some .fail) : (x, Action.merge) ∉ xs.zip (flow r xs) := by + intro h + have := (merge_safe r xs x h).1 + rw [hf] at this + cases this + +/-- The fixture of tests/flow: the decisions flow.om must print for tests/flow/state.tsv. -/ +def fixtureReceipts : Receipts := fun t => + if t = 1 ∨ t = 2 ∨ t = 3 ∨ t = 99 then some .pass else if t = 4 then some .fail else none + +def fixtureState : List Row := + [⟨1, 1, 1, .ff⟩, ⟨2, 2, 2, .ff⟩, ⟨3, 3, 3, .div⟩, ⟨4, 4, 4, .ff⟩, ⟨5, 5, 5, .ff⟩, + ⟨6, 6, 5, .div⟩, ⟨7, 7, 7, .div⟩, ⟨8, 8, 1, .ff⟩, ⟨9, 9, 9, .inB⟩] + +theorem fixture_plan : flow fixtureReceipts fixtureState = + [.merge, .queued, .rebase, .fail, .gate, .same, .gate, .queued, .merged] := by + decide + +end Flow diff --git a/tests/flow/bad/rec-dup-verdict.tsv b/tests/flow/bad/rec-dup-verdict.tsv new file mode 100644 index 0000000..9b0220f --- /dev/null +++ b/tests/flow/bad/rec-dup-verdict.tsv @@ -0,0 +1,3 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 fail 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/rec-dup.tsv b/tests/flow/bad/rec-dup.tsv new file mode 100644 index 0000000..20c84f4 --- /dev/null +++ b/tests/flow/bad/rec-dup.tsv @@ -0,0 +1,4 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/rec-fields5.tsv b/tests/flow/bad/rec-fields5.tsv new file mode 100644 index 0000000..67ba1ad --- /dev/null +++ b/tests/flow/bad/rec-fields5.tsv @@ -0,0 +1,2 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff x diff --git a/tests/flow/bad/rec-gate-upper.tsv b/tests/flow/bad/rec-gate-upper.tsv new file mode 100644 index 0000000..afb9ff5 --- /dev/null +++ b/tests/flow/bad/rec-gate-upper.tsv @@ -0,0 +1,2 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9A7E777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/rec-header.tsv b/tests/flow/bad/rec-header.tsv new file mode 100644 index 0000000..03b7f5b --- /dev/null +++ b/tests/flow/bad/rec-header.tsv @@ -0,0 +1,2 @@ +tree gate verdict sha256 +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/rec-log-short.tsv b/tests/flow/bad/rec-log-short.tsv new file mode 100644 index 0000000..76e6824 --- /dev/null +++ b/tests/flow/bad/rec-log-short.tsv @@ -0,0 +1,2 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/rec-nonl.tsv b/tests/flow/bad/rec-nonl.tsv new file mode 100644 index 0000000..6ecab95 --- /dev/null +++ b/tests/flow/bad/rec-nonl.tsv @@ -0,0 +1,2 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff \ No newline at end of file diff --git a/tests/flow/bad/rec-verdict.tsv b/tests/flow/bad/rec-verdict.tsv new file mode 100644 index 0000000..c66948f --- /dev/null +++ b/tests/flow/bad/rec-verdict.tsv @@ -0,0 +1,2 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 PASS 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/bad/state-blank.tsv b/tests/flow/bad/state-blank.tsv new file mode 100644 index 0000000..7bd92e7 --- /dev/null +++ b/tests/flow/bad/state-blank.tsv @@ -0,0 +1,4 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff + +02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 02cccccccccccccccccccccccccccccccccccccc 02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/bad/state-cr.tsv b/tests/flow/bad/state-cr.tsv new file mode 100644 index 0000000..445eaff --- /dev/null +++ b/tests/flow/bad/state-cr.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/bad/state-dup.tsv b/tests/flow/bad/state-dup.tsv new file mode 100644 index 0000000..4d64b0b --- /dev/null +++ b/tests/flow/bad/state-dup.tsv @@ -0,0 +1,4 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 02cccccccccccccccccccccccccccccccccccccc 02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee div diff --git a/tests/flow/bad/state-empty.tsv b/tests/flow/bad/state-empty.tsv new file mode 100644 index 0000000..e69de29 diff --git a/tests/flow/bad/state-fields3.tsv b/tests/flow/bad/state-fields3.tsv new file mode 100644 index 0000000..79de250 --- /dev/null +++ b/tests/flow/bad/state-fields3.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee diff --git a/tests/flow/bad/state-fields5.tsv b/tests/flow/bad/state-fields5.tsv new file mode 100644 index 0000000..6fbbf60 --- /dev/null +++ b/tests/flow/bad/state-fields5.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff x diff --git a/tests/flow/bad/state-head-upper.tsv b/tests/flow/bad/state-head-upper.tsv new file mode 100644 index 0000000..2141b29 --- /dev/null +++ b/tests/flow/bad/state-head-upper.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01Cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/bad/state-header.tsv b/tests/flow/bad/state-header.tsv new file mode 100644 index 0000000..208ca0d --- /dev/null +++ b/tests/flow/bad/state-header.tsv @@ -0,0 +1,2 @@ +patch head tree ff +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/bad/state-nonl.tsv b/tests/flow/bad/state-nonl.tsv new file mode 100644 index 0000000..a40f7c1 --- /dev/null +++ b/tests/flow/bad/state-nonl.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff \ No newline at end of file diff --git a/tests/flow/bad/state-patch-short.tsv b/tests/flow/bad/state-patch-short.tsv new file mode 100644 index 0000000..46b8ad2 --- /dev/null +++ b/tests/flow/bad/state-patch-short.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/bad/state-quote.tsv b/tests/flow/bad/state-quote.tsv new file mode 100644 index 0000000..cef9788 --- /dev/null +++ b/tests/flow/bad/state-quote.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee f' diff --git a/tests/flow/bad/state-rel.tsv b/tests/flow/bad/state-rel.tsv new file mode 100644 index 0000000..ea0bbb4 --- /dev/null +++ b/tests/flow/bad/state-rel.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee FF diff --git a/tests/flow/bad/state-tree-nonhex.tsv b/tests/flow/bad/state-tree-nonhex.tsv new file mode 100644 index 0000000..31f76b0 --- /dev/null +++ b/tests/flow/bad/state-tree-nonhex.tsv @@ -0,0 +1,2 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01geeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff diff --git a/tests/flow/e2e.sh b/tests/flow/e2e.sh new file mode 100644 index 0000000..5773990 --- /dev/null +++ b/tests/flow/e2e.sh @@ -0,0 +1,137 @@ +#!/bin/sh +# tests/flow/e2e.sh: the Omega merge queue against a real Radicle repository, offline. +# +# sh tests/flow/e2e.sh FLOW +# +# FLOW is the planner built from tools/flow.om. Needs git, rad and git-remote-rad; it makes a +# throwaway Radicle identity and repository under a temporary directory (RAD_HOME is never the +# user's), so no node, network or key of yours is touched. Without rad it prints NOT_RUN and +# exits 3. +# +# The repository's gate, build.sh, passes when the file state says good, and fails if it can +# see the delegate's passphrase, Radicle home or agent socket. Five patches: +# A a good change C a different commit with A's exact tree +# B state=bad E a good change to another file +# D state=bad, and its own build.sh rewritten to pass anyway +# Cycle 1 gates A (or C, whichever comes first) once for both, and B, D and E; D must fail, +# because the default branch's gate runs, never the patch's. Cycle 2 merges the first of A, C +# and E (patch ids are random, so any of them) as a fast-forward and queues the other two. +# Cycle 3 gates nothing and sends those two to rebase. +# After every cycle the default branch's tree must hold a pass receipt. A plan made before the +# default branch moved must refuse to run. Each check prints PASS or FAIL; last line RC=0. +set -eu +[ $# -eq 1 ] || { echo 'usage: sh tests/flow/e2e.sh FLOW' >&2; exit 2; } +R=$(CDPATH= cd -- "$(dirname -- "$0")/../.." && pwd) +case $1 in /*) flow=$1 ;; *) flow=$(pwd)/$1 ;; esac +for c in git rad git-remote-rad; do + command -v $c > /dev/null 2>&1 || { echo "NOT_RUN flow e2e: no $c"; exit 3; } +done +fail() { printf 'FAIL %s\n' "$*"; exit 1; } +pass() { printf 'PASS %s\n' "$*"; } +T=$(mktemp -d) +# FLOW_E2E_KEEP=1 keeps the throwaway directory, for reading what a failed check saw. +if [ -n "${FLOW_E2E_KEEP:-}" ]; then echo "NOTE e2e directory $T"; else trap 'rm -rf "$T"' EXIT; fi +# No GIT_COMMITTER_DATE: radicle-cob reads it too, and panics on anything but integer seconds. +export RAD_HOME="$T/rad" RAD_PASSPHRASE=flow-e2e +unset GIT_AUTHOR_DATE GIT_COMMITTER_DATE +q() { "$@" > "$T/last.out" 2>&1 || { cat "$T/last.out" >&2; fail "$*"; }; } +q rad auth --alias flow-e2e +w=$T/demo +git init -q -b main "$w" +g() { git -C "$w" "$@"; } +g config user.email e2e@flow.invalid +g config user.name flow-e2e +cat > "$w/build.sh" << 'GATE' +#!/bin/sh +[ -z "${RAD_PASSPHRASE:-}${RAD_HOME:-}${SSH_AUTH_SOCK:-}" ] || { echo "FAIL the gate can see the delegate's key"; exit 1; } +if grep -qx good state; then echo PASS state; else echo "FAIL state is not good"; exit 1; fi +GATE +echo good > "$w/state" +g add build.sh state +g commit -qm init +(cd "$w" && q rad init --name demo --description 'flow e2e' --default-branch main --public --no-confirm --no-seed) +base=$(g rev-parse HEAD) + +mk() { # mk NAME FILE TEXT [BUILD]: a commit on base changing FILE to TEXT (and build.sh to BUILD) + g checkout -q --detach "$base" + printf '%s\n' "$3" > "$w/$2" + [ $# -lt 4 ] || printf '%s\n' "$4" > "$w/build.sh" + g add -A + g commit -qm "$1" + g rev-parse HEAD +} +open() { # open COMMIT TITLE: open a Radicle patch at COMMIT; prints the patch id + (cd "$w" && git push rad "$1:refs/patches" -o patch.message="$2" > "$T/open.out" 2>&1) || { cat "$T/open.out" >&2; fail "open $2"; } + sed -n 's/.*Patch \([0-9a-f]\{40\}\) opened.*/\1/p' "$T/open.out" +} +hA=$(mk A a 'change a'); pA=$(open "$hA" A) +hB=$(mk B state bad); pB=$(open "$hB" B) +hC=$(g commit-tree "$hA^{tree}" -p "$base" -m C); pC=$(open "$hC" C) +hD=$(mk D state bad '#!/bin/sh +exit 0'); pD=$(open "$hD" D) +hE=$(mk E e 'change e'); pE=$(open "$hE" E) +g checkout -q main +for p in "$pA" "$pB" "$pC" "$pD" "$pE"; do [ ${#p} -eq 40 ] || fail "a patch id is not 40 hex: '$p'"; done +tA=$(g rev-parse "$hA^{tree}") +[ "$(g rev-parse "$hC^{tree}")" = "$tA" ] && [ "$hC" != "$hA" ] || fail "C is not a second commit with A's tree" +pass "5 patches opened on a fresh Radicle repository" + +rec=$T/receipts.tsv +cycle() { # cycle N: one flow cycle; its plan stays in $T/cN + FLOW_DIR=$T/c$1 sh "$R/tools/flow-cycle.sh" "$flow" "$w" main "$rec" > "$T/c$1.out" 2>&1 || { cat "$T/c$1.out" >&2; fail "cycle $1 exited non-zero"; } +} +act() { awk -v p="$2" '$1 == "#" && $2 == "plan" && $4 == p { print $3 }' "$T/c$1/flow.sh"; } +verdict() { awk -F'\t' -v t="$1" '$1 == t { print $3 }' "$rec"; } +tip() { g rev-parse refs/remotes/rad/main; } +tip_has_pass() { # the default branch's tree has a pass receipt (or is the initial commit) + [ "$(tip)" = "$base" ] || [ "$(verdict "$(g rev-parse "$(tip)^{tree}")")" = pass ] || fail "cycle $1: rad/main's tree has no pass receipt" +} + +cycle 1 +[ "$(act 1 "$pA")$(act 1 "$pC")" = gatesame ] || [ "$(act 1 "$pA")$(act 1 "$pC")" = samegate ] || fail "cycle 1: A and C are '$(act 1 "$pA")' and '$(act 1 "$pC")', not one gate and one same" +for p in "$pB" "$pD" "$pE"; do [ "$(act 1 "$p")" = gate ] || fail "cycle 1: patch $p is $(act 1 "$p"), not gate"; done +[ "$(grep -c . "$rec")" -eq 5 ] || fail "cycle 1: the ledger has $(grep -c . "$rec") lines, not a header and 4 receipts" +[ "$(verdict "$tA")" = pass ] || fail "cycle 1: A's tree did not pass" +[ "$(verdict "$(g rev-parse "$hB^{tree}")")" = fail ] || fail "cycle 1: B did not fail" +[ "$(verdict "$(g rev-parse "$hD^{tree}")")" = fail ] || fail "cycle 1: D passed with its own build.sh" +[ "$(verdict "$(g rev-parse "$hE^{tree}")")" = pass ] || fail "cycle 1: E's tree did not pass" +[ "$(tip)" = "$base" ] || fail "cycle 1 moved rad/main" +pass "cycle 1: 4 trees gated for 5 patches; D's own build.sh was not the gate" +rid=$(cd "$w" && rad .) || fail "rad . in the demo repository" +(cd "$w" && rad cob show --repo "$rid" --type xyz.radicle.patch --object "$pB" > "$T/cobB" 2>&1) || fail "rad cob show B" +tB=$(g rev-parse "$hB^{tree}") +body=$(awk -F'\t' -v t="$tB" '$1 == t { printf "\"body\":\"flow gate %s tree %s gate %s log %s\\nFAIL state is not good\"", $3, $1, $2, $4 }' "$rec") +grep -qF "$body" "$T/cobB" || fail "B's Radicle comment is not exactly its receipt and the gate's FAIL line" +pass "cycle 1: B's Radicle comment is its receipt, word for word, and the gate's FAIL line" + +cycle 2 +m=$(awk '$1 == "#" && $2 == "plan" && $3 == "merge" { print $4 }' "$T/c2/flow.sh") +hm=$(awk '$1 == "#" && $2 == "plan" && $3 == "merge" { print $5 }' "$T/c2/flow.sh") +case $m in "$pA"|"$pC"|"$pE") ;; *) fail "cycle 2 merged '$m', not one of the passing fast-forwards A, C, E" ;; esac +for p in "$pA" "$pC" "$pE"; do + [ "$p" = "$m" ] || [ "$(act 2 "$p")" = queued ] || fail "cycle 2: patch $p is $(act 2 "$p"), not queued behind $m" +done +[ "$(act 2 "$pB")$(act 2 "$pD")" = failfail ] || fail "cycle 2: B and D are not fail" +[ "$(tip)" = "$hm" ] || fail "cycle 2: rad/main is $(tip), not the merged head $hm" +tip_has_pass 2 +[ "$(grep -c . "$rec")" -eq 5 ] || fail "cycle 2 gated a tree again" +pass "cycle 2: one fast-forward merge, of the first passing patch, to a tree that passed; nothing gated twice" + +if sh "$T/c2/flow.sh" "$w" main "$rec" > "$T/stale.out" 2>&1; then fail "cycle 2's plan ran after rad/main moved"; fi +grep -q 'make a new plan' "$T/stale.out" || fail "the stale plan did not say why it refused" +pass "a plan made before rad/main moved refuses to run" + +(cd "$w" && rad patch list --merged > "$T/merged" 2>&1) || fail "rad patch list --merged" +grep -q "$(printf '%s' "$m" | cut -c1-7)" "$T/merged" || fail "Radicle does not list the merged patch as merged" +pass "Radicle marks the merged patch merged" + +cycle 3 +[ "$(grep -c . "$rec")" -eq 5 ] || fail "cycle 3 gated a tree again" +for p in "$pA" "$pC" "$pE"; do + [ "$p" = "$m" ] || [ "$(act 3 "$p")" = rebase ] || fail "cycle 3: patch $p is $(act 3 "$p"), not rebase" +done +grep -q '^fl_merge ' "$T/c3/flow.sh" && fail "cycle 3 planned a merge" +[ -z "$(act 3 "$m")" ] || fail "cycle 3 still lists the merged patch" +tip_has_pass 3 +pass "cycle 3: no gate, no merge; the two passing patches left must rebase" +echo RC=0 diff --git a/tests/flow/empty.tsv b/tests/flow/empty.tsv new file mode 100644 index 0000000..60fcb27 --- /dev/null +++ b/tests/flow/empty.tsv @@ -0,0 +1 @@ +patch head tree rel diff --git a/tests/flow/expected-empty.sh b/tests/flow/expected-empty.sh new file mode 100644 index 0000000..2ad1ada --- /dev/null +++ b/tests/flow/expected-empty.sh @@ -0,0 +1,69 @@ +#!/bin/sh +# flow.sh: one step of the Omega merge queue for one Radicle repository. +# Generated by tools/flow.om from the repository's open patches and its receipt ledger; do not +# edit by hand. +# +# A patch merges only as a fast-forward of the default branch, and only when a receipt says the +# default branch's gate passed on the patch head's exact tree, so the default branch only ever +# moves to a tree that passed the gate guarding it. A patch whose tree has no receipt is gated: +# the default branch's build.sh, never the patch's own, runs in a worktree of the patch head, +# and the verdict goes into the ledger and onto the patch as a comment. A tree is gated once, +# however many patches carry it, and never again while the gate is unchanged. +# +# sh flow.sh REPO DEFAULT RECEIPTS +# REPO a Radicle working copy, fetched, with its delegate's key authenticated (rad auth) +# DEFAULT the name of its default branch +# RECEIPTS the receipt ledger this plan was made from; new verdicts are appended to it +# Exit status: 0 the step ran; 1 the repository is not as planned, or a gate, comment or push +# could not run; 2 usage. +# Each patch's decision is one "# plan ACTION PATCH HEAD TREE" line, in the order of the input. +# plan-branch b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0 +# plan-gate 9a7e777777777777777777777777777777777777 +# plan-counts patches 0 merged 0 merge 0 queued 0 rebase 0 fail 0 gate 0 same 0 + +branch='b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0' +gate='9a7e777777777777777777777777777777777777' +set -eu +umask 022 +[ $# -eq 3 ] || { echo 'usage: sh flow.sh REPO DEFAULT RECEIPTS' >&2; exit 2; } +repo=$1 def=$2 rec=$3 +fl_die() { printf 'flow: %s\n' "$*" >&2; exit 1; } +fl_git() { git -C "$repo" "$@"; } +command -v git >/dev/null 2>&1 || fl_die "git is required" +command -v rad >/dev/null 2>&1 || fl_die "rad is required: https://radicle.xyz" +[ -f "$rec" ] || fl_die "no receipt ledger $rec" +cur=$(fl_git rev-parse --verify -q "refs/remotes/rad/$def^{commit}") || fl_die "$repo has no rad/$def" +[ "$cur" = "$branch" ] || fl_die "rad/$def is $cur, not $branch: make a new plan" +[ "$(fl_git rev-parse --verify -q "$branch:build.sh")" = "$gate" ] || fl_die "build.sh at $branch is not blob $gate" +fl_work=$(mktemp -d) +trap 'rm -rf "$fl_work"; fl_git worktree prune' EXIT +trap 'exit 1' HUP INT TERM +fl_tree() { # fl_tree PATCH HEAD TREE: HEAD must be a commit whose tree is TREE + [ "$(fl_git rev-parse --verify -q "$2^{tree}")" = "$3" ] || fl_die "patch $1: $2 is not a commit with tree $3" +} +fl_gate() { # fl_gate PATCH HEAD TREE: run the default branch's build.sh on HEAD; record and post the verdict + fl_tree "$@" + w=$fl_work/$3 + fl_git worktree add -q --detach "$w" "$2" || fl_die "patch $1: cannot check out $2" + fl_git cat-file blob "$gate" > "$w/build.sh" + mkdir "$w.home" + # the gate never sees the delegate's key: no RAD_PASSPHRASE, RAD_SECRET, RAD_HOME, SSH_AUTH_SOCK or HOME + if (cd "$w" && env -u RAD_PASSPHRASE -u RAD_SECRET -u RAD_HOME -u SSH_AUTH_SOCK HOME="$w.home" sh ./build.sh) > "$w.log" 2>&1; then v=pass; else v=fail; fi + l=$(sha256sum < "$w.log" | cut -c1-64) + printf '%s\t%s\t%s\t%s\n' "$3" "$gate" "$v" "$l" >> "$rec" + m="flow gate $v tree $3 gate $gate log $l" + f=$(sed -n '/^FAIL/{p;q;}' "$w.log") + [ -z "$f" ] || m="$m +$f" + (cd "$repo" && rad patch comment "$1" -m "$m" > /dev/null) || fl_die "patch $1: cannot post the verdict" + fl_git worktree remove --force "$w" + printf 'flow: gate %s patch %s tree %s\n' "$v" "$1" "$3" +} +fl_merge() { # fl_merge PATCH HEAD TREE: fast-forward DEFAULT to HEAD, whose tree passed the gate + fl_tree "$@" + fl_git merge-base --is-ancestor "$branch" "$2" || fl_die "patch $1: $2 does not fast-forward $branch" + fl_git push -q rad "$2:refs/heads/$def" || fl_die "patch $1: the push of $2 to $def was refused" + printf 'flow: merged patch %s head %s\n' "$1" "$2" +} + +echo 'flow: step done' diff --git a/tests/flow/expected.sh b/tests/flow/expected.sh new file mode 100644 index 0000000..a3d0594 --- /dev/null +++ b/tests/flow/expected.sh @@ -0,0 +1,81 @@ +#!/bin/sh +# flow.sh: one step of the Omega merge queue for one Radicle repository. +# Generated by tools/flow.om from the repository's open patches and its receipt ledger; do not +# edit by hand. +# +# A patch merges only as a fast-forward of the default branch, and only when a receipt says the +# default branch's gate passed on the patch head's exact tree, so the default branch only ever +# moves to a tree that passed the gate guarding it. A patch whose tree has no receipt is gated: +# the default branch's build.sh, never the patch's own, runs in a worktree of the patch head, +# and the verdict goes into the ledger and onto the patch as a comment. A tree is gated once, +# however many patches carry it, and never again while the gate is unchanged. +# +# sh flow.sh REPO DEFAULT RECEIPTS +# REPO a Radicle working copy, fetched, with its delegate's key authenticated (rad auth) +# DEFAULT the name of its default branch +# RECEIPTS the receipt ledger this plan was made from; new verdicts are appended to it +# Exit status: 0 the step ran; 1 the repository is not as planned, or a gate, comment or push +# could not run; 2 usage. +# Each patch's decision is one "# plan ACTION PATCH HEAD TREE" line, in the order of the input. +# plan-branch b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0 +# plan-gate 9a7e777777777777777777777777777777777777 +# plan-counts patches 9 merged 1 merge 1 queued 2 rebase 1 fail 1 gate 2 same 1 + +branch='b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0' +gate='9a7e777777777777777777777777777777777777' +set -eu +umask 022 +[ $# -eq 3 ] || { echo 'usage: sh flow.sh REPO DEFAULT RECEIPTS' >&2; exit 2; } +repo=$1 def=$2 rec=$3 +fl_die() { printf 'flow: %s\n' "$*" >&2; exit 1; } +fl_git() { git -C "$repo" "$@"; } +command -v git >/dev/null 2>&1 || fl_die "git is required" +command -v rad >/dev/null 2>&1 || fl_die "rad is required: https://radicle.xyz" +[ -f "$rec" ] || fl_die "no receipt ledger $rec" +cur=$(fl_git rev-parse --verify -q "refs/remotes/rad/$def^{commit}") || fl_die "$repo has no rad/$def" +[ "$cur" = "$branch" ] || fl_die "rad/$def is $cur, not $branch: make a new plan" +[ "$(fl_git rev-parse --verify -q "$branch:build.sh")" = "$gate" ] || fl_die "build.sh at $branch is not blob $gate" +fl_work=$(mktemp -d) +trap 'rm -rf "$fl_work"; fl_git worktree prune' EXIT +trap 'exit 1' HUP INT TERM +fl_tree() { # fl_tree PATCH HEAD TREE: HEAD must be a commit whose tree is TREE + [ "$(fl_git rev-parse --verify -q "$2^{tree}")" = "$3" ] || fl_die "patch $1: $2 is not a commit with tree $3" +} +fl_gate() { # fl_gate PATCH HEAD TREE: run the default branch's build.sh on HEAD; record and post the verdict + fl_tree "$@" + w=$fl_work/$3 + fl_git worktree add -q --detach "$w" "$2" || fl_die "patch $1: cannot check out $2" + fl_git cat-file blob "$gate" > "$w/build.sh" + mkdir "$w.home" + # the gate never sees the delegate's key: no RAD_PASSPHRASE, RAD_SECRET, RAD_HOME, SSH_AUTH_SOCK or HOME + if (cd "$w" && env -u RAD_PASSPHRASE -u RAD_SECRET -u RAD_HOME -u SSH_AUTH_SOCK HOME="$w.home" sh ./build.sh) > "$w.log" 2>&1; then v=pass; else v=fail; fi + l=$(sha256sum < "$w.log" | cut -c1-64) + printf '%s\t%s\t%s\t%s\n' "$3" "$gate" "$v" "$l" >> "$rec" + m="flow gate $v tree $3 gate $gate log $l" + f=$(sed -n '/^FAIL/{p;q;}' "$w.log") + [ -z "$f" ] || m="$m +$f" + (cd "$repo" && rad patch comment "$1" -m "$m" > /dev/null) || fl_die "patch $1: cannot post the verdict" + fl_git worktree remove --force "$w" + printf 'flow: gate %s patch %s tree %s\n' "$v" "$1" "$3" +} +fl_merge() { # fl_merge PATCH HEAD TREE: fast-forward DEFAULT to HEAD, whose tree passed the gate + fl_tree "$@" + fl_git merge-base --is-ancestor "$branch" "$2" || fl_die "patch $1: $2 does not fast-forward $branch" + fl_git push -q rad "$2:refs/heads/$def" || fl_die "patch $1: the push of $2 to $def was refused" + printf 'flow: merged patch %s head %s\n' "$1" "$2" +} + +# plan merge 01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +fl_merge '01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' '01cccccccccccccccccccccccccccccccccccccc' '01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' +# plan queued 02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 02cccccccccccccccccccccccccccccccccccccc 02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +# plan rebase 03aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 03cccccccccccccccccccccccccccccccccccccc 03eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +# plan fail 04aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 04cccccccccccccccccccccccccccccccccccccc 04eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +# plan gate 05aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 05cccccccccccccccccccccccccccccccccccccc 05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +fl_gate '05aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' '05cccccccccccccccccccccccccccccccccccccc' '05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' +# plan same 06aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 06cccccccccccccccccccccccccccccccccccccc 05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +# plan gate 07aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 07cccccccccccccccccccccccccccccccccccccc 07eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +fl_gate '07aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' '07cccccccccccccccccccccccccccccccccccccc' '07eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' +# plan queued 08aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 08cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +# plan merged 09aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 09cccccccccccccccccccccccccccccccccccccc 09eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee +echo 'flow: step done' diff --git a/tests/flow/receipts.tsv b/tests/flow/receipts.tsv new file mode 100644 index 0000000..a300044 --- /dev/null +++ b/tests/flow/receipts.tsv @@ -0,0 +1,8 @@ +tree gate verdict log +01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 01ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 02ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 0de7777777777777777777777777777777777777 pass 05ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +03eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 03ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +04eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 fail 04ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +04eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 0de7777777777777777777777777777777777777 pass 44ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff +99eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee 9a7e777777777777777777777777777777777777 pass 99ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff diff --git a/tests/flow/state.tsv b/tests/flow/state.tsv new file mode 100644 index 0000000..50d4779 --- /dev/null +++ b/tests/flow/state.tsv @@ -0,0 +1,10 @@ +patch head tree rel +01aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 01cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +02aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 02cccccccccccccccccccccccccccccccccccccc 02eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +03aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 03cccccccccccccccccccccccccccccccccccccc 03eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee div +04aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 04cccccccccccccccccccccccccccccccccccccc 04eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +05aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 05cccccccccccccccccccccccccccccccccccccc 05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +06aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 06cccccccccccccccccccccccccccccccccccccc 05eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee div +07aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 07cccccccccccccccccccccccccccccccccccccc 07eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee div +08aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 08cccccccccccccccccccccccccccccccccccccc 01eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee ff +09aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 09cccccccccccccccccccccccccccccccccccccc 09eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee in diff --git a/tools/FLOW.md b/tools/FLOW.md new file mode 100644 index 0000000..8714b7e --- /dev/null +++ b/tools/FLOW.md @@ -0,0 +1,79 @@ +# Omega flow: everything GitHub did, without GitHub + +GitHub did four jobs for this project: it held the repositories, it carried the pull requests and +reviews, it ran the checks (Actions), and it decided what could merge (branch protection). Radicle +already does the first two, peer to peer, with no company in the path. Omega flow does the other +two, as seeded Omega that anyone can rebuild from the 408-byte seed and check. + +## What replaces what + +| GitHub | Replacement | Status | +|---|---|---| +| Repositories, forks, clones | Radicle repositories, served from my own seed | in use (README) | +| Pull requests | Radicle patches: `git push rad HEAD:refs/patches` | Radicle, in use | +| Review comments | Radicle patch comments and reviews | Radicle; flow posts every gate verdict as a comment | +| Issues | Radicle issues: `rad issue` | Radicle | +| Identity, permissions | Radicle node keys, delegates and their threshold | Radicle | +| Actions (CI) | `tools/flow.om` gates every patch with the default branch's `build.sh` | HOST_TESTED 2026-11-08 | +| Required status checks, branch protection | flow merges only a fast-forward whose exact tree has a pass receipt | HOST_TESTED; Lean model, see below | +| Merge queue | flow merges one patch per cycle, queues the rest, refuses a stale plan | HOST_TESTED | +| Pages | www.aienos.com on my own hardware | in use | +| Discovery | `repos.tsv`, the README's link table, radicle.network | in use | +| A read-only copy for people who look there | the GitHub mirror of this one repository | in use | + +## How a cycle works + +`sh tools/flow-cycle.sh FLOW REPO DEFAULT RECEIPTS` runs one cycle in three parts. Only the +middle part decides anything, and it is seeded Omega: + +1. **Collect** (git plumbing, no decisions): fetch, then list each open patch's head, the head's + tree, and whether merging it is a fast-forward (`ff`), needs a rebase (`div`) or is already + in the default branch (`in`). +2. **Plan** (`tools/flow.om`): from those facts and the receipt ledger, give each patch one action, + `merged`, `merge`, `queued`, `rebase`, `fail`, `gate` or `same`, and write the script `flow.sh` + that carries them out. The output depends only on the input bytes. +3. **Act** (`flow.sh`): refuse if the default branch moved since step 1; gate each new tree in a + worktree with the *default branch's* `build.sh`, never the patch's own; append the verdict to + the ledger and post it on the patch; fast-forward the default branch to at most one patch. + +Receipts are keyed by (tree, gate): the git tree of the patch head and the blob id of the gate +that judged it. A tree is gated once, however many patches carry it and however often agents +re-push it, and never again until `build.sh` itself changes. That keeps CI cost proportional to +distinct work, which is what about 20 agents pushing in parallel need, and is the load GitHub's +abuse filters refused. + +## What has been checked, and how + +| Check | Where | Result, 2026-11-08 | +|---|---|---| +| flow.om reproduces both fixtures byte for byte, AArch64 and native x86-64 | `build.sh` step 12 | PASS | +| refuses 21 malformed inputs and 3 malformed arguments, one `flow:` line each, no output | step 12 | PASS | +| 4096 patches against 16384 receipts | by hand | 0.06 s native x86-64, 0.13 s AArch64 under qemu | +| the whole queue against a real, offline Radicle repository: 5 patches, 3 cycles | `tests/flow/e2e.sh`, step 12b | 7 of 7 PASS with each build (radicle-cli 0.23.0, radicle-node 0.21.1, radicle-remote-helper 0.18.1) | +| a patch that rewrites its own `build.sh` to `exit 0` still fails | e2e cycle 1 | PASS | +| the gate runs without the delegate's `RAD_PASSPHRASE`, `RAD_HOME`, `SSH_AUTH_SOCK` or `HOME`; a planner built without that scrub fails e2e | e2e cycle 1, control by hand | PASS | +| the merged tree has a pass receipt after every cycle; a stale plan refuses to run | e2e | PASS | +| merge only to a passing fast-forward, at most one merge, each tree gated once, a failed tree never merges | `tests/flow/Flow.lean` | written, **not yet machine-checked** | + +The Lean file models the decision rules, not the Omega program: the fixtures and the e2e run test +that flow.om follows the model; they do not prove it. + +## Next, in order + +1. **Run it on the seed.** A timer or a `rad node events` watcher on the seed node runs + `flow-cycle.sh` for SI with the delegate's key, so a patch is gated within seconds of reaching + the seed. This is the step that makes GitHub unnecessary for day-to-day work. A gate runs the + patch's code: flow already keeps the delegate's passphrase, Radicle home and agent socket out of + its environment, but on the seed it must also run as its own user with no network and no read + access to the delegate's key file, before it gates anyone else's patches. +2. **Patches from other peers.** e2e today opens patches as the delegate itself. Next: agents as + separate Radicle peers on two local nodes, with the delegate's node gating and merging. +3. **Signed, shared receipts.** Move the ledger from a local file into a Radicle collaborative + object signed by the gate node's key, so every seed can trust and reuse a verdict without + re-running it. +4. **Parallel gates.** Distinct trees are independent, so gate them on all cores, then on several + machines; content addressing makes the results safe to merge. +5. **No shell glue.** Read refs and objects directly from Omega instead of through `git`, so the + collector is seeded Omega too. +6. **Releases and triage.** Signed release tags with their gate receipts; issue triage from the + same receipts. diff --git a/tools/flow-cycle.sh b/tools/flow-cycle.sh new file mode 100644 index 0000000..c54e4e3 --- /dev/null +++ b/tools/flow-cycle.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# flow-cycle.sh: one cycle of the Omega merge queue for one Radicle working copy. +# +# sh tools/flow-cycle.sh FLOW REPO DEFAULT RECEIPTS +# +# FLOW the planner built from tools/flow.om by the seed-born compiler (build.sh writes +# out/flow-a64 and out/flow-x86) +# REPO a Radicle working copy of the project, with its delegate's key authenticated +# DEFAULT the name of the project's default branch +# RECEIPTS the receipt ledger, one per project: tree, gate, verdict, log sha256; created with +# its header line when missing, and only ever appended to +# +# The cycle has three parts, and only the middle one decides anything: +# 1. collect: fetch the repository, then list every open patch's head, the head's tree, and +# how the head relates to the default branch (ff, div or in), with git plumbing only +# 2. plan: FLOW turns those facts and the ledger into a script, flow.sh; every decision is +# made there, in seeded Omega +# 3. act: run flow.sh, which gates, comments and merges exactly as the plan says, and +# refuses if the default branch moved since the facts were collected +# Run it again to advance the queue: each cycle merges at most one patch. +# FLOW_DIR=path keeps the cycle's facts and plan there (default: a temporary directory that is +# removed). Exit status: flow.sh's, or 1 when a part before it fails, or 2 for usage. +set -eu +[ $# -eq 4 ] || { echo 'usage: sh tools/flow-cycle.sh FLOW REPO DEFAULT RECEIPTS' >&2; exit 2; } +flow=$1 repo=$2 def=$3 rec=$4 +die() { printf 'flow-cycle: %s\n' "$*" >&2; exit 1; } +g() { git -C "$repo" "$@"; } +[ -x "$flow" ] || die "no planner $flow" +if [ -n "${FLOW_DIR:-}" ]; then d=$FLOW_DIR; mkdir -p -- "$d"; else d=$(mktemp -d); trap 'rm -rf "$d"' EXIT; fi + +g fetch -q --prune rad || die "cannot fetch rad in $repo" +branch=$(g rev-parse --verify -q "refs/remotes/rad/$def^{commit}") || die "$repo has no rad/$def" +gate=$(g rev-parse --verify -q "$branch:build.sh") || die "the default branch at $branch has no build.sh" +[ -f "$rec" ] || printf 'tree\tgate\tverdict\tlog\n' > "$rec" + +printf 'patch\thead\ttree\trel\n' > "$d/state.tsv" +g for-each-ref --format='%(refname:lstrip=4) %(objectname)' refs/remotes/rad/patches/ > "$d/heads" +while read -r p h; do + t=$(g rev-parse --verify -q "$h^{tree}") || die "patch $p: head $h has no tree" + if g merge-base --is-ancestor "$h" "$branch"; then r=in + elif g merge-base --is-ancestor "$branch" "$h"; then r=ff + else r=div; fi + printf '%s\t%s\t%s\t%s\n' "$p" "$h" "$t" "$r" >> "$d/state.tsv" +done < "$d/heads" + +"$flow" "$branch" "$gate" "$d/state.tsv" "$rec" "$d/flow.sh" || die "the planner refused the facts in $d" +sh "$d/flow.sh" "$repo" "$def" "$rec" diff --git a/tools/flow.om b/tools/flow.om new file mode 100644 index 0000000..5522161 --- /dev/null +++ b/tools/flow.om @@ -0,0 +1,774 @@ +om2 +note tools/flow.om: the decision core of the Omega merge queue for one Radicle repository, in pure +note seeded Omega. +note +note A repository's open patches wait for the default branch. For one default-branch head this +note program decides what each patch needs next and writes the plan as the POSIX sh script that +note carries it out, so every decision is made from data, never by hand. Build with the seed-born +note compiler: +note omc < tools/flow.om > flow && chmod +x flow +note { printf 'target x86linux\n'; cat tools/flow.om; } | omc-x86 > flow (native x86-64) +note Run: +note flow BRANCH GATE STATE RECEIPTS OUT +note BRANCH exactly 40 lowercase hex digits: the default-branch head the plan is for +note GATE exactly 40 lowercase hex digits: the git blob id of build.sh at BRANCH +note STATE read: the open patches. The header line is exactly +note patch TAB head TAB tree TAB rel +note then one patch per line, exactly 4 TAB-separated fields: +note patch 40 lowercase hex digits, the patch id +note head 40 lowercase hex digits, the patch head commit +note tree 40 lowercase hex digits, the tree of that commit +note rel ff (BRANCH is an ancestor of head), div (the two diverged) or in +note (head is already in BRANCH) +note A patch id appears once. The header alone is valid. At most 4096 rows and 4 MiB. +note RECEIPTS read: the receipt ledger. The header line is exactly +note tree TAB gate TAB verdict TAB log +note then one receipt per line, exactly 4 TAB-separated fields: +note tree 40 lowercase hex digits, the tree that was gated +note gate 40 lowercase hex digits, the blob of the build.sh that gated it +note verdict pass or fail +note log 64 lowercase hex digits, the sha256 of the gate's log +note A (tree, gate) pair appears once, even with the same verdict. At most 16384 rows +note and 4 MiB. Only rows whose gate is GATE are consulted. +note In both files every line ends in a newline: an empty file, a blank line, a CR +note anywhere or a last line without its newline is refused. +note OUT written: the script. +note Decision for each STATE row, in input order, with r the receipt for (the row's tree, GATE): +note 1. rel in merged (whatever r says) +note 2. r pass, rel ff, no earlier row got merge merge +note r pass, rel ff, an earlier row got merge queued (one merge per plan; the next +note plan, made after it, decides) +note r pass, rel div rebase +note 3. r fail fail +note 4. no r, no earlier row has the same tree gate +note no r, an earlier row has the same tree same (a tree is gated once) +note In rule 4 every earlier row with that tree counts, whatever its own decision; with no +note receipt for the tree they all lack one. +note Lookups go through open-addressing hash tables (FNV-1a 64 over the hex bytes, linear probing, +note never more than half full): receipts by (tree, gate), patches by id, trees seen so far by +note tree. The same tables find the duplicates, so a run is linear in the input size. +note The script: a fixed comment header; "# plan-branch BRANCH", "# plan-gate GATE" and +note "# plan-counts patches N merged A merge B queued C rebase D fail E gate F same G" (N rows, +note then the rows given each decision); a blank line; branch='BRANCH' and gate='GATE'; the fixed +note shell body, which checks the repository against BRANCH and GATE and defines fl_die, fl_git, +note fl_tree, fl_gate and fl_merge; then for every row, in input order, +note "# plan ACTION PATCH HEAD TREE", followed for merge by "fl_merge 'PATCH' 'HEAD' 'TREE'" and +note for gate by "fl_gate 'PATCH' 'HEAD' 'TREE'"; last "echo 'flow: step done'". Every value in +note the script is validated hex, so none needs quoting beyond its single quotes. +note Exit 0 when OUT is written; 2 for the wrong number of arguments, with the usage line on +note stderr; 1 for any refusal, with one stderr line "flow: ..." that names the argument, or the +note file and its line, and OUT not opened (the script is built in memory and written once, at +note the end). The output depends only on the arguments and the input bytes: no clock, no +note environment. + +note the input regions of inp: STATE at 0, RECEIPTS at RB (each 4 MiB and one byte to see the +note end), BRANCH at BO and GATE at GO (40 bytes each). Every byte compared or copied lives here. +const RB ulong 4194312 +const BO ulong 8388624 +const GO ulong 8388664 +global inp uchar 8388704 +note the script bytes +global ob uchar 4194304 +global obn ulong 1 +note the path named by the current argument +global pb uchar 4096 +global pbn ulong 1 +note STATE rows: line offset, rel (1 ff, 2 div, 3 in), decision; and the row count +global so ulong 4096 +global sr ulong 4096 +global sa ulong 4096 +global ns ulong 1 +note RECEIPTS rows: line offset, verdict (1 pass, 2 fail) +global ro ulong 16384 +global rv ulong 16384 +note hash tables, each slot row index + 1 or 0 for empty: receipts by tree and gate, patches by +note id, trees seen by tree +global hr ulong 32768 +global hp ulong 8192 +global ht ulong 8192 +note the current line's 4 fields: offset and length of each +global fld ulong 8 +note rows per decision: merged merge queued rebase fail gate same +global cnt ulong 7 + +text tpre "flow: " +text tnl "\n" +text tusage "usage: flow BRANCH GATE STATE RECEIPTS OUT\n" +text targ "cannot read a command-line argument" +text tbr "BRANCH is not 40 lowercase hex digits" +text tgt "GATE is not 40 lowercase hex digits" +text topen "cannot open " +text tread "cannot read " +text tbig "file over 4 MiB: " +text twrite "cannot write " +text toverf "script over the 4 MiB buffer" +text tline ": line " +text tcol ": " +text tempty "empty file" +text tnonl "no newline at the end of the line" +text tblank "blank line" +text tcr "carriage return in the line" +text tcols "row needs exactly 4 TAB-separated fields" +text tshdr "patch\thead\ttree\trel" +text trhdr "tree\tgate\tverdict\tlog" +text tbsh "header line is not patch TAB head TAB tree TAB rel" +text tbrh "header line is not tree TAB gate TAB verdict TAB log" +text tmanys "more than 4096 rows" +text tmanyr "more than 16384 rows" +text tbpa "patch is not 40 lowercase hex digits" +text tbhd "head is not 40 lowercase hex digits" +text tbtr "tree is not 40 lowercase hex digits" +text tbrel "rel is not ff, div or in" +text tbgt "gate is not 40 lowercase hex digits" +text tbvd "verdict is not pass or fail" +text tblog "log is not 64 lowercase hex digits" +text tdupp "patch already on line " +text tdupr "tree and gate already on line " +text rff "ff" +text rdiv "div" +text rin "in" +text vpass "pass" +text vfail "fail" +note the decision names, by code 0..6 (fail is vfail) +text n0 "merged" +text n1 "merge" +text n2 "queued" +text n3 "rebase" +text n5 "gate" +text n6 "same" +text pbr "# plan-branch " +text pgt "\n# plan-gate " +text pc0 "\n# plan-counts patches " +text pc1 " merged " +text pc2 " merge " +text pc3 " queued " +text pc4 " rebase " +text pc5 " fail " +text pc6 " gate " +text pc7 " same " +text pv1 "\n\nbranch='" +text pv2 "'\ngate='" +text q2 "' '" +text q3 "'\n" +text sp " " +text pln "# plan " +text fm "fl_merge '" +text fg "fl_gate '" +text tend "echo 'flow: step done'\n" + +note the fixed parts of the script, paragraph by paragraph: the comment header (h0, h1), then the +note body (b0 checks, b1 fl_tree, b2 fl_gate, b3 fl_merge) +text h0 "#!/bin/sh\n# flow.sh: one step of the Omega merge queue for one Radicle repository.\n# Generated by tools/flow.om from the repository's open patches and its receipt ledger; do not\n# edit by hand.\n#\n# A patch merges only as a fast-forward of the default branch, and only when a receipt says the\n# default branch's gate passed on the patch head's exact tree, so the default branch only ever\n# moves to a tree that passed the gate guarding it. A patch whose tree has no receipt is gated:\n# the default branch's build.sh, never the patch's own, runs in a worktree of the patch head,\n# and the verdict goes into the ledger and onto the patch as a comment. A tree is gated once,\n# however many patches carry it, and never again while the gate is unchanged.\n#\n" +text h1 "# sh flow.sh REPO DEFAULT RECEIPTS\n# REPO a Radicle working copy, fetched, with its delegate's key authenticated (rad auth)\n# DEFAULT the name of its default branch\n# RECEIPTS the receipt ledger this plan was made from; new verdicts are appended to it\n# Exit status: 0 the step ran; 1 the repository is not as planned, or a gate, comment or push\n# could not run; 2 usage.\n# Each patch's decision is one \"# plan ACTION PATCH HEAD TREE\" line, in the order of the input.\n" +text b0 "set -eu\numask 022\n[ $# -eq 3 ] || { echo 'usage: sh flow.sh REPO DEFAULT RECEIPTS' >&2; exit 2; }\nrepo=$1 def=$2 rec=$3\nfl_die() { printf 'flow: %s\\n' \"$*\" >&2; exit 1; }\nfl_git() { git -C \"$repo\" \"$@\"; }\ncommand -v git >/dev/null 2>&1 || fl_die \"git is required\"\ncommand -v rad >/dev/null 2>&1 || fl_die \"rad is required: https://radicle.xyz\"\n[ -f \"$rec\" ] || fl_die \"no receipt ledger $rec\"\ncur=$(fl_git rev-parse --verify -q \"refs/remotes/rad/$def^{commit}\") || fl_die \"$repo has no rad/$def\"\n[ \"$cur\" = \"$branch\" ] || fl_die \"rad/$def is $cur, not $branch: make a new plan\"\n[ \"$(fl_git rev-parse --verify -q \"$branch:build.sh\")\" = \"$gate\" ] || fl_die \"build.sh at $branch is not blob $gate\"\nfl_work=$(mktemp -d)\ntrap 'rm -rf \"$fl_work\"; fl_git worktree prune' EXIT\n" +text b1 "trap 'exit 1' HUP INT TERM\nfl_tree() { # fl_tree PATCH HEAD TREE: HEAD must be a commit whose tree is TREE\n [ \"$(fl_git rev-parse --verify -q \"$2^{tree}\")\" = \"$3\" ] || fl_die \"patch $1: $2 is not a commit with tree $3\"\n}\n" +text b2 "fl_gate() { # fl_gate PATCH HEAD TREE: run the default branch's build.sh on HEAD; record and post the verdict\n fl_tree \"$@\"\n w=$fl_work/$3\n fl_git worktree add -q --detach \"$w\" \"$2\" || fl_die \"patch $1: cannot check out $2\"\n fl_git cat-file blob \"$gate\" > \"$w/build.sh\"\n mkdir \"$w.home\"\n # the gate never sees the delegate's key: no RAD_PASSPHRASE, RAD_SECRET, RAD_HOME, SSH_AUTH_SOCK or HOME\n if (cd \"$w\" && env -u RAD_PASSPHRASE -u RAD_SECRET -u RAD_HOME -u SSH_AUTH_SOCK HOME=\"$w.home\" sh ./build.sh) > \"$w.log\" 2>&1; then v=pass; else v=fail; fi\n l=$(sha256sum < \"$w.log\" | cut -c1-64)\n printf '%s\\t%s\\t%s\\t%s\\n' \"$3\" \"$gate\" \"$v\" \"$l\" >> \"$rec\"\n m=\"flow gate $v tree $3 gate $gate log $l\"\n f=$(sed -n '/^FAIL/{p;q;}' \"$w.log\")\n [ -z \"$f\" ] || m=\"$m\n$f\"\n (cd \"$repo\" && rad patch comment \"$1\" -m \"$m\" > /dev/null) || fl_die \"patch $1: cannot post the verdict\"\n fl_git worktree remove --force \"$w\"\n printf 'flow: gate %s patch %s tree %s\\n' \"$v\" \"$1\" \"$3\"\n}\n" +text b3 "fl_merge() { # fl_merge PATCH HEAD TREE: fast-forward DEFAULT to HEAD, whose tree passed the gate\n fl_tree \"$@\"\n fl_git merge-base --is-ancestor \"$branch\" \"$2\" || fl_die \"patch $1: $2 does not fast-forward $branch\"\n fl_git push -q rad \"$2:refs/heads/$def\" || fl_die \"patch $1: the push of $2 to $def was refused\"\n printf 'flow: merged patch %s head %s\\n' \"$1\" \"$2\"\n}\n\n" + +note wdec: write v in decimal to stderr +fn wdec v ulong to void +d = new uchar 20 0 +x = v +k = ulong 20 +note a ulong has at most 20 digits +while true bound 20 +k = sub k 1 +put d k uchar add rem x 10 48 +x = div x 10 +if eq x 0 +break +end +end +w = write 2 d k sub 20 k +end + +note diem: refuse with m alone, then exit 1 +fn diem m view uchar * to void +w = write 2 tpre 0 len tpre +w = write 2 m 0 len m +w = write 2 tnl 0 len tnl +exit 1 +end + +note dief: refuse with m and the current path, then exit 1 +fn dief m view uchar * to void +w = write 2 tpre 0 len tpre +w = write 2 m 0 len m +w = write 2 pb 0 get pbn 0 +w = write 2 tnl 0 len tnl +exit 1 +end + +note diel: refuse line ln of the current file with m, then the line number j when j is not 0; +note exit 1 +fn diel m view uchar * ln ulong j ulong to void +w = write 2 tpre 0 len tpre +w = write 2 pb 0 get pbn 0 +w = write 2 tline 0 len tline +wdec ln +w = write 2 tcol 0 len tcol +w = write 2 m 0 len m +if ne j 0 +wdec j +end +w = write 2 tnl 0 len tnl +exit 1 +end + +note chk: refuse line ln with m when ok is false +fn chk ok bool m view uchar * ln ulong to void +if not ok +diel m ln 0 +end +end + +note setpath: copy command-line argument ai into pb +fn setpath ai ulong to void +n = argread ai pb 0 4096 +if le n 0 +diem targ +end +put pbn 0 ulong n +end + +note load: read the file named by argument ai into inp from b; return the offset of its end +fn load ai ulong b ulong to ulong +setpath ai +fd = fopen pb 0 get pbn 0 0 0 +if lt fd 0 +dief topen +end +p = ulong 0 +note every pass but the last reads at least one byte of the 4194305-byte region +while lt p 4194305 bound 4194306 +k = read int fd inp add b p sub 4194305 p +if lt k 0 +dief tread +end +if eq k 0 +break +end +p = add p ulong k +end +c = fclose int fd +if gt p 4194304 +dief tbig +end +ret add b p +end + +note wout: write ob to the file named by argument ai (created or truncated, mode 0644) +fn wout ai ulong to void +setpath ai +fd = fopen pb 0 get pbn 0 577 420 +if lt fd 0 +dief topen +end +n = get obn 0 +p = ulong 0 +while lt p n bound 4194305 +k = write int fd ob p sub n p +if le k 0 +dief twrite +end +p = add p ulong k +end +c = fclose int fd +if ne c 0 +dief twrite +end +end + +note em: append a text to ob +fn em t view uchar * to void +p = get obn 0 +n = len t +if gt n sub 4194304 p +diem toverf +end +for i ulong n bound 4194304 +put ob add p i get t i +end +put obn 0 add p n +end + +note emi: append n bytes of inp from o to ob +fn emi o ulong n ulong to void +p = get obn 0 +if gt n sub 4194304 p +diem toverf +end +for i ulong n bound 4194304 +put ob add p i get inp add o i +end +put obn 0 add p n +end + +note emb: append one byte to ob +fn emb c uchar to void +p = get obn 0 +if ge p 4194304 +diem toverf +end +put ob p c +put obn 0 add p 1 +end + +note emu: append v in decimal to ob +fn emu v ulong to void +d = new uchar 20 0 +x = v +put d 0 uchar add rem x 10 48 +k = ulong 1 +x = div x 10 +while gt x 0 bound 20 +put d k uchar add rem x 10 48 +k = add k 1 +x = div x 10 +end +while gt k 0 bound 20 +k = sub k 1 +emb get d k +end +end + +note eqx: do the n bytes of inp at o equal the text t +fn eqx o ulong n ulong t view uchar * to bool +if ne n len t +ret false +end +for i ulong n bound 4194304 +if ne get inp add o i get t i +ret false +end +end +ret true +end + +note eqs: do the n bytes of inp at a equal the n bytes at b (n at most 64) +fn eqs a ulong b ulong n ulong to bool +for i ulong n bound 64 +if ne get inp add a i get inp add b i +ret false +end +end +ret true +end + +fn ishex c uchar to bool +ret or and ge c 48 le c 57 and ge c 97 le c 102 +end + +note vhex: is inp[o, o+n) exactly w lowercase hex digits (w at most 64) +fn vhex o ulong n ulong w ulong to bool +if ne n w +ret false +end +for i ulong w bound 64 +if not ishex get inp add o i +ret false +end +end +ret true +end + +note hx: FNV-1a 64 of the n bytes of inp at o (n at most 64), continuing from h +fn hx o ulong n ulong h ulong to ulong +x = h +for i ulong n bound 64 +x = mul xor x ulong get inp add o i 1099511628211 +end +ret x +end + +note hash40: the hash of the 40 bytes of inp at k, high half folded into the low +fn hash40 k ulong to ulong +x = hx k 40 14695981039346656037 +ret xor x shr x 32 +end + +note pslot: the slot of hp that holds the patch id at k, or the empty slot where it goes. +note At most 4096 of the 8192 slots are full, so an empty one comes within 4097 probes. +fn pslot k ulong to ulong +s = and hash40 k 8191 +while true bound 8192 +j = get hp s +if eq j 0 +ret s +end +if eqs get so sub j 1 k 40 +ret s +end +s = and add s 1 8191 +end +ret s +end + +note tslot: the slot of ht that holds the tree at k, or the empty slot where it goes +fn tslot k ulong to ulong +s = and hash40 k 8191 +while true bound 8192 +j = get ht s +if eq j 0 +ret s +end +if eqs add get so sub j 1 82 k 40 +ret s +end +s = and add s 1 8191 +end +ret s +end + +note rslot: the slot of hr that holds the receipt for the tree at t and the gate at g, or the +note empty slot where it goes. At most 16384 of the 32768 slots are full. +fn rslot t ulong g ulong to ulong +x = hx g 40 hx t 40 14695981039346656037 +s = and xor x shr x 32 32767 +while true bound 32768 +j = get hr s +if eq j 0 +ret s +end +o = get ro sub j 1 +if eqs o t 40 +if eqs add o 41 g 40 +ret s +end +end +s = and add s 1 32767 +end +ret s +end + +note lend: the newline that ends line ln, which starts at p (e is the end of the file); refuse a +note blank line, a CR and a last line without its newline +fn lend p ulong e ulong ln ulong to ulong +q = p +note every pass looks at one byte of the file +while lt q e bound 4194305 +c = get inp q +if eq c 10 +if eq q p +diel tblank ln 0 +end +ret q +end +if eq c 13 +diel tcr ln 0 +end +q = add q 1 +end +diel tnonl ln 0 +ret e +end + +note split4: store the 4 TAB-separated fields of line ln, [p, q), in fld; refuse another count +fn split4 p ulong q ulong ln ulong to void +k = ulong 0 +s = p +t = p +while lt t q bound 4194305 +if eq get inp t 9 +if ge k 3 +diel tcols ln 0 +end +put fld mul k 2 s +put fld add mul k 2 1 sub t s +k = add k 1 +s = add t 1 +end +t = add t 1 +end +if ne k 3 +diel tcols ln 0 +end +put fld 6 s +put fld 7 sub q s +end + +note fo / fl: offset and length of field k of the current line +fn fo k ulong to ulong +ret get fld mul k 2 +end + +fn fl k ulong to ulong +ret get fld add mul k 2 1 +end + +note srow: check STATE row r, the line [p, q); record its offset and rel; refuse a repeated +note patch. With every field checked, patch is at p, head at p+41, tree at p+82. +fn srow r ulong p ulong q ulong to void +ln = add r 2 +split4 p q ln +chk vhex fo 0 fl 0 40 tbpa ln +chk vhex fo 1 fl 1 40 tbhd ln +chk vhex fo 2 fl 2 40 tbtr ln +o = fo 3 +n = fl 3 +x = ulong 0 +if eqx o n rff +x = ulong 1 +end +if eqx o n rdiv +x = ulong 2 +end +if eqx o n rin +x = ulong 3 +end +chk ne x 0 tbrel ln +put so r p +put sr r x +s = pslot p +j = get hp s +if ne j 0 +diel tdupp ln add j 1 +end +put hp s add r 1 +end + +note rrow: check RECEIPTS row i, the line [p, q); record its offset and verdict; refuse a +note repeated (tree, gate). With every field checked, tree is at p, gate at p+41. +fn rrow i ulong p ulong q ulong to void +ln = add i 2 +split4 p q ln +chk vhex fo 0 fl 0 40 tbtr ln +chk vhex fo 1 fl 1 40 tbgt ln +o = fo 2 +n = fl 2 +x = ulong 0 +if eqx o n vpass +x = ulong 1 +end +if eqx o n vfail +x = ulong 2 +end +chk ne x 0 tbvd ln +chk vhex fo 3 fl 3 64 tblog ln +put ro i p +put rv i x +s = rslot p add p 41 +j = get hr s +if ne j 0 +diel tdupr ln add j 1 +end +put hr s add i 1 +end + +note pstate: check the STATE bytes [0, e): the header, then every row +fn pstate e ulong to void +if eq e 0 +diel tempty 1 0 +end +q = lend 0 e 1 +if not eqx 0 q tshdr +diel tbsh 1 0 +end +p = add q 1 +r = ulong 0 +note every pass consumes at least one byte of the file +while lt p e bound 4194305 +if ge r 4096 +diel tmanys add r 2 0 +end +q = lend p e add r 2 +srow r p q +r = add r 1 +p = add q 1 +end +put ns 0 r +end + +note prec: check the RECEIPTS bytes [RB, e): the header, then every row +fn prec e ulong to void +if eq e RB +diel tempty 1 0 +end +q = lend RB e 1 +if not eqx RB sub q RB trhdr +diel tbrh 1 0 +end +p = add q 1 +i = ulong 0 +note every pass consumes at least one byte of the file +while lt p e bound 4194305 +if ge i 16384 +diel tmanyr add i 2 0 +end +q = lend p e add i 2 +rrow i p q +i = add i 1 +p = add q 1 +end +end + +note argx: copy argument ai, which must be exactly 40 lowercase hex digits, into inp at o; +note refuse with m +fn argx ai ulong o ulong m view uchar * to void +n = argread ai pb 0 4096 +if ne n 40 +diem m +end +for i ulong 40 +c = get pb i +if not ishex c +diem m +end +put inp add o i c +end +end + +note act: the decision for STATE row r, 0 merged, 1 merge, 2 queued, 3 rebase, 4 fail, 5 gate, +note 6 same; mg: an earlier row got merge; sn: an earlier row has the same tree +fn act r ulong mg bool sn bool to ulong +x = get sr r +if eq x 3 +ret 0 +end +j = get hr rslot add get so r 82 GO +if eq j 0 +if sn +ret 6 +end +ret 5 +end +if eq get rv sub j 1 2 +ret 4 +end +if eq x 2 +ret 3 +end +if mg +ret 2 +end +ret 1 +end + +note decide: decide every STATE row in input order, recording each tree as it is seen +fn decide to void +n = get ns 0 +mg = false +for r ulong n bound 4096 +s = tslot add get so r 82 +sn = ne get ht s 0 +if not sn +put ht s add r 1 +end +a = act r mg sn +put sa r a +put cnt a add get cnt a 1 +if eq a 1 +mg = true +end +end +end + +note emname: append the name of decision a +fn emname a ulong to void +if eq a 0 +em n0 +end +if eq a 1 +em n1 +end +if eq a 2 +em n2 +end +if eq a 3 +em n3 +end +if eq a 4 +em vfail +end +if eq a 5 +em n5 +end +if eq a 6 +em n6 +end +end + +note emq: append 'PATCH' 'HEAD' 'TREE' and a newline for the row whose line starts at p +fn emq p ulong to void +emi p 40 +em q2 +emi add p 41 40 +em q2 +emi add p 82 40 +em q3 +end + +note emrow: append row r's plan line, and its call for merge and gate +fn emrow r ulong to void +p = get so r +a = get sa r +em pln +emname a +em sp +emi p 40 +em sp +emi add p 41 40 +em sp +emi add p 82 40 +em tnl +if eq a 1 +em fm +emq p +end +if eq a 5 +em fg +emq p +end +end + +fn main to int +if ne argcnt 6 +w = write 2 tusage 0 len tusage +ret 2 +end +argx 1 BO tbr +argx 2 GO tgt +e = load 3 0 +pstate e +e = load 4 RB +prec e +decide +em h0 +em h1 +em pbr +emi BO 40 +em pgt +emi GO 40 +em pc0 +emu get ns 0 +em pc1 +emu get cnt 0 +em pc2 +emu get cnt 1 +em pc3 +emu get cnt 2 +em pc4 +emu get cnt 3 +em pc5 +emu get cnt 4 +em pc6 +emu get cnt 5 +em pc7 +emu get cnt 6 +em pv1 +emi BO 40 +em pv2 +emi GO 40 +em q3 +em b0 +em b1 +em b2 +em b3 +n = get ns 0 +for r ulong n bound 4096 +emrow r +end +em tend +wout 5 +ret 0 +end