Skip to content

Commit a2bd16d

Browse files
committed
fix(mcp): base-branch fallback fails on git errors and skips dangling defaults
A failed git read while picking an unbound schedule's base now returns the retryable orchestration_error and saves nothing, instead of being read as a missing branch. A folder that is not a git repository still gets main. An origin default whose branch no longer exists is skipped for the checked-out branch.
1 parent 2049cd0 commit a2bd16d

2 files changed

Lines changed: 93 additions & 30 deletions

File tree

‎apps/server/src/mcp/OrchestratorMcpService.test.ts‎

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { assert, describe, it } from "@effect/vitest";
33
import {
44
CommandId,
55
EnvironmentId,
6+
GitCommandError,
67
NodeId,
78
type OrchestrationV2ThreadShell,
89
type ScheduledTask,
@@ -2004,13 +2005,14 @@ describe("OrchestratorMcpService scheduled tasks that launch a fresh worktree pe
20042005
return cwd;
20052006
});
20062007

2007-
/** The service over a real git driver, recording every scheduled task upsert. */
2008+
/** The service over a real git driver (or `layerGit`), recording every scheduled task upsert. */
20082009
const withService = <A, E>(
20092010
workspaceRoot: string,
20102011
body: (
20112012
service: OrchestratorMcpService.OrchestratorMcpService["Service"],
20122013
upserts: Ref.Ref<ReadonlyArray<ScheduledTaskUpsertInput>>,
20132014
) => Effect.Effect<A, E>,
2015+
layerGit: typeof layerRealGit = layerRealGit,
20142016
) =>
20152017
Effect.gen(function* () {
20162018
const upserts = yield* Ref.make<ReadonlyArray<ScheduledTaskUpsertInput>>([]);
@@ -2055,7 +2057,7 @@ describe("OrchestratorMcpService scheduled tasks that launch a fresh worktree pe
20552057
}),
20562058
Layer.mock(SecretRequests.SecretRequests)({}),
20572059
ServerSettings.layerTest(),
2058-
layerRealGit,
2060+
layerGit,
20592061
Layer.mock(ScheduledTaskService.ScheduledTaskService)({
20602062
list: () =>
20612063
Ref.get(upserts).pipe(Effect.map((all) => ({ tasks: all.map(taskFromUpsert) }))),
@@ -2177,4 +2179,53 @@ describe("OrchestratorMcpService scheduled tasks that launch a fresh worktree pe
21772179
);
21782180
}).pipe(Effect.provide(NodeServices.layer)),
21792181
);
2182+
2183+
it.effect("skips an origin default that no longer exists for the checked-out branch", () =>
2184+
Effect.gen(function* () {
2185+
const workspaceRoot = yield* cloneOf("master");
2186+
yield* git(workspaceRoot, ["checkout", "-b", "feature"]);
2187+
// origin/HEAD still names a branch deleted since the clone.
2188+
yield* git(workspaceRoot, [
2189+
"symbolic-ref",
2190+
"refs/remotes/origin/HEAD",
2191+
"refs/remotes/origin/gone",
2192+
]);
2193+
yield* expectBase(workspaceRoot, "feature");
2194+
}).pipe(Effect.provide(NodeServices.layer)),
2195+
);
2196+
2197+
it.effect("fails retryably without saving a base when the branches cannot be listed", () =>
2198+
Effect.gen(function* () {
2199+
const workspaceRoot = yield* localRepository("trunk");
2200+
const layerGitFailingBranchList = Layer.effect(
2201+
GitVcsDriver.GitVcsDriver,
2202+
Effect.gen(function* () {
2203+
const driver = yield* GitVcsDriver.GitVcsDriver;
2204+
return GitVcsDriver.GitVcsDriver.of({
2205+
...driver,
2206+
listLocalBranchNames: (cwd) =>
2207+
Effect.fail(
2208+
new GitCommandError({
2209+
operation: "test.listLocalBranchNames",
2210+
command: "git",
2211+
cwd,
2212+
detail: "fatal: unable to read refs",
2213+
}),
2214+
),
2215+
});
2216+
}),
2217+
).pipe(Layer.provide(layerRealGit));
2218+
yield* withService(
2219+
workspaceRoot,
2220+
(service, upserts) =>
2221+
Effect.gen(function* () {
2222+
const error = yield* scheduleUnbound(service).pipe(Effect.flip);
2223+
assert.equal(error.code, "orchestration_error");
2224+
assert.match(error.message, /try again/);
2225+
assert.equal((yield* Ref.get(upserts)).length, 0);
2226+
}),
2227+
layerGitFailingBranchList,
2228+
);
2229+
}).pipe(Effect.provide(NodeServices.layer)),
2230+
);
21802231
});

‎apps/server/src/mcp/OrchestratorMcpService.ts‎

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
CommandId,
3+
type GitCommandError,
34
type RunId,
45
isProviderAvailable,
56
MessageId,
@@ -913,42 +914,53 @@ const make = Effect.gen(function* () {
913914
* post into the existing thread (the strategy is unused, keep root); unbound
914915
* runs launch a fresh worktree per run from main. Only when the project has no
915916
* main (neither `origin/main` nor a local branch, the refs a run's launch
916-
* falls back across) does it branch from origin's default branch, else the
917-
* checked-out branch, else (detached HEAD or not a git repository) main. Every
918-
* check reads local refs; nothing is fetched.
917+
* falls back across) does it branch from origin's default branch if that
918+
* branch exists, else the checked-out branch, else (detached HEAD or not a git
919+
* repository) main. Every check reads local refs; nothing is fetched. A git
920+
* read that fails saves nothing, so a guessed base is never stored.
919921
*/
920922
const scheduledTaskWorkspaceStrategy = (
921923
boundToThread: boolean,
922924
projectId: ProjectId,
923925
): Effect.Effect<ScheduledTask["workspaceStrategy"], OrchestratorMcpFailure> =>
924926
Effect.gen(function* () {
925927
if (boundToThread) return { type: "root" };
926-
const { workspaceRoot } = yield* requireProject(projectId);
927-
const hasMain =
928-
(yield* git
929-
.remoteBranchExists({ cwd: workspaceRoot, remoteName: "origin", refName: "main" })
930-
.pipe(Effect.orElseSucceed(() => false))) ||
931-
(yield* git.listLocalBranchNames(workspaceRoot).pipe(
932-
Effect.map((branches) => branches.includes("main")),
933-
Effect.orElseSucceed(() => false),
934-
));
935-
if (hasMain) return { type: "worktree", baseRef: "main", startFromOrigin: true };
936-
const checkedOutBranch = git.statusDetailsLocal(workspaceRoot).pipe(
937-
Effect.map((status) => status.branch),
938-
Effect.mapError(() =>
939-
failure(
940-
"orchestration_error",
941-
"Could not read the project's checked-out branch to pick a base branch; try again.",
942-
),
943-
),
928+
const { workspaceRoot: cwd } = yield* requireProject(projectId);
929+
const worktreeFrom = (baseRef: string) =>
930+
({ type: "worktree", baseRef, startFromOrigin: true }) as const;
931+
const unreadable = failure(
932+
"orchestration_error",
933+
"Could not read the project's branches to pick a base branch; try again.",
944934
);
945-
const baseRef =
946-
(yield* git
947-
.resolveDefaultBranchName(workspaceRoot, "origin")
948-
.pipe(Effect.orElseSucceed(() => null))) ??
949-
(yield* checkedOutBranch) ??
950-
"main";
951-
return { type: "worktree", baseRef, startFromOrigin: true };
935+
const readGit = <A>(read: Effect.Effect<A, GitCommandError>) =>
936+
read.pipe(Effect.mapError(() => unreadable));
937+
const probe = yield* readGit(
938+
git.execute({
939+
operation: "OrchestratorMcpService.scheduledTaskBase",
940+
cwd,
941+
args: ["rev-parse", "--git-dir"],
942+
allowNonZeroExit: true,
943+
}),
944+
);
945+
if (probe.exitCode !== 0) {
946+
if (probe.stderr.toLowerCase().includes("not a git repository")) {
947+
return worktreeFrom("main");
948+
}
949+
return yield* unreadable;
950+
}
951+
const localBranches = yield* readGit(git.listLocalBranchNames(cwd));
952+
const branchExists = (branch: string) =>
953+
localBranches.includes(branch)
954+
? Effect.succeed(true)
955+
: readGit(git.remoteBranchExists({ cwd, remoteName: "origin", refName: branch }));
956+
if (yield* branchExists("main")) return worktreeFrom("main");
957+
const originDefault = yield* readGit(git.resolveDefaultBranchName(cwd, "origin"));
958+
// origin/HEAD can still name a branch deleted since; skip it then.
959+
if (originDefault !== null && (yield* branchExists(originDefault))) {
960+
return worktreeFrom(originDefault);
961+
}
962+
const { branch } = yield* readGit(git.statusDetailsLocal(cwd));
963+
return worktreeFrom(branch ?? "main");
952964
});
953965

954966
/** A client has no thread to inherit a model from, so it falls back to the project default. */

0 commit comments

Comments
 (0)