From 0587d8abdad2e38a3549cd5abefcbf87a150ddb0 Mon Sep 17 00:00:00 2001 From: Julius Marminge Date: Fri, 1 May 2026 18:15:24 -0700 Subject: [PATCH] Use typed exit codes in VCS process results - Switch VCS process and git driver results to `ChildProcessSpawner.ExitCode` - Update status checks and fallback results to use the new typed exit code - Refresh tests to match the new result shape --- apps/server/src/git/GitManager.test.ts | 8 +- apps/server/src/server.test.ts | 3 +- .../SourceControlProviderRegistry.test.ts | 5 +- .../SourceControlProviderRegistry.ts | 2 +- apps/server/src/vcs/GitVcsDriver.test.ts | 3 +- apps/server/src/vcs/GitVcsDriver.ts | 3 +- apps/server/src/vcs/GitVcsDriverCore.ts | 73 +++++++++++-------- apps/server/src/vcs/VcsDriverRegistry.test.ts | 3 +- apps/server/src/vcs/VcsProcess.ts | 3 +- 9 files changed, 62 insertions(+), 41 deletions(-) diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.ts index 3283d31c49..1c4e15fea1 100644 --- a/apps/server/src/git/GitManager.test.ts +++ b/apps/server/src/git/GitManager.test.ts @@ -211,7 +211,11 @@ function runGit( args: readonly string[], allowNonZeroExit = false, ): Effect.Effect< - { readonly code: number; readonly stdout: string; readonly stderr: string }, + { + readonly exitCode: GitVcsDriver.ExecuteGitResult["exitCode"]; + readonly stdout: string; + readonly stderr: string; + }, GitCommandError, GitVcsDriver.GitVcsDriver > { @@ -224,7 +228,7 @@ function runGit( allowNonZeroExit, }); return { - code: result.code, + exitCode: result.exitCode, stdout: result.stdout, stderr: result.stderr, }; diff --git a/apps/server/src/server.test.ts b/apps/server/src/server.test.ts index eadcbb0f47..9e13ad999a 100644 --- a/apps/server/src/server.test.ts +++ b/apps/server/src/server.test.ts @@ -39,6 +39,7 @@ import { Path, Stream, } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; import { FetchHttpClient, HttpBody, @@ -384,7 +385,7 @@ const buildAppUnderTest = (options?: { }, execute: () => Effect.succeed({ - exitCode: 0, + exitCode: ChildProcessSpawner.ExitCode(0), stdout: "", stderr: "", stdoutTruncated: false, diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts index f5e8b8f880..7c130ab1ed 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import { Effect, Layer } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; import { GitHubCli } from "./GitHubCli.ts"; import * as GitVcsDriver from "../vcs/GitVcsDriver.ts"; @@ -8,9 +9,7 @@ import * as SourceControlProviderRegistry from "./SourceControlProviderRegistry. const processResult = (stdout: string) => ({ stdout, stderr: "", - code: 0, - signal: null, - timedOut: false, + exitCode: ChildProcessSpawner.ExitCode(0), stdoutTruncated: false, stderrTruncated: false, }); diff --git a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts index c76f1a554f..52a863808f 100644 --- a/apps/server/src/sourceControl/SourceControlProviderRegistry.ts +++ b/apps/server/src/sourceControl/SourceControlProviderRegistry.ts @@ -89,7 +89,7 @@ export const make = Effect.fn("makeSourceControlProviderRegistry")(function* () }) .pipe( Effect.map((result) => - result.code === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null, + result.exitCode === 0 ? firstRemoteUrlFromVerboseOutput(result.stdout) : null, ), Effect.mapError((error) => providerDetectionError("detectProvider", cwd, error)), )); diff --git a/apps/server/src/vcs/GitVcsDriver.test.ts b/apps/server/src/vcs/GitVcsDriver.test.ts index 422b0c7fe4..0e5ba5b82e 100644 --- a/apps/server/src/vcs/GitVcsDriver.test.ts +++ b/apps/server/src/vcs/GitVcsDriver.test.ts @@ -1,5 +1,6 @@ import * as NodeServices from "@effect/platform-node/NodeServices"; import { Effect, FileSystem, Layer, Path, PlatformError } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; import { assert, it } from "@effect/vitest"; import { GitCommandError } from "@t3tools/contracts"; @@ -85,7 +86,7 @@ it.effect("GitVcsDriver forwards execute env to the VCS process", () => { Effect.sync(() => { observedEnv = input.env; return { - exitCode: 0, + exitCode: ChildProcessSpawner.ExitCode(0), stdout: "", stderr: "", stdoutTruncated: false, diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.ts index e103da59c6..605242427f 100644 --- a/apps/server/src/vcs/GitVcsDriver.ts +++ b/apps/server/src/vcs/GitVcsDriver.ts @@ -1,4 +1,5 @@ import { Context, DateTime, Effect, Layer, Option } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; import { GitCommandError, @@ -35,7 +36,7 @@ export interface ExecuteGitInput { } export interface ExecuteGitResult { - readonly code: number; + readonly exitCode: ChildProcessSpawner.ExitCode; readonly stdout: string; readonly stderr: string; readonly stdoutTruncated: boolean; diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts index 97edb8c49a..3ba1c27981 100644 --- a/apps/server/src/vcs/GitVcsDriverCore.ts +++ b/apps/server/src/vcs/GitVcsDriverCore.ts @@ -448,7 +448,7 @@ const createTrace2Monitor = Effect.fn("createTrace2Monitor")(function* ( if (event === "child_exit") { hookStartByChildKey.delete(childKey); - const code = traceRecord.success.code; + const code = traceRecord.success.exitCode; const exitCode = typeof code === "number" && Number.isInteger(code) ? code : null; const now = yield* DateTime.now; const durationMs = started @@ -666,7 +666,6 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* input.progress?.onStderrLine, ), child.exitCode.pipe( - Effect.map((value) => Number(value)), Effect.mapError(toGitCommandError(commandInput, "failed to report exit code.")), ), input.stdin === undefined @@ -693,7 +692,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* } return { - code: exitCode, + exitCode, stdout: stdout.text, stderr: stderr.text, stdoutTruncated: stdout.truncated, @@ -761,7 +760,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ...(options.progress ? { progress: options.progress } : {}), }).pipe( Effect.flatMap((result) => { - if (options.allowNonZeroExit || result.code === 0) { + if (options.allowNonZeroExit || result.exitCode === 0) { return Effect.succeed(result); } const stderr = result.stderr.trim(); @@ -778,7 +777,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* operation, cwd, args, - `${commandLabel(args)} failed: code=${result.code ?? "null"}`, + `${commandLabel(args)} failed: code=${result.exitCode ?? "null"}`, ), ); }), @@ -823,7 +822,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* allowNonZeroExit: true, timeoutMs: 5_000, }, - ).pipe(Effect.map((result) => result.code === 0)); + ).pipe(Effect.map((result) => result.exitCode === 0)); const resolveAvailableBranchName = Effect.fn("resolveAvailableBranchName")(function* ( cwd: string, @@ -939,7 +938,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* { allowNonZeroExit: true }, ).pipe( Effect.map((result) => { - if (result.code !== 0) { + if (result.exitCode !== 0) { return null; } return parseDefaultBranchFromRemoteHeadRef(result.stdout, remoteName); @@ -958,12 +957,12 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* { allowNonZeroExit: true, }, - ).pipe(Effect.map((result) => result.code === 0)); + ).pipe(Effect.map((result) => result.exitCode === 0)); const originRemoteExists = (cwd: string): Effect.Effect => executeGit("GitVcsDriver.originRemoteExists", cwd, ["remote", "get-url", "origin"], { allowNonZeroExit: true, - }).pipe(Effect.map((result) => result.code === 0)); + }).pipe(Effect.map((result) => result.exitCode === 0)); const listRemoteNames = (cwd: string): Effect.Effect, GitCommandError> => runGitStdout("GitVcsDriver.listRemoteNames", cwd, ["remote"]).pipe( @@ -1115,7 +1114,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ["rev-list", "--count", `${baseRef}..HEAD`], { allowNonZeroExit: true }, ); - if (result.code !== 0) { + if (result.exitCode !== 0) { return 0; } @@ -1140,7 +1139,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const branchLastCommit = new Map(); - if (branchRecency.code !== 0) { + if (branchRecency.exitCode !== 0) { return branchLastCommit; } @@ -1173,7 +1172,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* return NON_REPOSITORY_STATUS_DETAILS; } - if (statusResult.code !== 0) { + if (statusResult.exitCode !== 0) { const stderr = statusResult.stderr.trim(); return yield* createGitCommandError( "GitVcsDriver.statusDetails.status", @@ -1206,7 +1205,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const statusStdout = statusResult.stdout; const defaultBranch = - defaultRefResult.code === 0 + defaultRefResult.exitCode === 0 ? defaultRefResult.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : null; @@ -1614,7 +1613,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ).pipe( Effect.catchIf(isMissingGitCwdError, () => Effect.succeed({ - code: 128, + exitCode: ChildProcessSpawner.ExitCode(128), stdout: "", stderr: "fatal: not a git repository", stdoutTruncated: false, @@ -1623,7 +1622,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ), ); - if (localBranchResult.code !== 0) { + if (localBranchResult.exitCode !== 0) { const stderr = localBranchResult.stderr.trim(); if (stderr.toLowerCase().includes("not a git repository")) { return { @@ -1654,7 +1653,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.catch((error) => Effect.logWarning( `GitVcsDriver.listRefs: remote refName lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote refName list.`, - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), + ).pipe( + Effect.as({ + exitCode: ChildProcessSpawner.ExitCode(1), + stdout: "", + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + } satisfies ExecuteGitResult), + ), ), ); @@ -1670,7 +1677,15 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* Effect.catch((error) => Effect.logWarning( `GitVcsDriver.listRefs: remote name lookup failed for ${input.cwd}: ${error.message}. Falling back to an empty remote name list.`, - ).pipe(Effect.as({ code: 1, stdout: "", stderr: "" })), + ).pipe( + Effect.as({ + exitCode: ChildProcessSpawner.ExitCode(1), + stdout: "", + stderr: "", + stdoutTruncated: false, + stderrTruncated: false, + } satisfies ExecuteGitResult), + ), ), ); @@ -1703,25 +1718,25 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* ); const remoteNames = - remoteNamesResult.code === 0 ? parseRemoteNames(remoteNamesResult.stdout) : []; - if (remoteBranchResult.code !== 0 && remoteBranchResult.stderr.trim().length > 0) { + remoteNamesResult.exitCode === 0 ? parseRemoteNames(remoteNamesResult.stdout) : []; + if (remoteBranchResult.exitCode !== 0 && remoteBranchResult.stderr.trim().length > 0) { yield* Effect.logWarning( - `GitVcsDriver.listRefs: remote refName lookup returned code ${remoteBranchResult.code} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote refName list.`, + `GitVcsDriver.listRefs: remote refName lookup returned code ${remoteBranchResult.exitCode} for ${input.cwd}: ${remoteBranchResult.stderr.trim()}. Falling back to an empty remote refName list.`, ); } - if (remoteNamesResult.code !== 0 && remoteNamesResult.stderr.trim().length > 0) { + if (remoteNamesResult.exitCode !== 0 && remoteNamesResult.stderr.trim().length > 0) { yield* Effect.logWarning( - `GitVcsDriver.listRefs: remote name lookup returned code ${remoteNamesResult.code} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`, + `GitVcsDriver.listRefs: remote name lookup returned code ${remoteNamesResult.exitCode} for ${input.cwd}: ${remoteNamesResult.stderr.trim()}. Falling back to an empty remote name list.`, ); } const defaultBranch = - defaultRef.code === 0 + defaultRef.exitCode === 0 ? defaultRef.stdout.trim().replace(/^refs\/remotes\/origin\//, "") : null; const worktreeMap = new Map(); - if (worktreeList.code === 0) { + if (worktreeList.exitCode === 0) { let currentPath: string | null = null; for (const line of worktreeList.stdout.split("\n")) { if (line.startsWith("worktree ")) { @@ -1762,7 +1777,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }); const remoteBranches = - remoteBranchResult.code === 0 + remoteBranchResult.exitCode === 0 ? remoteBranchResult.stdout .split("\n") .map(parseBranchLine) @@ -1943,7 +1958,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* timeoutMs: 5_000, allowNonZeroExit: true, }, - ).pipe(Effect.map((result) => result.code === 0)), + ).pipe(Effect.map((result) => result.exitCode === 0)), executeGit( "GitVcsDriver.switchRef.remoteExists", input.cwd, @@ -1952,7 +1967,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* timeoutMs: 5_000, allowNonZeroExit: true, }, - ).pipe(Effect.map((result) => result.code === 0)), + ).pipe(Effect.map((result) => result.exitCode === 0)), ], { concurrency: "unbounded" }, ); @@ -1968,7 +1983,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* }, ).pipe( Effect.map((result) => - result.code === 0 + result.exitCode === 0 ? parseTrackingBranchByUpstreamRef(result.stdout, input.refName) : null, ), @@ -1986,7 +2001,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function* timeoutMs: 5_000, allowNonZeroExit: true, }, - ).pipe(Effect.map((result) => result.code === 0)) + ).pipe(Effect.map((result) => result.exitCode === 0)) : false; const checkoutArgs = localInputExists diff --git a/apps/server/src/vcs/VcsDriverRegistry.test.ts b/apps/server/src/vcs/VcsDriverRegistry.test.ts index 4741a6a12b..425b7c1865 100644 --- a/apps/server/src/vcs/VcsDriverRegistry.test.ts +++ b/apps/server/src/vcs/VcsDriverRegistry.test.ts @@ -1,5 +1,6 @@ import { assert, it } from "@effect/vitest"; import { Effect, Layer } from "effect"; +import { ChildProcessSpawner } from "effect/unstable/process"; import { describe } from "vitest"; import { VcsProcess, type VcsProcessInput, type VcsProcessOutput } from "./VcsProcess.ts"; @@ -7,7 +8,7 @@ import { VcsProjectConfig } from "./VcsProjectConfig.ts"; import { VcsDriverRegistry, make as makeVcsDriverRegistry } from "./VcsDriverRegistry.ts"; const processOutput = (stdout: string): VcsProcessOutput => ({ - exitCode: 0, + exitCode: ChildProcessSpawner.ExitCode(0), stdout, stderr: "", stdoutTruncated: false, diff --git a/apps/server/src/vcs/VcsProcess.ts b/apps/server/src/vcs/VcsProcess.ts index 5196a870cd..7576ae1644 100644 --- a/apps/server/src/vcs/VcsProcess.ts +++ b/apps/server/src/vcs/VcsProcess.ts @@ -23,7 +23,7 @@ export interface VcsProcessInput { } export interface VcsProcessOutput { - readonly exitCode: number; + readonly exitCode: ChildProcessSpawner.ExitCode; readonly stdout: string; readonly stderr: string; readonly stdoutTruncated: boolean; @@ -147,7 +147,6 @@ export const make = Effect.fn("makeVcsProcess")(function* () { input.truncateOutputAtMaxBytes ?? false, ), child.exitCode.pipe( - Effect.map((value) => Number(value)), Effect.mapError( (cause) => new VcsOutputDecodeError({