Skip to content

Commit 3ca8dc1

Browse files
feat: catch up the review base from the nearest saved ancestor
With no saved base for the merge base and no usable baseline committed there, a review analyzed the merge base from scratch, even when a saved analysis of a commit a few steps below it was sitting in the artifact store. find-ancestor-base.sh walks the merge base's first-parent history up to 100 commits, deepening the shallow checkout, then pages through the artifact listing newest first, keeping base analyses for this configuration that a run on the repository's own code produced (the same provenance rule as fetch-state.sh). It stops at the first page holding a walked commit (50 pages at most; one or two calls in practice, and only on a run that would otherwise analyze from scratch). The nearest hit seeds an incremental catch-up to the merge base, which is then published under the merge base's own name. Order: exact artifact, committed at the merge base, nearest ancestor, full. Seeding keeps the merge base's own .codeboardingignore and health configuration. Reported as base_analysis_method=incremental, the reason naming the ancestor and the commits caught up. A saved analysis of the merge base under another configuration makes a full run's reason "incompatible"; anything else is "no usable analysis was available". There is no separate "too far behind" reason: proving it took compare API calls that only changed the wording. Sync uses the same lookup when the branch has no usable committed baseline, so the first sync after the setup pull request merges catches up from the base that pull request's review saved. FORCE_FULL is now lowercased with tr, which also runs on the bash 3.2 macOS ships. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 6b46ac3 commit 3ca8dc1

5 files changed

Lines changed: 470 additions & 13 deletions

File tree

‎action.yml‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,13 @@ runs:
451451
CHECKOUT_DIR: ${{ github.workspace }}/.codeboarding-target
452452
STAGE_DIR: ${{ runner.temp }}/cb-state/${{ github.action }}/out
453453
FORCE_FULL: ${{ inputs.force_full }}
454+
CFG_HASH: ${{ steps.state.outputs.cfg_hash }}
455+
# Lets a branch without a usable committed baseline catch up from a saved analysis.
456+
ANCESTOR_LOOKUP: ${{ github.server_url == 'https://github.com' && steps.state.outputs.cfg_hash != '' }}
457+
GIT_TOKEN: ${{ inputs.github_token }}
458+
REPOSITORY: ${{ github.repository }}
459+
GH_HOST: ${{ github.server_url }}
460+
GITHUB_SERVER_URL: ${{ github.server_url }}
454461
DEPTH_CAP: ${{ inputs.depth_cap }}
455462
MODEL: ${{ inputs.model }}
456463
AGENT_MODEL_INPUT: ${{ inputs.agent_model }}
@@ -539,6 +546,8 @@ runs:
539546
BASE_FETCH_SECONDS: ${{ steps.fetch_base.outputs.seconds }}
540547
WARMSTART_DIR: ${{ runner.temp }}/cb-state/${{ github.action }}/warmstart
541548
RENEW_BASE: ${{ steps.fetch_base.outputs.renew }}
549+
# Lets a branch without a usable committed baseline catch up from a saved analysis.
550+
ANCESTOR_LOOKUP: ${{ github.server_url == 'https://github.com' && steps.state.outputs.cfg_hash != '' }}
542551
# For rewriting the progress comment while a base is built from scratch.
543552
PROGRESS_HEADER: ${{ steps.guard.outputs.comment_id }}
544553
BASE_REF: ${{ steps.guard.outputs.base_ref }}

‎docs/COMMIT_STRATEGY.md‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ never fires.
7272
| `base_sha` | string | the base branch tip when the event fired — *not* what was compared against |
7373
| `kind` | string | always `review`, so a reader can tell this artifact from a base or warm-start bundle |
7474
| `analysed_files_changed` | string | analysed files whose content hash differs between base and head; `unknown` when the analyses cannot say |
75-
| `base_analysis_method` | string | how this run obtained the analysis of the merge base: `reused` (the merge base already had one: its saved artifact, or a committed baseline with nothing to catch up), `incremental` (an earlier commit's analysis updated to the merge base) or `full` (analyzed from scratch in this run). Whichever, the base graph is the merge base's own analysis |
75+
| `base_analysis_method` | string | how this run obtained the analysis of the merge base: `reused` (the merge base already had one: its saved artifact, or a committed baseline with nothing to catch up), `incremental` (an earlier commit's analysis, committed or saved, updated to the merge base) or `full` (analyzed from scratch in this run). Whichever, the base graph is the merge base's own analysis |
7676
| `base_analysis_reason` | string | the method in words, e.g. `updated the analysis of 9f8e7d6 to a1b2c3d, 4 commits caught up`; where an incremental run started and how far it caught up are detail here, present when known |
7777
| `base_seconds` | string | wall time spent obtaining the base, the artifact lookup included |
7878
| `head_seconds` | string | wall time of the head analysis |
@@ -107,15 +107,29 @@ them:
107107
|---|---|
108108
| the published `codeboarding-base-<cfg>-<merge_base>` artifact with a compatible depth cap | none |
109109
| no usable artifact — check out the merge base, seed from a compatible baseline committed there, catch up | one incremental, full if Core requires it |
110-
| no compatible committed baseline either | full analysis directly, at the configured `depth_cap` |
110+
| no compatible committed baseline either: the nearest `codeboarding-base-<cfg>-<sha>` artifact among the merge base's last 100 first-parent ancestors | one incremental from that commit to the merge base |
111+
| none within 100 commits either | full analysis directly, at the configured `depth_cap` |
111112

112113
A trusted run that computed the base publishes it, so the next pull request
113-
forking from that commit gets the first row. The review metadata reports the
114+
forking from that commit gets the first row; that includes a base caught up from
115+
an ancestor. The review metadata reports the
114116
row as `base_analysis_method` (`reused`, `incremental` or `full`) with a
115117
`base_analysis_reason`, and the review comment repeats both under the diagram,
116118
with measured times. While a base is computed, the progress
117119
comment says so in two steps, with the elapsed time and the reason.
118120

121+
The ancestor lookup walks the merge base's first-parent history, deepening the
122+
shallow checkout to 101 commits, then pages through the repository's artifacts
123+
newest first, keeping those named for this configuration and produced by a run on
124+
the repository's own code. It stops at the first page holding one of the walked
125+
commits (usually the first; at most 50 pages) and takes the nearest commit seen.
126+
The merge base's own `.codeboardingignore` and health configuration replace the
127+
seed's. A base caught up this way reports `incremental`, its reason naming the
128+
ancestor and the commits caught up. Sync uses the same lookup when the branch has
129+
no usable committed baseline, so the first sync after the setup pull request
130+
merges catches up from the base that pull request's review saved, instead of
131+
analyzing from scratch.
132+
119133
The configuration hash includes `depth_cap`. The workflow input controls depth
120134
for both fresh and fallback analyses; stored legacy depth values never override it.
121135

‎scripts/action/analyze.sh‎

Lines changed: 72 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,24 @@ analyze_sync() {
124124
rm -rf "$work"
125125
seed_state "$CHECKOUT_DIR" "$state"
126126

127-
if [ "${FORCE_FULL,,}" = true ] || [ "$(depth_cap_from "$state/analysis.json")" != "$DEPTH_CAP" ]; then
128-
full "$CHECKOUT_DIR" "$state" "$DEPTH_CAP"
129-
else
130-
incremental "$CHECKOUT_DIR" "$state"
131-
if [ "$REQUIRES_FULL" = true ]; then
132-
full "$CHECKOUT_DIR" "$state" "$DEPTH_CAP"
127+
REQUIRES_FULL=true
128+
if [ "$(printf '%s' "${FORCE_FULL:-false}" | tr '[:upper:]' '[:lower:]')" != true ]; then
129+
if [ "$(depth_cap_from "$state/analysis.json")" = "$DEPTH_CAP" ]; then
130+
incremental "$CHECKOUT_DIR" "$state"
131+
fi
132+
# A branch with no usable committed baseline, such as the first sync after the
133+
# setup pull request merged, catches up from that pull request's saved base.
134+
if [ "$REQUIRES_FULL" = true ] &&
135+
seed_from_ancestor "${REPOSITORY:-}" "$(git -C "$CHECKOUT_DIR" rev-parse HEAD 2>/dev/null || true)" "$state" true "$CHECKOUT_DIR"; then
136+
incremental "$CHECKOUT_DIR" "$state"
137+
[ "$REQUIRES_FULL" = true ] ||
138+
echo "::notice::Caught up from the saved analysis of $ANCESTOR_SHA instead of analyzing from scratch."
133139
fi
134140
fi
141+
unset GIT_TOKEN
142+
if [ "$REQUIRES_FULL" = true ]; then
143+
full "$CHECKOUT_DIR" "$state" "$DEPTH_CAP"
144+
fi
135145
# Sync already computes the graph every review of this branch compares against,
136146
# so publish it instead of making the first pull request recompute it.
137147
stage "$state" base
@@ -140,8 +150,9 @@ analyze_sync() {
140150
}
141151

142152
# How far below the merge base this run looks for the commit a saved analysis
143-
# describes. Past it, a catch-up count is reported as unknown.
144-
CATCHUP_BOUND=100
153+
# describes, and for a saved ancestor to catch up from. Past it, a catch-up count
154+
# is unknown and no ancestor is used. It is also the fetch depth.
155+
CATCHUP_BOUND="${CATCHUP_BOUND:-100}"
145156

146157
# A depth above 1 also deepens a commit the shallow checkout already holds.
147158
fetch_commit() {
@@ -209,6 +220,46 @@ catchup_count() {
209220
echo "$count"
210221
}
211222

223+
# Replaces $3 with the saved analysis of the nearest first-parent ancestor of $2
224+
# (or of $2 itself when $4 is true), keeping the user-authored configuration of
225+
# checkout $5. Sets ANCESTOR_SHA, or ANCESTOR_REASON=incompatible when the only
226+
# candidate was saved under another configuration or depth.
227+
ANCESTOR_SHA="" ANCESTOR_REASON=""
228+
seed_from_ancestor() {
229+
local repository="$1" tip="$2" state="$3" include_tip="$4" config_from="$5" found dest="$RUNNER_TEMP/codeboarding-ancestor"
230+
ANCESTOR_SHA="" ANCESTOR_REASON=""
231+
[ "${ANCESTOR_LOOKUP:-false}" = true ] && [ -n "${CFG_HASH:-}" ] && [ -n "${REPOSITORY:-}" ] && [ -n "$tip" ] || return 1
232+
fetch_commit "$repository" "$tip" "$(( CATCHUP_BOUND + 1 ))" || true
233+
found="$(GH_TOKEN="${GIT_TOKEN:-}" GH_ENTERPRISE_TOKEN="${GIT_TOKEN:-}" TIP_SHA="$tip" DEST="$dest" \
234+
INCLUDE_TIP="$include_tip" CATCHUP_BOUND="$CATCHUP_BOUND" \
235+
"$ACTION_PATH/scripts/action/find-ancestor-base.sh" || true)"
236+
ANCESTOR_SHA="$(awk -F= '$1 == "ancestor_sha" {print $2; exit}' <<< "$found")"
237+
if [ -z "$ANCESTOR_SHA" ]; then
238+
! grep -qx 'other_cfg_at_tip=true' <<< "$found" || ANCESTOR_REASON=incompatible
239+
return 1
240+
fi
241+
if [ "$(depth_cap_from "$dest/analysis.json")" != "$DEPTH_CAP" ]; then
242+
ANCESTOR_SHA="" ANCESTOR_REASON=incompatible
243+
return 1
244+
fi
245+
rm -rf "$state"
246+
cp -a "$dest" "$state"
247+
keep_user_config "$config_from" "$state"
248+
}
249+
# What the user writes under .codeboarding/ belongs to the commit being analysed,
250+
# not to whichever analysis seeded it.
251+
USER_CONFIG=(.codeboardingignore health/health_config.json health/.healthignore)
252+
keep_user_config() {
253+
local checkout="$1" state="$2" file
254+
for file in "${USER_CONFIG[@]}"; do
255+
rm -f "${state:?}/$file"
256+
[ ! -f "$checkout/.codeboarding/$file" ] || {
257+
mkdir -p "$(dirname "$state/$file")"
258+
cp "$checkout/.codeboarding/$file" "$state/$file"
259+
}
260+
done
261+
}
262+
212263
# Rewrites the sticky progress comment while the base is built from scratch. A
213264
# fork's read-only token makes every call fail, which costs nothing.
214265
PROGRESS_PID=""
@@ -291,9 +342,20 @@ analyze_review() {
291342
elif [ -f "$base_state/analysis.json" ]; then
292343
full_cause=incompatible
293344
fi
345+
# Nothing at the merge base to grow from: catch up from the nearest saved
346+
# ancestor, and publish the result under the merge base's own name below.
347+
if [ "$REQUIRES_FULL" = true ] && seed_from_ancestor "$REVIEW_BASE_REPO" "$REVIEW_BASE_SHA" "$base_state" false "$base_checkout"; then
348+
incremental "$base_checkout" "$base_state"
349+
if [ "$REQUIRES_FULL" = true ]; then
350+
full_cause=incompatible
351+
else
352+
base_method=incremental base_from_sha="$ANCESTOR_SHA"
353+
catchup_commits="$(catchup_count "$ANCESTOR_SHA" "$REVIEW_BASE_SHA")"
354+
fi
355+
fi
294356
if [ "$REQUIRES_FULL" = true ]; then
295357
base_method=full
296-
full_cause="${full_cause:-no_baseline}"
358+
full_cause="${full_cause:-${ANCESTOR_REASON:-no_baseline}}"
297359
trap progress_stop EXIT
298360
progress_start "$base_started"
299361
full "$base_checkout" "$base_state" "$DEPTH_CAP"
@@ -365,7 +427,7 @@ base_reason() {
365427
else
366428
reason="updated the analysis of ${from:0:7} to $base"
367429
case "$count" in
368-
'') ;;
430+
'' | 0) ;;
369431
1) reason="$reason, 1 commit caught up" ;;
370432
*) reason="$reason, $count commits caught up" ;;
371433
esac
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env bash
2+
# Finds the nearest first-parent ancestor of TIP_SHA with a saved base analysis
3+
# under this configuration and downloads it into DEST, so a run without an exact
4+
# base can catch up from it instead of analyzing from scratch. Best effort.
5+
#
6+
# Prints key=value lines on stdout and everything else on stderr:
7+
# ancestor_sha the commit whose analysis is now in DEST, or empty
8+
# other_cfg_at_tip true when TIP_SHA has a saved analysis under another configuration
9+
#
10+
# Needs the tip's first-parent history in CHECKOUT_DIR, at least CATCHUP_BOUND deep.
11+
set -euo pipefail
12+
: "${REPOSITORY:?}" "${CFG_HASH:?}" "${TIP_SHA:?}" "${CHECKOUT_DIR:?}" "${DEST:?}"
13+
BOUND="${CATCHUP_BOUND:-100}"
14+
rm -rf "$DEST"
15+
16+
api() { gh api -H 'Accept: application/vnd.github+json' "$@"; }
17+
export GH_HOST="${GH_HOST:-github.com}"
18+
GH_HOST="${GH_HOST#*://}"
19+
20+
ancestor="" other_cfg=false
21+
report() {
22+
printf 'ancestor_sha=%s\nother_cfg_at_tip=%s\n' "$ancestor" "$other_cfg"
23+
}
24+
trap report EXIT
25+
26+
# The merge base's first-parent history, nearest first. Distance 0 is the tip
27+
# itself, which a review already looked up by its exact name; sync has no such
28+
# lookup and passes INCLUDE_TIP=true.
29+
walk="$(git -C "$CHECKOUT_DIR" rev-list --first-parent --max-count=$(( BOUND + 1 )) "$TIP_SHA" 2>/dev/null || true)"
30+
[ "${INCLUDE_TIP:-false}" = true ] || walk="$(tail -n +2 <<< "$walk")"
31+
nearest() {
32+
local commit
33+
for commit in $walk; do
34+
if grep -qx "$commit" <<< "$saved"; then
35+
echo "$commit"
36+
return 0
37+
fi
38+
done
39+
}
40+
41+
# The artifact listing, newest first. Its name filter is exact, so a prefix needs
42+
# the pages themselves; paging stops at the first page holding a saved ancestor.
43+
# Bases are published as their commits are synced or reviewed, so the nearest one
44+
# is nearly always the newest: one or two calls in practice, MAX_PAGES (50, 5,000
45+
# artifacts) at worst, against GITHUB_TOKEN's 1,000 requests an hour per
46+
# repository, and only on a run that would otherwise analyze from scratch. The
47+
# provenance rule is fetch-state.sh's: only artifacts from a run on this
48+
# repository's own code, since a fork's workflow can upload under any name and
49+
# its bytes would reach a pickle loader.
50+
trusted='.artifacts[]?
51+
| select(.expired == false)
52+
| select(.workflow_run != null)
53+
| select(.workflow_run.head_repository_id == .workflow_run.repository_id)
54+
| select(.name | startswith("codeboarding-base-"))
55+
| .name'
56+
prefix="codeboarding-base-$CFG_HASH-"
57+
saved="" page=1
58+
while [ "$page" -le "${MAX_PAGES:-50}" ]; do
59+
if ! listing="$(api "repos/$REPOSITORY/actions/artifacts?per_page=100&page=$page" 2>/dev/null)"; then
60+
echo "::warning::Could not list artifacts in $REPOSITORY; not looking for an older saved analysis." >&2
61+
exit 0
62+
fi
63+
names="$(jq -r "$trusted" <<< "$listing" 2>/dev/null || true)"
64+
saved="$saved$(grep "^$prefix" <<< "$names" | cut -c$(( ${#prefix} + 1 ))- || true)"$'\n'
65+
if grep "^codeboarding-base-.*-$TIP_SHA\$" <<< "$names" | grep -vq "^$prefix"; then
66+
other_cfg=true
67+
fi
68+
ancestor="$(nearest)"
69+
[ -z "$ancestor" ] || break
70+
returned="$(jq -r '.artifacts | length' <<< "$listing" 2>/dev/null || echo 0)"
71+
[ "${returned:-0}" -eq 100 ] || break
72+
page=$(( page + 1 ))
73+
done
74+
75+
[ -n "$ancestor" ] || exit 0
76+
77+
# Downloaded by its exact name, through the same checks as every other lookup.
78+
if ! GITHUB_OUTPUT="" ARTIFACT_NAME="$prefix$ancestor" DEST="$DEST" \
79+
"$(dirname "$0")/fetch-state.sh" >&2 || [ ! -f "$DEST/analysis.json" ]; then
80+
ancestor=""
81+
fi

0 commit comments

Comments
 (0)