From 7e8fa5db79e0cbfce2ab5a497a561f4e14466aec Mon Sep 17 00:00:00 2001 From: norvalbv Date: Fri, 20 Nov 2026 13:00:11 +0000 Subject: [PATCH] fix(ship): match origin's HEAD advertisement in-shell so grep -q cannot SIGPIPE the default check (sc-6615) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes [sc-6615] ## What was wrong `ship_origin_default_still` (`cli/lib/ship/origin-base.sh`) checked origin's `ls-remote` advertisement with `printf '%s\n' "$out" | grep -qxF …`. Its callers run under `set -euo pipefail`. - bash line-buffers stdout, so printf writes the advertisement one line at a time. - `grep -q` exits as soon as line 1 (`ref: refs/heads/mainHEAD`) matches. - If printf still has a line to write, that write gets SIGPIPE (exit 141). pipefail turns it into `return 1`: "the default moved". - `ship_suggested_base_proven` then falls back to the nearest ancestor. That produced `ancestor 0.0.8` instead of `default main`, the exact output of both CI failures: line 404, and line 412 in the report's correction. This race was not limited to tests. Under load, a real `devkit ship` could suggest a stale ancestor branch as `--base`. ## Fix - Match the advertisement inside the shell with `case`, using exact whole-line patterns anchored by a newline on both sides. Nothing is piped, so there is no early-exit reader. - Remove the branch that ran when no oid was passed. All four callers pass one, so `oid` is now required. ## Proof - **Cause, at CI size, on Linux** (node:24-bookworm, bash 5.2.15, 10 CPU burners): the old 3-line pipe failed 100 of 20,000 runs, all with exit 141. The new `case` match failed 0 of 20,000. - **New regression test** "still answers the default when grep -q would exit before the advertisement is written". It pads origin with 3,000 `refs/heads/padN/HEAD` refs; `ls-remote` tail-matches `HEAD`, so they join the advertisement. That makes the race happen on every run. - On the old code it fails with `out: "ancestor 0.0.8"` on macOS (bash 3.2) and in the Linux container. - On the fix it passes. - **Story AC** (Linux container, under parallel CPU load): the "no tracking ref", "unseen default" and new test, 50 runs each, gave `pass=50 fail=0`. - vitest has no `--repeat` CLI flag, so this is a shell loop. - The flake rate was about 1 in 1,500 per test, so 50 runs alone cannot tell old code from new. The deterministic red/green test above is the discriminating proof. - **Existing coverage of the new branches:** I made each check never reject and reran the suites. - Disabling the HEAD-line check fails "never names a default whose origin HEAD was repointed…" and "reuses one proven base across a refusal…". - Disabling the oid check fails "does not fall back to a default force-pushed between its judgment and its re-proof". - **Test runs:** `ship-origin-base.test.mts` passes 43/43. `ship-base-ancestry` and `ship-base-and-self-checkout` pass 80/80. - **Match semantics:** exact whole-line matching was checked on bash 3.2, 5.2 and 5.3. A 2 MB, 30,000-ref advertisement matches in 132 ms or less. - `dist/cli/lib/ship/origin-base.sh` is unchanged; it is release-only. ## Follow-ups (not in this PR) - `cli/lib/ship/queue/slot.sh:66` has the same `printf | grep -Fxq` shape under pipefail (bug, sev 3, traced). - An opt-in trace naming which resolver probe silently failed (feature). - A transient file:// `ls-remote` failure is still a possible second cause. It is unevidenced and now unlikely (bug, sev 3, claimed). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Complete against acceptance criteria: yes --- cli/__tests__/ship-origin-base.test.mts | 12 ++++++++++++ cli/lib/ship/origin-base.sh | 19 +++++++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/cli/__tests__/ship-origin-base.test.mts b/cli/__tests__/ship-origin-base.test.mts index 43f52b4da..c6862a16f 100644 --- a/cli/__tests__/ship-origin-base.test.mts +++ b/cli/__tests__/ship-origin-base.test.mts @@ -404,6 +404,18 @@ describe('origin-base.sh — ship_suggested_base prefers a default that contains expect(suggestedOf(dir)).toMatchObject({ status: 0, out: 'default main' }); }); + it('still answers the default when grep -q would exit before the advertisement is written', () => { + // Piped into `grep -q` under pipefail, a writer still mid-advertisement took SIGPIPE: "default moved". + // A 3-line advertisement lost that race under load; refs/heads/*/HEAD (ls-remote tail-matches) force it. + const { dir, bare } = seedAdvancedDefault(); + const tip = execFileSync('git', ['-C', bare, 'rev-parse', 'main'], { encoding: 'utf8' }).trim(); + const pads = Array.from({ length: 3000 }, (_, i) => `create refs/heads/pad${i}/HEAD ${tip}\n`); + execFileSync('git', ['-C', bare, 'update-ref', '--stdin'], { input: pads.join('') }); + execFileSync('git', ['-C', bare, 'pack-refs', '--all']); + + expect(suggestedOf(dir)).toMatchObject({ status: 0, out: 'default main' }); + }); + it('fetches an unseen default commit to judge it, without writing any ref', () => { const { dir, git, bare } = seedAdvancedDefault(); git(['update-ref', '-d', 'refs/remotes/origin/main']); diff --git a/cli/lib/ship/origin-base.sh b/cli/lib/ship/origin-base.sh index b3698869e..921905e93 100644 --- a/cli/lib/ship/origin-base.sh +++ b/cli/lib/ship/origin-base.sh @@ -268,20 +268,15 @@ ship_origin_branch_oid() { printf '%s\n' "$line" } -# ship_origin_default_still [oid] -# Is STILL origin's default, AND (when is given) still at it -- else merely present? -# Both read from ONE ls-remote advertisement, so a HEAD repointed to another branch, or the default -# force-pushed, since it was judged cannot pass as the same answer. Anything that fails to read is "no". +# ship_origin_default_still +# Is STILL origin's default, AND still at ? Both read from ONE ls-remote advertisement, +# so a HEAD repointed or a default force-pushed since it was judged cannot pass. A failed read is "no". +# Matched in-shell, never piped into `grep -q`: under pipefail its early exit SIGPIPEs the writer. ship_origin_default_still() { - local repo=$1 def=$2 oid=${3-} out= tab - tab=$(printf '\t') + local repo=$1 def=$2 oid=$3 out= nl=$'\n' tab=$'\t' out=$(git -C "$repo" ls-remote --symref origin HEAD "refs/heads/$def" 2>/dev/null) || return 1 - printf '%s\n' "$out" | grep -qxF "ref: refs/heads/$def${tab}HEAD" || return 1 - if [ -n "$oid" ]; then - printf '%s\n' "$out" | grep -qxF "$oid${tab}refs/heads/$def" - else - printf '%s\n' "$out" | cut -f2 | grep -qxF "refs/heads/$def" - fi + case "$nl$out$nl" in *"${nl}ref: refs/heads/$def${tab}HEAD$nl"*) ;; *) return 1 ;; esac + case "$nl$out$nl" in *"$nl$oid${tab}refs/heads/$def$nl"*) return 0 ;; *) return 1 ;; esac } # ship_origin_default_contains