|
1 | 1 | import { |
2 | 2 | CommandId, |
| 3 | + type GitCommandError, |
3 | 4 | type RunId, |
4 | 5 | isProviderAvailable, |
5 | 6 | MessageId, |
@@ -913,42 +914,53 @@ const make = Effect.gen(function* () { |
913 | 914 | * post into the existing thread (the strategy is unused, keep root); unbound |
914 | 915 | * runs launch a fresh worktree per run from main. Only when the project has no |
915 | 916 | * 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. |
919 | 921 | */ |
920 | 922 | const scheduledTaskWorkspaceStrategy = ( |
921 | 923 | boundToThread: boolean, |
922 | 924 | projectId: ProjectId, |
923 | 925 | ): Effect.Effect<ScheduledTask["workspaceStrategy"], OrchestratorMcpFailure> => |
924 | 926 | Effect.gen(function* () { |
925 | 927 | 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.", |
944 | 934 | ); |
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"); |
952 | 964 | }); |
953 | 965 |
|
954 | 966 | /** A client has no thread to inherit a model from, so it falls back to the project default. */ |
|
0 commit comments