Skip to content

Commit b0ef3b3

Browse files
fix(server): keep missing-ref diagnostics focused and stable
1 parent 746e303 commit b0ef3b3

4 files changed

Lines changed: 71 additions & 110 deletions

File tree

‎apps/server/src/vcs/GitVcsDriverCore.test.ts‎

Lines changed: 53 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1269,88 +1269,74 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
12691269
}),
12701270
);
12711271

1272-
it.effect("names the missing base ref behind a failed worktree add", () =>
1273-
Effect.gen(function* () {
1274-
const parent = yield* makeTmpDir();
1275-
const pathService = yield* Path.Path;
1276-
const cwd = pathService.join(parent, "repo");
1277-
const fileSystem = yield* FileSystem.FileSystem;
1278-
yield* fileSystem.makeDirectory(cwd);
1279-
const driver = yield* GitVcsDriver.GitVcsDriver;
1280-
yield* initRepoWithCommit(cwd);
1281-
1282-
const error = yield* driver
1283-
.createWorktree({
1284-
cwd,
1285-
refName: "t3/deleted-base",
1286-
newRefName: "t3/new-thread",
1287-
path: pathService.join(parent, "worktree"),
1288-
})
1289-
.pipe(Effect.flip);
1290-
1291-
assert.deepInclude(error, {
1292-
_tag: "GitCommandError",
1293-
detail: "git worktree add failed",
1294-
reason: "ref_not_found",
1295-
});
1296-
assert.include(error.message, "git worktree add failed (ref_not_found)");
1297-
assert.notInclude(error.message, "t3/deleted-base");
1298-
}),
1299-
);
1300-
13011272
it.effect.each([
1302-
{ label: "worktree add", args: ["worktree", "add", "../worktree", "missing"] },
1303-
{ label: "switch", args: ["switch", "missing"] },
1304-
{ label: "branch", args: ["branch", "new-branch", "missing"] },
1305-
{ label: "checkout -b", args: ["checkout", "-b", "new-branch", "missing"] },
1306-
{ label: "checkout -b of a quoted name", args: ["checkout", "-b", "new", "o'brien"] },
1307-
{ label: "merge-base", args: ["merge-base", "HEAD", "missing"] },
1308-
{ label: "rev-parse --verify", args: ["rev-parse", "--verify", "missing"] },
1309-
{ label: "log", args: ["log", "-1", "missing", "--"] },
1310-
])("names a ref that does not exist behind a failed $label", ({ args }) =>
1311-
Effect.gen(function* () {
1312-
const parent = yield* makeTmpDir();
1313-
const pathService = yield* Path.Path;
1314-
const cwd = pathService.join(parent, "repo");
1315-
const fileSystem = yield* FileSystem.FileSystem;
1316-
yield* fileSystem.makeDirectory(cwd);
1317-
const driver = yield* GitVcsDriver.GitVcsDriver;
1318-
yield* initRepoWithCommit(cwd);
1319-
1320-
const error = yield* driver
1321-
.execute({
1322-
operation: "GitVcsDriver.test.missingRef",
1323-
cwd,
1324-
args,
1325-
env: { LC_ALL: "C" },
1326-
})
1327-
.pipe(Effect.flip);
1328-
1329-
assert.deepInclude(error, { reason: "ref_not_found" });
1330-
}),
1273+
{ action: "worktree add", detail: "git worktree add failed" },
1274+
{ action: "switch", detail: "git checkout failed" },
1275+
])(
1276+
"reports a missing ref during $action with stable, redacted diagnostics",
1277+
({ action, detail }) =>
1278+
Effect.gen(function* () {
1279+
const cwd = yield* makeTmpDir();
1280+
yield* initRepoWithCommit(cwd);
1281+
const path = yield* Path.Path;
1282+
const refName = "private/deleted-base";
1283+
const locales: Array<string | undefined> = [];
1284+
const delegate = yield* ChildProcessSpawner.ChildProcessSpawner;
1285+
const spawner = ChildProcessSpawner.make((command) => {
1286+
if (
1287+
ChildProcess.isStandardCommand(command) &&
1288+
(command.args.includes("worktree") || command.args[0] === "checkout")
1289+
) {
1290+
locales.push(command.options.env?.LC_ALL);
1291+
}
1292+
return delegate.spawn(command);
1293+
});
1294+
const driver = yield* makeGitVcsDriverCore().pipe(
1295+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
1296+
Effect.provide(layerServerConfig),
1297+
);
1298+
const error =
1299+
action === "switch"
1300+
? yield* driver.switchRef({ cwd, refName }).pipe(Effect.flip)
1301+
: yield* driver
1302+
.createWorktree({
1303+
cwd,
1304+
refName,
1305+
newRefName: "t3/new-thread",
1306+
path: path.join(cwd, "worktree"),
1307+
})
1308+
.pipe(Effect.flip);
1309+
1310+
assert.deepInclude(error, { _tag: "GitCommandError", detail, reason: "ref_not_found" });
1311+
assert.include(error.message, `${detail} (ref_not_found)`);
1312+
assert.notInclude(error.message, refName);
1313+
assert.notInclude(yield* encodeGitCommandError(error), refName);
1314+
assert.deepEqual(locales, ["C"]);
1315+
assert.notProperty(error, "stderr");
1316+
assert.notProperty(error, "args");
1317+
}),
13311318
);
13321319

13331320
it.effect.each([
13341321
// Git cannot tell a missing revision from a missing file in these.
13351322
{ label: "an argument that may be a path", args: ["log", "-1", "missing"] },
1336-
{ label: "a pathspec", args: ["checkout", "missing"] },
13371323
{
13381324
label: "a pathspec worded like a diagnostic",
13391325
args: ["checkout", "HEAD", "--", "fatal: invalid reference: missing"],
13401326
},
13411327
// The tag exists; it names a tree, which git reports in the same words.
1342-
{ label: "a worktree from a non-commit", args: ["worktree", "add", "../tree", "tree-tag"] },
1343-
{ label: "a non-commit revision", args: ["rev-parse", "--verify", "tree-tag^{commit}"] },
1328+
{
1329+
label: "a worktree from a non-commit",
1330+
args: ["worktree", "add", "tree-worktree", "tree-tag"],
1331+
},
13441332
])("leaves $label unclassified rather than calling it a missing ref", ({ args }) =>
13451333
Effect.gen(function* () {
1346-
const parent = yield* makeTmpDir();
1347-
const pathService = yield* Path.Path;
1348-
const cwd = pathService.join(parent, "repo");
1349-
const fileSystem = yield* FileSystem.FileSystem;
1350-
yield* fileSystem.makeDirectory(cwd);
1334+
const cwd = yield* makeTmpDir();
13511335
const driver = yield* GitVcsDriver.GitVcsDriver;
13521336
yield* initRepoWithCommit(cwd);
1353-
yield* git(cwd, ["tag", "tree-tag", "HEAD^{tree}"]);
1337+
if (args.includes("tree-tag")) {
1338+
yield* git(cwd, ["tag", "tree-tag", "HEAD^{tree}"]);
1339+
}
13541340

13551341
const error = yield* driver
13561342
.execute({
@@ -3306,18 +3292,6 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
33063292
}),
33073293
);
33083294

3309-
it.effect("names the missing ref behind a failed switch", () =>
3310-
Effect.gen(function* () {
3311-
const cwd = yield* makeTmpDir();
3312-
yield* initRepoWithCommit(cwd);
3313-
const driver = yield* GitVcsDriver.GitVcsDriver;
3314-
3315-
const error = yield* driver.switchRef({ cwd, refName: "deleted-branch" }).pipe(Effect.flip);
3316-
3317-
assert.deepInclude(error, { detail: "git checkout failed", reason: "ref_not_found" });
3318-
}),
3319-
);
3320-
33213295
it.effect("still creates and reuses remote tracking branches and allows detached refs", () =>
33223296
Effect.gen(function* () {
33233297
const cwd = yield* makeTmpDir();

‎apps/server/src/vcs/GitVcsDriverCore.ts‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,17 @@ const GIT_FAILURE_REASON_PATTERNS: ReadonlyArray<readonly [RegExp, GitCommandFai
471471
// Quoted-path forms only: an unquoted `fatal: <thing> already exists` also
472472
// covers tag and ref collisions, which are not path collisions.
473473
[/fatal: '[^']+' already exists|destination path .+ already exists/i, "path_already_exists"],
474-
// Wordings git uses for a ref or revision that does not resolve. `unknown
475-
// revision or path not in the working tree` and `pathspec ... did not match`
476-
// are left out: they also cover a missing file. Anchored because a quoted
477-
// path in another diagnostic can contain these words.
474+
// Ambiguous revision/path and pathspec failures can name files, so omit them.
475+
// Anchor to a diagnostic line to avoid matching quoted paths in other errors.
478476
[
479477
/^fatal: (?:invalid reference: |not a valid object name|needed a single revision|bad revision |'.+' is not a commit and a branch '.+' cannot be created from it)/im,
480478
"ref_not_found",
481479
],
482480
];
483481

484-
// Git words a ref that exists but is not a commit the same way as a missing
485-
// one, and only an `error:` line before it tells them apart.
486-
const GIT_NOT_A_COMMIT_PATTERN = /error: .+(?:, not a commit|expected commit type)/i;
482+
// Existing non-commit objects share the missing-ref fatal diagnostic; Git
483+
// distinguishes them with a separate error line.
484+
const GIT_NOT_A_COMMIT_PATTERN = /^error: .+(?:, not a commit|expected commit type)/im;
487485

488486
// Hooks write to the same stream git does, and nothing distinguishes their text
489487
// from git's: a pre-push hook echoing "authentication failed" would otherwise

‎packages/contracts/src/git.test.ts‎

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
VcsStatusLocalResult,
1313
} from "./git.ts";
1414

15+
const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
1516
const decodeCreateWorktreeInput = Schema.decodeUnknownSync(VcsCreateWorktreeInput);
1617
const decodePreparePullRequestThreadInput = Schema.decodeUnknownSync(
1718
GitPreparePullRequestThreadInput,
@@ -25,26 +26,15 @@ const decodeResolvePullRequestResult = Schema.decodeUnknownSync(GitResolvePullRe
2526
const decodeVcsStatusLocalResult = Schema.decodeUnknownSync(VcsStatusLocalResult);
2627

2728
describe("GitCommandError", () => {
28-
const decodeGitCommandError = Schema.decodeUnknownSync(Schema.toCodecJson(GitCommandError));
29-
const encoded = {
30-
_tag: "GitCommandError",
31-
operation: "GitVcsDriver.createWorktree",
32-
command: "git",
33-
cwd: "/repo",
34-
detail: "git worktree add failed",
35-
};
36-
37-
it("names a known failure reason in the message", () => {
38-
const error = decodeGitCommandError({ ...encoded, reason: "ref_not_found" });
39-
40-
expect(error.reason).toBe("ref_not_found");
41-
expect(error.message).toBe(
42-
"Git command failed in GitVcsDriver.createWorktree (/repo): git worktree add failed (ref_not_found)",
43-
);
44-
});
45-
46-
it("keeps decoding when a newer server sends a reason this build does not know", () => {
47-
const error = decodeGitCommandError({ ...encoded, reason: "added_by_a_newer_server" });
29+
it("decodes an unknown failure reason as absent", () => {
30+
const error = decodeGitCommandError({
31+
_tag: "GitCommandError",
32+
operation: "GitVcsDriver.createWorktree",
33+
command: "git",
34+
cwd: "/repo",
35+
detail: "git worktree add failed",
36+
reason: "added_by_a_newer_server",
37+
});
4838

4939
expect(error.reason).toBeUndefined();
5040
expect(error.message).toBe(

‎packages/contracts/src/git.ts‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,10 +345,9 @@ export const VcsPullResult = Schema.Struct({
345345
});
346346
export type VcsPullResult = typeof VcsPullResult.Type;
347347

348-
// Git's stderr itself stays off the error: it echoes argv and remote URLs,
349-
// which can hold credentials. The tag names the cause for logs and callers; it
350-
// does not select a message. The set grows, so GitCommandError decodes a tag
351-
// this build does not know as absent.
348+
// Git stderr can echo credentials from argv and remote URLs. Only a fixed
349+
// reason reaches the error and its diagnostic suffix; newer, unknown reasons
350+
// decode as absent through GitCommandError.
352351
export const GitCommandFailureReason = Schema.Literals([
353352
"authentication_failed",
354353
"branch_already_exists",

0 commit comments

Comments
 (0)