fix(host-service): pin detached-HEAD v1 imports as non-blocking

Merged
KI
2mo ago
+129-4

The cohort

About 8 external users a week run the v1→v2 auto-migration on a machine where a v1 project's checkout sits on a detached HEAD (bisecting, a CI-style checkout, a tag). The repo is otherwise valid. Until #7485, project.create {importLocal} and project.setup {import} both ended in ensureMainWorkspaceStrict, which threw PRECONDITION_FAILED "Repository is in detached-HEAD state…". The migrator ledgers a throwing project import as error, so the flip gate never completed for that machine and those users stayed on v1 until they happened to check out a branch. Where the migration stands overall: https://app.superset.sh/page/v1-v2-migration-where-we-stand-yxfx0w

What this PR does

#7485 (already on main) removed the implicit main workspace and with it the strict HEAD check from both import paths, so the cohort is unblocked on main today. Nothing guarded that, though, and the branch requirement still exists one step later in createLocalWorkspace. This PR pins the whole path with host-service regression tests against a real temp git repo (git init, commit, git checkout --detach):

  • importLocal on a detached HEAD returns created: true, the project row exists, and no workspace row is created.
  • project.setup with mode.kind = "import" on a detached HEAD links the project the same way.
  • listProjectWorktrees omits the detached main checkout and reports it again once git checkout main runs. This is what keeps the migrator off the branch requirement: planWorkspaceAdoptions only routes a v1 workspace to workspaces.createLocal when the main worktree's branch matches, so a detached main lands in missingWorktree and is ledgered skipped (no-worktree-on-disk), which does not block the gate.
  • createLocalWorkspace still refuses a detached HEAD with PRECONDITION_FAILED and succeeds after a branch is checked out. Clone and new-repo creation always produce a branch, so they are unaffected.

Also fixes the initLocalRepoInPlace comment that still named the deleted strict helper. A temporary mutation that re-added the branch check to createFromImportLocal made three of the four new tests fail, so they do guard the behaviour.

What it deliberately does not do

  • No source change to the import paths: they already tolerate a detached HEAD after #7485.
  • The v1 "main" workspace of a detached-HEAD project is skipped, not created, and the ledger treats skipped as terminal. There is no boot-time sweep any more (#7485 removed runMainWorkspaceSweep), so nothing creates that local workspace later; the user creates one from the sidebar once they are on a branch. If we want the migrator to retry that workspace after a branch appears, that is a ledger-classification change and belongs with #7480.
  • Desktop side untouched: importV1Project no longer carries a mainWorkspaceId, so there is nothing to accept as null.

Checks

  • bun test packages/host-service/src/trpc/router/project/project-import.test.ts: 7 pass
  • bun run typecheck in packages/host-service: clean
  • biome check on the two touched files: clean
  • bun test in packages/host-service: see the first comment for the aggregate result

https://claude.ai/code/session_01YJ7iF8hZHKfEjmvzs1RYz3


Summary by cubic

Adds host-service regression tests pinning that v1 project imports succeed on detached-HEAD checkouts, so the v1→v2 auto-migration no longer stalls on machines at a tag or bisect. Also fixes a stale comment in initLocalRepoInPlace that referenced a removed strict-HEAD check.

  • importLocal and project.setup (mode=import) persist the project row with no workspace row when HEAD is detached.
  • listProjectWorktrees omits the detached main checkout and reports it again once a branch is checked out, keeping the importer from hitting createLocalWorkspace's branch requirement.
  • createLocalWorkspace still rejects a detached HEAD with PRECONDITION_FAILED and succeeds after a branch is checked out; tests restore the repo's real initial branch rather than assuming main.

Summary by CodeRabbit

  • Bug Fixes

    • Projects in detached-HEAD repositories can now be imported and linked without automatically creating a workspace.
    • Worktree discovery no longer includes detached main checkouts.
    • Local workspace creation now rejects detached repositories and succeeds once the repository’s initial branch is checked out.
    • Repository initialization now records the active branch and HEAD consistently.
  • Tests

    • Added coverage for detached repository imports, worktree discovery, and local workspace creation behavior.