Files
fusion/packages/engine/src/base-commit-capture.ts
gsxdsm f4a9c65509 fix(review): address PR #1714 review findings
- base-commit-capture: POSIX single-quote integration branch refs instead of
  JSON.stringify (double quotes are subject to $-expansion in the shell)
- executor: add per-repo no_commits guard to the workspace verifyWorktreeInvariants
  branch (parity with the singular path), gated by the same task-wide no-commit
  eligibility
- executor: reviewWorkspacePerRepo failure message now states the per-repo verdict
  list is partial (evaluation stops at first failure)
- worktree-acquisition: defensively wrap non-fatal/outer-catch logEntry/audit so a
  logging throw cannot promote a non-fatal error to fatal or mask the original error
- docs/plans: add code-fence language tags and fix MD028 blank-line-in-blockquote

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-22 00:21:49 -07:00

78 lines
3.4 KiB
TypeScript

import { exec } from "node:child_process";
import { promisify } from "node:util";
const execAsync = promisify(exec);
/**
* Resolve the fork-point base SHA for a freshly acquired task worktree.
*
* Called immediately after worktree acquisition, when the task branch was
* just created/force-reset from the local integration branch
* (`prepareForTask` forks from local `main` via `resolveIntegrationBranch`).
*
* The merge-base MUST be measured against LOCAL main first (origin/main only
* as a fallback), matching the contamination-base sites in
* `worktree-acquisition.ts` and `auto-recovery-handlers/branch-worktree.ts`.
* The merger lands tasks on local main before pushing, so at fork time local
* main can be ahead of origin/main by merged-but-unpushed commits. Measuring
* against origin/main rewinds the base past those commits; once the
* post-merge rebase-and-push rewrites their SHAs, `baseCommitSha..HEAD`
* permanently sweeps the predecessors' files into this task's diff (FN-5937:
* in-review tasks showing 31 "files changed" instead of 12).
*
* Returns `undefined` only when every git invocation fails (caller treats a
* missing base as non-fatal).
*
* FNXC:Workspace 2026-06-21-20:10:
* `integrationBranch` is an OPTIONAL TRAILING param defaulting to the historic
* "main" literal so the single-repo executor caller and the real-git tests stay
* green without change. Workspace mode (U2/KTD3) passes each sub-repo's RESOLVED
* integration branch so per-repo base capture forks against the right branch
* instead of a hardcoded "main". The local-first ordering (merge-base HEAD
* <local> then origin/<branch>) is preserved per-branch to keep the
* inflation-prevention invariant (FN-5937) intact for non-main integration
* branches too.
*/
export async function resolveCapturedBaseCommitSha(
worktreePath: string,
logger?: { warn: (msg: string) => void },
integrationBranch: string = "main",
): Promise<string | undefined> {
const branch = integrationBranch.trim() || "main";
// FNXC:Workspace 2026-06-22-00:00:
// Shell-quote with POSIX single quotes, NOT JSON.stringify. JSON.stringify wraps
// in double quotes, under which the shell expands `$VAR`/backticks — a branch like
// `release/$2.0` would expand `$2` to a positional. Admin-configured integration
// branch names are not guaranteed to exclude `$`, and `$` is valid in git refs, so
// double-quoting is an injection/correctness risk. Single-quoting (with the embedded
// `'` → `'\''` escape) is literal and safe for slashes (e.g. "release/2026-06") too.
const shellQuote = (s: string): string => `'${s.replace(/'/g, "'\\''")}'`;
const localRef = shellQuote(branch);
const originRef = shellQuote(`origin/${branch}`);
let baseCommitSha: string | undefined;
try {
const { stdout } = await execAsync(
`git merge-base HEAD ${localRef} 2>/dev/null || git merge-base HEAD ${originRef}`,
{ cwd: worktreePath, encoding: "utf-8" },
);
baseCommitSha = stdout.trim() || undefined;
} catch (err: unknown) {
const errorMessage = err instanceof Error ? err.message : String(err);
logger?.warn(`merge-base failed, falling back to HEAD: ${errorMessage}`);
}
if (!baseCommitSha) {
try {
const { stdout } = await execAsync("git rev-parse HEAD", {
cwd: worktreePath,
encoding: "utf-8",
});
baseCommitSha = stdout.trim() || undefined;
} catch {
return undefined;
}
}
return baseCommitSha;
}