diff --git a/README.md b/README.md index 192cc71..0463932 100644 --- a/README.md +++ b/README.md @@ -117,6 +117,8 @@ 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. + 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. ### Pull requests that change nothing analysed @@ -403,6 +405,7 @@ The update runs after the baseline is saved, stops itself after 10 minutes and n | `agent_model` | both | empty | Analysis-only override for `model`. | | `parsing_model` | both | empty | Parsing-only override for `model`. | | `depth_cap` | both | `2` | Positive integer maximum analysis depth, including full-analysis fallbacks. Changing it rebuilds incompatible state. | +| `fresh_analysis` | both | `false` | With `'true'`, the run reuses nothing saved: every analysis is built from scratch, static analysis included. A fresh review republishes only the base analysis at its merge base; later syncs still continue from what the analysis branch holds. See [Reused analysis](#reused-analysis). | | `github_token` | both | `${{ github.token }}` | Token for comments and sync delivery. | | `codeboarding_analysis_location` | both | `codeboarding_branch` | Where the synced branch's analysis is stored: `codeboarding_branch` (on `codeboarding/baseline`, for the default branch; the code is never written) or `in_place` (committed as `.codeboarding/` on the synced branch). Sync saves it there and review reads it from there. | | `warmstart_retention_days` | review | `1` | Days to keep the reusable analysis. Only the next run reads it. | diff --git a/action.yml b/action.yml index 9009995..c7a060d 100644 --- a/action.yml +++ b/action.yml @@ -143,6 +143,15 @@ inputs: description: 'Positive integer analysis depth cap, used for both review and sync, including full fallbacks.' required: false default: '2' + fresh_analysis: + description: >- + When 'true', this run reuses nothing saved: every analysis, its static analysis + 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. + required: false + default: 'false' review_guide_url: description: >- Experimental. Service base URL for POST /v1/review-guide and POST /v1/architecture. @@ -477,7 +486,9 @@ 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. + # 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' @@ -488,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. @@ -504,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" @@ -525,6 +536,7 @@ runs: GH_HOST: ${{ github.server_url }} GITHUB_SERVER_URL: ${{ github.server_url }} DEPTH_CAP: ${{ inputs.depth_cap }} + FRESH_ANALYSIS: ${{ inputs.fresh_analysis }} MODEL: ${{ inputs.model }} AGENT_MODEL_INPUT: ${{ inputs.agent_model }} PARSING_MODEL_INPUT: ${{ inputs.parsing_model }} @@ -639,6 +651,7 @@ runs: GIT_TOKEN: ${{ inputs.github_token }} GITHUB_SERVER_URL: ${{ github.server_url }} DEPTH_CAP: ${{ inputs.depth_cap }} + FRESH_ANALYSIS: ${{ inputs.fresh_analysis }} MODEL: ${{ inputs.model }} AGENT_MODEL_INPUT: ${{ inputs.agent_model }} PARSING_MODEL_INPUT: ${{ inputs.parsing_model }} @@ -662,6 +675,7 @@ runs: ENGINE_VERSION: ${{ steps.state.outputs.engine_version }} CFG_HASH: ${{ steps.state.outputs.cfg_hash }} DEPTH_CAP: ${{ inputs.depth_cap }} + FRESH_ANALYSIS: ${{ inputs.fresh_analysis }} MODEL: ${{ inputs.model }} AGENT_MODEL_INPUT: ${{ inputs.agent_model }} PARSING_MODEL_INPUT: ${{ inputs.parsing_model }} diff --git a/scripts/action/analysis-state.sh b/scripts/action/analysis-state.sh index 0e10b05..e291788 100644 --- a/scripts/action/analysis-state.sh +++ b/scripts/action/analysis-state.sh @@ -6,6 +6,11 @@ if [[ ! "$DEPTH_CAP" =~ ^[1-9][0-9]*$ ]]; then echo "::error::depth_cap must be a positive integer." exit 1 fi +FRESH_ANALYSIS="${FRESH_ANALYSIS:-false}" +if [[ ! "$FRESH_ANALYSIS" =~ ^(true|false)$ ]]; then + echo "::error::fresh_analysis must be true or false." + exit 1 +fi # Metadata is only a compatibility check, never the source of configuration. # Legacy baselines without a configured cap are rebuilt at the requested depth. diff --git a/scripts/action/analyze.sh b/scripts/action/analyze.sh index bfff9fd..58cd8fb 100755 --- a/scripts/action/analyze.sh +++ b/scripts/action/analyze.sh @@ -28,8 +28,14 @@ run_core() { ANALYSIS_PATH="$(awk -F= '$1 == "analysis_path" {print substr($0, index($0, "=") + 1); exit}' <<< "$output")" } +# fresh_analysis builds from nothing, the static-analysis cache included. Not the +# head: it still grows from the base this run built, since a head analyzed apart +# from its base need not name components alike and would diff as changes. +fresh="" +if [ "${FRESH_ANALYSIS:-false}" = true ] && [ "$role" != head ]; then fresh=--fresh; fi + mkdir -p "$state" -if compatible_state "$state"; then +if [ -z "$fresh" ] && 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." @@ -37,7 +43,7 @@ if compatible_state "$state"; then [ "$REQUIRES_FULL" != true ] || echo "::notice::Core cannot update the $role analysis incrementally; analyzing in full." >&2 fi if [ "$ANALYSIS_MODE" != incremental ] || [ "$REQUIRES_FULL" = true ]; then - run_core full --depth-cap "$DEPTH_CAP" + run_core full --depth-cap "$DEPTH_CAP" ${fresh:+"$fresh"} if [ "$ANALYSIS_MODE" != full ] || [ ! -f "$ANALYSIS_PATH" ]; then fail "Core returned an invalid full-analysis result." fi diff --git a/scripts/action/fetch-state.sh b/scripts/action/fetch-state.sh index cf16a1e..ad4985c 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/find-ancestor-base.sh b/scripts/action/find-ancestor-base.sh index 1038796..1e2300f 100755 --- a/scripts/action/find-ancestor-base.sh +++ b/scripts/action/find-ancestor-base.sh @@ -3,12 +3,20 @@ # 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. +# # Prints ancestor_sha= on # stdout and everything else on stderr. # # Needs the tip's first-parent history in CHECKOUT_DIR, at least CATCHUP_BOUND deep. set -euo pipefail -: "${REPOSITORY:?}" "${CFG_HASH:?}" "${TIP_SHA:?}" "${CHECKOUT_DIR:?}" "${DEST:?}" +: "${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) +else + pattern="codeboarding-base-${CFG_HASH:?}-" needs=(analysis.json) +fi BOUND="${CATCHUP_BOUND:-100}" rm -rf "$DEST" @@ -21,18 +29,18 @@ report() { echo "ancestor_sha=$ancestor"; } 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; sync has no such -# lookup and passes INCLUDE_TIP=true. +# itself, which a review already looked up by its exact name under this +# configuration. Sync and STATIC_ONLY 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")" +# Prints the newest saved name for the nearest commit. nearest() { local commit for commit in $walk; do - if grep -qx "$commit" <<< "$saved"; then - echo "$commit" - return 0 - fi + grep -m1 -E "^$pattern$commit\$" <<< "$saved" && return 0 done + return 0 } # The artifact listing, newest first. Its name filter is exact, so a prefix needs @@ -50,26 +58,26 @@ trusted='.artifacts[]? | select(.workflow_run.head_repository_id == .workflow_run.repository_id) | select(.name | startswith("codeboarding-base-")) | .name' -prefix="codeboarding-base-$CFG_HASH-" -saved="" page=1 +saved="" name="" page=1 while [ "$page" -le "${MAX_PAGES:-50}" ]; do if ! listing="$(api "repos/$REPOSITORY/actions/artifacts?per_page=100&page=$page" 2>/dev/null)"; then echo "::warning::Could not list artifacts in $REPOSITORY; not looking for an older saved analysis." >&2 exit 0 fi names="$(jq -r "$trusted" <<< "$listing" 2>/dev/null || true)" - saved="$saved$(grep "^$prefix" <<< "$names" | cut -c$(( ${#prefix} + 1 ))- || true)"$'\n' - ancestor="$(nearest)" - [ -z "$ancestor" ] || break + saved="$saved$names"$'\n' + name="$(nearest)" + [ -z "$name" ] || break returned="$(jq -r '.artifacts | length' <<< "$listing" 2>/dev/null || echo 0)" [ "${returned:-0}" -eq 100 ] || break page=$(( page + 1 )) done -[ -n "$ancestor" ] || exit 0 +[ -n "$name" ] || exit 0 # Downloaded by its exact name, through the same checks as every other lookup. -if ! GITHUB_OUTPUT="" ARTIFACT_NAME="$prefix$ancestor" DEST="$DEST" \ - "$(dirname "$0")/fetch-state.sh" >&2 || [ ! -f "$DEST/analysis.json" ]; then - ancestor="" -fi +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 +ancestor="${name##*-}" diff --git a/scripts/action/review-generate-baseline.sh b/scripts/action/review-generate-baseline.sh index bfe664e..a8b1bcf 100755 --- a/scripts/action/review-generate-baseline.sh +++ b/scripts/action/review-generate-baseline.sh @@ -8,6 +8,11 @@ # 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. +# # Outputs base_analysis_path, and publish_base when this run produced a base # other pull requests forking there should reuse. set -euo pipefail @@ -34,13 +39,18 @@ if compatible_state "${BASE_DIR:-}"; then else fetch_commit "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" git -C "$CHECKOUT_DIR" worktree add --detach "$checkout" "$REVIEW_BASE_SHA" >/dev/null - if from_codeboarding_baseline "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$state" "$checkout" || + seeded=false + if [ "$FRESH_ANALYSIS" = true ]; then + echo "::notice::fresh_analysis is set; analyzing ${REVIEW_BASE_SHA:0:7} from scratch." + keep_user_config "$checkout" "$state" + elif from_codeboarding_baseline "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$state" "$checkout" || from_committed_baseline "$checkout" "$state" || from_ancestor_artifact "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$state" false "$checkout"; then seeded=true else - seeded=false + 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" fi # Core never sees the token. unset GIT_TOKEN @@ -49,7 +59,6 @@ else elif [ "$seeded" = true ]; then "$ACTION_PATH/scripts/action/analyze.sh" base "$checkout" "$state" >/dev/null else - echo "::notice::No saved analysis to start from; analyzing ${REVIEW_BASE_SHA:0:7} from scratch." progress base "$ACTION_PATH/scripts/action/analyze.sh" base "$checkout" "$state" >/dev/null progress head diff --git a/scripts/action/seed-sources.sh b/scripts/action/seed-sources.sh index 6922d10..d624ccc 100644 --- a/scripts/action/seed-sources.sh +++ b/scripts/action/seed-sources.sh @@ -66,3 +66,31 @@ from_ancestor_artifact() { SEED_SHA="$ancestor" 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 + # 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." + return 0 + fi + if ! cp "$dest/static_analysis.pkl" "$dest/static_analysis.sha" "$state/"; 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}." +} diff --git a/scripts/action/sync-generate-baseline.sh b/scripts/action/sync-generate-baseline.sh index 88daf72..cf97adc 100755 --- a/scripts/action/sync-generate-baseline.sh +++ b/scripts/action/sync-generate-baseline.sh @@ -5,7 +5,10 @@ # 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 +# 4. nothing: analyzed from scratch, reusing the static analysis of the +# nearest saved base, made under any configuration +# +# With FRESH_ANALYSIS=true nothing saved is read. set -euo pipefail # shellcheck source=scripts/action/seed-sources.sh source "$(dirname "$0")/seed-sources.sh" @@ -21,11 +24,15 @@ if [ -n "$(git -C "$CHECKOUT_DIR" log -1 --format='%(trailers:key=CodeBoarding-S exit 1 fi -if ! from_codeboarding_baseline "${REPOSITORY:-}" "$head_sha" "$state" "$CHECKOUT_DIR" && +if [ "$FRESH_ANALYSIS" = true ]; then + echo "::notice::fresh_analysis is set; analyzing from scratch." + keep_user_config "$CHECKOUT_DIR" "$state" +elif ! from_codeboarding_baseline "${REPOSITORY:-}" "$head_sha" "$state" "$CHECKOUT_DIR" && ! from_committed_baseline "$CHECKOUT_DIR" "$state" && ! 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" fi # Core never sees the token. diff --git a/scripts/analyze_repository.py b/scripts/analyze_repository.py index 517d38b..bbfd122 100755 --- a/scripts/analyze_repository.py +++ b/scripts/analyze_repository.py @@ -13,6 +13,7 @@ from threading import Thread PROG = "codeboarding" +STATIC_CACHE = {"static_analysis.pkl", "static_analysis.sha"} # Shared with CodeBoarding/codeboarding_cli/main.py; keep the prefix in sync. ENGINE_ERROR_PREFIX = "CodeBoarding: " @@ -133,6 +134,9 @@ def main(argv: list[str] | None = None) -> int: parser.add_argument("--checkout", required=True, help="Path to repository checkout") parser.add_argument("--output-dir", required=True, help="Action-owned output directory") parser.add_argument("--depth-cap", help="Maximum hierarchy depth allowed for full analyses") + parser.add_argument( + "--fresh", action="store_true", help="Full analysis from nothing, not even the static-analysis cache" + ) args = parser.parse_args(argv) checkout = Path(args.checkout) @@ -151,19 +155,18 @@ def main(argv: list[str] | None = None) -> int: if not args.depth_cap: raise SystemExit("--depth-cap is required for mode=full") - shutil.rmtree(output_dir) - output_dir.mkdir(parents=True) - command = [ - PROG, - "full", - "--local", - str(checkout), - "--output-dir", - str(output_dir), - "--depth-cap", - args.depth_cap, - "--force", - ] + # 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. + for entry in output_dir.iterdir(): + if not args.fresh and entry.name in STATIC_CACHE: + continue + if entry.is_dir() and not entry.is_symlink(): + shutil.rmtree(entry) + else: + entry.unlink() + 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(): diff --git a/tests/test_action_inputs.py b/tests/test_action_inputs.py index 5fc96e7..5ff0372 100644 --- a/tests/test_action_inputs.py +++ b/tests/test_action_inputs.py @@ -71,6 +71,14 @@ def test_depth_is_wired_to_state_identity_and_both_analysis_modes(self) -> None: block = ACTION[start : ACTION.index("\n run:", start)] self.assertIn("DEPTH_CAP: ${{ inputs.depth_cap }}", block) + def test_fresh_analysis_is_off_by_default_and_reaches_every_analysis(self) -> None: + self.assertIn("default: 'false'", self.inputs["fresh_analysis"]) + for identifier in ("id: sync_analyze", "id: review_baseline", "id: review_analyze"): + start = ACTION.index(identifier) + block = ACTION[start : ACTION.index("\n run:", start)] + self.assertIn("FRESH_ANALYSIS: ${{ inputs.fresh_analysis }}", block) + self.assertIn("| `fresh_analysis` |", (ROOT / "README.md").read_text(encoding="utf-8")) + def test_default_workflow_reviews_drafts_on_open_and_new_commits(self) -> None: self.assertNotIn("github.event.pull_request.draft", DOGFOOD) start = DOGFOOD.index("types:") diff --git a/tests/test_action_state.py b/tests/test_action_state.py index 0a02ea4..1901621 100644 --- a/tests/test_action_state.py +++ b/tests/test_action_state.py @@ -65,6 +65,8 @@ def run_steps(env: dict[str, str], output: Path) -> tuple[int, str, dict[str, st "checkout": argv[argv.index("--local") + 1], "depth": argv[argv.index("--depth-cap") + 1] if "--depth-cap" in argv else None, "run_id": os.environ.get("CODEBOARDING_RUN_ID", ""), + "force": "--force" in argv, + "files": sorted(os.listdir(output)), }) + "\\n") analysis = os.path.join(output, "analysis.json") metadata = json.load(open(analysis))["metadata"] if os.path.isfile(analysis) else {} @@ -368,6 +370,52 @@ def test_base_and_head_fallbacks_keep_configured_depth(self) -> None: self.assertEqual([c["mode"] for c in calls], ["incremental", "full", "incremental", "full"]) self.assertEqual([c["depth"] for c in calls if c["mode"] == "full"], ["4", "4"]) + def test_a_fresh_review_starts_from_nothing_saved(self) -> None: + # action.yml fetches no published base or warm start; the committed baseline is skipped here. + sha = self._commit_base(cap=2) + + values = self._analyze(REVIEW_BASE_SHA=sha, FRESH_ANALYSIS="true") + + calls = self._engine_calls() + self.assertEqual([c["mode"] for c in calls], ["full", "incremental"]) + self.assertTrue(calls[0]["force"], "the base must not reuse the static-analysis cache") + self.assertEqual(calls[0]["files"], [], "the committed baseline seeded a fresh base") + # The head still grows from the base built here, or the diff would compare unrelated names. + self.assertFalse(calls[1]["force"]) + # Published, it replaces the base other reviews were reusing. + self.assertEqual(values["publish_base"], "true") + + def test_an_invalid_fresh_analysis_fails_before_analysis(self) -> None: + result = subprocess.run( + [str(STEPS["review"][0])], + env={"PATH": os.environ["PATH"], "FRESH_ANALYSIS": "yes"}, + capture_output=True, + text=True, + ) + self.assertNotEqual(result.returncode, 0) + self.assertIn("fresh_analysis must be true or false", result.stdout) + self.assertEqual(self._engine_calls(), []) + + def test_a_fresh_sync_skips_the_incremental_attempt(self) -> None: + self._commit_base(cap=2) + + self._analyze(ANALYSIS_KIND="sync", FRESH_ANALYSIS="true") + + calls = self._engine_calls() + self.assertEqual([c["mode"] for c in calls], ["full"]) + self.assertTrue(calls[0]["force"]) + self.assertEqual(calls[0]["files"], [], "a committed baseline seeded a fresh run") + + def test_a_full_run_keeps_the_static_analysis_cache(self) -> None: + sha = self._commit_base(cap=2) + + self._analyze(REVIEW_BASE_SHA=sha, CB_REQUIRE_FULL="true") + + 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"]) + # How the base was obtained is reported, not just used: the review comment and # the webview explain a slow run by it. @@ -852,6 +900,179 @@ 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. + + OTHER_CFG = "0123456789abcdef" + + def _add_static_sha(self) -> None: + import zipfile + + with zipfile.ZipFile(self.bundle, "a") as archive: + archive.writestr("static_analysis.sha", "a" * 40) + 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. + + `ignore` is the analysed commit's .codeboardingignore, as keep_user_config leaves it.""" + state = Path(tempfile.mkdtemp(dir=self.root)) + if ignore is not None: + (state / ".codeboardingignore").write_text(ignore, encoding="utf-8") + result = subprocess.run( + [ + "bash", + "-c", + 'source "$1" && reuse_static_analysis origin "$2" "$3"', + "_", + str(SCRIPTS / "seed-sources.sh"), + tip, + str(state), + ], + env={ + "PATH": f"{self.bin_dir}:{os.environ['PATH']}", + "RUNNER_TEMP": str(self.runner_temp), + "CB_GH_CONFIG": str(self.gh_config), + "CB_GH_LOG": str(self.gh_log), + "CHECKOUT_DIR": str(self.origin), + "REPOSITORY": "owner/repo", + "GITHUB_SERVER_URL": f"file://{self.root}", + "ANCESTOR_LOOKUP": "true", + }, + capture_output=True, + text=True, + check=False, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.log = result.stdout + result.stderr + return state + + def test_a_base_built_from_scratch_reuses_any_configurations_static_analysis(self) -> None: + shas = self._history(4) + self._add_static_sha() + self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[1]}")]) + + self._analyze(self.origin, REVIEW_BASE_SHA=shas[-1]) + + 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"]) + + def test_only_the_static_analysis_cache_is_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. + self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[-1]}")]) + + state = self._reuse_static(shas[-1]) + + self.assertEqual(sorted(p.name for p in state.iterdir()), ["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: + shas = self._history(2) + self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[-1]}")]) + + state = self._reuse_static(shas[-1]) + + self.assertEqual(list(state.iterdir()), []) + self.assertNotIn("Reusing", self.log) + + 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 + + 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()) + + 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(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( + [ + self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[2]}"), + self._artifact(f"codeboarding-base-cfg-{shas[0]}"), + ] + ) + + self._analyze(self.origin, REVIEW_BASE_SHA=shas[-1]) + + self.assertIn(f"Starting from the saved analysis of {shas[0][:7]}.", self.log) + + def test_static_analysis_saved_by_a_run_on_forked_code_is_never_read(self) -> None: + shas = self._history(3) + self._add_static_sha() + self._serve([self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[0]}", fork=True)]) + + self._analyze(self.origin, REVIEW_BASE_SHA=shas[-1]) + + self.assertNotIn("/zip", self.gh_log.read_text(), "a fork's bundle was downloaded") + self.assertNotIn("Reusing", self.log) + + def test_a_fresh_run_reads_no_saved_analysis(self) -> None: + shas = self._history(3) + self._add_static_sha() + self._serve( + [ + self._artifact(f"codeboarding-base-cfg-{shas[0]}"), + self._artifact(f"codeboarding-base-{self.OTHER_CFG}-{shas[1]}"), + ] + ) + + for kind in ("review", "sync"): + with self.subTest(kind=kind): + self._analyze(self.origin, ANALYSIS_KIND=kind, REVIEW_BASE_SHA=shas[-1], FRESH_ANALYSIS="true") + self.assertEqual(self.gh_log.read_text(), "") + modes = [(c["mode"], c["force"]) for c in map(json.loads, self.engine_log.read_text().splitlines())] + self.assertEqual(modes, [("full", True), ("incremental", False), ("full", True)]) + class ReviewArtifactTests(unittest.TestCase): """The artifact is the only channel a reader outside the run can use: cache @@ -989,6 +1210,36 @@ def test_state_is_published_from_the_directory_the_analysis_stages(self) -> None continue self.assertIn(path.rsplit("/", 1)[-1], staged, f"{step['name']} publishes an unstaged path") + 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. + # 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: + 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 # reads it; a fork's analysis must never be what they read. diff --git a/tests/test_analyze_repository.py b/tests/test_analyze_repository.py index b9969aa..49a74f5 100644 --- a/tests/test_analyze_repository.py +++ b/tests/test_analyze_repository.py @@ -230,6 +230,44 @@ 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.""" + temp = tempfile.TemporaryDirectory() + self.addCleanup(temp.cleanup) + root = Path(temp.name) + checkout, out_dir = root / "repo", root / "out" + checkout.mkdir() + (out_dir / "logs").mkdir(parents=True) + 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]] = [] + + def fake_run(args, output_dir): + commands.append(args) + self.assertFalse((output_dir / "analysis.json").exists(), "the old analysis reached the engine") + (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] + + 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() + + 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.assertNotIn("--force", command) + + def test_a_fresh_full_run_starts_from_nothing(self) -> None: + out_dir, command = self._full_over_saved_state("--fresh") + + self.assertEqual(sorted(p.name for p in out_dir.iterdir()), ["analysis.json"]) + self.assertIn("--force", command) + def test_old_depth_input_is_rejected(self) -> None: with patch("sys.stderr", io.StringIO()), patch.object(ar, "_run_command") as command: with self.assertRaises(SystemExit) as raised: