Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
fix(server): avoid PRs inherited from default upstreams (#7317)
Gianmarco636caf4c7034
diff --git a/apps/server/src/git/GitManager.test.ts b/apps/server/src/git/GitManager.test.tsindex a5f8fa659..2db58bbec 100644--- a/apps/server/src/git/GitManager.test.ts+++ b/apps/server/src/git/GitManager.test.ts@@ -1129,6 +1129,71 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {20_000,);+ it.effect(+ "status preserves a fork PR whose head is named after the default branch",+ () =>+ Effect.gen(function* () {+ const repoDir = yield* makeTempDir("t3code-git-manager-");+ yield* initRepo(repoDir);+ const originDir = yield* createBareRemote();+ const forkDir = yield* createBareRemote();+ yield* runGit(repoDir, ["remote", "add", "origin", originDir]);+ yield* runGit(repoDir, ["push", "-u", "origin", "main"]);+ yield* runGit(repoDir, ["remote", "set-head", "origin", "main"]);+ yield* runGit(repoDir, ["remote", "add", "fork-seed", forkDir]);+ yield* runGit(repoDir, ["push", "fork-seed", "main"]);+ yield* runGit(repoDir, ["checkout", "-b", "t3code/pr-777/main"]);+ yield* runGit(repoDir, ["branch", "--set-upstream-to", "fork-seed/main"]);+ yield* configureVisibleRemoteUrlWithLocalRewrite(+ repoDir,+ "fork-seed",+ "[email protected]:contributor/codething-mvp.git",+ forkDir,+ );++ const { manager, ghCalls } = yield* makeManager({+ ghScenario: {+ prListByHeadSelector: {+ // @effect-diagnostics-next-line preferSchemaOverJson:off+ "contributor:main": JSON.stringify([+ {+ number: 777,+ title: "Fork PR from main",+ url: "https://github.com/pingdotgg/codething-mvp/pull/777",+ baseRefName: "main",+ headRefName: "main",+ state: "OPEN",+ updatedAt: "2026-03-10T07:00:00Z",+ isCrossRepository: true,+ headRepository: {+ nameWithOwner: "contributor/codething-mvp",+ },+ headRepositoryOwner: {+ login: "contributor",+ },+ },+ ]),+ },+ },+ });++ const status = yield* manager.status({ cwd: repoDir });+ expect(status.refName).toBe("t3code/pr-777/main");+ expect(status.pr).toEqual({+ number: 777,+ title: "Fork PR from main",+ url: "https://github.com/pingdotgg/codething-mvp/pull/777",+ baseRef: "main",+ headRef: "main",+ state: "open",+ });+ expect(ghCalls).toContain(+ "pr list --head contributor:main --state all --limit 20 --json number,title,url,baseRefName,headRefName,state,mergedAt,updatedAt,isCrossRepository,headRepository,headRepositoryOwner",+ );+ }),+ 20_000,+ );+it.effect("status ignores synthetic local branch aliases when the upstream remote name contains slashes",() =>@@ -1315,6 +1380,43 @@ it.layer(GitManagerTestLayer)("GitManager", (it) => {}),);+ it.effect("status does not inherit a merged PR from a feature branch's default upstream", () =>+ Effect.gen(function* () {+ const repoDir = yield* makeTempDir("t3code-git-manager-");+ yield* initRepo(repoDir);+ const remoteDir = yield* createBareRemote();+ yield* runGit(repoDir, ["remote", "add", "origin", remoteDir]);+ yield* runGit(repoDir, ["push", "-u", "origin", "main"]);+ yield* runGit(repoDir, ["remote", "set-head", "origin", "main"]);+ yield* runGit(repoDir, ["checkout", "-b", "feature/from-main", "origin/main"]);++ const { manager, ghCalls } = yield* makeManager({+ ghScenario: {+ prListSequence: [+ // @effect-diagnostics-next-line preferSchemaOverJson:off+ JSON.stringify([+ {+ number: 54,+ title: "Reverse merge from main",+ url: "https://github.com/pingdotgg/codething-mvp/pull/54",+ baseRefName: "je-filter-list",+ headRefName: "main",+ state: "MERGED",+ mergedAt: "2023-09-28T03:21:10Z",+ updatedAt: "2023-09-28T03:21:10Z",+ },+ ]),+ ],+ },+ });++ const status = yield* manager.status({ cwd: repoDir });+ expect(status.refName).toBe("feature/from-main");+ expect(status.pr).toBeNull();+ expect(ghCalls.some((call) => call.includes("pr list"))).toBe(false);+ }),+ );+it.effect("status prefers open PR when merged PR has newer updatedAt", () =>Effect.gen(function* () {const repoDir = yield* makeTempDir("t3code-git-manager-");diff --git a/apps/server/src/git/GitManager.ts b/apps/server/src/git/GitManager.tsindex 1c9684b0d..1020df217 100644--- a/apps/server/src/git/GitManager.ts+++ b/apps/server/src/git/GitManager.ts@@ -904,11 +904,24 @@ export const make = Effect.gen(function* () {prLookupEpochByCwd.set(cacheKey, prLookupEpoch(cacheKey) + 1);}),);- // Cache keys are NUL-joined [cwd, branch, upstreamRef, epoch] — none of the+ // Cache keys are NUL-joined [cwd, branch, upstreamRef, defaultBranch, epoch] — none of the// segments can contain a NUL byte, and refs are never empty, so "" decodes- // back to a null upstreamRef.- const prLookupCacheKey = (cwd: string, details: { branch: string; upstreamRef: string | null }) =>- [cwd, details.branch, details.upstreamRef ?? "", String(prLookupEpoch(cwd))].join("\u0000");+ // back to a null ref.+ const prLookupCacheKey = (+ cwd: string,+ details: {+ branch: string;+ upstreamRef: string | null;+ defaultBranch: string | null;+ },+ ) =>+ [+ cwd,+ details.branch,+ details.upstreamRef ?? "",+ details.defaultBranch ?? "",+ String(prLookupEpoch(cwd)),+ ].join("\u0000");// Consecutive failures per cache key, so a branch that keeps failing waits// longer before the next attempt. Cleared as soon as a lookup succeeds.const prLookupFailureStreakByKey = new Map<string, number>();@@ -928,13 +941,29 @@ export const make = Effect.gen(function* () {};const prLookupCache = yield* Cache.makeWith((key: string) => {- const [cwd = "", branch = "", upstreamRef = ""] = key.split("\u0000");+ const [cwd = "", branch = "", upstreamRef = "", defaultBranch = ""] = key.split("\u0000");const details = {branch,upstreamRef: upstreamRef.length > 0 ? upstreamRef : null,+ defaultBranch: defaultBranch.length > 0 ? defaultBranch : null,};return Effect.gen(function* () {const headContext = yield* resolveBranchHeadContext(cwd, details);+ const upstreamHeadIsDefault =+ headContext.headBranch === details.defaultBranch ||+ (details.defaultBranch === null &&+ (headContext.headBranch === "main" || headContext.headBranch === "master"));+ // `git worktree add -b feature origin/main` makes the new local branch+ // track origin/main. That upstream is the branch's base, not its+ // published PR head. Looking up PRs for it can attach an old reverse+ // merge from main and auto-settle an unrelated feature thread.+ if (+ headContext.headBranch !== details.branch &&+ upstreamHeadIsDefault &&+ !headContext.isCrossRepository+ ) {+ return { latest: null, headContext };+ }// Only skip when the branch is untracked as well: anything carrying an// upstream keeps the old behaviour.if (details.upstreamRef === null && (yield* isUnpublishedBranch(cwd, headContext))) {@@ -1015,7 +1044,12 @@ export const make = Effect.gen(function* () {};const lookupStatusPr = Effect.fn("lookupStatusPr")(function* (cwd: string,- details: { branch: string; upstreamRef: string | null; isDefaultBranch: boolean },+ details: {+ branch: string;+ upstreamRef: string | null;+ defaultBranch: string | null;+ isDefaultBranch: boolean;+ },) {// Keyed by (cwd, branch) only: the upstream ref changing (e.g. a first// `push -u`) must not orphan the fallback value for the same branch.@@ -1089,6 +1123,7 @@ export const make = Effect.gen(function* () {? yield* lookupStatusPr(cwd, {branch: details.branch,upstreamRef: details.upstreamRef,+ defaultBranch: details.defaultBranch,isDefaultBranch: details.isDefaultBranch,}): null;diff --git a/apps/server/src/vcs/GitVcsDriver.ts b/apps/server/src/vcs/GitVcsDriver.tsindex fdfd4eac5..b9ef99212 100644--- a/apps/server/src/vcs/GitVcsDriver.ts+++ b/apps/server/src/vcs/GitVcsDriver.ts@@ -72,6 +72,7 @@ export interface GitStatusDetails {export interface GitRemoteStatusDetails {isRepo: boolean;+ defaultBranch: string | null;isDefaultBranch: boolean;branch: string | null;upstreamRef: string | null;diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.tsindex 5930caf49..3d0f66c34 100644--- a/apps/server/src/vcs/GitVcsDriverCore.ts+++ b/apps/server/src/vcs/GitVcsDriverCore.ts@@ -89,6 +89,7 @@ const NON_REPOSITORY_STATUS_DETAILS = Object.freeze<GitVcsDriver.GitStatusDetail});const NON_REPOSITORY_REMOTE_STATUS_DETAILS = Object.freeze<GitVcsDriver.GitRemoteStatusDetails>({isRepo: false,+ defaultBranch: null,isDefaultBranch: false,branch: null,upstreamRef: null,@@ -1550,6 +1551,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*return {isRepo: true,+ defaultBranch,isDefaultBranch,branch,upstreamRef,