diff --git a/README.md b/README.md index 0463932..8cb0455 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 c7a060d..c62a8cb 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 58cd8fb..111e4ac 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 be19508..25c46ad 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 ---- @@ -51,13 +61,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 @@ -68,37 +78,48 @@ from_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 && $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" || - baseline_fail "Could not extract 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_read_fail "Could not extract the analysis ${entry:0:7} from $BASELINE_BRANCH."; return 1; } # 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 1e2300f..13be9f3 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 a8b1bcf..edae28f 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 d624ccc..36bafdc 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,30 +69,41 @@ 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 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 "$ancestor" "$tip" -- .codeboarding/.codeboardingignore; then - echo "::notice::The saved static analysis of ${ancestor:0:7} was made with another .codeboardingignore; not reusing it." + 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 - 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 cf97adc..1a5a443 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 bbfd122..775ecab 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 @@ -13,7 +14,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 +156,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) @@ -167,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 1901621..da4b5f7 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] @@ -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 {} @@ -75,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})) @@ -414,7 +418,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 +904,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 +918,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 +928,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 +962,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 +973,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 +984,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 _publish(self, staged: Path) -> None: diff --git a/tests/test_analyze_repository.py b/tests/test_analyze_repository.py index 49a74f5..d3cc5f5 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,24 @@ 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 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. + + 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 +262,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 eebc855..3ba706c 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,40 @@ 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 _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="") @@ -495,7 +530,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 +541,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" diff --git a/tests/test_merge_base_contract.py b/tests/test_merge_base_contract.py index 67c0f81..7a31ee4 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})) '''