Skip to content

fix(locks): locks dir is cwd-relative while the hook reads the git toplevel — acquiring from a subdirectory silently protects nothing #909

Closed

Description

@admarble

Found during adversarial validation of #906 (Design v2, "Adjacent defects"). Verified against main at e2e57a7c.

Bug

resolveLocksDir resolves the locks directory against the current working directory (src/lib/locks/lock-manager.ts:87-90):

export function resolveLocksDir(explicit?: string): string {
  const fromEnv = process.env.SEQUANT_LOCKS_DIR;
  return resolve(explicit ?? fromEnv ?? DEFAULT_LOCKS_DIR);   // DEFAULT_LOCKS_DIR = ".sequant/locks"
}

resolve(".sequant/locks") is process.cwd() + "/.sequant/locks".

The pre-tool.sh checkout guard, by contrast, reads the git toplevel of the command's cwd:

_CO_ROOT=$(git -C "$_CO_CWD" rev-parse --show-toplevel 2>/dev/null || echo "")
_CO_LOCK="$_CO_ROOT/.sequant/locks/checkout.lock"

Consequence

The two disagree whenever the CLI is invoked from anywhere other than the repository root:

  • cd src && npx sequant locks checkout acquire --issue=42 writes src/.sequant/locks/checkout.lock
  • the hook keeps reading <toplevel>/.sequant/locks/checkout.lock, sees no lock, and waves every session through

So the acquire silently succeeds while providing no protection at all, and locks list from the root reports nothing held. The same divergence applies to the per-issue locks (status, merge, resume, run-orchestrator all construct LockManager with no explicit dir).

Severity: moderate. Silent no-op rather than a wrong answer, but it defeats the feature exactly when someone works from a subdirectory — and nothing reports it.

Fix sketch

Resolve against the git toplevel, matching the hook:

const root = execFileSync("git", ["rev-parse", "--show-toplevel"], { cwd }).trim();
return resolve(root, DEFAULT_LOCKS_DIR);

with the cwd-relative form kept only as the fallback when git fails (not a repo). SEQUANT_LOCKS_DIR and the explicit option keep priority, since the tests depend on them.

Worth deciding at the same time whether a worktree should share the main checkout's locks dir. The checkout lock represents the main tree, so it should resolve to the main checkout even when called from a linked worktree — git rev-parse --path-format=absolute --git-common-dir gives that, where --show-toplevel does not.

Test plan

  • Acquire from a subdirectory; assert the lock file lands at <toplevel>/.sequant/locks/.
  • Acquire from a subdirectory, then run the real hook with cwd at the toplevel; assert it blocks (today it does not).
  • Acquire from a linked worktree; assert the intended target (see the open question above) once decided.

Activity

  1. added
    bug
    on Aug 7, 2026
  2. admarble commented on Aug 7, 2026

    @admarble
    ContributorAuthor

    #909 — fix(locks): locks dir is cwd-relative while the hook reads the git toplevel — acquiring from a subdirectory silently protects nothing
    Open · bug
    ────────────────────────────────────────────────────────────────

    → PROCEED — Divergence confirmed on main at 5a68930 — resolveLocksDir uses resolve(".sequant/locks"), pre-tool.sh:563 uses git --show-toplevel

    Commands:
    sequant run 909 -Q # after 908 lands

    spec → exec → qa

    Flags:
    -Q path resolution changes every LockManager construction site (status, merge, resume, run-orchestrator) plus a worktree semantics decision

    Considered:
    --testgen bug-only label; testgen skips bug/fix issues even though the body carries a Test plan
    --security-review no security/auth label — path resolution, not authz
    ────────────────────────────────────────────────────────────────
    ⚠ Body has zero - [ ] checkboxes — spec must derive ACs from the Fix sketch / Test plan; --strict-preflight would hard-stop this issue
    ⚠ Body carries an unresolved design question — should a linked worktree share the main checkout's locks dir? It proposes git rev-parse --path-format=absolute --git-common-dir. Decide this in spec, before exec; the third test-plan bullet is blocked on the answer
    ⚠ Order: land after 908 — both edit src/lib/locks/lock-manager.ts, and this resolveLocksDir change is the smaller of the two to rebase
    ────────────────────────────────────────────────────────────────

  3. admarble commented on Aug 8, 2026

    @admarble
    ContributorAuthor

    Plan Complete

    AC Checklist

    • AC-1: resolveLocksDir resolves against the shared git checkout root, not process.cwd()
    • AC-2: A linked worktree resolves to the main checkout's locks directory, not its own
    • AC-3: Non-git cwd falls back to today's cwd-relative behavior
    • AC-4: SEQUANT_LOCKS_DIR and the explicit locksDir option keep top priority over git-derived resolution

    Implementation Plan

    1. Add a git rev-parse --path-format=absolute --git-common-dir helper in src/lib/locks/lock-manager.ts, resolving to the parent directory (shared checkout root across main + worktrees), with a swallowed-error fallback to today's cwd-relative resolution.
    2. Update resolveLocksDir to use it as the new default (below explicit/SEQUANT_LOCKS_DIR, above plain cwd fallback).
    3. No call-site changes needed — LockManager/CheckoutLock construction is the single choke point.
    4. Add unit tests (subdirectory resolution, non-git fallback) and one integration test (linked worktree resolves to the same path as the main checkout).

    Key Decision

    Resolves the issue's open question in favor of sharing the locks directory between the main checkout and its linked worktrees (via --git-common-dir), rather than giving each worktree its own. This matches the intent of both lock types: the checkout lock represents "the tree," and the per-issue lock exists specifically to catch collisions across concurrent sessions — including sessions that are each in a different worktree of the same repo.

    Workflow

    Phases: spec → exec → qa (no /test — backend-only; no /testgen — targeted tests sufficient for a bug-labeled single-function fix)

  4. added
    planned
    on Aug 8, 2026
  5. admarble commented on Aug 8, 2026

    @admarble
    ContributorAuthor

    /fullsolve Complete

    Issue: #909
    Status: READY_FOR_MERGE

    Summary

    • AC: 4/4 met
    • Build/Lint/Tests: all clean (211 tests passed across affected modules)
    • QA: All checks passed (type safety, deleted-tests, scope/size, security — all PASS)
    • Fix mutation-verified: reverting resolveLocksDir to pre-fix behavior makes the new AC-1/AC-2 tests fail exactly as expected

    Root cause & fix

    resolveLocksDir resolved .sequant/locks against process.cwd(), while pre-tool.sh's checkout guard reads it from the git toplevel — the two only agreed when the CLI ran from the repo root. Fixed via git rev-parse --path-format=absolute --git-common-dir, which also unifies a linked worktree with the main checkout's locks dir (a deliberate improvement over the issue's own --show-toplevel fix sketch, documented in the CHANGELOG entry). No call-site changes needed — every consumer already routes through this one function.

    Iterations

    • Spec: 1 (no iteration needed)
    • Exec: 1 (no iteration needed)
    • Test: skipped (no UI labels — backend-only fix)
    • QA: 1 (no iteration needed, READY_FOR_MERGE on first pass)

    PR: #918
    Commit: ee35fd085e9531d6049268af02334c286b64a4e7


    Ready for human review and merge.

  6. admarble commented on Aug 8, 2026

    @admarble
    ContributorAuthor

    Companion follow-up parked here Moved to #919 — #909 was already in flight in another session, so the #908 steal-restore residual is tracked as its own issue instead. Nothing here is in #909's scope.

  7. added 2 commits that reference this issue on Aug 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions