Skip to content

Populate before-worktree submodules at the merge-base pin in unit-test bugfix validation - #111495

Closed
groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:ci-bugfix-validation-before-src-submodule-pins
Closed

groeneai wants to merge 3 commits into
ClickHouse:masterfrom
groeneai:ci-bugfix-validation-before-src-submodule-pins

Conversation

@groeneai

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • CI Fix or Improvement (changelog entry is not required)

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

...

Description

Fixes Bugfix validation (unit tests) erroring with "the before-binary failed to CONFIGURE (cmake)" for bugfix PRs whose merge-base predates a submodule pin/URL change on master.

Root cause: prepare_before_worktree in ci/jobs/unit_tests_bugfix_validation_job.py builds the "before" binary in a merge-base worktree and populates its submodules by hardlinking (cp -al) the primary (master) checkout's submodule content. That is content-correct only when the merge-base pins the same commit the primary has checked out. When master bumps a submodule after the merge-base, the before-worktree gets master's content but uses the merge-base cmake wrappers, which reference the merge-base submodule's file layout. The 2026-07-20 abseil-cpp switch from the ClickHouse/abseil-cpp fork (255c84dadd) to upstream abseil/abseil-cpp (5650e9cf) is the trigger: the merge-base wrapper references absl/base/internal/throw_delegate.cc and absl/debugging/internal/borrowed_fixup_buffer.cc, which do not exist at those paths upstream, so configure fails with "Cannot find source file". The old fail-close check only looked for a single unrelated submodule marker (contrib/sysroot/README.md), so it did not catch this.

Fix: populate each submodule the merge-base needs by comparing the merge-base gitlink SHA to the primary's checked-out SHA. When they match, hardlink as before (fast, no network). When they differ (drift), or the primary lacks the submodule, fetch that submodule at its own merge-base pin (git -C before_src submodule sync/update, so the merge-base URL is used). The submodule list is taken from the merge-base's own .gitmodules so submodules added or removed on master are handled correctly. The fail-close now reports the specific unpopulated submodule instead of a generic marker. All existing security guards (path shape validation, realpath containment, shell-quoting) are preserved.

Verified locally by reproducing the exact CI cmake error against a pre-2026-07-20 merge-base (upstream abseil hardlinked -> configure fails) and confirming the fix repopulates the 6 drifted submodules for that merge-base and configure succeeds. Added unit tests covering the match/drift/primary-absent cases.

No related open issue found.

groeneai and others added 3 commits July 22, 2026 22:41
The unit-test bugfix validator builds a before-binary in a merge-base worktree
and populated its submodules by hardlinking the primary (master) checkout. That
is content-correct only when the merge-base pins the same commit master has
checked out. When master bumps a submodule pin/URL after the PR merge-base, the
before-worktree gets master content but uses the merge-base cmake wrappers,
which reference the merge-base submodule file layout, so cmake configure fails
with 'Cannot find source file' (e.g. the 2026-07-20 abseil-cpp fork->upstream
switch dropped absl/*/internal/*.cc files the merge-base wrapper expects).

Populate each submodule the merge-base needs by comparing the merge-base gitlink
SHA to the primary checked-out SHA: hardlink when they match (fast, no network),
otherwise fetch that submodule at its own merge-base pin (submodule sync+update,
so the merge-base URL is used). Iterate the merge-base .gitmodules so submodules
added/removed on master are handled. Strengthen the fail-close to name the
unpopulated submodule. Security guards (path shape, realpath containment,
shell-quoting) unchanged.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Address internal review: a failed `git submodule update` for a drifted
submodule can leave the path holding only a `.git` gitlink (nonempty, but no
sources), which the populated-check would have accepted. Return (ok, detail)
and fail closed immediately on a failed update; require a non-`.git` entry in
the final populated-check; and report the specific unpopulated submodule to the
caller instead of the generic SUBMODULE_MARKER.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Address internal review (security): the drift path fetches submodules using the
merge-base worktree's .gitmodules, but the shape guard in main() validated only
the PR checkout's .gitmodules. An older merge-base can carry a non-GitHub URL or
an unsafe path even when the PR's current file is clean, so a submodule sync +
update driven by it could point the privileged runner at an arbitrary URL.
Generalize gitmodules_shape_violation() to take a file path and validate the
merge-base .gitmodules before any fetch; fail closed on a violation.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. git worktree add --detach <pre-2026-07-20 merge-base> + hardlink primary(master) abseil into it + cmake configure fails 100% with Cannot find source file: .../abseil-cpp/absl/base/internal/throw_delegate.cc (+ borrowed_fixup_buffer.cc), byte-identical to the 3 CI job.logs (PRs 109676/110231/110833). Logs: .scratch/repro-configure-BEFORE.log.
b Root cause explained? Yes. prepare_before_worktree populates the merge-base before-worktree's submodules by hardlinking the PRIMARY (master) checkout content. When master bumps a submodule pin/URL after the PR's merge-base (abseil-cpp fork ClickHouse/abseil-cpp@255c84dadd -> upstream abseil/abseil-cpp@5650e9cf, 2026-07-20), before_src gets master's UPSTREAM abseil content but uses the merge-base FORK-layout cmake wrapper (references absl/*/internal/throw_delegate.cc + borrowed_fixup_buffer.cc, absent upstream) -> "Cannot find source file" -> configure fails -> job ERRORs. The SUBMODULE_MARKER fail-close (contrib/sysroot/README.md, unchanged submodule) doesn't catch it.
c Fix matches root cause? Yes. Per submodule, compare merge-base gitlink SHA ("want") to the primary checked-out SHA ("have"): want==have -> hardlink (fast path, unchanged); drift (or primary lacks it) -> populate before_src's own copy at the merge-base pin via git -C before_src submodule sync/update. Addresses the mechanism (blind hardlink ignores drift), not a symptom. Not a retry/guard band-aid.
d Test intent preserved / new tests added? Yes. The check still validates the before-binary exactly as before for the normal (no-drift) case (byte-for-byte same hardlink path). Added 3 pytest cases (test_unit_tests_bugfix_validation.py): matching-pin -> hardlink (no fetch); drifted-pin -> submodule sync+update (no hardlink); primary-lacks-submodule -> populated at merge-base pin. Strengthened fail-close now names the missing submodule.
e Both directions demonstrated? Yes. BEFORE fix: cmake configure exit 1 with the exact abseil error. AFTER fix (real-git run of the fixed algorithm over all 137 merge-base submodules: 131 hardlinked, 6 drifted repopulated, 0 failures): cmake configure exit 0, "Configuring/Generating done", zero errors. Logs: repro-configure-BEFORE.log, repro-configure-FIXED-integrated.log. Plus 44/44 pytest.
f Fix is general across code paths? Yes. The fix is in the one function that owns before_src submodule population; there is no sibling path. It generalizes across ALL drifted submodules, not just abseil: the integrated run repopulated 6 drifted submodules (boost, replxx, abseil-cpp, s2geometry, rust_vendor, chdig) for this merge-base — the reported abseil instance is one of a class the fix handles uniformly.
g Fix generalizes across inputs? Yes (harness analog of "inputs" = the drift shapes). Handles: pin-bump only (same URL), URL switch (fork->upstream, needs submodule sync), submodule ADDED by master (want present, have absent -> fetch), submodule REMOVED by master (not in merge-base .gitmodules -> not iterated). Iterating the MERGE-BASE .gitmodules (not the primary's) is what covers add/remove correctly.
h Backward compatible? Yes / N/A user-facing. CI-harness-internal change; no server behavior, setting, on-disk/wire format, or user-facing surface touched. No SettingsChangesHistory. The no-drift fast path is behaviorally identical to before.
i Invariants and contracts preserved? Yes. SECURITY guards preserved and still applied to every path (realpath containment check, shlex.quote on all PR-controlled submodule paths, -- option terminator; gitmodules_shape_violation still runs earlier in main()). Fail-close contract strengthened, not weakened: previously returned on a single-marker check; now returns False (infra ERROR) if ANY merge-base submodule is unpopulated. The drift branch fetches only from the merge-base's own (already shape-validated) .gitmodules URL.

Session id: cron:clickhouse-author-slot-0:20260722-214900

@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review — adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 3 findings across 1 full pass + 1 delta recheck; bounded: 1 full pass + 1 fix round + 1 delta recheck).

# Sev Finding Verdict Evidence / action
1 ❌ Drift path fetches using the merge-base .gitmodules without applying the URL/path allow-list to that file (a clean PR file + unsafe merge-base file could point the runner at an arbitrary URL). AGREE — fixed @ 2c127be Verified: shape guard validated only cwd .gitmodules; historical commit 2425a07d had [email protected]. Generalized gitmodules_shape_violation(path), validate the merge-base file before any fetch, fail closed. +2 tests.
2 ⚠️ A failed drifted-submodule update could still return success (a .git-only dir is nonempty, so the populated-check passed). AGREE — fixed @ c0b1869 Return (ok, detail), fail closed immediately on a failed update, and require a non-.git entry in the final check. +2 tests.
3 💡 Any submodule failure was reported to the CI result as a missing contrib/sysroot/README.md. AGREE — fixed @ c0b1869 prepare_before_worktree now returns the specific offending submodule; the caller reports it instead of the hardcoded marker.

Severity: ❌ blocker / ⚠️ major / 💡 nit. All findings verified against the code before agreeing; each source fix was re-verified (integrated cmake configure still exit 0; 48/48 focused tests). Findings on hunks unchanged by the fix round are auto-dropped.

Session id: cron:clickhouse-author-slot-0:20260722-214900

@groeneai

Copy link
Copy Markdown
Collaborator Author

cc @leshikus @maxknv — could you review this CI-harness fix? The unit-test bugfix validator populated the merge-base before-worktree's submodules by hardlinking the primary (master) checkout, which is content-incorrect when master bumps a submodule pin/URL after the PR merge-base (the 2026-07-20 abseil-cpp fork->upstream switch makes cmake configure fail: Cannot find source file .../absl/base/internal/throw_delegate.cc). It now hardlinks only when the merge-base pins the same commit master has, and otherwise fetches the submodule at its own merge-base pin. (Original check author: @ nickitat.)

@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Jul 22, 2026
@clickhouse-gh

clickhouse-gh Bot commented Jul 22, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [2c127be]

Summary: ✅


AI Review

Summary

This PR fixes the unit-test bugfix validator so the merge-base worktree no longer hardlinks drifted submodule contents from the primary checkout; it now repopulates only the mismatched submodules at the merge-base pin and fails closed with a specific infrastructure error when submodule metadata or population is unsafe or incomplete. I did not find a remaining correctness, safety, or contract issue in the current diff, and the added focused unit coverage matches the new failure modes in the helper.

Final Verdict
  • Status: ✅ Approve

@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger — 2c127be

All checks green on this head. No failing test rows in CIDB across all shards. The only non-test line is the private sync check.

Check / test Reason Owner / fixing PR
CH Inc sync — CH Inc sync (private, not actionable by us)

Session id: cron:our-pr-ci-monitor:20260723-063000

@maxknv

maxknv commented Jul 24, 2026

Copy link
Copy Markdown
Member

cc @nickitat

@groeneai

Copy link
Copy Markdown
Collaborator Author

This PR now conflicts with master, and resolving the conflict would ship unreachable code, so I am asking for a disposition instead of force-rebasing it.

#111530 (merged 2026-07-25) fixes the same cmake failure this PR targets, by the opposite strategy:

  • this PR makes prepare_before_worktree drift-aware: per submodule, compare the merge-base pin against the primary checkout's pin, hardlink when equal, otherwise fetch that submodule at its own merge-base pin, so the before-binary still gets validated.
  • Fix unit-test bugfix validation: force submodule checkout so the before-binary configures #111530 makes the job fail closed on drift: get_submodule_state_changes() plus an early return in main() reporting "Submodule state differs ... inconclusive", and separately --force in ensure_primary_submodules (the empty-working-tree root cause) and submodule_worktree_populated().

The two cannot coexist as written. On master, main() runs the drift guard and returns at line 624, and only calls prepare_before_worktree at line 626. My drift branch lives inside that function, so after a rebase it is reachable only when every submodule pin already matches, which is exactly the case where the branch does nothing. For the abseil fork-to-upstream switch this PR was written for, git diff --raw --ignore-submodules=none <pre-2026-07-20 merge-base> origin/master reports 11 drifted paths (.gitmodules, abseil-cpp, boost, chdig, jemalloc, libucontext, orc, replxx, rust_vendor, s2geometry, zstd), so the guard fires first every time and my code never runs.

The target signature is also dormant: failed to CONFIGURE (cmake) has 7 hits in CIDB with the last on 2026-07-22 23:10, and of the 27 runs since #111530 merged, 26 skip for no changed unit-test files, 1 is a pre-existing compile-fail on an unrelated PR, and none hit either the configure failure or the new guard. Submodule state differs has 0 hits so far.

One residual point, for whoever owns this next. Master moved a gitlink or .gitmodules in 22 commits over the last 14 days, spread across 11 distinct days, so the new guard will convert a fair number of otherwise-valid runs into inconclusive ERROR results once PRs with older merge-bases hit it. Fetching the drifted submodule at its merge-base pin is what would keep those validating. That is a design call on top of already-merged code, not something I should decide here.

So: close this as superseded by #111530, or should I re-scope it to just the drift-inconclusive gap on top of the current guard? I have not pushed anything and the branch is untouched.

@alexey-milovidov alexey-milovidov self-assigned this Jul 26, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

Thanks, closing.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Following up on my question above, because I now think the answer is "neither", and the reason is worth writing down rather than opening another PR.

I looked at re-scoping this to just the drift-inconclusive gap on top of the merged guard, i.e. leaving the fail-close exactly as you built it and only changing the status it reports from ERROR to SKIPPED. That looked clean, and there is even a precedent for the same status in the sibling validator (ci/jobs/functional_tests.py:1365-1374, the no-repro branch, including the comment about Result.create_from skipping SKIPPED children). It does not work, and I could not find a variant that does.

Why SKIPPED is not available here. The unit validator is cacheable (ci/defs/job_configs.py:912-927), and the cache success record is pushed under if result.is_ok() (ci/praktika/runner.py:770-773), where is_ok includes SKIPPED (ci/praktika/result.py:236-241). So a drift skip would be recorded as a success for that digest and then suppress the job on a later commit via cache_success_base64 in the workflow if (ci/praktika/hook_cache.py:185-186).

The digest collision is exact, not theoretical. The job's digest hashes the checked-out tree plus submodule SHAs, and this workflow checks out the synthetic merge ref, so the tree it hashes after a rebase is the same tree it hashed before. Measured on the two hits I have:

PR merge-ref tree tree after merging the same master commit into the PR head
107134 981cd8b165ba45ef2a278eae3e340c9a7738a3f5 981cd8b165ba45ef2a278eae3e340c9a7738a3f5
110833 0c99d71f686e9bd6a08211c575712f52c71f1892 0c99d71f686e9bd6a08211c575712f52c71f1892

Byte-identical, and every digest input path (./src, ./contrib/, ./.gitmodules, ./cmake, ./base, ./programs, ./rust, CMakeLists.txt, PreLoad.cmake, the two ci/jobs files) matches. So the exact commit that would finally have no drift and could produce a real verdict is the one whose digest already carries the cached skip. That is the failure mode the FT/IT validators were made non-cacheable for, documented at ci/defs/job_configs.py:642-656. Turning the drift outcome green would reintroduce it for the unit validator, which is strictly worse than a cosmetic red.

And no other status is available either. The two constraints are opposite over the whole status set: the cache gate is is_ok, and the workflow-status gate is the has_failed set in Result._update_status (ci/praktika/result.py:691-704). OK/SKIPPED/XFAIL are cached; ERROR/FAIL/UNKNOWN/XPASS/DROPPED all put the workflow result at FAIL. The intersection of "not cached" and "does not redden the workflow" is empty, so there is no status-only fix. do_not_block_pipeline_on_failure does not help: it is only recorded when not is_ok() (result.py:1034) and is consumed for dependee dropping and process exit (hook_html.py:249, runner.py:600,888), not for the workflow status.

What is actually left, and why I am not picking one. Every remaining option is a tradeoff you already priced once:

  1. Leave it as is. allow_failure already keeps it out of the merge gate, which I verified: on all three completed hits the job is absent from the Mergeable Check failure list (107943 Failed: Integration tests (amd_msan, 6/8), 110833 Failed: Stateless tests (amd_debug, parallel), 107134 Failed: 3). The cost is a red job and an error row in CIDB for a non-failure, and the workflow result going FAIL on drift alone, since _update_status has no allow_failure awareness.
  2. Drop digest_config for this job, then report SKIPPED. Consistent with the FT/IT validators, and it makes the drift outcome honest. The cost is that a roughly one hour cold before-build (your own note at unit_tests_bugfix_validation_job.py:412) stops being cacheable, on a job gated to bugfix PRs with changed unit tests.
  3. Narrow the guard to submodules the before-build actually consumes. I do not recommend it: unit_tests_dbms links dbms plus functions, aggregate functions, parsers, storages_system and conditionally krb5/simdjson/simdcomp/yaml_cpp/delta_kernel_rs/azure_sdk/hdfs/parquet/silk_fibers (src/CMakeLists.txt:935-984), so transitively most of contrib/, and every drifted path I observed (boost, orc, zstd, jemalloc, abseil-cpp, libucontext, replxx) is in that set. It would also mean reworking the predicate of a guard whose purpose is preventing false validations.

Frequency, in case it changes the priority. Drift here is entirely base-branch motion. Merge-base to PR-head submodule drift is 0 paths on all 4 CIDB hits; what the guard sees (merge-base to merge ref) is 2, 4, 13 and 21 paths. Against master tip, drift by merge-base age is 0 paths at 1 day, 2 at 2 days, 6 at 4 days, 11 at 7 days, 23 at 14 days, 34 at 30 days, with 23 commits touching .gitmodules in the last 30 days. So the guard fires for most bugfix PRs older than a day or two. In 200 rows since 2026-07-20 it fired 4 times, on #109602, #107943, #110833 and #107134, so 2 of 4 are not mine.

My own preference would be option 2, but it trades your cache for my report cleanliness on a job you own, so I would rather ask than push it. If you would like option 2 I will open it as a two line change (drop digest_config, report SKIPPED) with the cache reasoning in the description; if you prefer to leave it as is, that is a fine answer and I will stop tracking it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors pr-ci

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants