bitfab 0.65.5 → 0.65.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a9c94aca223ea9794e110a16dae0e3e4f23ef45a2cf345c6bbaf396fc6b10b5f
4
- data.tar.gz: 9c99a4266ec0109d155caeea5e1bb7792fb38c5e9d510e3552a2df60d192160a
3
+ metadata.gz: 84d81613e344f01a011f28ac437476d22e73867eb7ab2df04579075730b1c73b
4
+ data.tar.gz: d3bb4d9f79d587dca9ee2ef2e1ee36a41fbeb59da7cb69960bf9fdbe75e8d663
5
5
  SHA512:
6
- metadata.gz: fa01c2d028e06cc338df64527d011df63e0a01dcec25559b4816523c951aed2b55ab0034075c7c09babec22d927bb4e5f0654456e82fd76e5d41815d0851fa30
7
- data.tar.gz: 6f865c897e2ac1975c1b2a64cfbdf5c933f24356db7294e97a8ce11712cbf1937b942701c5237cd62dd79a68ae422dfaf46ed6eba3545e6c8522bdf0070831af
6
+ metadata.gz: 18ceec3ef05433309efff835c6bcf050789439e9d0424f9919972a70dd5d5b358eb07641fbed220d36b52e92f503d719dee044ba18003fb1371d77d0dba2e8e3
7
+ data.tar.gz: a6743709c41b9b69597e43e5c1336af5038bf9e5c56d5e3b247051e12fb4527d9732d79bb98e3587f90aedfbc59f7e23ebeae6c0d02e1bfad712f37d6789dd00
@@ -13,7 +13,11 @@ module Bitfab
13
13
  end
14
14
 
15
15
  # Each returned assertion carries "category_id" and, with the same value,
16
- # the deprecated "category_assertion_id".
16
+ # the deprecated "category_assertion_id". It also carries "rewrite", the
17
+ # latest check of its text (the same hash a save returns), or nil when no
18
+ # check ever ran on it. Read a "pending" check from a save back here once
19
+ # it finishes, and save each of its "separateAssertions" as its own
20
+ # assertion.
17
21
  def get_assertions(trace_id)
18
22
  response = @http_client.get(assertions_path(trace_id))
19
23
  return response unless response.is_a?(Hash) && response["assertions"].is_a?(Array)
@@ -24,17 +28,41 @@ module Bitfab
24
28
  # Assertion hashes take "category_id": omit it to keep the assignment, or
25
29
  # pass nil to clear it. The deprecated "category_assertion_id" is still
26
30
  # accepted and sent as "category_id"; "category_id" wins when both are set.
27
- def save_assertions(trace_id, assertions, source: "agent")
28
- save_assertions_all([{"traceId" => trace_id, "assertions" => assertions}], source:)
31
+ #
32
+ # By default, after the save, Bitfab checks each assertion's text against its
33
+ # trace. When the text bundles two claims, adds a condition, points at a
34
+ # value instead of stating it, names an id from the recorded run, or cannot
35
+ # be measured, Bitfab rewrites it and saves the rewritten text in place of
36
+ # the text you sent. This method returns as soon as the text is saved: each
37
+ # checked assertion comes back with "status" "pending", and Bitfab rewrites
38
+ # it in place in the background, usually within a minute. Read the result
39
+ # later with {#get_assertions}, whose assertions carry the same "rewrite"
40
+ # hash. Pass skip_guided_rewrite: true to save the text exactly as sent,
41
+ # with no check.
42
+ #
43
+ # Each returned assertion carries a "rewrite" hash: "status" ("pending",
44
+ # "rewritten", "unchanged", "not_checked", or "check_failed"),
45
+ # "sentAssertion", "changes", "separateAssertions", "unresolved", and
46
+ # "error". A check not finished 15 minutes after the save reads as
47
+ # "check_failed", with the text as sent. Save each of
48
+ # "separateAssertions" as its own assertion, since the saved one keeps
49
+ # only one claim.
50
+ def save_assertions(trace_id, assertions, source: "agent", skip_guided_rewrite: false)
51
+ save_assertions_all([{"traceId" => trace_id, "assertions" => assertions}], source:, skip_guided_rewrite:)
29
52
  end
30
53
 
31
- # Same assertion hash keys as {#save_assertions}, for several traces at once.
32
- def save_assertions_all(updates, source: "agent")
54
+ # Same assertion hash keys, rewrite, and returned "rewrite" hash as
55
+ # {#save_assertions}, for several traces at once.
56
+ def save_assertions_all(updates, source: "agent", skip_guided_rewrite: false)
33
57
  return [] if updates.empty?
34
58
 
35
59
  saved = @http_client.request(
36
60
  "/api/sdk/traces/assertions",
37
- {"updates" => updates.map { |update| with_new_category_key(update) }, "source" => source}
61
+ {
62
+ "updates" => updates.map { |update| with_new_category_key(update) },
63
+ "source" => source,
64
+ "guidedRewrite" => !skip_guided_rewrite
65
+ }
38
66
  )["assertions"]
39
67
  saved.is_a?(Array) ? saved.map { |assertion| with_both_category_keys(assertion) } : saved
40
68
  end
@@ -154,6 +154,7 @@ CODE_CHANGE_PATH_ENV = "BITFAB_CODE_CHANGE_PATH"
154
154
  CODE_CHANGE_MAX_FILES = 60
155
155
  CODE_CHANGE_MAX_FILE_BYTES = 500_000
156
156
  CODE_CHANGE_MAX_TOTAL_BYTES = 2_000_000
157
+ BRANCH_WALK_LIMIT = 1000
157
158
  CHECK_ARGUMENTS_SECONDS = 300
158
159
  CHECK_ARGUMENTS_FLAG = "--check-arguments"
159
160
  REPLAY_ARGUMENTS_FLAG = "--replay-arguments"
@@ -933,6 +934,98 @@ def current_branch(root):
933
934
  return None
934
935
 
935
936
 
937
+ def quiet_git(root, *args):
938
+ try:
939
+ return git(root, *args)
940
+ except CommandError:
941
+ return ""
942
+
943
+
944
+ def default_branch(root):
945
+ origin_head = quiet_git(root, "rev-parse", "--abbrev-ref", "origin/HEAD")
946
+ candidates = [
947
+ origin_head if origin_head and origin_head != "origin/HEAD" else None,
948
+ "origin/main",
949
+ "origin/master",
950
+ "main",
951
+ "master",
952
+ ]
953
+ return next(
954
+ (
955
+ ref
956
+ for ref in dict.fromkeys(filter(None, candidates))
957
+ if quiet_git(root, "rev-parse", "--verify", "-q", f"{ref}^{{commit}}")
958
+ ),
959
+ None,
960
+ )
961
+
962
+
963
+ def current_branch_refs(root):
964
+ branch = current_branch(root)
965
+ if not branch:
966
+ return set()
967
+ return {f"refs/heads/{branch}"} | {
968
+ f"refs/remotes/{remote}/{branch}"
969
+ for remote in quiet_git(root, "remote").split()
970
+ }
971
+
972
+
973
+ def own_branch_refs(root):
974
+ upstream = quiet_git(root, "rev-parse", "--symbolic-full-name", "@{upstream}")
975
+ own = current_branch_refs(root)
976
+ if upstream:
977
+ own.add(upstream)
978
+ return own
979
+
980
+
981
+ def on_default_branch(root, default):
982
+ full_name = quiet_git(root, "rev-parse", "--symbolic-full-name", default)
983
+ return bool(full_name) and full_name in current_branch_refs(root)
984
+
985
+
986
+ def branch_tips(root):
987
+ own = own_branch_refs(root)
988
+ listing = quiet_git(
989
+ root,
990
+ "for-each-ref",
991
+ "--format=%(objectname) %(refname)",
992
+ "refs/heads",
993
+ "refs/remotes",
994
+ )
995
+ tips = {}
996
+ for line in listing.splitlines():
997
+ sha, _, ref = line.partition(" ")
998
+ if ref and ref not in own and not ref.endswith("/HEAD"):
999
+ tips.setdefault(
1000
+ sha, ref.removeprefix("refs/heads/").removeprefix("refs/remotes/")
1001
+ )
1002
+ return tips
1003
+
1004
+
1005
+ def branch_point(root):
1006
+ head = quiet_git(root, "rev-parse", "HEAD")
1007
+ if not head:
1008
+ return None
1009
+ default = default_branch(root)
1010
+ if default and on_default_branch(root, default):
1011
+ return None
1012
+ default_base = quiet_git(root, "merge-base", "HEAD", default) if default else ""
1013
+ walk = ["rev-list", "--first-parent", f"--max-count={BRANCH_WALK_LIMIT}", "HEAD"]
1014
+ if default_base:
1015
+ walk.append(f"^{default_base}")
1016
+ tips = branch_tips(root)
1017
+ for sha in quiet_git(root, *walk).split()[1:]:
1018
+ if sha in tips:
1019
+ return {"sha": sha, "ref": tips[sha]}
1020
+ if default_base and default_base != head:
1021
+ return {"sha": default_base, "ref": default}
1022
+ return None
1023
+
1024
+
1025
+ def replay_artifact(path):
1026
+ return path == ".bitfab" or path.startswith(".bitfab/")
1027
+
1028
+
936
1029
  def sensitive(path):
937
1030
  parts = Path(path).parts
938
1031
  name = Path(path).name.lower()
@@ -1033,12 +1126,20 @@ def snapshot(
1033
1126
  }
1034
1127
  if preview:
1035
1128
  return summary
1129
+ code_change_base = request.get("codeChangeBase") or {}
1130
+ extra_parent = (
1131
+ ["-p", code_change_base["sha"]]
1132
+ if not any(not replay_artifact(path) for _, path in changes)
1133
+ and SHA.fullmatch(str(code_change_base.get("sha", "")))
1134
+ else []
1135
+ )
1036
1136
  sha = git(
1037
1137
  root,
1038
1138
  "commit-tree",
1039
1139
  tree,
1040
1140
  "-p",
1041
1141
  head,
1142
+ *extra_parent,
1042
1143
  input=f"{snapshot_subject(request['id'], on_push)}\n\n{json.dumps(request)}\n",
1043
1144
  )
1044
1145
  return {**summary, "sha": sha}
@@ -1360,6 +1461,9 @@ def submit(root, repo, workflow, parsed, path):
1360
1461
  branch = current_branch(root)
1361
1462
  if branch:
1362
1463
  request["branch"] = branch
1464
+ code_change_base = branch_point(root)
1465
+ if code_change_base:
1466
+ request["codeChangeBase"] = code_change_base
1363
1467
  check_arguments(parsed["replay"])
1364
1468
  allowed = allowed_files(root, parsed)
1365
1469
  if "--cloud-preview" in parsed["switches"]:
@@ -2067,7 +2171,9 @@ def changed_paths(root, base, commit):
2067
2171
  yield status, before_path, path
2068
2172
 
2069
2173
 
2070
- def snapshot_code_change(root, base, commit, name):
2174
+ def snapshot_code_change(
2175
+ root, base, commit, name, against="uncommitted (vs HEAD)", subject_ref=None
2176
+ ):
2071
2177
  files, total = [], 0
2072
2178
  for status, before_path, path in changed_paths(root, base, commit):
2073
2179
  if len(files) >= CODE_CHANGE_MAX_FILES:
@@ -2083,14 +2189,34 @@ def snapshot_code_change(root, base, commit, name):
2083
2189
  files.append({"path": path, "before": before, "after": after})
2084
2190
  if not files:
2085
2191
  return None
2086
- head = (name or "").strip() or git(root, "log", "-1", "--format=%s", base)
2192
+ head = (name or "").strip() or git(
2193
+ root, "log", "-1", "--format=%s", subject_ref or base
2194
+ )
2087
2195
  word = "file" if len(files) == 1 else "files"
2088
2196
  return {
2089
- "description": f"{head or 'Working-tree change'} ({len(files)} {word} changed uncommitted (vs HEAD))",
2197
+ "description": f"{head or 'Working-tree change'} ({len(files)} {word} changed {against})",
2090
2198
  "files": files,
2091
2199
  }
2092
2200
 
2093
2201
 
2202
+ def branch_code_change(root, base, commit, request):
2203
+ code_change_base = request.get("codeChangeBase")
2204
+ if not isinstance(code_change_base, dict):
2205
+ return None
2206
+ sha, ref = code_change_base.get("sha"), code_change_base.get("ref")
2207
+ if not (
2208
+ isinstance(sha, str)
2209
+ and SHA.fullmatch(sha)
2210
+ and isinstance(ref, str)
2211
+ and BRANCH_NAME.fullmatch(ref)
2212
+ and quiet_git(root, "rev-parse", "--verify", "-q", f"{commit}^2") == sha
2213
+ ):
2214
+ return None
2215
+ return snapshot_code_change(
2216
+ root, sha, commit, replay_name(request["args"]), f"vs {ref}", base
2217
+ )
2218
+
2219
+
2094
2220
  def write_code_change(change):
2095
2221
  handle, path = tempfile.mkstemp(
2096
2222
  prefix="bitfab-code-change-",
@@ -2127,6 +2253,7 @@ def execute():
2127
2253
  None
2128
2254
  if explicit_change or base is None
2129
2255
  else snapshot_code_change(root, base, commit, replay_name(request["args"]))
2256
+ or branch_code_change(root, base, commit, request)
2130
2257
  )
2131
2258
  run_environment = dict(environment)
2132
2259
  if change:
data/lib/bitfab/replay.rb CHANGED
@@ -598,6 +598,7 @@ module Bitfab
598
598
  CC_MAX_FILES = 60
599
599
  CC_MAX_FILE_BYTES = 500_000
600
600
  CC_MAX_TOTAL_BYTES = 2_000_000
601
+ CC_MAX_BRANCH_WALK = 1000
601
602
 
602
603
  def resolve_skip_assertion_judging(skip_assertion_judging, judge_assertions)
603
604
  return skip_assertion_judging if judge_assertions.nil?
@@ -658,7 +659,9 @@ module Bitfab
658
659
  # in replay() (the one call guaranteed to run on every replay) so it works no
659
660
  # matter how the replay was launched. Precedence: a BITFAB_CODE_CHANGE_PATH
660
661
  # override, else the git diff of the working tree vs HEAD (or vs the
661
- # merge-base with BITFAB_CODE_CHANGE_BASE). Best-effort: any failure yields nil.
662
+ # merge-base with BITFAB_CODE_CHANGE_BASE). A clean working tree falls back to
663
+ # the point where HEAD split from the default branch, so committed branch work
664
+ # still shows. Best-effort: any failure yields nil.
662
665
  # An explicit code_change_files always wins over this.
663
666
  def resolve_auto_code_change(label = nil)
664
667
  return nil if ENV["BITFAB_DISABLE_CODE_CHANGE_CAPTURE"]
@@ -711,6 +714,73 @@ module Bitfab
711
714
  cc_ref_exists?(root, "HEAD") ? ["HEAD", "uncommitted (vs HEAD)"] : nil
712
715
  end
713
716
 
717
+ def cc_list_changes(root, base)
718
+ tracked = git(root, ["diff", "--name-status", "--find-renames", "-z", base, "--", ":!.bitfab"]) || ""
719
+ untracked = git(root, ["ls-files", "--others", "--exclude-standard", "-z", "--", ":!.bitfab"]) || ""
720
+ entries = cc_parse_name_status_z(tracked)
721
+ untracked.split("\0").reject(&:empty?).each { |p| entries << ["A", p, p] }
722
+ entries
723
+ end
724
+
725
+ def cc_default_branch_candidates(root)
726
+ origin_head = (git(root, ["rev-parse", "--abbrev-ref", "origin/HEAD"]) || "").strip
727
+ origin_head = nil if origin_head.empty? || origin_head == "origin/HEAD"
728
+ [origin_head, "origin/main", "origin/master", "main", "master"].compact.uniq
729
+ end
730
+
731
+ def cc_default_branch(root)
732
+ cc_default_branch_candidates(root).find { |ref| cc_ref_exists?(root, ref) }
733
+ end
734
+
735
+ def cc_current_branch_refs(root)
736
+ branch = (git(root, ["symbolic-ref", "--short", "-q", "HEAD"]) || "").strip
737
+ return [] if branch.empty?
738
+
739
+ ["refs/heads/#{branch}"] + (git(root, ["remote"]) || "").split.map { |remote| "refs/remotes/#{remote}/#{branch}" }
740
+ end
741
+
742
+ def cc_own_branch_refs(root)
743
+ upstream = (git(root, ["rev-parse", "--symbolic-full-name", "@{upstream}"]) || "").strip
744
+ own = cc_current_branch_refs(root)
745
+ own << upstream unless upstream.empty?
746
+ own
747
+ end
748
+
749
+ def cc_on_default_branch?(root, default)
750
+ full_name = (git(root, ["rev-parse", "--symbolic-full-name", default]) || "").strip
751
+ !full_name.empty? && cc_current_branch_refs(root).include?(full_name)
752
+ end
753
+
754
+ def cc_branch_tips(root)
755
+ own = cc_own_branch_refs(root)
756
+ listing = git(root, ["for-each-ref", "--format=%(objectname) %(refname)", "refs/heads", "refs/remotes"]) || ""
757
+ listing.each_line.with_object({}) do |line, tips|
758
+ sha, ref = line.strip.split(" ", 2)
759
+ next if ref.nil? || own.include?(ref) || ref.end_with?("/HEAD") || tips.key?(sha)
760
+
761
+ tips[sha] = ref.sub(%r{\Arefs/(heads|remotes)/}, "")
762
+ end
763
+ end
764
+
765
+ def cc_resolve_branch_point(root)
766
+ head = (git(root, ["rev-parse", "HEAD"]) || "").strip
767
+ return nil if head.empty?
768
+
769
+ default = cc_default_branch(root)
770
+ return nil if default && cc_on_default_branch?(root, default)
771
+
772
+ default_base = default ? (git(root, ["merge-base", "HEAD", default]) || "").strip : ""
773
+ walk = ["rev-list", "--first-parent", "--max-count=#{CC_MAX_BRANCH_WALK}", "HEAD"]
774
+ walk << "^#{default_base}" unless default_base.empty?
775
+ tips = cc_branch_tips(root)
776
+ (git(root, walk) || "").split.drop(1).each do |sha|
777
+ return [sha, "vs #{tips[sha]}"] if tips.key?(sha)
778
+ end
779
+ return [default_base, "vs #{default}"] unless default_base.empty? || default_base == head
780
+
781
+ nil
782
+ end
783
+
714
784
  def cc_blob_bytes(root, base, path)
715
785
  out = git(root, ["cat-file", "-s", "#{base}:#{path}"])
716
786
  (out && !out.strip.empty?) ? out.strip.to_i : 0
@@ -732,15 +802,18 @@ module Bitfab
732
802
  resolved = cc_resolve_base(root)
733
803
  return nil if resolved.nil?
734
804
 
735
- base, against = resolved
736
-
737
- tracked = git(root, ["diff", "--name-status", "--find-renames", "-z", base, "--", ":!.bitfab"]) || ""
738
- untracked = git(root, ["ls-files", "--others", "--exclude-standard", "-z", "--", ":!.bitfab"]) || ""
739
-
740
- entries = cc_parse_name_status_z(tracked)
741
- untracked.split("\0").reject(&:empty?).each { |p| entries << ["A", p, p] }
805
+ entries = cc_list_changes(root, resolved[0])
806
+ if entries.empty? && resolved[0] == "HEAD"
807
+ branch_point = cc_resolve_branch_point(root)
808
+ if branch_point
809
+ resolved = branch_point
810
+ entries = cc_list_changes(root, branch_point[0])
811
+ end
812
+ end
742
813
  return nil if entries.empty?
743
814
 
815
+ base, against = resolved
816
+
744
817
  files = []
745
818
  total = 0
746
819
  entries.each do |status, before_path, path|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bitfab
4
- VERSION = "0.65.5"
4
+ VERSION = "0.65.7"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bitfab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.65.5
4
+ version: 0.65.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harvest Team