Skip to content

Commit b3fecba

Browse files
fix: compare review diagrams against target baseline
Amp-Thread-ID: https://ampcode.com/threads/T-019ed0f0-d9f9-75da-a034-cbfda2dcd6d1 Co-authored-by: Amp <amp@ampcode.com>
1 parent fbb0900 commit b3fecba

3 files changed

Lines changed: 39 additions & 32 deletions

File tree

‎action.yml‎

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -700,47 +700,33 @@ runs:
700700
working-directory: target-repo
701701
env:
702702
BASE_SHA: ${{ steps.guard.outputs.base_sha }}
703-
HEAD_SHA: ${{ steps.guard.outputs.head_sha }}
704703
ACTION_PATH: ${{ github.action_path }}
704+
DEPTH: ${{ steps.resolve_depth.outputs.depth }}
705705
run: |
706706
BASE_DIR="${RUNNER_TEMP}/cb-base"
707707
HEAD_DIR="${RUNNER_TEMP}/cb-head"
708708
mkdir -p "$BASE_DIR" "$HEAD_DIR"
709709
echo "base_dir=$BASE_DIR" >> $GITHUB_OUTPUT
710710
echo "head_dir=$HEAD_DIR" >> $GITHUB_OUTPUT
711-
BASELINE_SHA=""
712-
# Use the newest analysis.json reachable by walking backwards through
713-
# the PR head branch ancestry. If the PR branch is a->b->c->d and master
714-
# is a->b->m1->m2, this deliberately searches d,c,b,a — not m2,m1,b,a.
715-
while IFS= read -r candidate; do
716-
if git cat-file -e "${candidate}:.codeboarding/analysis.json" 2>/dev/null; then
717-
BASELINE_SHA="$candidate"
718-
break
719-
fi
720-
done < <(git rev-list "$HEAD_SHA" -- .codeboarding/analysis.json 2>/dev/null || true)
721-
722-
if [ -n "$BASELINE_SHA" ] && git show "${BASELINE_SHA}:.codeboarding/analysis.json" > "${BASE_DIR}/analysis.json" 2>/dev/null; then
711+
if git show "${BASE_SHA}:.codeboarding/analysis.json" > "${BASE_DIR}/analysis.json" 2>/dev/null; then
723712
if python3 "$ACTION_PATH/scripts/engine_adapter.py" validate-base \
724713
--analysis "${BASE_DIR}/analysis.json" \
725-
--expected-sha "$BASELINE_SHA"; then
714+
--expected-sha "$BASE_SHA" \
715+
--expected-depth "$DEPTH"; then
726716
echo "committed=true" >> $GITHUB_OUTPUT
727-
echo "baseline_sha=$BASELINE_SHA" >> $GITHUB_OUTPUT
728-
if [ "$BASELINE_SHA" = "$HEAD_SHA" ]; then
729-
echo "Using committed .codeboarding/analysis.json at PR head ${HEAD_SHA}."
730-
else
731-
echo "Using nearest committed .codeboarding/analysis.json at ${BASELINE_SHA} from PR head history ${HEAD_SHA}."
732-
fi
717+
echo "baseline_sha=$BASE_SHA" >> $GITHUB_OUTPUT
718+
echo "Using committed .codeboarding/analysis.json at PR base ${BASE_SHA}."
733719
else
734720
rm -f "${BASE_DIR}/analysis.json"
735721
echo "committed=false" >> $GITHUB_OUTPUT
736722
echo "baseline_sha=$BASE_SHA" >> $GITHUB_OUTPUT
737-
echo "Committed baseline at ${BASELINE_SHA} is unusable; will generate a fresh base analysis at ${BASE_SHA}."
723+
echo "Committed baseline at PR base ${BASE_SHA} is unusable; will generate a fresh base analysis."
738724
fi
739725
else
740726
rm -f "${BASE_DIR}/analysis.json"
741727
echo "committed=false" >> $GITHUB_OUTPUT
742728
echo "baseline_sha=$BASE_SHA" >> $GITHUB_OUTPUT
743-
echo "No committed baseline found in PR head history; will generate one via a full analysis on the base commit ${BASE_SHA}."
729+
echo "No committed baseline found at PR base ${BASE_SHA}; will generate one via a full analysis on the base commit."
744730
fi
745731
746732
- name: Restore base artifacts (keyed by baseline SHA)
@@ -757,6 +743,27 @@ runs:
757743
# analyses; the mode discriminator keeps them from reusing each other's cache.
758744
key: cb-base-v2-${{ runner.os }}-${{ steps.base.outputs.baseline_sha }}-d${{ steps.resolve_depth.outputs.depth }}-${{ inputs.engine_ref }}-${{ steps.llm.outputs.mode }}-${{ inputs.llm_provider }}-${{ inputs.agent_model }}-${{ inputs.parsing_model }}
759745

746+
- name: Reassert committed base analysis after cache restore
747+
if: steps.guard.outputs.skip != 'true' && steps.guard.outputs.mode == 'review' && steps.base.outputs.committed == 'true'
748+
shell: bash
749+
working-directory: target-repo
750+
env:
751+
BASE_SHA: ${{ steps.guard.outputs.base_sha }}
752+
BASE_DIR: ${{ steps.base.outputs.base_dir }}
753+
ACTION_PATH: ${{ github.action_path }}
754+
DEPTH: ${{ steps.resolve_depth.outputs.depth }}
755+
run: |
756+
# The cache stores generated base artifacts under the baseline SHA so the
757+
# static-analysis pkl can be reused. It must never replace the committed
758+
# target-branch baseline used for the PR diff. Re-copy the versioned
759+
# baseline after restore: cache may supply pkl/sha, but git is the source
760+
# of truth for cb-base/analysis.json.
761+
git show "${BASE_SHA}:.codeboarding/analysis.json" > "${BASE_DIR}/analysis.json"
762+
python3 "$ACTION_PATH/scripts/engine_adapter.py" validate-base \
763+
--analysis "${BASE_DIR}/analysis.json" \
764+
--expected-sha "$BASE_SHA" \
765+
--expected-depth "$DEPTH"
766+
760767
# A committed analysis.json gives the head analysis stable component ids,
761768
# but the engine's incremental path ALSO needs the base static_analysis.pkl
762769
# with its cluster baseline — which git can't provide (the pkl is never

‎scripts/diff_to_mermaid.py‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,15 +225,15 @@ def _sanitize(name: str) -> str:
225225
return re.sub(r"\W+", "_", name or "")
226226

227227

228-
# Mermaid label metacharacters → numeric/named char-refs (the ``#NNN;`` form
229-
# GitHub's strict renderer accepts). A bare ``]`` / ``)`` / ``}`` terminates a
230-
# node label and breaks the whole diagram, so escape the shape chars too — not
231-
# just ``#`` and ``"``.
228+
# Mermaid label metacharacters → numeric char refs (the ``#NNN;`` form GitHub's
229+
# strict renderer accepts). A bare ``]`` / ``)`` / ``}`` terminates a node label
230+
# and breaks the whole diagram, so escape the shape chars too — not just ``#``
231+
# and ``"``.
232232
_ESC_MAP = {
233-
"&": "#amp;",
234-
'"': "#quot;",
235-
"<": "#lt;",
236-
">": "#gt;",
233+
"&": "#38;",
234+
'"': "#34;",
235+
"<": "#60;",
236+
">": "#62;",
237237
"[": "#91;",
238238
"]": "#93;",
239239
"(": "#40;",

‎tests/test_diff_to_mermaid.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,14 @@ def test_label_escaping(self):
121121
head = {"components": [comp('A "q" #h'), comp("B")], "components_relations": []}
122122
base = {"components": [comp("B")], "components_relations": []}
123123
text, _ = dm.render_mermaid(dm.build_diff(base, head), render_depth=1)
124-
self.assertIn("#quot;", text)
124+
self.assertIn("#34;", text)
125125
self.assertIn("#35;", text)
126126

127127
def test_label_escaping_brackets_break_chars(self):
128128
# `]` / `(` / `&` would break GitHub's renderer if left raw.
129129
self.assertEqual(dm._esc("Has]Bracket"), "Has#93;Bracket")
130130
self.assertEqual(dm._esc("f(x)"), "f#40;x#41;")
131-
self.assertEqual(dm._esc("A & B"), "A #amp; B")
131+
self.assertEqual(dm._esc("A & B"), "A #38; B")
132132
head = {"components": [comp("Weird]Name(x)"), comp("B")], "components_relations": []}
133133
base = {"components": [comp("B")], "components_relations": []}
134134
text, _ = dm.render_mermaid(dm.build_diff(base, head))

0 commit comments

Comments
 (0)