fix(server): name the cause of a failed git command #8645
Mergedwalid-baharwal(GitHub profile, opens in a new tab)merged 5 commits into
main fromrefs/pull/8645/headmerged by juliusmarminge View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/vcs/GitVcsDriverCore.ts line 1083Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -895,3 +927,4 @@895927 if (options.allowNonZeroExit || result.exitCode === 0) {896928 return Effect.succeed(result);897929 }930+ const reason = classifyGitFailure(result.stderr);
- @@ -344,8 +372,11 @@344372 stdoutLength: Schema.optional(Schema.Number),345373 stderrLength: Schema.optional(Schema.Number),346374 outputLength: Schema.optional(Schema.Number),375+ reason: Schema.optional(GitCommandFailureReason),347376 detail: Schema.String,348377 cause: Schema.optional(Schema.Defect()),349378 }) {350379 override get message(): string {351- return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;380+ const reason =381+ this.reason === undefined ? "" : ` ${gitCommandFailureReasonMessage(this.reason)}`;382+ return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
- apps/server/src/vcs/GitVcsDriverCore.ts line 488Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -393,0 +410,21 @@410+ ],411+ // Quoted-path forms only: an unquoted `fatal: <thing> already exists` also412+ // covers tag and ref collisions, which are not path collisions.413+ [/fatal: '[^']+' already exists|destination path .+ already exists/i, "path_already_exists"],414+];415+416+// Hooks write to the same stream git does, unprefixed and uncontrolled: a417+// pre-push hook echoing "authentication failed" would otherwise be read as a418+// credential failure. Git prefixes its own diagnostics, and states a push419+// rejection on a `! [rejected]` line, so only those lines are classified.420+const GIT_DIAGNOSTIC_LINE_PATTERN = /^(?:fatal|error|remote):|^!\s|^\s+!\s/;421+422+function classifyGitFailure(stderr: string): GitCommandFailureReason | null {423+ const diagnostics = stderr424+ .split(/\r?\n/)425+ .filter((line) => GIT_DIAGNOSTIC_LINE_PATTERN.test(line))426+ .join("\n");427+ if (diagnostics.length === 0) return null;428+ if (isNonRepositoryGitStderr(diagnostics)) return "not_a_repository";429+ for (const [pattern, reason] of GIT_FAILURE_REASON_PATTERNS) {430+ if (pattern.test(diagnostics)) return reason;
- apps/server/src/vcs/GitVcsDriverCore.ts line 473Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -393,0 +406,21 @@406+ ],407+ [408+ /(?:could not read from remote repository|does not appear to be a git repository|repository .+ not found)/i,409+ "remote_unreachable",410+ ],411+ // Quoted-path forms only: an unquoted `fatal: <thing> already exists` also412+ // covers tag and ref collisions, which are not path collisions.413+ [/fatal: '[^']+' already exists|destination path .+ already exists/i, "path_already_exists"],414+];415+416+// Hooks write to the same stream git does, unprefixed and uncontrolled: a417+// pre-push hook echoing "authentication failed" would otherwise be read as a418+// credential failure. Git prefixes its own diagnostics, and states a push419+// rejection on a `! [rejected]` line, so only those lines are classified.420+const GIT_DIAGNOSTIC_LINE_PATTERN = /^(?:fatal|error|remote):|^!\s|^\s+!\s/;421+// ssh reports the refusal itself, unprefixed, and git only adds a generic422+// "Could not read from remote repository" after it. Dropping ssh's line would423+// leave that generic one to be read as an unreachable remote when the real424+// cause is credentials, so these specific refusals are classified too.425+const SSH_TRANSPORT_REFUSAL_PATTERN =426+ /Permission denied \((?:publickey|password|keyboard-interactive)|Permission denied, please try again|Host key verification failed/i;
- packages/contracts/src/git.ts lines 354 to 366OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -337,0 +346,21 @@346+ "host_key_unverified",347+ "not_a_repository",348+ "path_already_exists",349+ "remote_unreachable",350+ "tag_would_be_clobbered",351+]);352+export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;353+354+const GIT_COMMAND_FAILURE_REASON_MESSAGES: Record<GitCommandFailureReason, string> = {355+ authentication_failed: "Git could not authenticate with the remote.",356+ branch_already_exists: "A branch with that name already exists.",357+ branch_checked_out_in_worktree: "That branch is already checked out in another worktree.",358+ host_key_unverified: "The remote's host key could not be verified.",359+ not_a_repository: "That directory is not a Git repository.",360+ path_already_exists: "The target path already exists.",361+ remote_unreachable: "The remote could not be reached.",362+ tag_would_be_clobbered: "A local tag differs from the remote tag and would be overwritten.",363+};364+365+export const gitCommandFailureReasonMessage = (reason: GitCommandFailureReason): string =>366+ GIT_COMMAND_FAILURE_REASON_MESSAGES[reason];
- apps/server/src/vcs/GitVcsDriverCore.ts line 425OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -393,0 +405,21 @@405+ [406+ /(?:authentication failed|could not read Username|could not read Password|permission denied \([a-z-]+(?:,[a-z-]+)*\)|permission denied, please try again)/i,407+ "authentication_failed",408+ ],409+ // Distinct from a credential failure: the remote answered and the key it410+ // presented is untrusted, so pointing the user at credentials would misdirect.411+ [/host key verification failed/i, "host_key_unverified"],412+ [413+ /(?:could not read from remote repository|does not appear to be a git repository|repository .+ not found)/i,414+ "remote_unreachable",415+ ],416+ // Quoted-path forms only: an unquoted `fatal: <thing> already exists` also417+ // covers tag and ref collisions, which are not path collisions.418+ [/fatal: '[^']+' already exists|destination path .+ already exists/i, "path_already_exists"],419+];420+421+// Hooks write to the same stream git does, unprefixed and uncontrolled: a422+// pre-push hook echoing "authentication failed" would otherwise be read as a423+// credential failure. Git prefixes its own diagnostics, and states a push424+// rejection on a `! [rejected]` line, so only those lines are classified.425+const GIT_DIAGNOSTIC_LINE_PATTERN = /^(?:fatal|error|remote):|^!\s|^\s+!\s/;
juliusmarminge added labelsmacroscope-review
- apps/server/src/vcs/GitVcsDriverCore.ts line 479OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -432,0 +459,21 @@459+460+// Hooks write to the same stream git does, and nothing distinguishes their461+// text from git's: a pre-push hook echoing "authentication failed" would462+// otherwise be read as a credential failure. Git prefixes its own diagnostics463+// and states a push rejection on a `! [rejected]` line, so only those are464+// classified. `remote:` is deliberately excluded — git prefixes every byte the465+// server sends that way, remote hook output included, so trusting it would466+// reintroduce the same false positive from the other end of the connection.467+const GIT_DIAGNOSTIC_LINE_PATTERN = /^(?:fatal|error):|^!\s|^\s+!\s/;468+// ssh reports the refusal itself, unprefixed, and git only adds a generic469+// "Could not read from remote repository" after it. Dropping ssh's line would470+// leave that generic one to be read as an unreachable remote when the real471+// cause is credentials, so these specific refusals are classified too.472+const SSH_TRANSPORT_REFUSAL_PATTERN =473+ /Permission denied \((?:publickey|password|keyboard-interactive)|Permission denied, please try again|Host key verification failed/i;474+475+function classifyGitFailure(stderr: string): GitCommandFailureReason | null {476+ const diagnostics = stderr477+ .split(/\r?\n/)478+ .filter(479+ (line) => GIT_DIAGNOSTIC_LINE_PATTERN.test(line) || SSH_TRANSPORT_REFUSAL_PATTERN.test(line),apps/server/src/vcs/GitVcsDriverCore.ts line 995Resolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -932,3 +989,4 @@932989 yield* trace2Monitor.flush;933990934991 if (!input.allowNonZeroExit && exitCode !== 0) {992+ const reason = classifyGitFailure(stderr.text);
juliusmarminge merged thisf973b1d
juliusmarminge closed this
Sign in to join this conversation.


Navigate logical layers of code changes, visualize relationships, and explore their blast radius.
Important
Review skipped#
We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting
@coderabbitai full review.You can disable this status message by setting the
reviews.review_statustofalsein the CodeRabbit configuration file.Use the checkbox below for a quick retry:
No actionable comments were generated in the recent review. 🎉
ℹ️ Recent review info
⚙️ Run configuration
4975c958-9692-4c97-88c2-254e7159240e📥 Commits
Reviewing files that changed from the base of the PR and between 3a596fdf7f8077adbaf949518be0c7dbf402898e and 99756d50dda88420f366f2776504e71bdb24b4e2.
📒 Files selected for processing (2)
apps/server/src/vcs/GitVcsDriverCore.test.tsapps/server/src/vcs/GitVcsDriverCore.tsIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.
📝 Walkthrough
Walkthrough#
Git command errors now carry a recognized failure reason when stderr matches a supported diagnostic. The reason appears in the error message, while existing error details remain available.
Changes#
Git failure reason classification
packages/contracts/src/git.tsGitCommandFailureReasonschema and type.GitCommandErroraccepts an optional reason and appends it to the message when present.apps/server/src/vcs/GitVcsDriverCore.ts,apps/server/src/vcs/GitVcsDriverCore.test.tsPriority: ➖ Normal
Estimated code review effort: 3 (Moderate) | ~20 minutes
Change: Feature · Severity of issue fixed: Medium
Suggested reviewers:
juliusmarmingeMerge Risk: 🔵 Low · up to
99756Git failures can now carry a reason tag. Coverage is partial: non-English Git output and fetch errors may not get a tag. Existing error messages are unchanged in those cases, so the merge risk is low.
Security Architecture Review#
Security architecture risk: 🔵 Low · up to
99756The change exposes only bounded diagnostic tags, preserving the existing exclusion of raw stderr from command errors. No new privileges or security-control bypass were identified. Compatibility with older deployed clients remains unverified.
Retained concerns No architecture-level concerns identified.
Security review details
Security Blast Radius
Trust Boundaries and Controls
🚥 Pre-merge checks | ✅ 3 | ❌ 2
❌ Failed checks (2 warnings)#
reasontags, message suffix, shared failure-path classification, and redaction tests address [#4380]. The head also excludesremote:SSH-refusal lines and adds remote-hook tests. Howeve…fatal:orerror:output as Git diagnostics. Add a failing local-hook test that prints a diagnostic-shaped line and assert that the error has no reason.✅ Passed checks (3 passed)
#4380]. The reviewed changes contain no demonstrated unrelated work.#4380but does not include explicit maintainer approval or explain why the change qualifies for the smal…Full details: Linked Issues check
Explanation
The closed
reasontags, message suffix, shared failure-path classification, and redaction tests address [#4380]. The head also excludesremote:SSH-refusal lines and adds remote-hook tests. However, the Git-diagnostic filter still trustsfatal:anderror:lines. A local hook can printfatal: authentication failed, which can be classified as a Git authentication failure even when the hook caused the command failure. The local-hook test covers only unprefixed output, so it does not catch this false reason.✨ Finishing Touches 💡 1
🧪 Generate unit tests (beta)
🛠️ Fix failing CI checks 💡
Comment `@coderabbitai help` to get the list of available commands.