@patronage/factory-ci 1.0.0-alpha.41 → 1.0.0-alpha.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -407,7 +407,7 @@ The **proof-reuse gate** decides whether a hosted job may reuse the local verifi
407
407
 
408
408
  There is no mode to choose. The emitted condition is a trust predicate, so the package owns it: the merge-target opt-out and its push-event merge fallback (#611) are deleted, and no consumer option can reuse proof outside a pull request.
409
409
 
410
- A proof is reusable only when the complete Checks API result (`filter=all`, every page) establishes one unambiguous newest generation by greatest `started_at`, produced by the pinned App for the exact repository and head, completed successfully with `outcome: passed`, and covering every command identity the guarded surface requires. Coverage is the union of `executedCommands` and commands released by `notRequiredCommands` only after the gate validates each release against that same proof binding's command-to-target map and identity-bound impact stamp. Missing, malformed, duplicated, affected, unknown, or differently bound release data refuses reuse and runs the hosted surface. `mode` is reported as diagnostic metadata, never authorized on: a reduced-mode proof whose executed and stamp-authorized released commands cover a surface is reusable.
410
+ A proof is reusable only when the complete Checks API result (`filter=all`, every page) establishes one unambiguous newest generation, produced by the pinned App for the exact repository and head, completed successfully with `outcome: passed`, whose verification started after every other run at that head was posted, and covering every command identity the guarded surface requires. A pass is ordered at its typed proof's start, never later than its own `started_at`, and every other run at its `started_at`; the doc comment above `GATE_JQ` in `src/proof-reuse-gate.ts` states the full rule (#1490, #1506). Coverage is the union of `executedCommands` and commands released by `notRequiredCommands` only after the gate validates each release against that same proof binding's command-to-target map and identity-bound impact stamp. Missing, malformed, duplicated, affected, unknown, or differently bound release data refuses reuse and runs the hosted surface. `mode` is reported as diagnostic metadata, never authorized on: a reduced-mode proof whose executed and stamp-authorized released commands cover a surface is reusable.
411
411
 
412
412
  The only thing a consumer chooses is **what its surface requires**, expressed as the plain profile command objects that surface selects — a repository with distinct core and docs jobs selects distinct sets and gets distinct required coverage. `proofReuseRequiredCommands()` is the single derivation both the gate and the assertion go through. Identities are baked into the emitted script, so they are held to a plain `[A-Za-z0-9_][\w.:@/-]*` allow-list and shell-quoted at the interpolation site; a selection carrying anything else is unusable and degrades to a gate that always refuses.
413
413
 
@@ -1,3 +1,3 @@
1
1
  {
2
- "fingerprint": "3b1f520867e761216243d4a3f5577bd738a5931601b0e3ecc60e2e54147316d1"
2
+ "fingerprint": "5fb898a6c12742fa0bce46740c73196710cd687d4cc29e4874f91067083ef1e3"
3
3
  }
package/dist/index.d.ts CHANGED
@@ -750,7 +750,9 @@ declare const FACTORY_PROOF_GATE_IF = "github.event_name == 'pull_request'";
750
750
  * cover every required command
751
751
  * - `ambiguous` two different generations share the newest instant, the
752
752
  * newest repeats another run's verdict with no proof time to
753
- * tell them apart, or a run's start time cannot be ranked
753
+ * tell them apart, a run's start time cannot be ranked, or the
754
+ * newest pass started before another run at this head was
755
+ * posted
754
756
  * - `error` the gate could not reach a decision (fail open)
755
757
  *
756
758
  * There is no `reduced`: mode stopped being an authorization condition in
package/dist/index.js CHANGED
@@ -534,7 +534,9 @@ const FACTORY_PROOF_GATE_IF = "github.event_name == 'pull_request'";
534
534
  * cover every required command
535
535
  * - `ambiguous` two different generations share the newest instant, the
536
536
  * newest repeats another run's verdict with no proof time to
537
- * tell them apart, or a run's start time cannot be ranked
537
+ * tell them apart, a run's start time cannot be ranked, or the
538
+ * newest pass started before another run at this head was
539
+ * posted
538
540
  * - `error` the gate could not reach a decision (fail open)
539
541
  *
540
542
  * There is no `reduced`: mode stopped being an authorization condition in
@@ -574,6 +576,24 @@ const COMMAND_IDENTITY_MAX_LENGTH = 120;
574
576
  * lockstep with the proof/check-payload producer.
575
577
  */
576
578
  const TRUSTED_IMPACT_STAMP_VERSION = 4;
579
+ /**
580
+ * The verify modes the CLI reads a payload under (`isResolvedPrVerifyMode` in
581
+ * `software-factory/src/pr-verify-mode.ts`). factory-ci cannot import it, so
582
+ * the list is repeated here, and the software-factory differential suite pins
583
+ * the two lists together (#1510).
584
+ */
585
+ const RESOLVED_VERIFY_MODES = [
586
+ "docs-only",
587
+ "trivial",
588
+ "full"
589
+ ];
590
+ /**
591
+ * The check-text size over which the CLI drops a transported typed proof
592
+ * (`readTransportedVerifyProof` in
593
+ * `software-factory/src/pr-verify-check-payload.ts`), in UTF-8 bytes of the
594
+ * raw text it parsed, fence included (#1510).
595
+ */
596
+ const TRANSPORT_CAP_BYTES = 6e4;
577
597
  const isProofReuseCommand = (value) => {
578
598
  if (!(value && typeof value === "object")) return false;
579
599
  const entry = value;
@@ -658,64 +678,168 @@ const resolveProofReuseCommands = (catalog, names, selectionLabel) => names.map(
658
678
  * document. Anything unparseable degrades to `null`, and therefore to a
659
679
  * refusal.
660
680
  *
661
- * This is the *second* parser of that payload: `parsePrVerifyCheckPayload`
681
+ * This is the *second* parser of that payload, and it does not reimplement
682
+ * the first: `parsePrVerifyCheckPayload`
662
683
  * (`software-factory/src/pr-verify-check-payload.ts`) regex-extracts the fenced
663
- * block, while this one strips fence lines and `fromjson`s what remains. The
664
- * one input that separates them is prose around the fence, which parses there
665
- * and refuses here. Both halves of that are asserted in the test suite rather
666
- * than asserted here in prose — the exact shape the renderer emits is proven,
667
- * and the divergent shape is proven to fail safe. Two parsers of one payload
668
- * can drift; what must not drift silently is which direction they drift in.
684
+ * block up to the next line that starts with a fence and `JSON.parse`s it,
685
+ * while this one strips every such line and `fromjson`s what remains. The
686
+ * writer renders JSON, whose strings hold no raw line feed, between one
687
+ * opening and one closing fence line, so on its text the two read the same
688
+ * JSON, three backticks inside a string included. On other text they can
689
+ * differ in either direction. Prose around the fence parses there and refuses
690
+ * here; the test suite asserts that the exact shape the renderer emits is
691
+ * proven here and that the prose-wrapped shape refuses. Text that parses here
692
+ * and not there is part of the trust boundary below.
669
693
  *
670
- * Ordering is by the proof's own generation, never by when a run reached
671
- * GitHub (#1409). It is the rule `fetchPrVerifyProofBinding` applies
694
+ * Once parsed, a payload is read by the CLI's readability rules (#1510).
695
+ * `bound` requires the binding's
696
+ * `kind`, `schemaVersion` 1, this head, this repository and a resolved verify
697
+ * mode (`resolvedMode`, the CLI's `isResolvedPrVerifyMode`), and the verdict
698
+ * stage refuses a run that is not `bound` as `unreadable`; an `outcome` other
699
+ * than `passed` is never a pass. `binding` drops a transported typed proof
700
+ * (`verificationProof`) when the text it parsed, fence included, is over
701
+ * 60,000 UTF-8 bytes, as `readTransportedVerifyProof` does, so the run is a
702
+ * pass with no typed proof to both readers. On text the writer never emits,
703
+ * this reader can still read a payload differently; see the trust boundary
704
+ * below.
705
+ *
706
+ * A passing generation is ordered by when its verification started, never
707
+ * later than when its run reached GitHub and never by when its verification
708
+ * ended (#1409, #1490). Every other run is ordered by when it reached GitHub
709
+ * (#1506). It is the rule `fetchPrVerifyProofBinding` applies
672
710
  * (`software-factory/src/github-check-runs.ts`), mirrored here because this
673
711
  * reader is jq in generated CI and cannot import it:
674
712
  *
675
- * - A run whose binding is bound to this head and repository and transports a
676
- * typed proof (`verificationProof`) is ordered at the `endedAt` that proof
677
- * records. Such runs with the same binding, status and conclusion are one
678
- * generation posted more than once (`pr:verify` posts, `pr:publish`
679
- * re-posts). So a re-post of an older pass never outranks a newer failure,
680
- * and a post plus its re-post is no tie.
681
- * - Any other run is its own generation at its `started_at`: an unbound one,
682
- * or a bound one with no typed proof (over the text cap, or written before
683
- * proofs travelled). The factory records a proof before it posts it, so
684
- * that is the latest its proof could have been recorded.
685
- * - Such a run carries no generation identity, so it is never grouped. Two
686
- * separate executions with the same command names and verdict post the same
687
- * compact binding, and so does a re-post. When the newest generation is one
688
- * of these and repeats another run's verdict (binding, status and
689
- * conclusion), it could be a re-post of an older verdict or a new one, so it
690
- * is `ambiguous`. Proofs that size are rare; refusing costs a hosted run.
713
+ * - A passing run (`passing`) is completed with conclusion `success`, and its
714
+ * binding is bound to this head and repository with outcome `passed`. When it
715
+ * transports a typed proof (`verificationProof`) whose start is computable
716
+ * (`proofStart`: its `endedAt` minus its `durationMs`), it is ordered at that
717
+ * start, or at its own `started_at` when that is earlier. Such runs with the
718
+ * same binding, status and conclusion are one generation posted more than
719
+ * once (`pr:verify` posts, `pr:publish` re-posts), ordered at the earliest of
720
+ * its members' instants, whatever order GitHub serves them in. A re-post does
721
+ * not move the proof's start, so a re-post of an older pass never outranks a
722
+ * newer failure, and a post plus its re-post is no tie.
723
+ * - Any other run is its own generation at its `started_at`: one still
724
+ * running, a failure or cancellation whether or not it carries a proof, an
725
+ * unbound one, or a pass with no usable typed proof (over the text cap,
726
+ * written before proofs travelled, or with no computable start). A run is
727
+ * created no earlier than its verification began, so `started_at` is an
728
+ * upper bound on that start. Ranking there can only make such a run newer
729
+ * than it is, so it vetoes more, never less.
730
+ * - Neither `endedAt` nor a passing run's `started_at` is its start. The
731
+ * factory posts a `pr-verify` run only once verification has finished, so
732
+ * both say when it ended, and an older pass ordered there would hide a run
733
+ * that started after the pass began: still running, or failed (#1490). A
734
+ * pass's `started_at` only caps its proof's start. A failure's `started_at`
735
+ * is when it was posted, not when it started, and that is deliberate: a
736
+ * veto's instant only has to be no earlier than its start.
737
+ * - Only a pass is ordered by its proof because this reader cannot run the
738
+ * proof schema (#1506). The CLI drops a payload whose proof fails that
739
+ * schema and orders the run at its `started_at`; ordered by its proof's
740
+ * claimed start, a failure carrying a malformed proof ranked under an older
741
+ * pass here, and the gate returned `proven` where the CLI refused. For an
742
+ * honest proof, `started_at` is no earlier than `endedAt`, which is no
743
+ * earlier than the start, so the cap changes nothing for a valid pass.
744
+ * - A pass is reused only when its verification started strictly after every
745
+ * other run at this head was posted (#1506). Its start is its instant: its
746
+ * proof's start, capped as above, or its `started_at` when it is ordered
747
+ * there. Every other run's post is its `started_at`, rounded down to the
748
+ * whole second. Only the pass's own generation is left out: its re-post
749
+ * group, or, for a pass with no typed proof, that run alone
750
+ * (`ownGeneration`). At most one generation can satisfy this, and that one
751
+ * is the newest, so the gate selects the newest generation and, when it is
752
+ * a pass (`passing`), refuses it as `ambiguous` unless every other run was
753
+ * posted before its instant. When the newest generation is not a pass, the
754
+ * verdict is a refusal anyway, so its reason (`failed`, `pending`, ...)
755
+ * stands and the check does not run.
756
+ * - The check exists because any other run may be ordered earlier than its
757
+ * post by a proof this reader cannot validate: a pass that only claims
758
+ * `passed` looks to jq exactly like one the CLI validates. Without it, a
759
+ * successful run whose malformed proof claimed an early start ranked under
760
+ * an older pass, whether that pass was ordered by its proof or at its
761
+ * `started_at`, and the gate reused the older pass where the CLI, which
762
+ * drops that payload and ranks the run at its `started_at`, refused. With
763
+ * it, whether a pass may be reused never consults another run's proof
764
+ * fields, only when each other run was posted, which GitHub records. Those
765
+ * fields can only cause a refusal; they never hide a veto. The pass's own
766
+ * clock is capped at its post, so it can only count against that pass.
767
+ * - Two changes on valid input follow, and both only add refusals, each
768
+ * costing one hosted run in a rare case. A failure carrying a proof is
769
+ * ordered when it was posted instead of when it started, so an older failure
770
+ * that finished after a newer pass began now refuses reuse. Two pass
771
+ * generations whose verifications overlapped now refuse, as does an older
772
+ * pass re-posted after a newer pass started. Passes that ran one after the
773
+ * other, and a pass with its own re-posts, are reused as before.
774
+ * - The hosted gate's trust boundary is a class. This reader does not
775
+ * reimplement the CLI's payload parser. On payloads the factory's writer
776
+ * emits, both readers select the same pass or both refuse: the differential
777
+ * suite, `software-factory/src/pr-verify-reuse-differential.test.ts`,
778
+ * asserts it over 9,653 generated run sets. On App-signed text the writer
779
+ * never emits, the two can differ in either direction. The known instances
780
+ * where this gate reuses and the CLI refuses are: (a) a reused run whose
781
+ * typed proof fails `validatePrVerifyProof`; (b) a newest pass that repeats
782
+ * another run's verdict only once the CLI normalizes that run's payload, say
783
+ * by dropping a key, since the CLI compares normalized payloads and this
784
+ * reader raw ones; (c) a payload in `output.summary` beside a text payload
785
+ * the CLI cannot read, which the CLI reads from the summary and this reader
786
+ * from the text; (d) text that this reader's strip-and-`fromjson` reads and
787
+ * the CLI's fence match or `JSON.parse` refuses, such as a closing fence
788
+ * with no opening one, a doubled opening fence, a fence line inside the
789
+ * JSON, a payload split across two fenced blocks, or a number literal such
790
+ * as `NaN` where the jq in use accepts it (jq 1.8 does). Mirroring any of it
791
+ * would turn this reader into a parser. Only the factory App can post the
792
+ * run, and that identity bounds the whole class (#1511).
793
+ * - A run not ordered by a typed proof carries no generation identity, so it
794
+ * is never grouped. Two separate executions with the same command names and
795
+ * verdict post the same compact binding, and so does a re-post. When the
796
+ * newest generation is one of these and repeats another run's verdict
797
+ * (binding, status and conclusion), it could be a re-post of an older
798
+ * verdict or a new one, so it is `ambiguous`. Proofs that size are rare;
799
+ * refusing costs a hosted run.
691
800
  *
692
801
  * Two different generations that share the newest instant are `ambiguous`
693
802
  * rather than arbitrarily resolved: the gate cannot tell which one describes
694
803
  * the tree, and silently picking either is exactly the kind of coin-flip
695
- * authorization this whole mechanism exists to remove. Instants are
696
- * normalized to whole seconds so `…:00Z` and `…:00.000Z` compare equal; two
697
- * genuine verification generations within the same second at the same head
698
- * is not a real state, and collapsing them errs toward refusing.
804
+ * authorization this whole mechanism exists to remove.
805
+ *
806
+ * Every instant is a whole epoch second, rounded down. A `started_at` keeps
807
+ * its whole second, so `…:00Z` and `…:00.500Z` compare equal. A proof's start
808
+ * is `floor((endedAt - durationMs) / 1000)` with `endedAt` in epoch
809
+ * milliseconds, its fraction read to the millisecond the way `Date.parse`
810
+ * reads it. Rounding down is monotonic, so it can merge two instants into a
811
+ * tie, which is `ambiguous`, but never invert them. It also fails closed: a
812
+ * proof's start never moves later, so it never outranks an unbound run in the
813
+ * same second. The TypeScript reader rounds the same way, so the two agree on
814
+ * every canonical instant. Two genuine verification generations within the
815
+ * same second at the same head is not a real state.
699
816
  *
700
- * The comparison is lexical, which is correct only for fixed-width UTC. A
701
- * `+HH:MM` offset would sort by its literal text rather than by its instant,
702
- * and an absent `started_at` would sort to the bottom. The Checks API returns
703
- * neither today, so this is guarded rather than merely noted: every candidate
704
- * must carry a canonical UTC `started_at`, and one that does not makes the
705
- * newest generation unestablishable, which is `ambiguous`. A proof `endedAt`
706
- * that is not canonical UTC is not a clock, and its run falls back to
707
- * `started_at`. Refusing costs a hosted run; ranking an unorderable set could
708
- * authorize the wrong one.
817
+ * `fromdateiso8601` reads only canonical UTC, and the gate never asks it to
818
+ * read anything else. Every candidate must carry a canonical UTC
819
+ * `started_at`, and one that does not makes the newest generation
820
+ * unestablishable, which is `ambiguous`. A typed proof whose `endedAt` is not
821
+ * canonical UTC, or whose `durationMs` is missing or not a finite non-negative
822
+ * number, records no start, and its run falls back to `started_at` like any
823
+ * other run without a typed proof. Refusing costs a hosted run; ranking an
824
+ * unorderable set could authorize the wrong one.
709
825
  */
710
826
  const GATE_JQ = String.raw`
711
827
  def binding:
712
828
  [ .output.text, .output.summary ]
713
829
  | map(select(type == "string"))
714
- | map(split("\n") | map(select(startswith("${"```"}") | not)) | join("\n"))
715
- | map(. as $body | try ($body | fromjson) catch null)
716
- | map(select(type == "object"))
830
+ | map(. as $text
831
+ | (split("\n") | map(select(startswith("${"```"}") | not)) | join("\n")
832
+ | try fromjson catch null)
833
+ | select(type == "object")
834
+ | if ($text | utf8bytelength) > ${TRANSPORT_CAP_BYTES}
835
+ then del(.verificationProof)
836
+ else .
837
+ end)
717
838
  | .[0];
718
839
 
840
+ def resolvedMode:
841
+ IN(${RESOLVED_VERIFY_MODES.map((mode) => JSON.stringify(mode)).join(", ")});
842
+
719
843
  def startedAt: (.started_at // "") | tostring;
720
844
 
721
845
  def rankable:
@@ -723,17 +847,45 @@ def rankable:
723
847
 
724
848
  def wholeSecond: sub("\\.[0-9]+Z$"; "Z");
725
849
 
850
+ def epochSecond:
851
+ if type == "string" and rankable
852
+ then (try (wholeSecond | fromdateiso8601) catch null)
853
+ else null
854
+ end;
855
+
856
+ def millisecondPart:
857
+ ((capture("\\.(?<digits>[0-9]+)Z$") | .digits) // "") + "000"
858
+ | .[0:3]
859
+ | tonumber;
860
+
726
861
  def bound:
727
862
  (.binding | type) == "object"
728
863
  and .binding.kind == "pr-verify-proof-binding"
729
864
  and .binding.schemaVersion == 1
730
865
  and .binding.headSha == $sha
731
- and .binding.repository == $repository;
866
+ and .binding.repository == $repository
867
+ and (.binding.mode | resolvedMode);
868
+
869
+ def passing:
870
+ .status == "completed"
871
+ and .conclusion == "success"
872
+ and bound
873
+ and .binding.outcome == "passed";
732
874
 
733
- def recordedAt:
734
- if bound then
735
- (.binding.verificationProof | if type == "object" then .endedAt else null end)
736
- | if type == "string" and rankable then . else null end
875
+ def proofStart:
876
+ if bound and (.binding.verificationProof | type) == "object" then
877
+ .binding.verificationProof as $proof
878
+ | ($proof.endedAt | epochSecond) as $ended
879
+ | $proof.durationMs as $duration
880
+ | if $ended != null
881
+ and ($duration | type) == "number"
882
+ and ($duration | isinfinite | not)
883
+ and $duration >= 0
884
+ then ($ended * 1000 + ($proof.endedAt | millisecondPart) - $duration)
885
+ / 1000
886
+ | floor
887
+ else null
888
+ end
737
889
  else null
738
890
  end;
739
891
 
@@ -743,6 +895,12 @@ def sameVerdict($other):
743
895
  and .conclusion == $other.conclusion
744
896
  and .binding == $other.binding;
745
897
 
898
+ def ownGeneration($generation):
899
+ if $generation.recorded
900
+ then .recorded and sameVerdict($generation)
901
+ else .index == $generation.index
902
+ end;
903
+
746
904
  def nonemptyString:
747
905
  type == "string" and length > 0;
748
906
 
@@ -767,15 +925,19 @@ def releaseAuthorized($release; $commands; $stamp):
767
925
  url: ((.html_url // "") | tostring),
768
926
  binding: binding }
769
927
  ] as $runs
770
- | ($runs | map(.started | rankable) | all) as $orderable
771
- | [ $runs[]
772
- | recordedAt as $recorded
773
- | . + { at: (($recorded // .started) | wholeSecond), recorded: ($recorded != null) }
928
+ | ($runs | map(.started | epochSecond != null) | all) as $orderable
929
+ | [ range(0; $runs | length) as $index
930
+ | $runs[$index]
931
+ | (.started | epochSecond) as $posted
932
+ | (if passing then proofStart else null end) as $start
933
+ | . + { at: ([$start, $posted] | map(select(. != null)) | min),
934
+ index: $index,
935
+ recorded: ($start != null) }
774
936
  ] as $observed
775
937
  | ( [ $observed[] | select(.recorded | not) ]
776
938
  + ( [ $observed[] | select(.recorded) ]
777
939
  | group_by([.status, .conclusion, .binding])
778
- | map(.[0]) ) ) as $generations
940
+ | map(.[0] + { at: (map(.at) | min) }) ) ) as $generations
779
941
  | ($generations | map(.at) | max) as $newest
780
942
  | [ $generations[] | select(.at == $newest) ] as $generation
781
943
  | (if ($runs | length) == 0 then ["none", "", "", ""]
@@ -785,6 +947,11 @@ def releaseAuthorized($release; $commands; $stamp):
785
947
  | ($newestRun.recorded | not)
786
948
  and ([ $runs[] | select(sameVerdict($newestRun)) ] | length) > 1)
787
949
  then ["ambiguous", "", "", ""]
950
+ elif ($generation[0] as $newestRun
951
+ | ($newestRun | passing)
952
+ and any($observed[] | select(ownGeneration($newestRun) | not);
953
+ (.started | epochSecond) >= $newestRun.at))
954
+ then ["ambiguous", "", "", ""]
788
955
  else
789
956
  $generation[0] as $run
790
957
  | (if ($run.binding | type) == "object" then $run.binding else {} end) as $proof
@@ -834,11 +1001,7 @@ def releaseAuthorized($release; $commands; $stamp):
834
1001
  | ($required - $executed - $released) as $missing
835
1002
  | (if $run.status != "completed" then "pending"
836
1003
  elif $run.conclusion != "success" then "failed"
837
- elif (($run.binding | type) != "object")
838
- or ($proof.kind != "pr-verify-proof-binding")
839
- or ($proof.schemaVersion != 1)
840
- or ($proof.headSha != $sha)
841
- or ($proof.repository != $repository) then "unreadable"
1004
+ elif ($run | bound | not) then "unreadable"
842
1005
  elif $proof.outcome != "passed" then "failed"
843
1006
  elif ($required | length) == 0 then "incomplete"
844
1007
  elif ($releasesValid | not) then "incomplete"
@@ -1039,7 +1202,7 @@ case "$reason" in
1039
1202
  ambiguous)
1040
1203
  say 'Ran the full suite. The newest factory proof for this commit is ambiguous.'
1041
1204
  say ''
1042
- say "Two different \`$CHECK_NAME\` proofs at head \`$HEAD_SHA\` share the newest generation, the newest repeats an earlier run's verdict with no proof time to tell them apart, or a run's start time cannot be ranked, so there is no single newest generation to trust. The gate refuses rather than picking one."
1205
+ say "Two different \`$CHECK_NAME\` proofs at head \`$HEAD_SHA\` share the newest generation, the newest repeats an earlier run's verdict with no proof time to tell them apart, a run's start time cannot be ranked, or the newest pass started before another run at this head was posted, so there is no single newest generation to trust. The gate refuses rather than picking one."
1043
1206
  say ''
1044
1207
  say '${CORRECTIVE_LINE}'
1045
1208
  ;;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@patronage/factory-ci",
3
- "version": "1.0.0-alpha.41",
3
+ "version": "1.0.0-alpha.42",
4
4
  "description": "Deep CI and deploy building blocks for Patronage factory projects: workflow source artifacts, hosted diff classification, Alchemy entry execution, and disposable-stage semantics",
5
5
  "keywords": [
6
6
  "alchemy",
@@ -148,7 +148,9 @@ export const FACTORY_PROOF_GATE_IF = "github.event_name == 'pull_request'";
148
148
  * cover every required command
149
149
  * - `ambiguous` two different generations share the newest instant, the
150
150
  * newest repeats another run's verdict with no proof time to
151
- * tell them apart, or a run's start time cannot be ranked
151
+ * tell them apart, a run's start time cannot be ranked, or the
152
+ * newest pass started before another run at this head was
153
+ * posted
152
154
  * - `error` the gate could not reach a decision (fail open)
153
155
  *
154
156
  * There is no `reduced`: mode stopped being an authorization condition in
@@ -205,6 +207,22 @@ const COMMAND_IDENTITY_MAX_LENGTH = 120;
205
207
  */
206
208
  const TRUSTED_IMPACT_STAMP_VERSION = 4;
207
209
 
210
+ /**
211
+ * The verify modes the CLI reads a payload under (`isResolvedPrVerifyMode` in
212
+ * `software-factory/src/pr-verify-mode.ts`). factory-ci cannot import it, so
213
+ * the list is repeated here, and the software-factory differential suite pins
214
+ * the two lists together (#1510).
215
+ */
216
+ const RESOLVED_VERIFY_MODES = ["docs-only", "trivial", "full"] as const;
217
+
218
+ /**
219
+ * The check-text size over which the CLI drops a transported typed proof
220
+ * (`readTransportedVerifyProof` in
221
+ * `software-factory/src/pr-verify-check-payload.ts`), in UTF-8 bytes of the
222
+ * raw text it parsed, fence included (#1510).
223
+ */
224
+ const TRANSPORT_CAP_BYTES = 60_000;
225
+
208
226
  const isProofReuseCommand = (value: unknown): value is ProofReuseCommand => {
209
227
  if (!(value && typeof value === "object")) {
210
228
  return false;
@@ -314,64 +332,168 @@ export const resolveProofReuseCommands = (
314
332
  * document. Anything unparseable degrades to `null`, and therefore to a
315
333
  * refusal.
316
334
  *
317
- * This is the *second* parser of that payload: `parsePrVerifyCheckPayload`
335
+ * This is the *second* parser of that payload, and it does not reimplement
336
+ * the first: `parsePrVerifyCheckPayload`
318
337
  * (`software-factory/src/pr-verify-check-payload.ts`) regex-extracts the fenced
319
- * block, while this one strips fence lines and `fromjson`s what remains. The
320
- * one input that separates them is prose around the fence, which parses there
321
- * and refuses here. Both halves of that are asserted in the test suite rather
322
- * than asserted here in prose — the exact shape the renderer emits is proven,
323
- * and the divergent shape is proven to fail safe. Two parsers of one payload
324
- * can drift; what must not drift silently is which direction they drift in.
338
+ * block up to the next line that starts with a fence and `JSON.parse`s it,
339
+ * while this one strips every such line and `fromjson`s what remains. The
340
+ * writer renders JSON, whose strings hold no raw line feed, between one
341
+ * opening and one closing fence line, so on its text the two read the same
342
+ * JSON, three backticks inside a string included. On other text they can
343
+ * differ in either direction. Prose around the fence parses there and refuses
344
+ * here; the test suite asserts that the exact shape the renderer emits is
345
+ * proven here and that the prose-wrapped shape refuses. Text that parses here
346
+ * and not there is part of the trust boundary below.
347
+ *
348
+ * Once parsed, a payload is read by the CLI's readability rules (#1510).
349
+ * `bound` requires the binding's
350
+ * `kind`, `schemaVersion` 1, this head, this repository and a resolved verify
351
+ * mode (`resolvedMode`, the CLI's `isResolvedPrVerifyMode`), and the verdict
352
+ * stage refuses a run that is not `bound` as `unreadable`; an `outcome` other
353
+ * than `passed` is never a pass. `binding` drops a transported typed proof
354
+ * (`verificationProof`) when the text it parsed, fence included, is over
355
+ * 60,000 UTF-8 bytes, as `readTransportedVerifyProof` does, so the run is a
356
+ * pass with no typed proof to both readers. On text the writer never emits,
357
+ * this reader can still read a payload differently; see the trust boundary
358
+ * below.
325
359
  *
326
- * Ordering is by the proof's own generation, never by when a run reached
327
- * GitHub (#1409). It is the rule `fetchPrVerifyProofBinding` applies
360
+ * A passing generation is ordered by when its verification started, never
361
+ * later than when its run reached GitHub and never by when its verification
362
+ * ended (#1409, #1490). Every other run is ordered by when it reached GitHub
363
+ * (#1506). It is the rule `fetchPrVerifyProofBinding` applies
328
364
  * (`software-factory/src/github-check-runs.ts`), mirrored here because this
329
365
  * reader is jq in generated CI and cannot import it:
330
366
  *
331
- * - A run whose binding is bound to this head and repository and transports a
332
- * typed proof (`verificationProof`) is ordered at the `endedAt` that proof
333
- * records. Such runs with the same binding, status and conclusion are one
334
- * generation posted more than once (`pr:verify` posts, `pr:publish`
335
- * re-posts). So a re-post of an older pass never outranks a newer failure,
336
- * and a post plus its re-post is no tie.
337
- * - Any other run is its own generation at its `started_at`: an unbound one,
338
- * or a bound one with no typed proof (over the text cap, or written before
339
- * proofs travelled). The factory records a proof before it posts it, so
340
- * that is the latest its proof could have been recorded.
341
- * - Such a run carries no generation identity, so it is never grouped. Two
342
- * separate executions with the same command names and verdict post the same
343
- * compact binding, and so does a re-post. When the newest generation is one
344
- * of these and repeats another run's verdict (binding, status and
345
- * conclusion), it could be a re-post of an older verdict or a new one, so it
346
- * is `ambiguous`. Proofs that size are rare; refusing costs a hosted run.
367
+ * - A passing run (`passing`) is completed with conclusion `success`, and its
368
+ * binding is bound to this head and repository with outcome `passed`. When it
369
+ * transports a typed proof (`verificationProof`) whose start is computable
370
+ * (`proofStart`: its `endedAt` minus its `durationMs`), it is ordered at that
371
+ * start, or at its own `started_at` when that is earlier. Such runs with the
372
+ * same binding, status and conclusion are one generation posted more than
373
+ * once (`pr:verify` posts, `pr:publish` re-posts), ordered at the earliest of
374
+ * its members' instants, whatever order GitHub serves them in. A re-post does
375
+ * not move the proof's start, so a re-post of an older pass never outranks a
376
+ * newer failure, and a post plus its re-post is no tie.
377
+ * - Any other run is its own generation at its `started_at`: one still
378
+ * running, a failure or cancellation whether or not it carries a proof, an
379
+ * unbound one, or a pass with no usable typed proof (over the text cap,
380
+ * written before proofs travelled, or with no computable start). A run is
381
+ * created no earlier than its verification began, so `started_at` is an
382
+ * upper bound on that start. Ranking there can only make such a run newer
383
+ * than it is, so it vetoes more, never less.
384
+ * - Neither `endedAt` nor a passing run's `started_at` is its start. The
385
+ * factory posts a `pr-verify` run only once verification has finished, so
386
+ * both say when it ended, and an older pass ordered there would hide a run
387
+ * that started after the pass began: still running, or failed (#1490). A
388
+ * pass's `started_at` only caps its proof's start. A failure's `started_at`
389
+ * is when it was posted, not when it started, and that is deliberate: a
390
+ * veto's instant only has to be no earlier than its start.
391
+ * - Only a pass is ordered by its proof because this reader cannot run the
392
+ * proof schema (#1506). The CLI drops a payload whose proof fails that
393
+ * schema and orders the run at its `started_at`; ordered by its proof's
394
+ * claimed start, a failure carrying a malformed proof ranked under an older
395
+ * pass here, and the gate returned `proven` where the CLI refused. For an
396
+ * honest proof, `started_at` is no earlier than `endedAt`, which is no
397
+ * earlier than the start, so the cap changes nothing for a valid pass.
398
+ * - A pass is reused only when its verification started strictly after every
399
+ * other run at this head was posted (#1506). Its start is its instant: its
400
+ * proof's start, capped as above, or its `started_at` when it is ordered
401
+ * there. Every other run's post is its `started_at`, rounded down to the
402
+ * whole second. Only the pass's own generation is left out: its re-post
403
+ * group, or, for a pass with no typed proof, that run alone
404
+ * (`ownGeneration`). At most one generation can satisfy this, and that one
405
+ * is the newest, so the gate selects the newest generation and, when it is
406
+ * a pass (`passing`), refuses it as `ambiguous` unless every other run was
407
+ * posted before its instant. When the newest generation is not a pass, the
408
+ * verdict is a refusal anyway, so its reason (`failed`, `pending`, ...)
409
+ * stands and the check does not run.
410
+ * - The check exists because any other run may be ordered earlier than its
411
+ * post by a proof this reader cannot validate: a pass that only claims
412
+ * `passed` looks to jq exactly like one the CLI validates. Without it, a
413
+ * successful run whose malformed proof claimed an early start ranked under
414
+ * an older pass, whether that pass was ordered by its proof or at its
415
+ * `started_at`, and the gate reused the older pass where the CLI, which
416
+ * drops that payload and ranks the run at its `started_at`, refused. With
417
+ * it, whether a pass may be reused never consults another run's proof
418
+ * fields, only when each other run was posted, which GitHub records. Those
419
+ * fields can only cause a refusal; they never hide a veto. The pass's own
420
+ * clock is capped at its post, so it can only count against that pass.
421
+ * - Two changes on valid input follow, and both only add refusals, each
422
+ * costing one hosted run in a rare case. A failure carrying a proof is
423
+ * ordered when it was posted instead of when it started, so an older failure
424
+ * that finished after a newer pass began now refuses reuse. Two pass
425
+ * generations whose verifications overlapped now refuse, as does an older
426
+ * pass re-posted after a newer pass started. Passes that ran one after the
427
+ * other, and a pass with its own re-posts, are reused as before.
428
+ * - The hosted gate's trust boundary is a class. This reader does not
429
+ * reimplement the CLI's payload parser. On payloads the factory's writer
430
+ * emits, both readers select the same pass or both refuse: the differential
431
+ * suite, `software-factory/src/pr-verify-reuse-differential.test.ts`,
432
+ * asserts it over 9,653 generated run sets. On App-signed text the writer
433
+ * never emits, the two can differ in either direction. The known instances
434
+ * where this gate reuses and the CLI refuses are: (a) a reused run whose
435
+ * typed proof fails `validatePrVerifyProof`; (b) a newest pass that repeats
436
+ * another run's verdict only once the CLI normalizes that run's payload, say
437
+ * by dropping a key, since the CLI compares normalized payloads and this
438
+ * reader raw ones; (c) a payload in `output.summary` beside a text payload
439
+ * the CLI cannot read, which the CLI reads from the summary and this reader
440
+ * from the text; (d) text that this reader's strip-and-`fromjson` reads and
441
+ * the CLI's fence match or `JSON.parse` refuses, such as a closing fence
442
+ * with no opening one, a doubled opening fence, a fence line inside the
443
+ * JSON, a payload split across two fenced blocks, or a number literal such
444
+ * as `NaN` where the jq in use accepts it (jq 1.8 does). Mirroring any of it
445
+ * would turn this reader into a parser. Only the factory App can post the
446
+ * run, and that identity bounds the whole class (#1511).
447
+ * - A run not ordered by a typed proof carries no generation identity, so it
448
+ * is never grouped. Two separate executions with the same command names and
449
+ * verdict post the same compact binding, and so does a re-post. When the
450
+ * newest generation is one of these and repeats another run's verdict
451
+ * (binding, status and conclusion), it could be a re-post of an older
452
+ * verdict or a new one, so it is `ambiguous`. Proofs that size are rare;
453
+ * refusing costs a hosted run.
347
454
  *
348
455
  * Two different generations that share the newest instant are `ambiguous`
349
456
  * rather than arbitrarily resolved: the gate cannot tell which one describes
350
457
  * the tree, and silently picking either is exactly the kind of coin-flip
351
- * authorization this whole mechanism exists to remove. Instants are
352
- * normalized to whole seconds so `…:00Z` and `…:00.000Z` compare equal; two
353
- * genuine verification generations within the same second at the same head
354
- * is not a real state, and collapsing them errs toward refusing.
458
+ * authorization this whole mechanism exists to remove.
355
459
  *
356
- * The comparison is lexical, which is correct only for fixed-width UTC. A
357
- * `+HH:MM` offset would sort by its literal text rather than by its instant,
358
- * and an absent `started_at` would sort to the bottom. The Checks API returns
359
- * neither today, so this is guarded rather than merely noted: every candidate
360
- * must carry a canonical UTC `started_at`, and one that does not makes the
361
- * newest generation unestablishable, which is `ambiguous`. A proof `endedAt`
362
- * that is not canonical UTC is not a clock, and its run falls back to
363
- * `started_at`. Refusing costs a hosted run; ranking an unorderable set could
364
- * authorize the wrong one.
460
+ * Every instant is a whole epoch second, rounded down. A `started_at` keeps
461
+ * its whole second, so `…:00Z` and `…:00.500Z` compare equal. A proof's start
462
+ * is `floor((endedAt - durationMs) / 1000)` with `endedAt` in epoch
463
+ * milliseconds, its fraction read to the millisecond the way `Date.parse`
464
+ * reads it. Rounding down is monotonic, so it can merge two instants into a
465
+ * tie, which is `ambiguous`, but never invert them. It also fails closed: a
466
+ * proof's start never moves later, so it never outranks an unbound run in the
467
+ * same second. The TypeScript reader rounds the same way, so the two agree on
468
+ * every canonical instant. Two genuine verification generations within the
469
+ * same second at the same head is not a real state.
470
+ *
471
+ * `fromdateiso8601` reads only canonical UTC, and the gate never asks it to
472
+ * read anything else. Every candidate must carry a canonical UTC
473
+ * `started_at`, and one that does not makes the newest generation
474
+ * unestablishable, which is `ambiguous`. A typed proof whose `endedAt` is not
475
+ * canonical UTC, or whose `durationMs` is missing or not a finite non-negative
476
+ * number, records no start, and its run falls back to `started_at` like any
477
+ * other run without a typed proof. Refusing costs a hosted run; ranking an
478
+ * unorderable set could authorize the wrong one.
365
479
  */
366
480
  const GATE_JQ = String.raw`
367
481
  def binding:
368
482
  [ .output.text, .output.summary ]
369
483
  | map(select(type == "string"))
370
- | map(split("\n") | map(select(startswith("${"```"}") | not)) | join("\n"))
371
- | map(. as $body | try ($body | fromjson) catch null)
372
- | map(select(type == "object"))
484
+ | map(. as $text
485
+ | (split("\n") | map(select(startswith("${"```"}") | not)) | join("\n")
486
+ | try fromjson catch null)
487
+ | select(type == "object")
488
+ | if ($text | utf8bytelength) > ${TRANSPORT_CAP_BYTES}
489
+ then del(.verificationProof)
490
+ else .
491
+ end)
373
492
  | .[0];
374
493
 
494
+ def resolvedMode:
495
+ IN(${RESOLVED_VERIFY_MODES.map((mode) => JSON.stringify(mode)).join(", ")});
496
+
375
497
  def startedAt: (.started_at // "") | tostring;
376
498
 
377
499
  def rankable:
@@ -379,17 +501,45 @@ def rankable:
379
501
 
380
502
  def wholeSecond: sub("\\.[0-9]+Z$"; "Z");
381
503
 
504
+ def epochSecond:
505
+ if type == "string" and rankable
506
+ then (try (wholeSecond | fromdateiso8601) catch null)
507
+ else null
508
+ end;
509
+
510
+ def millisecondPart:
511
+ ((capture("\\.(?<digits>[0-9]+)Z$") | .digits) // "") + "000"
512
+ | .[0:3]
513
+ | tonumber;
514
+
382
515
  def bound:
383
516
  (.binding | type) == "object"
384
517
  and .binding.kind == "pr-verify-proof-binding"
385
518
  and .binding.schemaVersion == 1
386
519
  and .binding.headSha == $sha
387
- and .binding.repository == $repository;
520
+ and .binding.repository == $repository
521
+ and (.binding.mode | resolvedMode);
522
+
523
+ def passing:
524
+ .status == "completed"
525
+ and .conclusion == "success"
526
+ and bound
527
+ and .binding.outcome == "passed";
388
528
 
389
- def recordedAt:
390
- if bound then
391
- (.binding.verificationProof | if type == "object" then .endedAt else null end)
392
- | if type == "string" and rankable then . else null end
529
+ def proofStart:
530
+ if bound and (.binding.verificationProof | type) == "object" then
531
+ .binding.verificationProof as $proof
532
+ | ($proof.endedAt | epochSecond) as $ended
533
+ | $proof.durationMs as $duration
534
+ | if $ended != null
535
+ and ($duration | type) == "number"
536
+ and ($duration | isinfinite | not)
537
+ and $duration >= 0
538
+ then ($ended * 1000 + ($proof.endedAt | millisecondPart) - $duration)
539
+ / 1000
540
+ | floor
541
+ else null
542
+ end
393
543
  else null
394
544
  end;
395
545
 
@@ -399,6 +549,12 @@ def sameVerdict($other):
399
549
  and .conclusion == $other.conclusion
400
550
  and .binding == $other.binding;
401
551
 
552
+ def ownGeneration($generation):
553
+ if $generation.recorded
554
+ then .recorded and sameVerdict($generation)
555
+ else .index == $generation.index
556
+ end;
557
+
402
558
  def nonemptyString:
403
559
  type == "string" and length > 0;
404
560
 
@@ -423,15 +579,19 @@ def releaseAuthorized($release; $commands; $stamp):
423
579
  url: ((.html_url // "") | tostring),
424
580
  binding: binding }
425
581
  ] as $runs
426
- | ($runs | map(.started | rankable) | all) as $orderable
427
- | [ $runs[]
428
- | recordedAt as $recorded
429
- | . + { at: (($recorded // .started) | wholeSecond), recorded: ($recorded != null) }
582
+ | ($runs | map(.started | epochSecond != null) | all) as $orderable
583
+ | [ range(0; $runs | length) as $index
584
+ | $runs[$index]
585
+ | (.started | epochSecond) as $posted
586
+ | (if passing then proofStart else null end) as $start
587
+ | . + { at: ([$start, $posted] | map(select(. != null)) | min),
588
+ index: $index,
589
+ recorded: ($start != null) }
430
590
  ] as $observed
431
591
  | ( [ $observed[] | select(.recorded | not) ]
432
592
  + ( [ $observed[] | select(.recorded) ]
433
593
  | group_by([.status, .conclusion, .binding])
434
- | map(.[0]) ) ) as $generations
594
+ | map(.[0] + { at: (map(.at) | min) }) ) ) as $generations
435
595
  | ($generations | map(.at) | max) as $newest
436
596
  | [ $generations[] | select(.at == $newest) ] as $generation
437
597
  | (if ($runs | length) == 0 then ["none", "", "", ""]
@@ -441,6 +601,11 @@ def releaseAuthorized($release; $commands; $stamp):
441
601
  | ($newestRun.recorded | not)
442
602
  and ([ $runs[] | select(sameVerdict($newestRun)) ] | length) > 1)
443
603
  then ["ambiguous", "", "", ""]
604
+ elif ($generation[0] as $newestRun
605
+ | ($newestRun | passing)
606
+ and any($observed[] | select(ownGeneration($newestRun) | not);
607
+ (.started | epochSecond) >= $newestRun.at))
608
+ then ["ambiguous", "", "", ""]
444
609
  else
445
610
  $generation[0] as $run
446
611
  | (if ($run.binding | type) == "object" then $run.binding else {} end) as $proof
@@ -490,11 +655,7 @@ def releaseAuthorized($release; $commands; $stamp):
490
655
  | ($required - $executed - $released) as $missing
491
656
  | (if $run.status != "completed" then "pending"
492
657
  elif $run.conclusion != "success" then "failed"
493
- elif (($run.binding | type) != "object")
494
- or ($proof.kind != "pr-verify-proof-binding")
495
- or ($proof.schemaVersion != 1)
496
- or ($proof.headSha != $sha)
497
- or ($proof.repository != $repository) then "unreadable"
658
+ elif ($run | bound | not) then "unreadable"
498
659
  elif $proof.outcome != "passed" then "failed"
499
660
  elif ($required | length) == 0 then "incomplete"
500
661
  elif ($releasesValid | not) then "incomplete"
@@ -704,7 +865,7 @@ case "$reason" in
704
865
  ambiguous)
705
866
  say 'Ran the full suite. The newest factory proof for this commit is ambiguous.'
706
867
  say ''
707
- say "Two different \`$CHECK_NAME\` proofs at head \`$HEAD_SHA\` share the newest generation, the newest repeats an earlier run's verdict with no proof time to tell them apart, or a run's start time cannot be ranked, so there is no single newest generation to trust. The gate refuses rather than picking one."
868
+ say "Two different \`$CHECK_NAME\` proofs at head \`$HEAD_SHA\` share the newest generation, the newest repeats an earlier run's verdict with no proof time to tell them apart, a run's start time cannot be ranked, or the newest pass started before another run at this head was posted, so there is no single newest generation to trust. The gate refuses rather than picking one."
708
869
  say ''
709
870
  say '${CORRECTIVE_LINE}'
710
871
  ;;