From 7512022cbdc4f1fddf849b47e945502a1dc0a3a8 Mon Sep 17 00:00:00 2001 From: Svilen Stefanov Date: Wed, 11 Nov 2026 19:59:54 +0100 Subject: [PATCH 1/2] feat: keep component names when the analysis runs in full A full run deleted analysis.json before Core started, and after a release nothing saved matched the new configuration, so every full run named the components from scratch, losing names someone had fixed. A full run now keeps the previous analysis.json beside the static-analysis cache, and Core carries ids and names over to the components it continues. With no seed under the current configuration, the nearest base saved under any configuration, on the baseline branch or as an artifact, lends both its analysis.json and its static analysis in one lookup (reuse_other_configuration, replacing reuse_static_analysis). SEED_NAMES_ONLY then sends analyze.sh straight to a full run. fresh_analysis still keeps nothing. restore_codeboarding_baseline splits the branch read out of from_codeboarding_baseline, so the raw entry's .codeboardingignore can be compared before its static analysis is reused. Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- action.yml | 2 +- scripts/action/analyze.sh | 4 +- scripts/action/codeboarding-baseline.sh | 40 +++++++++++------- scripts/action/find-ancestor-base.sh | 16 ++++--- scripts/action/review-generate-baseline.sh | 10 ++--- scripts/action/seed-sources.sh | 49 ++++++++++++++-------- scripts/action/sync-generate-baseline.sh | 6 +-- scripts/analyze_repository.py | 9 ++-- tests/test_action_state.py | 25 ++++++----- tests/test_analyze_repository.py | 32 ++++++++------ tests/test_baseline_branch.py | 21 +++++++++- 12 files changed, 135 insertions(+), 81 deletions(-) diff --git a/README.md b/README.md index 04639324..8cb04557 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ Everything here is best-effort. A missing artifact, an expired one, or a token w Because artifacts are readable and writable from every trigger, all paths behave the same: an automatic run, a `/codeboarding` command and a manual dispatch each reuse the previous analysis and publish their own. A stored analysis is discarded, and the head re-derived from the base, whenever the pinned CodeBoarding version, `.codeboardingignore`, the model selection, the analysis depth or the merge base changes — or when the base graph it grew from is not the one this run compares against. -With no analysis to continue from, a run still reuses the static analysis (the language servers' call graph, `static_analysis.pkl`) saved with the nearest base for the commit analysed or one of its last 100 first-parent ancestors, whatever configuration made it, and the engine brings it up to date. The engine checks that file itself and rebuilds one from another engine cache version, so a new CodeBoarding release or a changed model or depth costs the language-server pass only when it has to. Set `fresh_analysis: 'true'` to rebuild everything, static analysis included. +With no analysis to continue from, as after a new CodeBoarding release, a run still takes two things from the nearest base saved for the commit analysed or one of its last 100 first-parent ancestors, whatever configuration made it. Its component names and descriptions carry over to the components the new analysis continues, so names someone fixed are not lost. Its static analysis (the language servers' call graph, `static_analysis.pkl`) is brought up to date instead of rebuilt; the engine checks that file itself and rebuilds one from another engine cache version, so a release or a changed model or depth costs the language-server pass only when it has to. Every full analysis keeps both from the analysis it replaces. Set `fresh_analysis: 'true'` to rebuild everything, names and static analysis included. Fork pull requests never carry an analysis forward. They are reviewed on request, each review starts from the base, and nothing they produce is read by a run on this repository's own code: untrusted code must not shape state that a later run loads. The action also accepts `pull_request_target`, which runs on the base branch ref and lets both share one chain; that trigger has its own trade-offs (a PR that adds this workflow will not run it until merged, and the fork gate becomes load-bearing), so `pull_request` remains the recommended default. diff --git a/action.yml b/action.yml index 1c85d8db..b94e4d87 100644 --- a/action.yml +++ b/action.yml @@ -149,7 +149,7 @@ inputs: included, is built from scratch. A fresh review republishes only the base at its merge base; later syncs still continue from the analysis branch. Off by default, so a run continues from the nearest saved analysis and, without one, still reuses the - nearest saved static analysis. + component names and static analysis of the nearest one saved under any configuration. required: false default: 'false' review_guide_url: diff --git a/scripts/action/analyze.sh b/scripts/action/analyze.sh index 58cd8fb5..111e4aca 100755 --- a/scripts/action/analyze.sh +++ b/scripts/action/analyze.sh @@ -35,7 +35,9 @@ fresh="" if [ "${FRESH_ANALYSIS:-false}" = true ] && [ "$role" != head ]; then fresh=--fresh; fi mkdir -p "$state" -if [ -z "$fresh" ] && compatible_state "$state"; then +# A seed from another configuration only lends names and static analysis; it +# holds no engine state to continue from. +if [ -z "$fresh" ] && [ "${SEED_NAMES_ONLY:-false}" != true ] && compatible_state "$state"; then run_core incremental if [ "$ANALYSIS_MODE" != incremental ] || { [ "$REQUIRES_FULL" != true ] && [ ! -f "$ANALYSIS_PATH" ]; }; then fail "Core returned an invalid incremental-analysis result." diff --git a/scripts/action/codeboarding-baseline.sh b/scripts/action/codeboarding-baseline.sh index be19508d..62211b8c 100644 --- a/scripts/action/codeboarding-baseline.sh +++ b/scripts/action/codeboarding-baseline.sh @@ -51,13 +51,13 @@ codeboarding_baseline_index() { awk 'NF >= 2 {print $1, $2, (NF >= 3 ? $3 : "-")}' } -# Seeds $3 from the branch's entry for $2, or for its nearest first-parent -# ancestor, made under this run's configuration: the trailer pins it the way the -# name pins a saved artifact, and without a configuration hash no entry is -# trusted. Keeps checkout $4's user configuration. Sets SEED_SHA to the commit -# the seed describes. -from_codeboarding_baseline() { - local repository="$1" tip="$2" state="$3" config_from="$4" index commit entry="" scratch="$RUNNER_TEMP/codeboarding-baseline-restore" +# Extracts into $3/.codeboarding the branch's entry for $2, or for its nearest +# first-parent ancestor, made under this run's configuration: the trailer pins it +# the way the name pins a saved artifact, and without a configuration hash no entry +# is trusted. CFG_HASH='*' takes an entry made under any configuration. Sets +# BASELINE_SOURCE to the commit the entry describes. +restore_codeboarding_baseline() { + local repository="$1" tip="$2" dest="$3" index commit entry="" local status=0 [ -n "${BASELINE_BRANCH:-}" ] || return 1 [ -n "${CFG_HASH:-}" ] || return 1 @@ -78,27 +78,37 @@ from_codeboarding_baseline() { fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" || baseline_fail "Could not fetch the history of ${tip:0:7} to look for its analysis." for commit in $(git -C "$CHECKOUT_DIR" rev-list --first-parent --max-count=$(( CATCHUP_BOUND + 1 )) "$tip" 2>/dev/null); do - entry="$(awk -v source="$commit" -v cfg="$CFG_HASH" '$2 == source && $3 == cfg {print $1; exit}' <<< "$index")" + entry="$(awk -v source="$commit" -v cfg="$CFG_HASH" '$2 == source && (cfg == "*" || $3 == cfg) {print $1; exit}' <<< "$index")" [ -z "$entry" ] || break done if [ -z "$entry" ]; then - echo "::notice::$BASELINE_BRANCH has no analysis of ${tip:0:7} or its last $CATCHUP_BOUND ancestors made with this configuration." + [ "$CFG_HASH" = "*" ] || + echo "::notice::$BASELINE_BRANCH has no analysis of ${tip:0:7} or its last $CATCHUP_BOUND ancestors made with this configuration." return 1 fi # The entry is known to exist from here on, so failing to read it is an error. fetch_commit "$repository" "$entry" || baseline_fail "Could not fetch the analysis ${entry:0:7} from $BASELINE_BRANCH." - rm -rf "$scratch" - mkdir -p "$scratch" - git -C "$CHECKOUT_DIR" archive "$entry" .codeboarding | tar -x -C "$scratch" || + rm -rf "$dest" + mkdir -p "$dest" + git -C "$CHECKOUT_DIR" archive "$entry" .codeboarding | tar -x -C "$dest" || baseline_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH." # source.json is provenance, not engine state. - rm -f "$scratch/.codeboarding/source.json" + rm -f "$dest/.codeboarding/source.json" + BASELINE_SOURCE="$commit" +} + +# Seeds $3 from the branch's entry for $2 or its nearest ancestor (see +# restore_codeboarding_baseline). Keeps checkout $4's user configuration. Sets +# SEED_SHA to the commit the seed describes. +from_codeboarding_baseline() { + local repository="$1" tip="$2" state="$3" config_from="$4" scratch="$RUNNER_TEMP/codeboarding-baseline-restore" + restore_codeboarding_baseline "$repository" "$tip" "$scratch" || return 1 compatible_state "$scratch/.codeboarding" || return 1 replace_state "$scratch/.codeboarding" "$state" "$config_from" # shellcheck disable=SC2034 # read by the scripts that source this file - SEED_SHA="$commit" - echo "::notice::Starting from the analysis of ${commit:0:7} saved on $BASELINE_BRANCH." + SEED_SHA="$BASELINE_SOURCE" + echo "::notice::Starting from the analysis of ${BASELINE_SOURCE:0:7} saved on $BASELINE_BRANCH." } # ---- Writing ---- diff --git a/scripts/action/find-ancestor-base.sh b/scripts/action/find-ancestor-base.sh index 1e2300fc..13be9f31 100755 --- a/scripts/action/find-ancestor-base.sh +++ b/scripts/action/find-ancestor-base.sh @@ -3,8 +3,8 @@ # under this configuration and downloads it into DEST, so a run without an exact # base can catch up from it instead of analyzing from scratch. Best effort. # -# With STATIC_ONLY=true a base saved under any configuration will do, and only -# its static-analysis cache is required, since that is all the caller takes. +# With ANY_CONFIG=true a base saved under any configuration will do: the caller +# takes from it only what outlives a configuration. # # Prints ancestor_sha= on # stdout and everything else on stderr. @@ -12,10 +12,10 @@ # Needs the tip's first-parent history in CHECKOUT_DIR, at least CATCHUP_BOUND deep. set -euo pipefail : "${REPOSITORY:?}" "${TIP_SHA:?}" "${CHECKOUT_DIR:?}" "${DEST:?}" -if [ "${STATIC_ONLY:-false}" = true ]; then - pattern='codeboarding-base-[0-9a-f]{16}-' needs=(static_analysis.pkl static_analysis.sha) +if [ "${ANY_CONFIG:-false}" = true ]; then + pattern='codeboarding-base-[0-9a-f]{16}-' else - pattern="codeboarding-base-${CFG_HASH:?}-" needs=(analysis.json) + pattern="codeboarding-base-${CFG_HASH:?}-" fi BOUND="${CATCHUP_BOUND:-100}" rm -rf "$DEST" @@ -30,7 +30,7 @@ trap report EXIT # The merge base's first-parent history, nearest first. Distance 0 is the tip # itself, which a review already looked up by its exact name under this -# configuration. Sync and STATIC_ONLY lookups pass INCLUDE_TIP=true: nothing +# configuration. Sync and ANY_CONFIG lookups pass INCLUDE_TIP=true: nothing # looked the tip up for them. walk="$(git -C "$CHECKOUT_DIR" rev-list --first-parent --max-count=$(( BOUND + 1 )) "$TIP_SHA" 2>/dev/null || true)" [ "${INCLUDE_TIP:-false}" = true ] || walk="$(tail -n +2 <<< "$walk")" @@ -77,7 +77,5 @@ done # Downloaded by its exact name, through the same checks as every other lookup. GITHUB_OUTPUT="" ARTIFACT_NAME="$name" DEST="$DEST" "$(dirname "$0")/fetch-state.sh" >&2 || exit 0 -for file in "${needs[@]}"; do - [ -f "$DEST/$file" ] || exit 0 -done +[ -f "$DEST/analysis.json" ] || exit 0 ancestor="${name##*-}" diff --git a/scripts/action/review-generate-baseline.sh b/scripts/action/review-generate-baseline.sh index a8b1bcfd..edae28f3 100755 --- a/scripts/action/review-generate-baseline.sh +++ b/scripts/action/review-generate-baseline.sh @@ -8,10 +8,10 @@ # 4. the saved analysis of the nearest ancestor / # 5. nothing built from scratch # -# Built from scratch, it still reuses the static analysis of the nearest saved -# base, made under any configuration. With FRESH_ANALYSIS=true nothing saved is -# read (action.yml does not fetch source 1), and the base this run publishes -# replaces the one reviews forking there reused so far. +# Built in full, it still takes the component names and the static analysis of +# the nearest base saved under any configuration. With FRESH_ANALYSIS=true +# nothing saved is read (action.yml does not fetch source 1), and the base this +# run publishes replaces the one reviews forking there reused so far. # # Outputs base_analysis_path, and publish_base when this run produced a base # other pull requests forking there should reuse. @@ -50,7 +50,7 @@ else else echo "::notice::No saved analysis to start from; analyzing ${REVIEW_BASE_SHA:0:7} from scratch." keep_user_config "$checkout" "$state" - reuse_static_analysis "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$state" + reuse_other_configuration "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$state" fi # Core never sees the token. unset GIT_TOKEN diff --git a/scripts/action/seed-sources.sh b/scripts/action/seed-sources.sh index b191e4e3..698c1d0d 100644 --- a/scripts/action/seed-sources.sh +++ b/scripts/action/seed-sources.sh @@ -11,6 +11,8 @@ source "$HERE/analysis-state.sh" # shellcheck source=scripts/action/codeboarding-baseline.sh source "$HERE/codeboarding-baseline.sh" SEED_SHA="" +# Set by reuse_other_configuration; analyze.sh then goes straight to a full run. +export SEED_NAMES_ONLY=false # The analysis sync committed under .codeboarding/ on the branch itself. Which # commit it describes is not recorded, so it is always caught up. @@ -67,29 +69,40 @@ from_ancestor_artifact() { echo "::notice::Starting from the saved analysis of ${ancestor:0:7}." } -# With no analysis to continue from, the static analysis in the nearest saved base -# for $2 or an ancestor, under any configuration, still spares Core rebuilding it: -# Core refuses one from another cache version itself. Copies both cache files -# into $3 or neither. Best effort. -reuse_static_analysis() { - local repository="$1" tip="$2" state="$3" found ancestor dest="$RUNNER_TEMP/codeboarding-static" - [ "${ANCESTOR_LOOKUP:-false}" = true ] && [ -n "${REPOSITORY:-}" ] && [ -n "$tip" ] || return 0 - fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" || true - found="$(GH_TOKEN="${GIT_TOKEN:-}" GH_ENTERPRISE_TOKEN="${GIT_TOKEN:-}" TIP_SHA="$tip" DEST="$dest" \ - STATIC_ONLY=true INCLUDE_TIP=true CATCHUP_BOUND="$CATCHUP_BOUND" \ - "$HERE/find-ancestor-base.sh" || true)" - ancestor="$(awk -F= '$1 == "ancestor_sha" {print $2; exit}' <<< "$found")" - [ -n "$ancestor" ] || return 0 +# With no analysis to continue from, as after a release, the nearest base saved +# for $2 or an ancestor under any configuration still lends $3 what outlives a +# configuration: the component names in its analysis.json, which Core keeps for +# the components the full run continues, and its static analysis, which Core +# refuses itself when the cache version changed. Neither is engine state to +# continue from, so analyze.sh runs in full. $3 already holds the analysed +# commit's user configuration. Best effort. +reuse_other_configuration() { + local repository="$1" tip="$2" state="$3" found seed="" source="" dest="$RUNNER_TEMP/codeboarding-other-configuration" + [ -n "$tip" ] || return 0 + if [ -n "${CFG_HASH:-}" ] && CFG_HASH='*' restore_codeboarding_baseline "$repository" "$tip" "$dest"; then + seed="$dest/.codeboarding" source="$BASELINE_SOURCE" + elif [ "${ANCESTOR_LOOKUP:-false}" = true ] && [ -n "${REPOSITORY:-}" ]; then + fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" || true + found="$(GH_TOKEN="${GIT_TOKEN:-}" GH_ENTERPRISE_TOKEN="${GIT_TOKEN:-}" TIP_SHA="$tip" DEST="$dest" \ + ANY_CONFIG=true INCLUDE_TIP=true CATCHUP_BOUND="$CATCHUP_BOUND" \ + "$HERE/find-ancestor-base.sh" || true)" + seed="$dest" source="$(awk -F= '$1 == "ancestor_sha" {print $2; exit}' <<< "$found")" + fi + [ -n "$source" ] || return 0 + if cp "$seed/analysis.json" "$state/"; then + SEED_NAMES_ONLY=true + echo "::notice::Component names carry over from the analysis of ${source:0:7}, saved under another configuration." + fi # Core keeps a cached file whatever today's ignore rules say, so a cache made # under other rules would keep newly ignored files and miss newly analysed ones. - if { [ -e "$dest/.codeboardingignore" ] || [ -e "$state/.codeboardingignore" ]; } && - ! cmp -s "$dest/.codeboardingignore" "$state/.codeboardingignore"; then - echo "::notice::The saved static analysis of ${ancestor:0:7} was made with another .codeboardingignore; not reusing it." + if { [ -e "$seed/.codeboardingignore" ] || [ -e "$state/.codeboardingignore" ]; } && + ! cmp -s "$seed/.codeboardingignore" "$state/.codeboardingignore"; then + echo "::notice::The saved static analysis of ${source:0:7} was made with another .codeboardingignore; not reusing it." return 0 fi - if ! cp "$dest/static_analysis.pkl" "$dest/static_analysis.sha" "$state/"; then + if ! cp "$seed/static_analysis.pkl" "$seed/static_analysis.sha" "$state/" 2>/dev/null; then rm -f "$state/static_analysis.pkl" "$state/static_analysis.sha" return 0 fi - echo "::notice::Reusing the saved static analysis of ${ancestor:0:7}." + echo "::notice::Reusing the saved static analysis of ${source:0:7}." } diff --git a/scripts/action/sync-generate-baseline.sh b/scripts/action/sync-generate-baseline.sh index cf97adc1..1a5a4438 100755 --- a/scripts/action/sync-generate-baseline.sh +++ b/scripts/action/sync-generate-baseline.sh @@ -5,8 +5,8 @@ # 1. an entry on the baseline branch for this commit or an ancestor # 2. the analysis committed on the branch, or on the parent of a commit that removed it # 3. the saved analysis of this commit or the nearest ancestor -# 4. nothing: analyzed from scratch, reusing the static analysis of the -# nearest saved base, made under any configuration +# 4. nothing: analyzed in full, taking the component names and the static +# analysis of the nearest base saved under any configuration # # With FRESH_ANALYSIS=true nothing saved is read. set -euo pipefail @@ -32,7 +32,7 @@ elif ! from_codeboarding_baseline "${REPOSITORY:-}" "$head_sha" "$state" "$CHECK ! from_parent_committed_baseline "${REPOSITORY:-}" "$head_sha" "$state" "$CHECKOUT_DIR" && ! from_ancestor_artifact "${REPOSITORY:-}" "$head_sha" "$state" true "$CHECKOUT_DIR"; then keep_user_config "$CHECKOUT_DIR" "$state" - reuse_static_analysis "${REPOSITORY:-}" "$head_sha" "$state" + reuse_other_configuration "${REPOSITORY:-}" "$head_sha" "$state" fi # Core never sees the token. diff --git a/scripts/analyze_repository.py b/scripts/analyze_repository.py index bbfd1229..8362cd9c 100755 --- a/scripts/analyze_repository.py +++ b/scripts/analyze_repository.py @@ -13,7 +13,7 @@ from threading import Thread PROG = "codeboarding" -STATIC_CACHE = {"static_analysis.pkl", "static_analysis.sha"} +KEPT_BY_FULL_RUN = {"analysis.json", "static_analysis.pkl", "static_analysis.sha"} # Shared with CodeBoarding/codeboarding_cli/main.py; keep the prefix in sync. ENGINE_ERROR_PREFIX = "CodeBoarding: " @@ -155,10 +155,11 @@ def main(argv: list[str] | None = None) -> int: if not args.depth_cap: raise SystemExit("--depth-cap is required for mode=full") - # Core validates the static-analysis cache itself: one from another cache - # version is refused and rebuilt, so it survives every full run but a fresh one. + # What a full run continues from, every one but a fresh one: Core keeps the names + # of the components the new analysis continues, and refuses and rebuilds a + # static-analysis cache from another cache version itself. for entry in output_dir.iterdir(): - if not args.fresh and entry.name in STATIC_CACHE: + if not args.fresh and entry.name in KEPT_BY_FULL_RUN: continue if entry.is_dir() and not entry.is_symlink(): shutil.rmtree(entry) diff --git a/tests/test_action_state.py b/tests/test_action_state.py index bef4fa2e..b5187f2a 100644 --- a/tests/test_action_state.py +++ b/tests/test_action_state.py @@ -67,6 +67,8 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st "run_id": os.environ.get("CODEBOARDING_RUN_ID", ""), "force": "--force" in argv, "files": sorted(os.listdir(output)), + "seed": json.load(open(os.path.join(output, "analysis.json"))).get("components") + if os.path.isfile(os.path.join(output, "analysis.json")) else None, }) + "\\n") analysis = os.path.join(output, "analysis.json") metadata = json.load(open(analysis))["metadata"] if os.path.isfile(analysis) else {} @@ -414,7 +416,7 @@ def test_a_full_run_keeps_the_static_analysis_cache(self) -> None: full = [c for c in self._engine_calls() if c["mode"] == "full"][0] self.assertFalse(full["force"]) self.assertIn("static_analysis.pkl", full["files"]) - self.assertNotIn("analysis.json", full["files"]) + self.assertIn("analysis.json", full["files"], "the full run lost the names it continues") # How the base was obtained is reported, not just used: the review comment and # the webview explain a slow run by it. @@ -900,9 +902,9 @@ def test_a_first_sync_catches_up_from_the_setup_reviews_base(self) -> None: self.assertEqual(self._modes(), ["incremental"]) - # With no analysis to continue from, the static-analysis cache of a base saved - # under any configuration still spares the language servers' pass: Core - # refuses one from another cache version itself. + # With no analysis to continue from, a base saved under any configuration still + # lends its component names and its static-analysis cache, which spares the + # language servers' pass: Core refuses one from another cache version itself. OTHER_CFG = "0123456789abcdef" @@ -914,7 +916,7 @@ def _add_static_sha(self) -> None: archive.writestr("fingerprint.json", "{}") def _reuse_static(self, tip: str, ignore: str | None = None) -> Path: - """Runs the static-analysis lookup on its own and returns the state it filled. + """Runs the lookup under any configuration on its own and returns the state it filled. `ignore` is the analysed commit's .codeboardingignore, as keep_user_config leaves it.""" state = Path(tempfile.mkdtemp(dir=self.root)) @@ -924,7 +926,7 @@ def _reuse_static(self, tip: str, ignore: str | None = None) -> Path: [ "bash", "-c", - 'source "$1" && reuse_static_analysis origin "$2" "$3"', + 'source "$1" && reuse_other_configuration origin "$2" "$3"', "_", str(SCRIPTS / "seed-sources.sh"), tip, @@ -958,9 +960,10 @@ def test_a_base_built_from_scratch_reuses_any_configurations_static_analysis(sel self.assertIn(f"Reusing the saved static analysis of {shas[1][:7]}.", self.log) base = [json.loads(line) for line in self.engine_log.read_text().splitlines()][0] self.assertEqual((base["mode"], base["force"]), ("full", False)) - self.assertEqual(base["files"], ["static_analysis.pkl", "static_analysis.sha"]) + self.assertEqual(base["files"], ["analysis.json", "static_analysis.pkl", "static_analysis.sha"]) + self.assertIn(f"Component names carry over from the analysis of {shas[1][:7]}", self.log) - def test_only_the_static_analysis_cache_is_taken_from_another_configuration(self) -> None: + def test_only_the_names_and_the_static_analysis_cache_are_taken_from_another_configuration(self) -> None: shas = self._history(2) self._add_static_sha() # The commit itself counts: no lookup by its exact name covered other configurations. @@ -968,7 +971,9 @@ def test_only_the_static_analysis_cache_is_taken_from_another_configuration(self state = self._reuse_static(shas[-1]) - self.assertEqual(sorted(p.name for p in state.iterdir()), ["static_analysis.pkl", "static_analysis.sha"]) + self.assertEqual( + sorted(p.name for p in state.iterdir()), ["analysis.json", "static_analysis.pkl", "static_analysis.sha"] + ) self.assertEqual((state / "static_analysis.pkl").read_text(), "pickle") def test_a_bundle_without_both_static_analysis_files_is_not_used(self) -> None: @@ -977,7 +982,7 @@ def test_a_bundle_without_both_static_analysis_files_is_not_used(self) -> None: state = self._reuse_static(shas[-1]) - self.assertEqual(list(state.iterdir()), []) + self.assertEqual([p.name for p in state.iterdir()], ["analysis.json"]) self.assertNotIn("Reusing", self.log) def test_static_analysis_made_with_other_ignore_rules_is_not_used(self) -> None: diff --git a/tests/test_analyze_repository.py b/tests/test_analyze_repository.py index 49a74f5f..6fc3db4b 100644 --- a/tests/test_analyze_repository.py +++ b/tests/test_analyze_repository.py @@ -206,10 +206,15 @@ def test_main_full_uses_generated_analysis_file(self) -> None: out_dir.mkdir() stale_artifact = out_dir / "fingerprint.json" stale_artifact.write_text("stale", encoding="utf-8") + (out_dir / "health").mkdir() + (out_dir / "analysis.json").write_text("previous", encoding="utf-8") def fake_run(_args, output_dir): self.assertIn("--depth-cap", _args) self.assertNotIn("--depth-level", _args) + # Core names the components the new run continues after the previous analysis. + self.assertEqual(sorted(p.name for p in output_dir.iterdir()), ["analysis.json"]) + self.assertEqual((output_dir / "analysis.json").read_text(), "previous") (output_dir / "analysis.json").write_text("ok", encoding="utf-8") return "human-readable CLI output" @@ -230,8 +235,10 @@ def fake_run(_args, output_dir): self.assertIn(f"analysis_path={out_dir / 'analysis.json'}", stdout.getvalue()) self.assertFalse(stale_artifact.exists()) - def _full_over_saved_state(self, *extra: str) -> tuple[Path, list[str]]: - """Runs mode full over a state holding a static-analysis cache and other output.""" + def _full_over_saved_state(self, *extra: str) -> tuple[list[str], list[str]]: + """Runs mode full over a state holding a previous analysis and other output. + + Returns what the engine found in the output directory and its command.""" temp = tempfile.TemporaryDirectory() self.addCleanup(temp.cleanup) root = Path(temp.name) @@ -241,31 +248,30 @@ def _full_over_saved_state(self, *extra: str) -> tuple[Path, list[str]]: for name in ("static_analysis.pkl", "static_analysis.sha", "analysis.json", "fingerprint.json", "logs/run.log"): (out_dir / name).write_text("saved", encoding="utf-8") commands: list[list[str]] = [] + seen: list[str] = [] def fake_run(args, output_dir): commands.append(args) - self.assertFalse((output_dir / "analysis.json").exists(), "the old analysis reached the engine") + seen.extend(sorted(str(p.relative_to(output_dir)) for p in output_dir.rglob("*"))) (output_dir / "analysis.json").write_text("ok", encoding="utf-8") return "" with patch("sys.stdout", io.StringIO()), patch.object(ar, "_run_command", side_effect=fake_run): ar.main(["full", "--checkout", str(checkout), "--output-dir", str(out_dir), "--depth-cap", "2", *extra]) - return out_dir, commands[0] + return seen, commands[0] - def test_a_full_run_keeps_the_static_analysis_cache(self) -> None: - # Core refuses a cache from another cache version itself, so the action never judges it. - out_dir, command = self._full_over_saved_state() + def test_a_full_run_keeps_the_previous_analysis_and_the_static_analysis_cache(self) -> None: + # Core keeps the names of the components it continues, and refuses a cache from + # another cache version itself, so the action never judges either. + seen, command = self._full_over_saved_state() - self.assertEqual( - sorted(p.name for p in out_dir.iterdir()), ["analysis.json", "static_analysis.pkl", "static_analysis.sha"] - ) - self.assertEqual((out_dir / "static_analysis.pkl").read_text(encoding="utf-8"), "saved") + self.assertEqual(seen, ["analysis.json", "static_analysis.pkl", "static_analysis.sha"]) self.assertNotIn("--force", command) def test_a_fresh_full_run_starts_from_nothing(self) -> None: - out_dir, command = self._full_over_saved_state("--fresh") + seen, command = self._full_over_saved_state("--fresh") - self.assertEqual(sorted(p.name for p in out_dir.iterdir()), ["analysis.json"]) + self.assertEqual(seen, []) self.assertIn("--force", command) def test_old_depth_input_is_rejected(self) -> None: diff --git a/tests/test_baseline_branch.py b/tests/test_baseline_branch.py index eebc855b..56c70971 100644 --- a/tests/test_baseline_branch.py +++ b/tests/test_baseline_branch.py @@ -314,6 +314,7 @@ def setUp(self) -> None: json.dumps({"metadata": {"depth_cap": 2}, "components": [source]}), encoding="utf-8" ) (board / "static_analysis.pkl").write_text("pickle", encoding="utf-8") + (board / "static_analysis.sha").write_text("a" * 40, encoding="utf-8") (board / "source.json").write_text(json.dumps({"schema": 1, "source_sha": source}), encoding="utf-8") git(work, "add", "-A") git( @@ -434,6 +435,15 @@ def test_an_entry_made_under_another_configuration_is_never_reused(self) -> None self.assertEqual(self._modes(), ["full", "incremental"]) + def test_after_a_release_the_full_analysis_keeps_the_names_of_the_old_configuration(self) -> None: + # Its names and static analysis carry over; the rest of its engine state does not. + self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="othercfg") + + [base] = [json.loads(line) for line in self.engine_log.read_text().splitlines() if '-base"' in line] + self.assertEqual((base["mode"], base["seed"]), ("full", [self.shas[3]])) + self.assertEqual(base["files"], ["analysis.json", "static_analysis.pkl", "static_analysis.sha"]) + self.assertIn("Component names carry over", self.log) + def test_without_a_configuration_hash_no_entry_is_trusted(self) -> None: self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="") @@ -495,7 +505,9 @@ def test_sync_replaces_the_committed_state_with_the_branch_tip(self) -> None: state = Path(values["analysis_dir"]) self.assertEqual((state / "static_analysis.pkl").read_text(), "pickle", "the tip's engine state") - self.assertFalse((state / "static_analysis.sha").exists(), "a generated file from the old baseline") + self.assertEqual( + (state / "static_analysis.sha").read_text(), "a" * 40, "a generated file from the old baseline" + ) self.assertFalse((state / "source.json").exists()) self.assertEqual((state / ".codeboardingignore").read_text(), "docs/\n") @@ -504,6 +516,13 @@ def test_sync_does_not_continue_from_a_tip_made_under_another_configuration(self self.assertEqual(self._modes(), ["full"]) + def test_sync_after_a_release_keeps_the_names_of_the_old_configuration(self) -> None: + self._analyze(ANALYSIS_KIND="sync", CFG_HASH="othercfg") + + [call] = [json.loads(line) for line in self.engine_log.read_text().splitlines()] + self.assertEqual((call["mode"], call["seed"]), ("full", [self.shas[3]])) + self.assertEqual(call["files"], ["analysis.json", "static_analysis.pkl", "static_analysis.sha"]) + def test_sync_refuses_to_run_on_the_analysis_branch(self) -> None: # Committing an analysis onto it, code-branch style, would make it unreadable as one. analysis_checkout = self.root / "analysis-checkout" From c2fe3a0678ef27e14a2be26379bc92cdcd7c416c Mon Sep 17 00:00:00 2001 From: Svilen Stefanov Date: Sat, 14 Nov 2026 19:49:28 +0100 Subject: [PATCH 2/2] 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 --- action.yml | 13 ++-- scripts/action/codeboarding-baseline.sh | 23 +++++-- scripts/action/fetch-state.sh | 9 +-- scripts/action/seed-sources.sh | 9 +-- scripts/analyze_repository.py | 10 ++- tests/test_action_state.py | 90 ++++++++++++++++++++----- tests/test_analyze_repository.py | 14 ++++ tests/test_baseline_branch.py | 25 +++++++ tests/test_merge_base_contract.py | 6 +- 9 files changed, 159 insertions(+), 40 deletions(-) diff --git a/action.yml b/action.yml index b94e4d87..c62a8cb6 100644 --- a/action.yml +++ b/action.yml @@ -486,11 +486,12 @@ runs: # Both lookups are best effort. Without them the run derives the base from # the committed baseline and the head from the base, which is what every run - # did before any of this existed. A fresh run skips them, so no old base id - # reaches the review artifact either. + # did before any of this existed. A fresh run passes no name, so no old base + # id reaches the review artifact either, while fetch-state.sh still clears + # what an earlier use of the action in this job left there. - name: Fetch published base analysis id: fetch_base - 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' + if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.base_name != '' && github.server_url == 'https://github.com' continue-on-error: true shell: bash env: @@ -498,7 +499,7 @@ runs: GH_ENTERPRISE_TOKEN: ${{ inputs.github_token }} GH_HOST: ${{ github.server_url }} REPOSITORY: ${{ github.repository }} - ARTIFACT_NAME: ${{ steps.state.outputs.base_name }} + ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.base_name || '' }} DEST: ${{ runner.temp }}/cb-state/${{ github.action }}/base # Must match the review retention below: a review references a base by # id for its whole life, so a base with less than that left is renewed. @@ -506,7 +507,7 @@ runs: run: "$GITHUB_ACTION_PATH/scripts/action/fetch-state.sh" - name: Fetch this pull request's last analysis - 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' + if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.state.outputs.warmstart_name != '' && github.server_url == 'https://github.com' continue-on-error: true shell: bash env: @@ -514,7 +515,7 @@ runs: GH_ENTERPRISE_TOKEN: ${{ inputs.github_token }} GH_HOST: ${{ github.server_url }} REPOSITORY: ${{ github.repository }} - ARTIFACT_NAME: ${{ steps.state.outputs.warmstart_name }} + ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.warmstart_name || '' }} DEST: ${{ runner.temp }}/cb-state/${{ github.action }}/warmstart run: "$GITHUB_ACTION_PATH/scripts/action/fetch-state.sh" diff --git a/scripts/action/codeboarding-baseline.sh b/scripts/action/codeboarding-baseline.sh index 62211b8c..25c46add 100644 --- a/scripts/action/codeboarding-baseline.sh +++ b/scripts/action/codeboarding-baseline.sh @@ -16,9 +16,18 @@ baseline_fail() { exit 1 } +# Reading an entry made under any configuration (CFG_HASH='*') is best effort: it +# only lends names and static analysis, so a failure falls through to the next +# source the way a missing artifact does. Under this run's configuration it fails +# the run. Callers return 1 after it. +baseline_read_fail() { + [ "${CFG_HASH:-}" = "*" ] || baseline_fail "$1" + echo "::warning::$1 Not taking anything from it." +} + # Returns 0 when branch $2 (BASELINE_BRANCH by default) exists in repository $1 and # 2 when it does not. Any other failure is retried once, so one network blip does -# not fail the run. +# not fail the run, and then returns 1 if baseline_read_fail lets it. baseline_branch_exists() { local repository="$1" branch="${2:-$BASELINE_BRANCH}" auth status auth="$(printf 'x-access-token:%s' "${GIT_TOKEN:-}" | base64 -w0)" @@ -31,7 +40,8 @@ baseline_branch_exists() { esac [ "$attempt" = 2 ] || sleep "${BASELINE_RETRY_DELAY:-2}" done - baseline_fail "Could not read $branch from $repository. Check that github_token can read the repository." + baseline_read_fail "Could not read $branch from $repository. Check that github_token can read the repository." + return 1 } # ---- Reading ---- @@ -68,15 +78,16 @@ restore_codeboarding_baseline() { echo "::notice::$BASELINE_BRANCH does not exist yet; the first sync creates it." return 1 fi + [ "$status" = 0 ] || return 1 index="$(codeboarding_baseline_index "$repository")" || - baseline_fail "Could not fetch $BASELINE_BRANCH from $repository." + { baseline_read_fail "Could not fetch $BASELINE_BRANCH from $repository."; return 1; } if [ -z "$index" ]; then # A code branch, as when a review into another branch names the one that holds the analysis. echo "::warning::$BASELINE_BRANCH is not a CodeBoarding analysis branch, so its analysis is not read here." return 1 fi fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" || - baseline_fail "Could not fetch the history of ${tip:0:7} to look for its analysis." + { baseline_read_fail "Could not fetch the history of ${tip:0:7} to look for its analysis."; return 1; } for commit in $(git -C "$CHECKOUT_DIR" rev-list --first-parent --max-count=$(( CATCHUP_BOUND + 1 )) "$tip" 2>/dev/null); do entry="$(awk -v source="$commit" -v cfg="$CFG_HASH" '$2 == source && (cfg == "*" || $3 == cfg) {print $1; exit}' <<< "$index")" [ -z "$entry" ] || break @@ -88,11 +99,11 @@ restore_codeboarding_baseline() { fi # The entry is known to exist from here on, so failing to read it is an error. fetch_commit "$repository" "$entry" || - baseline_fail "Could not fetch the analysis ${entry:0:7} from $BASELINE_BRANCH." + { baseline_read_fail "Could not fetch the analysis ${entry:0:7} from $BASELINE_BRANCH."; return 1; } rm -rf "$dest" mkdir -p "$dest" git -C "$CHECKOUT_DIR" archive "$entry" .codeboarding | tar -x -C "$dest" || - baseline_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH." + { baseline_read_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH."; return 1; } # source.json is provenance, not engine state. rm -f "$dest/.codeboarding/source.json" BASELINE_SOURCE="$commit" diff --git a/scripts/action/fetch-state.sh b/scripts/action/fetch-state.sh index cf16a1e3..ad4985ce 100755 --- a/scripts/action/fetch-state.sh +++ b/scripts/action/fetch-state.sh @@ -3,12 +3,13 @@ # Best effort: a miss, an expired bundle or a token without actions:read all # leave the directory absent, and the caller derives from the base instead. set -euo pipefail -[ -n "${ARTIFACT_NAME:-}" ] || exit 0 -# Clear first, on every path. These destinations are fixed, so a second use of -# the action in one job would otherwise inherit the first one's files and treat -# them as this configuration's state. +# Clear first, on every path, a fresh run's empty name included. These +# destinations are fixed, so a second use of the action in one job would +# otherwise inherit the first one's files and treat them as this configuration's +# state. rm -rf "${DEST:?}" +[ -n "${ARTIFACT_NAME:-}" ] || exit 0 api() { gh api -H 'Accept: application/vnd.github+json' "$@"; } export GH_HOST="${GH_HOST#*://}" diff --git a/scripts/action/seed-sources.sh b/scripts/action/seed-sources.sh index 698c1d0d..36bafdc3 100644 --- a/scripts/action/seed-sources.sh +++ b/scripts/action/seed-sources.sh @@ -93,10 +93,11 @@ reuse_other_configuration() { SEED_NAMES_ONLY=true echo "::notice::Component names carry over from the analysis of ${source:0:7}, saved under another configuration." fi - # Core keeps a cached file whatever today's ignore rules say, so a cache made - # under other rules would keep newly ignored files and miss newly analysed ones. - if { [ -e "$seed/.codeboardingignore" ] || [ -e "$state/.codeboardingignore" ]; } && - ! cmp -s "$seed/.codeboardingignore" "$state/.codeboardingignore"; then + # Core reads the ignore rules from the checkout and keeps a cached file whatever + # they say later, so a cache made under other rules would keep newly ignored + # files and miss newly analysed ones. The two commits say which rules applied; + # the bundle cannot, since uploads drop dotfiles. + if ! git -C "$CHECKOUT_DIR" diff --quiet "$source" "$tip" -- .codeboarding/.codeboardingignore; then echo "::notice::The saved static analysis of ${source:0:7} was made with another .codeboardingignore; not reusing it." return 0 fi diff --git a/scripts/analyze_repository.py b/scripts/analyze_repository.py index 8362cd9c..775ecab6 100755 --- a/scripts/analyze_repository.py +++ b/scripts/analyze_repository.py @@ -4,6 +4,7 @@ from __future__ import annotations import argparse +import hashlib import json import os import shutil @@ -168,9 +169,14 @@ def main(argv: list[str] | None = None) -> int: command = [PROG, "full", "--local", str(checkout), "--output-dir", str(output_dir), "--depth-cap", args.depth_cap] if args.fresh: command.append("--force") - _run_command(command, output_dir) analysis_path = output_dir / "analysis.json" - if not analysis_path.is_file(): + # Core reads the previous analysis from where it writes the new one, so the file + # being there proves nothing; its content changing does, since every analysis + # Core writes carries its own generated_at. An untouched one would publish the + # previous analysis, maybe another configuration's, as this run's. + previous = hashlib.sha256(analysis_path.read_bytes()).digest() if analysis_path.is_file() else None + _run_command(command, output_dir) + if not analysis_path.is_file() or hashlib.sha256(analysis_path.read_bytes()).digest() == previous: raise AnalysisError(f"Full analysis did not produce: {analysis_path}") print(f"analysis_mode=full") print("requires_full_analysis=false") diff --git a/tests/test_action_state.py b/tests/test_action_state.py index b5187f2a..da4b5f7e 100644 --- a/tests/test_action_state.py +++ b/tests/test_action_state.py @@ -54,7 +54,7 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st ENGINE_STUB = '''#!/usr/bin/env python3 """CodeBoarding CLI stand-in: records each call and writes a minimal analysis.""" -import json, os, sys +import datetime, json, os, sys argv = sys.argv[1:] 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 sys.exit(0) if argv[0] == "full": metadata = {"depth_cap": int(argv[argv.index("--depth-cap") + 1])} +# Like Core, so no two analyses it writes are alike. +metadata["generated_at"] = datetime.datetime.now(datetime.timezone.utc).isoformat() with open(analysis, "w") as handle: json.dump({"metadata": metadata, "components": [], "components_relations": []}, handle) 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: self.assertEqual([p.name for p in state.iterdir()], ["analysis.json"]) self.assertNotIn("Reusing", self.log) - def test_static_analysis_made_with_other_ignore_rules_is_not_used(self) -> None: - # Core keeps cached files whatever the current rules say, so a mismatch would persist. + def _publish(self, staged: Path) -> None: + """Writes the bundle as action.yml's base uploads publish a staged state: + upload-artifact leaves dotfiles out unless the step sets include-hidden-files.""" import zipfile - shas = self._history(2) - self._add_static_sha() - with zipfile.ZipFile(self.bundle, "a") as archive: - archive.writestr(".codeboardingignore", "docs/\n") - self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[-1]}")]) + steps = (ROOT / "action.yml").read_text(encoding="utf-8").split("\n - name:") + uploads = [s for s in steps if "actions/upload-artifact@" in s and "/out/base" in s] + self.assertTrue(uploads) + hidden = all("include-hidden-files: true" in s for s in uploads) + with zipfile.ZipFile(self.bundle, "w") as archive: + for path in sorted(staged.rglob("*")): + relative = path.relative_to(staged) + if path.is_file() and (hidden or not any(part.startswith(".") for part in relative.parts)): + archive.write(path, relative.as_posix()) - for ignore in (None, "tests/\n"): - with self.subTest(ignore=ignore): - state = self._reuse_static(shas[-1], ignore) - self.assertFalse((state / "static_analysis.pkl").exists()) - self.assertIn("made with another .codeboardingignore", self.log) + def test_static_analysis_is_reused_only_under_the_ignore_rules_it_was_made_with(self) -> None: + # Core keeps cached files whatever the current rules say, so a mismatch would persist. + git = ["git", "-C", str(self.origin), "-c", "user.name=T", "-c", "user.email=t@example.com"] + ignore_file = self.origin / ".codeboarding" / ".codeboardingignore" + + def commit(ignore: str | None) -> str: + ignore_file.parent.mkdir(exist_ok=True) + if ignore is None: + ignore_file.unlink() + else: + ignore_file.write_text(ignore, encoding="utf-8") + subprocess.run([*git, "add", "-A"], check=True) + subprocess.run([*git, "-c", "commit.gpgsign=false", "commit", "-q", "--allow-empty", "-m", "c"], check=True) + return subprocess.check_output([*git, "rev-parse", "HEAD"], text=True).strip() + + self._history(1) + saved, same_rules, rules_removed = commit("docs/\n"), commit("docs/\n"), commit(None) + # The base saved for `saved`, staged as keep_user_config and the analysis left it. + staged = self.root / "staged" + staged.mkdir() + for name, content in ( + ("analysis.json", json.dumps({"metadata": {"depth_cap": 2}, "components": []})), + ("static_analysis.pkl", "pickle"), + ("static_analysis.sha", "a" * 40), + ("metadata.json", json.dumps({"kind": "base"})), + (".codeboardingignore", "docs/\n"), + ): + (staged / name).write_text(content, encoding="utf-8") + self._publish(staged) + self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{saved}")]) - state = self._reuse_static(shas[-1], "docs/\n") + state = self._reuse_static(same_rules, "docs/\n") self.assertTrue((state / "static_analysis.pkl").exists(), "the same rules must not block reuse") + state = self._reuse_static(rules_removed) + self.assertFalse((state / "static_analysis.pkl").exists()) + self.assertIn(f"The saved static analysis of {saved[:7]} was made with another .codeboardingignore", self.log) + def test_a_nearer_base_under_another_configuration_is_no_analysis_to_continue_from(self) -> None: shas = self._history(4) self._serve( @@ -1184,10 +1220,32 @@ def test_state_is_published_from_the_directory_the_analysis_stages(self) -> None def test_a_fresh_run_fetches_no_saved_analysis(self) -> None: # A fetched base's id would otherwise reach the review artifact whenever this # run's own base is not published, pairing two independently built graphs. - fetches = [s for s in self._steps() if s.get("run", "").endswith('fetch-state.sh"')] + # The steps still run, so what an earlier use of the action in this job + # fetched is cleared rather than continued from. + steps = (ROOT / "action.yml").read_text(encoding="utf-8").split("\n - name:") + fetches = [s for s in steps if 'fetch-state.sh"' in s] self.assertEqual(len(fetches), 2) for step in fetches: - self.assertIn("inputs.fresh_analysis != 'true'", step["if"], step["name"]) + condition = next(line for line in step.splitlines() if line.strip().startswith("if:")) + self.assertNotIn("fresh_analysis", condition) + self.assertIn("ARTIFACT_NAME: ${{ inputs.fresh_analysis != 'true' && steps.state.outputs.", step) + + def test_fetching_no_name_still_clears_the_destination(self) -> None: + with tempfile.TemporaryDirectory() as tmp: + dest = Path(tmp) / "base" + dest.mkdir() + (dest / "analysis.json").write_text("{}", encoding="utf-8") + + result = subprocess.run( + [str(SCRIPTS / "fetch-state.sh")], + env={"PATH": os.environ["PATH"], "ARTIFACT_NAME": "", "DEST": str(dest)}, + capture_output=True, + text=True, + check=False, + ) + + self.assertEqual(result.returncode, 0, result.stderr) + self.assertFalse(dest.exists(), "an earlier invocation's state survived") def test_a_fork_publishes_nothing_another_run_would_read(self) -> None: # A base graph is named for a commit, so every pull request forking there diff --git a/tests/test_analyze_repository.py b/tests/test_analyze_repository.py index 6fc3db4b..d3cc5f50 100644 --- a/tests/test_analyze_repository.py +++ b/tests/test_analyze_repository.py @@ -235,6 +235,20 @@ def fake_run(_args, output_dir): self.assertIn(f"analysis_path={out_dir / 'analysis.json'}", stdout.getvalue()) self.assertFalse(stale_artifact.exists()) + def test_a_full_run_that_leaves_the_previous_analysis_in_place_fails(self) -> None: + # The previous analysis stays for Core to carry names from, so the file being + # there proves nothing: publishing it would pass an old analysis off as this run's. + with tempfile.TemporaryDirectory() as tmp: + root = Path(tmp) + checkout, out_dir = root / "repo", root / "out" + checkout.mkdir() + out_dir.mkdir() + (out_dir / "analysis.json").write_text("previous", encoding="utf-8") + + with patch("sys.stdout", io.StringIO()), patch.object(ar, "_run_command", return_value=""): + with self.assertRaisesRegex(ar.AnalysisError, "did not produce"): + ar.main(["full", "--checkout", str(checkout), "--output-dir", str(out_dir), "--depth-cap", "2"]) + def _full_over_saved_state(self, *extra: str) -> tuple[list[str], list[str]]: """Runs mode full over a state holding a previous analysis and other output. diff --git a/tests/test_baseline_branch.py b/tests/test_baseline_branch.py index 56c70971..3ba706c6 100644 --- a/tests/test_baseline_branch.py +++ b/tests/test_baseline_branch.py @@ -444,6 +444,31 @@ def test_after_a_release_the_full_analysis_keeps_the_names_of_the_old_configurat self.assertEqual(base["files"], ["analysis.json", "static_analysis.pkl", "static_analysis.sha"]) self.assertIn("Component names carry over", self.log) + def _break_extraction(self) -> None: + tar = self.bin_dir / "tar" + tar.write_text("#!/bin/sh\nexit 1\n", encoding="utf-8") + tar.chmod(0o755) + + def test_an_entry_from_another_configuration_that_cannot_be_read_is_passed_over(self) -> None: + # It would only lend names and static analysis, so failing to read it is no + # reason to fail the run. + self._break_extraction() + + self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="othercfg") + + [base] = [json.loads(line) for line in self.engine_log.read_text().splitlines() if '-base"' in line] + self.assertEqual((base["mode"], base["seed"]), ("full", None)) + self.assertIn("Could not extract the analysis", self.log) + self.assertNotIn("failure_reason", self._outputs()) + + def test_an_entry_of_this_configuration_that_cannot_be_read_fails_the_run(self) -> None: + self._break_extraction() + + self._analyze(False, REVIEW_BASE_SHA=self.shas[3]) + + self.assertIn("Could not extract the analysis", self._outputs().get("failure_reason", "")) + self.assertEqual(self._modes(), []) + def test_without_a_configuration_hash_no_entry_is_trusted(self) -> None: self._analyze(REVIEW_BASE_SHA=self.shas[3], CFG_HASH="") diff --git a/tests/test_merge_base_contract.py b/tests/test_merge_base_contract.py index 67c0f810..7a31ee4f 100644 --- a/tests/test_merge_base_contract.py +++ b/tests/test_merge_base_contract.py @@ -69,7 +69,7 @@ def git(*args): ENGINE_STUB = '''#!/usr/bin/env python3 """Minimal CodeBoarding CLI stand-in recording which tree it was given.""" -import json, os, subprocess, sys +import datetime, json, os, subprocess, sys argv = sys.argv[1:] checkout = argv[argv.index("--local") + 1] @@ -84,7 +84,9 @@ def git(*args): }) + "\\n") analysis = os.path.join(output, "analysis.json") with open(analysis, "w") as handle: - json.dump({"metadata": {"depth_level": 2}, "components": [], "components_relations": []}, handle) + # Like Core, so no two analyses it writes are alike. + metadata = {"depth_level": 2, "generated_at": datetime.datetime.now(datetime.timezone.utc).isoformat()} + json.dump({"metadata": metadata, "components": [], "components_relations": []}, handle) print(json.dumps({"requiresFullAnalysis": False, "analysis_path": analysis})) '''