Skip to content

fix(#537): respect custom base branch in exec zero-diff guard - #538

Merged
admarble merged 6 commits into
mainfrom
feature/537-fix-orchestrator-respect-custom-base-branch-in
Apr 19, 2026
Merged

admarble merged 6 commits into
mainfrom
feature/537-fix-orchestrator-respect-custom-base-branch-in

Conversation

@admarble

Copy link
Copy Markdown
Contributor

Summary

AC Coverage

  • AC-1: hasExecChanges resolves correct base via git config branch.<name>.sequantBase
  • AC-2: templates/scripts/new-feature.sh (symlinked as scripts/new-feature.sh) writes the --base value to that config key
  • AC-3: Fallback to origin/main on missing config, detached HEAD, or git error
  • AC-4: Unit test matrix (custom base + new commits, zero diff, dirty, no-config fallback)
  • AC-5: mapAgentSuccessToPhaseResult signature unchanged; still calls hasExecChanges(cwd)
  • AC-6: Integration test in phase-executor.integration.test.ts — real bare-origin + work repo, feature/epic populated, feature/537-test branched from epic with recorded base; sanity-checks HEAD is ahead of origin/main but zero against origin/feature/epic
  • AC-7: CHANGELOG entry under [Unreleased] / Fixed (sub-bullet of fix(orchestrator): treat null QA verdict and zero-diff exec as failure, not success #534)

Test Plan

  • npm run build — clean
  • npm run lint — 0 errors, 0 warnings
  • Targeted vitest (phase-executor.*, spec-retry, batch-executor) — 150/150 passing (111 in the two phase-executor files alone)
  • bash -n templates/scripts/new-feature.sh — syntax OK
  • Optional post-merge: create a real worktree with --base feature/<branch> and confirm git config --get branch.<name>.sequantBase returns the expected value

Notes

Closes #537

The #534 zero-diff guard hardcoded origin/main as the comparison ref. On
worktrees created with `--base feature/<branch>`, HEAD is already ahead
of origin/main from the moment the worktree exists (inheriting the parent
branch's commits). The guard counted those commits, reported the exec as
having changes, and silently passed zero-diff runs — the same failure
mode #534 was written to prevent, but scoped to custom-base worktrees.

- New `resolveBaseRef(cwd)` helper reads `branch.<current>.sequantBase`
  (written by new-feature.sh on worktree creation), prepending `origin/`
  when the recorded value does not already reference a remote.
- Falls back to `origin/main` on missing config, detached HEAD, or any
  git error — preserves #534 behavior for pre-fix worktrees and
  non-sequant-managed ones.
- `hasExecChanges` now consults `resolveBaseRef` instead of hardcoding.
- `mapAgentSuccessToPhaseResult` signature unchanged (AC-5): base
  resolution lives inside the helper; callers stay base-agnostic.
- `new-feature.sh` writes `branch.<name>.sequantBase <base>` after
  `git worktree add`. Existing worktrees are unaffected and fall through
  to the origin/main fallback (documented in CHANGELOG, no backfill).
- Unit tests cover the AC-4 matrix and resolveBaseRef error paths.
- Integration test reproduces the end-to-end #537 scenario with real git.
…ction

The resolved base ref is interpolated into a shell-executed git command
via execSync template string. The recorded value originates from the
user-supplied --base CLI flag and is persisted in git config — so a
value containing shell metacharacters would be spliced into the
rev-list command and executed by /bin/sh.

Threat model is limited (the user setting the flag is the one at risk —
self-harm only, not cross-user), but hygiene matters and the validation
is a one-liner. Both the current branch name and the recorded base are
now matched against a conservative refname regex; anything outside the
safe subset falls through to the origin/main fallback.

Two new unit tests cover the injection cases (recorded value with
shell metacharacters, branch name with shell metacharacters).
…zero-diff guard

Root-cause fix for the shell-injection concern flagged in earlier QA.
The previous commit added a SAFE_REF regex as defense in depth, but the
actual problem was using execSync with template-string commands — that
routes through /bin/sh and parses metacharacters. The regex worked but
also rejected legal-but-unusual git refnames (feature+v1, tag=1, a,b).

Switch resolveBaseRef and hasExecChanges to execFileSync, which passes
argv directly to execve with no shell parse. Each arg is an opaque
string to git — a value like "--version" would be treated as a revspec,
not a flag, and fall through to the existing fail-open path.

Drop SAFE_REF. Keep only a paranoid "no embedded newline" sanity check
on rev-parse and config output, since git config --get is single-line
by contract and multi-line output would indicate a corrupt config.

Tests:
- Mock now exposes both execSync and execFileSync from child_process
- resolveBaseRef/hasExecChanges tests updated to use mockExecFileSync
- New test verifies argv shape (call[0] === "git", call[1] is Array)
- New test verifies legal-but-unusual refname chars (+, =, ,) now
  flow through instead of being rejected by the old regex
- Shell-metachar-rejection tests replaced with an embedded-newline test
  (the only value that still trips the sanity guard)
- Call-arg assertions updated from mock.calls[2][0] (command string)
  to mock.calls[2][1] (argv array)
Earlier integration tests used `git init` + `git checkout -b` to create
the "worktree under test" scenario. That emulates the consumer semantics
but does not exercise the worktree-specific config plumbing: branch-
scoped config keys live in `$GIT_COMMON_DIR/config` and are shared
across worktrees, and `new-feature.sh` writes the key from inside the
newly created worktree. This test walks that actual shape:

1. main repo + bare origin
2. commit on main, push
3. feature/epic with E1 commit, push
4. `git worktree add <wt> -b feature/537-test feature/epic`
5. `git -C <wt> config branch.feature/537-test.sequantBase feature/epic`
6. verify resolveBaseRef(<wt>) reads origin/feature/epic
7. verify hasExecChanges(<wt>) returns false (zero-diff, primary #537 fix)
8. verify hasExecChanges(<wt>) returns true after a commit on top

Teardown removes the worktree with --force before rmSync to avoid
leaving stale metadata in the main repo.

Pairs with the git-init-based tests (which remain as focused
consumer-contract checks).
…lt suite

The outer beforeEach only reset mockExecSync, so mockExecFileSync
accumulated state from exec-phase tests into the other-phases "no
subprocess calls" test. The existing assertion only checked execSync
so it passed, but it was masking a real mock-state leak.

Reset both mocks in the outer beforeEach, and extend the other-phases
assertion to cover execFileSync too. Trivial in isolation, but removes
a latent footgun for anyone adding a spec/testgen/verify/loop test
block below.
The exec-qa-phase-guards troubleshooting doc shipped a "known gap"
note pointing at this issue. Now that the fix has landed, rewrite
the section to describe:

- What was broken pre-#537 (for historical context and log-searchers)
- How the fix works (sequantBase config key + resolveBaseRef)
- The explicit fallback to origin/main for pre-fix worktrees
- A manual recovery command for legacy worktrees that missed the
  config write

Also corrects the script path from the gitignored scripts/dev/... copy
to the tracked scripts/new-feature.sh.
@admarble
admarble merged commit 9bb5431 into main Apr 19, 2026
5 checks passed
@admarble
admarble deleted the feature/537-fix-orchestrator-respect-custom-base-branch-in branch April 19, 2026 22:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(orchestrator): respect custom base branch in exec zero-diff guard (#534 follow-up)

1 participant