Skip to content

fix(desktop): fall back to local branch when base branch not found on remote - #1428

Merged
Kitenite merged 2 commits into
mainfrom
kitenite/new-branch-from-local
Feb 12, 2026
Merged

Kitenite merged 2 commits into
mainfrom
kitenite/new-branch-from-local

Conversation

@Kitenite

@Kitenite Kitenite commented Feb 12, 2026 •

Copy link
Copy Markdown
Member

Summary

  • When creating a workspace from a branch that exists locally but not on the remote, workspace init would hard-fail with "Branch does not exist on remote"
  • Now the not_found case falls back to local ref resolution (same as the existing network-error path)
  • Extracted resolveLocalRef() helper to centralize the duplicated resolve → apply-fallback → notify pattern across all 3 call sites
  • Fixed a bug where the fallback progress message referenced effectiveBaseBranch after it was already mutated
  • Removed comments that restated what the code already expresses

Test plan

  • Create a local-only branch, then create a workspace using it as the base — should succeed using the local ref
  • Verify remote-available branches still use origin/<branch> as before
  • Verify network-error fallback still works (same code path now)
  • CI passes (lint, typecheck, tests, sherif)

@coderabbitai

coderabbitai Bot commented Feb 12, 2026 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

Unifies workspace start-point resolution: centralizes local-ref fallback, distinguishes remote exists / not_found / error outcomes, refines fetch and fallback progress/error messaging, persists effectiveBaseBranch when a local fallback is applied, and preserves cancellation/cleanup behavior.

Changes

Cohort / File(s) Summary
Workspace init refactor
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts
Centralizes local vs remote start-point resolution (new resolveLocalRef / resolveLocalStartPoint flows), replaces branch-check with exists/not_found/error outcomes, refines fetch error handling and progress updates, persists effectiveBaseBranch on fallback, and removes legacy origin-first fallback paths. Minor import/message refinements.

Sequence Diagram(s)

sequenceDiagram
    participant Client as Client
    participant Init as WorkspaceInitService
    participant Remote as Remote Git
    participant Local as Local Repo
    participant DB as Database
    participant UI as Progress UI

    Client->>Init: start workspace init(request)
    Init->>Remote: probe remote branch (exists?/fetch)
    alt remote reachable & exists
        Remote-->>Init: exists
        Init->>DB: set startPoint = remote ref
        Init->>UI: progress "Using remote base branch"
    else remote reachable & not_found
        Remote-->>Init: not_found
        Init->>Local: resolve local ref / fallback
        alt local ref found
            Local-->>Init: local ref
            Init->>DB: persist effectiveBaseBranch (fallback)
            Init->>UI: progress "Using local fallback"
        else no local ref
            Init-->>UI: error "remote branch not found, no local fallback"
        end
    else remote error (network)
        Remote-->>Init: error
        Init->>Local: resolve local ref / fallback
        alt local ref found
            Local-->>Init: local ref
            Init->>DB: persist effectiveBaseBranch (fallback)
            Init->>UI: progress "Remote unreachable, using local fallback"
        else no local ref
            Init-->>UI: error "network error and no local fallback"
        end
    end
    Init->>Client: complete / error
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested reviewers

  • CharlieHelps

Poem

🐇 I sniff the branches, near and far,
When remote fades I follow a star,
Local trails guide where we steer,
Clearer notes now, nothing to fear,
Hop, commit — the workspace is here! 🌿

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: falling back to local branches when the base branch is not found on the remote.
Description check ✅ Passed The description includes Summary and Test plan sections with clear details, but lacks the full template structure with Related Issues, Type of Change, Testing, Screenshots, and Additional Notes sections.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch kitenite/new-branch-from-local

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts (2)

189-238: Consider using an options object for resolveLocalStartPoint as well.

resolveLocalRef correctly uses named parameters, but the inner resolveLocalStartPoint it delegates to still takes positional (reason, checkOriginRefs). For consistency with the coding guideline on object parameters for 2+ args, and to avoid the bare boolean positional, consider aligning it:

-const resolveLocalStartPoint = async (
-    reason: string,
-    checkOriginRefs: boolean,
-): Promise<LocalStartPointResult> => {
+const resolveLocalStartPoint = async ({
+    reason,
+    checkOriginRefs,
+}: {
+    reason: string;
+    checkOriginRefs: boolean;
+}): Promise<LocalStartPointResult> => {

Minor since it's a private closure only called from resolveLocalRef.


305-323: Long ternary strings are hard to scan — consider extracting message builders.

Lines 319-320 pack a lot of conditional text into deeply nested template literals. This is functional but makes the failure messages hard to review for correctness. A small helper or pre-computed message fragments would improve readability.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts (1)

308-315: ⚠️ Potential issue | 🔴 Critical

Bug: effectiveBaseBranch is read after applyFallbackBranch mutates it, producing a nonsensical message.

applyFallbackBranch (line 309) sets effectiveBaseBranch = fallbackBranch, so line 314 would emit something like Branch "main" not found. Using "main" instead. — the "not found" branch and the replacement are the same string.

Compare with the no-remote path (line 341) which correctly uses baseBranch (the original value). Capture the previous value before mutating, or reorder the calls.

🐛 Proposed fix
 			if (localResult.fallbackBranch) {
+				const previousBaseBranch = effectiveBaseBranch;
 				applyFallbackBranch(localResult.fallbackBranch);
 				manager.updateProgress(
 					workspaceId,
 					"verifying",
 					`Using "${localResult.fallbackBranch}" branch`,
-					`Branch "${effectiveBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`,
+					`Branch "${previousBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`,
 				);
 			}
🤖 Fix all issues with AI agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts`:
- Around line 270-276: Replace the intermediate boolean that prevents TypeScript
narrowing: instead of computing isNetworkError and then using
branchCheck.message, inline the condition when assigning fallbackReason so
TypeScript narrows branchCheck to the error variant; i.e., build fallbackReason
using branchCheck.status === "error" ? sanitizeGitError(branchCheck.message) :
`Branch "${effectiveBaseBranch}" not found on remote`, referencing the
branchCheck, fallbackReason, sanitizeGitError, and effectiveBaseBranch
identifiers.

Comment on lines +270 to +276
} else {
// Branch not available on remote (network error or not found) — fall back to local
const isNetworkError = branchCheck.status === "error";
const fallbackReason = isNetworkError
? sanitizeGitError(branchCheck.message)
: `Branch "${effectiveBaseBranch}" not found on remote`;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find the branchExistsOnRemote function definition
rg -n "branchExistsOnRemote" apps/desktop/src/lib/trpc/routers/workspaces/utils/ --type=ts -A 20

Repository: superset-sh/superset

Length of output: 10858


🏁 Script executed:

#!/bin/bash
# Also check for any type definitions related to the return value
rg -n "status.*error|error.*status" apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts --type=ts -B 3 -A 3

Repository: superset-sh/superset

Length of output: 449


🏁 Script executed:

#!/bin/bash
# Look at the actual workspace-init.ts file around the relevant lines
head -n 280 apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts | tail -n 20

Repository: superset-sh/superset

Length of output: 707


🏁 Script executed:

#!/bin/bash
# Find BranchExistsResult type definition
rg -n "BranchExistsResult" apps/desktop/src/lib/trpc/routers/workspaces/utils/ --type=ts -A 10

Repository: superset-sh/superset

Length of output: 3107


🏁 Script executed:

#!/bin/bash
# Also check the full implementation of branchExistsOnRemote to see what it returns
sed -n '1141,1180p' apps/desktop/src/lib/trpc/routers/workspaces/utils/git.ts

Repository: superset-sh/superset

Length of output: 1134


🏁 Script executed:

#!/bin/bash
# Check if this code actually compiles by looking at TypeScript config and any lint/type errors
cat apps/desktop/tsconfig.json 2>/dev/null | head -30

Repository: superset-sh/superset

Length of output: 440


🏁 Script executed:

#!/bin/bash
# Verify if there are similar patterns elsewhere in the file that work correctly
rg -n "status ===" apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts --type=ts -A 3 -B 1

Repository: superset-sh/superset

Length of output: 500


Fix type narrowing: use inline condition instead of intermediate boolean.

Line 272-274 breaks TypeScript's type narrowing. The intermediate boolean isNetworkError does not narrow the type of branchCheck, so accessing branchCheck.message is unsafe—the message field only exists on the error variant, not not_found. Use an inline condition:

const fallbackReason = branchCheck.status === "error"
	? sanitizeGitError(branchCheck.message)
	: `Branch "${effectiveBaseBranch}" not found on remote`;

This will properly narrow branchCheck to the error variant within the ternary branch, making the type access safe.

🤖 Prompt for AI Agents
In `@apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts` around
lines 270 - 276, Replace the intermediate boolean that prevents TypeScript
narrowing: instead of computing isNetworkError and then using
branchCheck.message, inline the condition when assigning fallbackReason so
TypeScript narrows branchCheck to the error variant; i.e., build fallbackReason
using branchCheck.status === "error" ? sanitizeGitError(branchCheck.message) :
`Branch "${effectiveBaseBranch}" not found on remote`, referencing the
branchCheck, fallbackReason, sanitizeGitError, and effectiveBaseBranch
identifiers.

… remote

Previously, creating a workspace from a branch that only existed locally
would fail with "Branch does not exist on remote". Now the error and
not_found cases share a single fallback path that resolves local refs
before giving up.
@Kitenite
Kitenite force-pushed the kitenite/new-branch-from-local branch from 422fd08 to d83b76a Compare February 12, 2026 07:03

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-init.ts (1)

308-316: ⚠️ Potential issue | 🟠 Major

Bug: effectiveBaseBranch already holds the fallback value when the message is composed.

applyFallbackBranch on line 309 reassigns effectiveBaseBranch to localResult.fallbackBranch. By line 314, both interpolations resolve to the same value, producing nonsensical text like Branch "main" not found. Using "main" instead.

Compare with the analogous no-remote path (line 341) which correctly uses the original baseBranch parameter.

Proposed fix
 				if (localResult.fallbackBranch) {
+					const originalBranch = effectiveBaseBranch;
 					applyFallbackBranch(localResult.fallbackBranch);
 					manager.updateProgress(
 						workspaceId,
 						"verifying",
 						`Using "${localResult.fallbackBranch}" branch`,
-						`Branch "${effectiveBaseBranch}" not found. Using "${localResult.fallbackBranch}" instead.`,
+						`Branch "${originalBranch}" not found. Using "${localResult.fallbackBranch}" instead.`,
 					);
 				}

Alternatively, just use baseBranch here as the other paths do.

Extract resolveLocalRef() helper that combines resolving a local start
point with applying the fallback branch update and progress notification.
Eliminates 3 duplicated resolve+apply+notify patterns. Also removes
comments that restate what the code already expresses.
@Kitenite
Kitenite merged commit d9176de into main Feb 12, 2026
6 checks passed
@Kitenite
Kitenite deleted the kitenite/new-branch-from-local branch February 12, 2026 07:18
@github-actions

Copy link
Copy Markdown
Contributor

🧹 Preview Cleanup Complete

The following preview resources have been cleaned up:

  • ⚠️ Neon database branch
  • ⚠️ Electric Fly.io app
  • ⚠️ Streams Fly.io app

Thank you for your contribution! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant