Skip to content

Commit c2fe3a0

Browse files
fix: prove a full run wrote its analysis, and pass over unreadable lent ones
Core reads the previous analysis from where it writes the new one, so the file being there after a full run proved nothing: a run that wrote nothing would publish the lent analysis, possibly another configuration's, as its own. The check now compares the file before and after; every analysis Core writes carries its own generated_at. The any-configuration lookup on the baseline branch is best effort, but a failed read there ran baseline_fail and failed the run. With CFG_HASH='*' a read failure now warns and the lookup falls through, as the artifact lookup does; a lookup for this configuration still fails loudly. Carries #172's ignore-rule and fetch-directory fixes unchanged, so the rebase onto it drops them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 7512022 commit c2fe3a0

9 files changed

Lines changed: 159 additions & 40 deletions

‎action.yml‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -486,35 +486,36 @@ runs:
486486

487487
# Both lookups are best effort. Without them the run derives the base from
488488
# the committed baseline and the head from the base, which is what every run
489-
# did before any of this existed. A fresh run skips them, so no old base id
490-
# reaches the review artifact either.
489+
# did before any of this existed. A fresh run passes no name, so no old base
490+
# id reaches the review artifact either, while fetch-state.sh still clears
491+
# what an earlier use of the action in this job left there.
491492
- name: Fetch published base analysis
492493
id: fetch_base
493-
if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.base_name != '' && github.server_url == 'https://github.com' && inputs.fresh_analysis != 'true'
494+
if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.base_name != '' && github.server_url == 'https://github.com'
494495
continue-on-error: true
495496
shell: bash
496497
env:
497498
GH_TOKEN: ${{ inputs.github_token }}
498499
GH_ENTERPRISE_TOKEN: ${{ inputs.github_token }}
499500
GH_HOST: ${{ github.server_url }}
500501
REPOSITORY: ${{ github.repository }}
501-
ARTIFACT_NAME: ${{ steps.state.outputs.base_name }}
502+
ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.base_name || '' }}
502503
DEST: ${{ runner.temp }}/cb-state/${{ github.action }}/base
503504
# Must match the review retention below: a review references a base by
504505
# id for its whole life, so a base with less than that left is renewed.
505506
RENEW_WITHIN_DAYS: '14'
506507
run: "$GITHUB_ACTION_PATH/scripts/action/fetch-state.sh"
507508

508509
- name: Fetch this pull request's last analysis
509-
if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.warmstart_name != '' && github.server_url == 'https://github.com' && inputs.fresh_analysis != 'true'
510+
if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.warmstart_name != '' && github.server_url == 'https://github.com'
510511
continue-on-error: true
511512
shell: bash
512513
env:
513514
GH_TOKEN: ${{ inputs.github_token }}
514515
GH_ENTERPRISE_TOKEN: ${{ inputs.github_token }}
515516
GH_HOST: ${{ github.server_url }}
516517
REPOSITORY: ${{ github.repository }}
517-
ARTIFACT_NAME: ${{ steps.state.outputs.warmstart_name }}
518+
ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.warmstart_name || '' }}
518519
DEST: ${{ runner.temp }}/cb-state/${{ github.action }}/warmstart
519520
run: "$GITHUB_ACTION_PATH/scripts/action/fetch-state.sh"
520521

‎scripts/action/codeboarding-baseline.sh‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,18 @@ baseline_fail() {
1616
exit 1
1717
}
1818

19+
# Reading an entry made under any configuration (CFG_HASH='*') is best effort: it
20+
# only lends names and static analysis, so a failure falls through to the next
21+
# source the way a missing artifact does. Under this run's configuration it fails
22+
# the run. Callers return 1 after it.
23+
baseline_read_fail() {
24+
[ "${CFG_HASH:-}" = "*" ] || baseline_fail "$1"
25+
echo "::warning::$1 Not taking anything from it."
26+
}
27+
1928
# Returns 0 when branch $2 (BASELINE_BRANCH by default) exists in repository $1 and
2029
# 2 when it does not. Any other failure is retried once, so one network blip does
21-
# not fail the run.
30+
# not fail the run, and then returns 1 if baseline_read_fail lets it.
2231
baseline_branch_exists() {
2332
local repository="$1" branch="${2:-$BASELINE_BRANCH}" auth status
2433
auth="$(printf 'x-access-token:%s' "${GIT_TOKEN:-}" | base64 -w0)"
@@ -31,7 +40,8 @@ baseline_branch_exists() {
3140
esac
3241
[ "$attempt" = 2 ] || sleep "${BASELINE_RETRY_DELAY:-2}"
3342
done
34-
baseline_fail "Could not read $branch from $repository. Check that github_token can read the repository."
43+
baseline_read_fail "Could not read $branch from $repository. Check that github_token can read the repository."
44+
return 1
3545
}
3646

3747
# ---- Reading ----
@@ -68,15 +78,16 @@ restore_codeboarding_baseline() {
6878
echo "::notice::$BASELINE_BRANCH does not exist yet; the first sync creates it."
6979
return 1
7080
fi
81+
[ "$status" = 0 ] || return 1
7182
index="$(codeboarding_baseline_index "$repository")" ||
72-
baseline_fail "Could not fetch $BASELINE_BRANCH from $repository."
83+
{ baseline_read_fail "Could not fetch $BASELINE_BRANCH from $repository."; return 1; }
7384
if [ -z "$index" ]; then
7485
# A code branch, as when a review into another branch names the one that holds the analysis.
7586
echo "::warning::$BASELINE_BRANCH is not a CodeBoarding analysis branch, so its analysis is not read here."
7687
return 1
7788
fi
7889
fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" ||
79-
baseline_fail "Could not fetch the history of ${tip:0:7} to look for its analysis."
90+
{ baseline_read_fail "Could not fetch the history of ${tip:0:7} to look for its analysis."; return 1; }
8091
for commit in $(git -C "$CHECKOUT_DIR" rev-list --first-parent --max-count=$(( CATCHUP_BOUND + 1 )) "$tip" 2>/dev/null); do
8192
entry="$(awk -v source="$commit" -v cfg="$CFG_HASH" '$2 == source && (cfg == "*" || $3 == cfg) {print $1; exit}' <<< "$index")"
8293
[ -z "$entry" ] || break
@@ -88,11 +99,11 @@ restore_codeboarding_baseline() {
8899
fi
89100
# The entry is known to exist from here on, so failing to read it is an error.
90101
fetch_commit "$repository" "$entry" ||
91-
baseline_fail "Could not fetch the analysis ${entry:0:7} from $BASELINE_BRANCH."
102+
{ baseline_read_fail "Could not fetch the analysis ${entry:0:7} from $BASELINE_BRANCH."; return 1; }
92103
rm -rf "$dest"
93104
mkdir -p "$dest"
94105
git -C "$CHECKOUT_DIR" archive "$entry" .codeboarding | tar -x -C "$dest" ||
95-
baseline_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH."
106+
{ baseline_read_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH."; return 1; }
96107
# source.json is provenance, not engine state.
97108
rm -f "$dest/.codeboarding/source.json"
98109
BASELINE_SOURCE="$commit"

‎scripts/action/fetch-state.sh‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
# Best effort: a miss, an expired bundle or a token without actions:read all
44
# leave the directory absent, and the caller derives from the base instead.
55
set -euo pipefail
6-
[ -n "${ARTIFACT_NAME:-}" ] || exit 0
76

8-
# Clear first, on every path. These destinations are fixed, so a second use of
9-
# the action in one job would otherwise inherit the first one's files and treat
10-
# them as this configuration's state.
7+
# Clear first, on every path, a fresh run's empty name included. These
8+
# destinations are fixed, so a second use of the action in one job would
9+
# otherwise inherit the first one's files and treat them as this configuration's
10+
# state.
1111
rm -rf "${DEST:?}"
12+
[ -n "${ARTIFACT_NAME:-}" ] || exit 0
1213

1314
api() { gh api -H 'Accept: application/vnd.github+json' "$@"; }
1415
export GH_HOST="${GH_HOST#*://}"

‎scripts/action/seed-sources.sh‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,11 @@ reuse_other_configuration() {
9393
SEED_NAMES_ONLY=true
9494
echo "::notice::Component names carry over from the analysis of ${source:0:7}, saved under another configuration."
9595
fi
96-
# Core keeps a cached file whatever today's ignore rules say, so a cache made
97-
# under other rules would keep newly ignored files and miss newly analysed ones.
98-
if { [ -e "$seed/.codeboardingignore" ] || [ -e "$state/.codeboardingignore" ]; } &&
99-
! cmp -s "$seed/.codeboardingignore" "$state/.codeboardingignore"; then
96+
# Core reads the ignore rules from the checkout and keeps a cached file whatever
97+
# they say later, so a cache made under other rules would keep newly ignored
98+
# files and miss newly analysed ones. The two commits say which rules applied;
99+
# the bundle cannot, since uploads drop dotfiles.
100+
if ! git -C "$CHECKOUT_DIR" diff --quiet "$source" "$tip" -- .codeboarding/.codeboardingignore; then
100101
echo "::notice::The saved static analysis of ${source:0:7} was made with another .codeboardingignore; not reusing it."
101102
return 0
102103
fi

‎scripts/analyze_repository.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from __future__ import annotations
55

66
import argparse
7+
import hashlib
78
import json
89
import os
910
import shutil
@@ -168,9 +169,14 @@ def main(argv: list[str] | None = None) -> int:
168169
command = [PROG, "full", "--local", str(checkout), "--output-dir", str(output_dir), "--depth-cap", args.depth_cap]
169170
if args.fresh:
170171
command.append("--force")
171-
_run_command(command, output_dir)
172172
analysis_path = output_dir / "analysis.json"
173-
if not analysis_path.is_file():
173+
# Core reads the previous analysis from where it writes the new one, so the file
174+
# being there proves nothing; its content changing does, since every analysis
175+
# Core writes carries its own generated_at. An untouched one would publish the
176+
# previous analysis, maybe another configuration's, as this run's.
177+
previous = hashlib.sha256(analysis_path.read_bytes()).digest() if analysis_path.is_file() else None
178+
_run_command(command, output_dir)
179+
if not analysis_path.is_file() or hashlib.sha256(analysis_path.read_bytes()).digest() == previous:
174180
raise AnalysisError(f"Full analysis did not produce: {analysis_path}")
175181
print(f"analysis_mode=full")
176182
print("requires_full_analysis=false")

‎tests/test_action_state.py‎

Lines changed: 74 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st
5454

5555
ENGINE_STUB = '''#!/usr/bin/env python3
5656
"""CodeBoarding CLI stand-in: records each call and writes a minimal analysis."""
57-
import json, os, sys
57+
import datetime, json, os, sys
5858
5959
argv = sys.argv[1:]
6060
output = argv[argv.index("--output-dir") + 1]
@@ -77,6 +77,8 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st
7777
sys.exit(0)
7878
if argv[0] == "full":
7979
metadata = {"depth_cap": int(argv[argv.index("--depth-cap") + 1])}
80+
# Like Core, so no two analyses it writes are alike.
81+
metadata["generated_at"] = datetime.datetime.now(datetime.timezone.utc).isoformat()
8082
with open(analysis, "w") as handle:
8183
json.dump({"metadata": metadata, "components": [], "components_relations": []}, handle)
8284
print(json.dumps({"requiresFullAnalysis": False, "analysis_path": analysis}))
@@ -985,25 +987,59 @@ def test_a_bundle_without_both_static_analysis_files_is_not_used(self) -> None:
985987
self.assertEqual([p.name for p in state.iterdir()], ["analysis.json"])
986988
self.assertNotIn("Reusing", self.log)
987989

988-
def test_static_analysis_made_with_other_ignore_rules_is_not_used(self) -> None:
989-
# Core keeps cached files whatever the current rules say, so a mismatch would persist.
990+
def _publish(self, staged: Path) -> None:
991+
"""Writes the bundle as action.yml's base uploads publish a staged state:
992+
upload-artifact leaves dotfiles out unless the step sets include-hidden-files."""
990993
import zipfile
991994

992-
shas = self._history(2)
993-
self._add_static_sha()
994-
with zipfile.ZipFile(self.bundle, "a") as archive:
995-
archive.writestr(".codeboardingignore", "docs/\n")
996-
self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[-1]}")])
995+
steps = (ROOT / "action.yml").read_text(encoding="utf-8").split("\n - name:")
996+
uploads = [s for s in steps if "actions/upload-artifact@" in s and "/out/base" in s]
997+
self.assertTrue(uploads)
998+
hidden = all("include-hidden-files: true" in s for s in uploads)
999+
with zipfile.ZipFile(self.bundle, "w") as archive:
1000+
for path in sorted(staged.rglob("*")):
1001+
relative = path.relative_to(staged)
1002+
if path.is_file() and (hidden or not any(part.startswith(".") for part in relative.parts)):
1003+
archive.write(path, relative.as_posix())
9971004

998-
for ignore in (None, "tests/\n"):
999-
with self.subTest(ignore=ignore):
1000-
state = self._reuse_static(shas[-1], ignore)
1001-
self.assertFalse((state / "static_analysis.pkl").exists())
1002-
self.assertIn("made with another .codeboardingignore", self.log)
1005+
def test_static_analysis_is_reused_only_under_the_ignore_rules_it_was_made_with(self) -> None:
1006+
# Core keeps cached files whatever the current rules say, so a mismatch would persist.
1007+
git = ["git", "-C", str(self.origin), "-c", "user.name=T", "-c", "user.email=t@example.com"]
1008+
ignore_file = self.origin / ".codeboarding" / ".codeboardingignore"
1009+
1010+
def commit(ignore: str | None) -> str:
1011+
ignore_file.parent.mkdir(exist_ok=True)
1012+
if ignore is None:
1013+
ignore_file.unlink()
1014+
else:
1015+
ignore_file.write_text(ignore, encoding="utf-8")
1016+
subprocess.run([*git, "add", "-A"], check=True)
1017+
subprocess.run([*git, "-c", "commit.gpgsign=false", "commit", "-q", "--allow-empty", "-m", "c"], check=True)
1018+
return subprocess.check_output([*git, "rev-parse", "HEAD"], text=True).strip()
1019+
1020+
self._history(1)
1021+
saved, same_rules, rules_removed = commit("docs/\n"), commit("docs/\n"), commit(None)
1022+
# The base saved for `saved`, staged as keep_user_config and the analysis left it.
1023+
staged = self.root / "staged"
1024+
staged.mkdir()
1025+
for name, content in (
1026+
("analysis.json", json.dumps({"metadata": {"depth_cap": 2}, "components": []})),
1027+
("static_analysis.pkl", "pickle"),
1028+
("static_analysis.sha", "a" * 40),
1029+
("metadata.json", json.dumps({"kind": "base"})),
1030+
(".codeboardingignore", "docs/\n"),
1031+
):
1032+
(staged / name).write_text(content, encoding="utf-8")
1033+
self._publish(staged)
1034+
self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{saved}")])
10031035

1004-
state = self._reuse_static(shas[-1], "docs/\n")
1036+
state = self._reuse_static(same_rules, "docs/\n")
10051037
self.assertTrue((state / "static_analysis.pkl").exists(), "the same rules must not block reuse")
10061038

1039+
state = self._reuse_static(rules_removed)
1040+
self.assertFalse((state / "static_analysis.pkl").exists())
1041+
self.assertIn(f"The saved static analysis of {saved[:7]} was made with another .codeboardingignore", self.log)
1042+
10071043
def test_a_nearer_base_under_another_configuration_is_no_analysis_to_continue_from(self) -> None:
10081044
shas = self._history(4)
10091045
self._serve(
@@ -1184,10 +1220,32 @@ def test_state_is_published_from_the_directory_the_analysis_stages(self) -> None
11841220
def test_a_fresh_run_fetches_no_saved_analysis(self) -> None:
11851221
# A fetched base's id would otherwise reach the review artifact whenever this
11861222
# run's own base is not published, pairing two independently built graphs.
1187-
fetches = [s for s in self._steps() if s.get("run", "").endswith('fetch-state.sh"')]
1223+
# The steps still run, so what an earlier use of the action in this job
1224+
# fetched is cleared rather than continued from.
1225+
steps = (ROOT / "action.yml").read_text(encoding="utf-8").split("\n - name:")
1226+
fetches = [s for s in steps if 'fetch-state.sh"' in s]
11881227
self.assertEqual(len(fetches), 2)
11891228
for step in fetches:
1190-
self.assertIn("inputs.fresh_analysis != 'true'", step["if"], step["name"])
1229+
condition = next(line for line in step.splitlines() if line.strip().startswith("if:"))
1230+
self.assertNotIn("fresh_analysis", condition)
1231+
self.assertIn("ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.", step)
1232+
1233+
def test_fetching_no_name_still_clears_the_destination(self) -> None:
1234+
with tempfile.TemporaryDirectory() as tmp:
1235+
dest = Path(tmp) / "base"
1236+
dest.mkdir()
1237+
(dest / "analysis.json").write_text("{}", encoding="utf-8")
1238+
1239+
result = subprocess.run(
1240+
[str(SCRIPTS / "fetch-state.sh")],
1241+
env={"PATH": os.environ["PATH"], "ARTIFACT_NAME": "", "DEST": str(dest)},
1242+
capture_output=True,
1243+
text=True,
1244+
check=False,
1245+
)
1246+
1247+
self.assertEqual(result.returncode, 0, result.stderr)
1248+
self.assertFalse(dest.exists(), "an earlier invocation's state survived")
11911249

11921250
def test_a_fork_publishes_nothing_another_run_would_read(self) -> None:
11931251
# A base graph is named for a commit, so every pull request forking there

‎tests/test_analyze_repository.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,20 @@ def fake_run(_args, output_dir):
235235
self.assertIn(f"analysis_path={out_dir / 'analysis.json'}", stdout.getvalue())
236236
self.assertFalse(stale_artifact.exists())
237237

238+
def test_a_full_run_that_leaves_the_previous_analysis_in_place_fails(self) -> None:
239+
# The previous analysis stays for Core to carry names from, so the file being
240+
# there proves nothing: publishing it would pass an old analysis off as this run's.
241+
with tempfile.TemporaryDirectory() as tmp:
242+
root = Path(tmp)
243+
checkout, out_dir = root / "repo", root / "out"
244+
checkout.mkdir()
245+
out_dir.mkdir()
246+
(out_dir / "analysis.json").write_text("previous", encoding="utf-8")
247+
248+
with patch("sys.stdout", io.StringIO()), patch.object(ar, "_run_command", return_value=""):
249+
with self.assertRaisesRegex(ar.AnalysisError, "did not produce"):
250+
ar.main(["full", "--checkout", str(checkout), "--output-dir", str(out_dir), "--depth-cap", "2"])
251+
238252
def _full_over_saved_state(self, *extra: str) -> tuple[list[str], list[str]]:
239253
"""Runs mode full over a state holding a previous analysis and other output.
240254

‎tests/test_baseline_branch.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,31 @@ def test_after_a_release_the_full_analysis_keeps_the_names_of_the_old_configurat
444444
self.assertEqual(base["files"], ["analysis.json", "static_analysis.pkl", "static_analysis.sha"])
445445
self.assertIn("Component names carry over", self.log)
446446

447+
def _break_extraction(self) -> None:
448+
tar = self.bin_dir / "tar"
449+
tar.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8")
450+
tar.chmod(0o755)
451+
452+
def test_an_entry_from_another_configuration_that_cannot_be_read_is_passed_over(self) -> None:
453+
# It would only lend names and static analysis, so failing to read it is no
454+
# reason to fail the run.
455+
self._break_extraction()
456+
457+
self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="othercfg")
458+
459+
[base] = [json.loads(line) for line in self.engine_log.read_text().splitlines() if '-base"' in line]
460+
self.assertEqual((base["mode"], base["seed"]), ("full", None))
461+
self.assertIn("Could not extract the analysis", self.log)
462+
self.assertNotIn("failure_reason", self._outputs())
463+
464+
def test_an_entry_of_this_configuration_that_cannot_be_read_fails_the_run(self) -> None:
465+
self._break_extraction()
466+
467+
self._analyze(False, REVIEW_BASE_SHA=self.shas[3])
468+
469+
self.assertIn("Could not extract the analysis", self._outputs().get("failure_reason", ""))
470+
self.assertEqual(self._modes(), [])
471+
447472
def test_without_a_configuration_hash_no_entry_is_trusted(self) -> None:
448473
self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="")
449474

‎tests/test_merge_base_contract.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def git(*args):
6969

7070
ENGINE_STUB = '''#!/usr/bin/env python3
7171
"""Minimal CodeBoarding CLI stand-in recording which tree it was given."""
72-
import json, os, subprocess, sys
72+
import datetime, json, os, subprocess, sys
7373
7474
argv = sys.argv[1:]
7575
checkout = argv[argv.index("--local") + 1]
@@ -84,7 +84,9 @@ def git(*args):
8484
}) + "\\n")
8585
analysis = os.path.join(output, "analysis.json")
8686
with open(analysis, "w") as handle:
87-
json.dump({"metadata": {"depth_level": 2}, "components": [], "components_relations": []}, handle)
87+
# Like Core, so no two analyses it writes are alike.
88+
metadata = {"depth_level": 2, "generated_at": datetime.datetime.now(datetime.timezone.utc).isoformat()}
89+
json.dump({"metadata": metadata, "components": [], "components_relations": []}, handle)
8890
print(json.dumps({"requiresFullAnalysis": False, "analysis_path": analysis}))
8991
'''
9092

0 commit comments

Comments
 (0)