Files
archived-orca/src/relay/git-handler-local-base-ref-refresh.ts
T
Brennan BensonandOrca 5424582b51 Suggest enabling local-main freshness when a new workspace finds it stale (#4888)
* Suggest enabling local-main freshness when a new workspace finds it stale

Adds a "Keep Local Main Up to Date" suggestion path: when the setting is
off and a new workspace's local base branch is behind its remote, Orca
surfaces a one-time, dismissible toast nudging the user to enable it. The
toast is sticky (no auto-expire) so it can't be missed, with explicit
Turn On / Dismiss actions; dismissing (button, close X, or swipe) persists
localBaseRefSuggestionDismissed so the nudge — and its backend probe —
never runs again.

Also refactors the refresh logic so the advisory and mutating paths share
one fast-forward-safety evaluator, adds an SSH relay RPC for the ref
mutation, and fixes remote-tracking base parsing for fully-qualified refs.

Co-authored-by: Orca <help@stably.ai>

* fix: restore update-ref fast-forward for un-checked-out local base ref

The refactor that split refresh into evaluate + mutate dropped the
non-owner case: a local base branch checked out in no worktree was left
stale (return undefined) instead of fast-forwarded. Restore it across all
three layers — local evaluator/mutator, SSH evaluator, and relay handler
(which also removes the dead duplicated throw) — using the expected-old-OID
compare-and-swap form of update-ref so a concurrent ref move is a no-op.
The suggestion toast now also fires for this case.

Co-authored-by: Orca <help@stably.ai>

* refactor: restore resultBase spread in local-base-ref mutators

The evaluate/mutate split spelled out { baseRef, localBranch, status }
literally in the mutating paths; main used a resultBase spread. Restore
that pattern in both the local and SSH mutators — behavior-preserving,
collapses two identical skipped_error returns.

Co-authored-by: Orca <help@stably.ai>

---------

Co-authored-by: Orca <help@stably.ai>
2026-06-08 13:19:45 -07:00

87 lines
2.9 KiB
TypeScript

import type { GitExec } from './git-handler-ops'
import { areRelayWorktreePathsEqual, readRelayWorktreeList } from './git-handler-worktree-ops'
export async function refreshLocalBaseRefForWorktreeCreateOp(
git: GitExec,
params: Record<string, unknown>
): Promise<void> {
const repoPath = params.repoPath as string
const fullRef = params.fullRef as string
const remoteTrackingRef = params.remoteTrackingRef as string
const ownerWorktreePath = params.ownerWorktreePath as string | undefined
const checkOnly = params.checkOnly === true
if (
typeof repoPath !== 'string' ||
typeof fullRef !== 'string' ||
typeof remoteTrackingRef !== 'string' ||
(ownerWorktreePath !== undefined && typeof ownerWorktreePath !== 'string')
) {
throw new Error('Invalid local base ref refresh request.')
}
if (!fullRef.startsWith('refs/heads/') || !remoteTrackingRef.startsWith('refs/remotes/')) {
throw new Error('Invalid local base ref refresh refs.')
}
await git(['check-ref-format', fullRef], repoPath)
await git(['check-ref-format', remoteTrackingRef], repoPath)
const localOid = await revParseCommit(git, repoPath, fullRef, 'Local base ref is missing.')
const remoteOid = await revParseCommit(
git,
repoPath,
remoteTrackingRef,
'Remote-tracking base ref is missing.'
)
// Why: this RPC mutates refs/worktrees, so the relay repeats main-process
// safety checks at mutation time to close stale-preflight and direct-call gaps.
try {
await git(['merge-base', '--is-ancestor', localOid, remoteOid], repoPath)
} catch {
throw new Error('Local base ref is not a fast-forward update.')
}
const worktrees = await readRelayWorktreeList(git, repoPath)
const ownerWorktree = worktrees.find((worktree) => worktree.branch === fullRef)
if (ownerWorktree) {
if (ownerWorktreePath && !areRelayWorktreePathsEqual(ownerWorktree.path, ownerWorktreePath)) {
throw new Error('Local base ref is checked out in a different worktree.')
}
const { stdout } = await git(
['status', '--porcelain', '--untracked-files=no'],
ownerWorktree.path
)
if (stdout.trim()) {
throw new Error('Local base ref worktree has tracked changes.')
}
if (checkOnly) {
return
}
await git(['reset', '--hard', remoteOid], ownerWorktree.path)
return
}
// Why: not checked out anywhere — fast-forward the bare ref. The
// expected-old-OID form is a no-op-safe compare-and-swap if the ref moved
// since the caller's evaluation snapshot.
if (checkOnly) {
return
}
await git(['update-ref', fullRef, remoteOid, localOid], repoPath)
}
async function revParseCommit(
git: GitExec,
repoPath: string,
ref: string,
missingMessage: string
): Promise<string> {
const { stdout } = await git(['rev-parse', '--verify', `${ref}^{commit}`], repoPath)
const oid = stdout.trim()
if (!oid) {
throw new Error(missingMessage)
}
return oid
}