Skip to content

feat(worktree): configurable worktree root #1199

Closed

Description

@admarble

Epic: field-2026-09-28 (downstream non-Node adoption) Tier: judgment Graph: tracker #1203

Problem. The root is path.join(path.dirname(gitRoot), "worktrees") (worktree-manager.ts:745) and ../worktrees/ in new-feature.sh:112,417. There is no setting or env var. Path-substring checks would break under a custom root:

  • pre-tool.sh:1199, in 3 copies: worktrees/feature/
  • quality-checks.sh:521, in 2 copies: *"/worktrees/"*

Lookups already go through git worktree list (#900).

AC

  • AC-1: A worktreeRoot setting and a SEQUANT_WORKTREE_ROOT env var set the root for both the TS path and new-feature.sh. Evidence: npx vitest run src/lib/workflow/worktree-manager.test.ts
  • AC-2: The hook and quality-checks detect "inside a sequant worktree" via git rev-parse --git-common-dir, not a path substring. Evidence: npx vitest run __tests__/pre-tool-hook.integration.test.ts

#1192 (native WorktreeCreate hooks) is not a dependency: it concerns the .exec-agents/ sub-worktrees inside an issue worktree (worktree-isolation.ts:60), not the root this issue configures. Owner decision, 2026-09-29. #1136 (in-place mode) already sidesteps the root.

Graph metadata

  • No dependency edges. The former Blocked by #1192 was dropped by owner decision on 2026-09-29.

May touch

  • src/lib/workflow/worktree-manager.ts
  • src/lib/settings.ts
  • templates/scripts/new-feature.sh
  • scripts/new-feature.sh (tracked mirror, byte-identical to the template)
  • src/lib/workflow/worktree-manager.test.ts
  • __tests__/pre-tool-hook.integration.test.ts
  • hooks/pre-tool.sh
  • .claude/hooks/pre-tool.sh
  • templates/hooks/pre-tool.sh
  • .claude/skills/qa/scripts/quality-checks.sh
  • skills/qa/scripts/quality-checks.sh
  • templates/skills/qa/scripts/quality-checks.sh

File overlap (don't run in the same wave; merge one first):

Activity

  1. added
    enhancement
    graph-2026-09-field
    on Sep 28, 2026
  2. admarble commented on Sep 30, 2026

    @admarble
    ContributorAuthor

    Context for the run (re-checked against main @ a12c441d, 2026-09-29)

    Owner decision: the Blocked by #1192 edge is dropped. #1192 is about the .exec-agents/ sub-worktrees (src/lib/worktree-isolation.ts:60), not the issue-worktree root, so this issue is unblocked. The body is patched: checkboxes are added, line refs updated, and the "May touch" list extended.

    Current line refs

    • Root in TS: src/lib/workflow/worktree-manager.ts:746, path.join(path.dirname(gitRoot), "worktrees").
    • Root in the script: templates/scripts/new-feature.sh:112 (WORKTREE_DIR="../worktrees/${BRANCH_NAME}"), :143 (hint text) and :417 (CACHE_DIR="${MAIN_REPO_DIR}/../worktrees/.npm-cache"). The cache dir should follow the configured root. scripts/new-feature.sh is a tracked, byte-identical mirror of the template, so edit both identically.
    • Path-substring checks for AC-2:
      • pre-tool.sh:1199 (grep -qE 'worktrees/feature/'), in hooks/, .claude/hooks/ and templates/hooks/, all identical.
      • quality-checks.sh:521 (*"/worktrees/"*), in .claude/skills/qa/scripts/, skills/qa/scripts/ and templates/skills/qa/scripts/, all identical.
      • Keep each set byte-identical after the change.
    • Settings: src/lib/settings.ts. RunSettings starts at :192. Put worktreeRoot there, next to the base-branch setting (:214).

    Design constraints

    • Precedence: the SEQUANT_WORKTREE_ROOT env var, then the worktreeRoot setting, then the current default (<parent of repo>/worktrees). A relative value resolves against the main repo root. Resolve it in one TS helper and use it at :746.
    • new-feature.sh must read the setting JSONC-safely. .sequant/settings.json may contain comments, so a plain jq read fails. Either honour only the env var in the shell script and have the TS caller export SEQUANT_WORKTREE_ROOT when it invokes the script, or use a JSONC-tolerant read. Say which you chose in the PR.
    • AC-2 detection: "inside a linked worktree" is git rev-parse --git-dir ≠ git rev-parse --git-common-dir (resolve both to absolute paths). Don't match on a path substring.
    • Out of scope: the hint strings that print ../worktrees/feature/<issue>-*/ (src/lib/locks/checkout-lock.ts:182,187, pre-tool.sh:1057) and the skill prose in SKILL.md files. They're guidance text, not detection logic. List them in the PR as a possible follow-up, but don't edit them here.
    • Hook rollout: the repo's pre-tool.sh and the plugin-cache copy both fire. The AC-2 change reaches plugin users only after a plugin version bump. Note that in the PR body.

    Standard block

  3. admarble commented on Sep 30, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (2 ACs, 6 directories)

    AC Quality Check

    • Both Evidence: commands pass on main today, so neither gates anything yet. worktree-manager.test.ts has no worktree-root cases. pre-tool-hook.integration.test.ts has no WORKTREE_WARNING cases. The evidence only counts once new cases land in those files and fail on unchanged code.
    • AC-1's evidence covers only the TS path. The AC also names new-feature.sh, and nothing tests that. → Derived AC-3.
    • AC-2's evidence covers only the hook. quality-checks.sh is named in the AC, but no test covers it. → Derived AC-4.
    • Wording mismatch. AC-2 says "inside a sequant worktree", but the method the owner prescribes (--git-dir ≠ --git-common-dir) detects any linked worktree. That includes .exec-agents/ sub-worktrees and worktrees made by other tools. For a warn-only commit check and a find-the-main-repo lookup, that is the right behaviour. I'm reading the AC as "inside a linked worktree".

    Scope Assessment

    Non-Goals: No ## Non-Goals heading. The owner comment's "Out of scope" list does the same job: hint strings in checkout-lock.ts:182,187 and pre-tool.sh:1057, and the SKILL.md prose.

    Metric Value Status
    Feature count 1 ✅
    AC items 2 (+2 derived) ✅
    Directory spread 6 (src/lib, src/lib/workflow, templates/scripts + scripts, 3× hooks, 3× qa scripts, __tests__) ⚠️ (mostly byte-identical mirrors)

    Verdict: ✅ SCOPE_OK. The spread is mirror copies, not separate features.

    Conflicts: #1196 and #1197 are closed. The in-flight worktrees for #1195 and #1217 don't touch the files in this plan. (#1217 has merged, and its worktree is stale.)


    Acceptance Criteria

    AC-1: worktreeRoot setting + SEQUANT_WORKTREE_ROOT env set the root for TS and new-feature.sh

    Verification: Unit Test (TS resolver) + Integration Test (script, see AC-3)
    Scenario: Given SEQUANT_WORKTREE_ROOT=/tmp/wt → When the root is resolved → Then it is /tmp/wt. Given no env var and run.worktreeRoot: "../trees" → Then it is <repoRoot>/../trees, resolved. Given neither → Then it is path.join(path.dirname(gitRoot), "worktrees"), same as today.
    Assumptions: A relative value resolves against the main repo root, not the cwd (owner comment). An empty or whitespace-only env var is treated as unset.

    AC-2: Hook + quality-checks detect "inside a worktree" via git rev-parse --git-common-dir

    Verification: Integration Test
    Scenario: Given a linked worktree at a custom root with no worktrees/feature/ in its path → When a git commit Bash payload runs through pre-tool.sh with that cwd → Then no WORKTREE_WARNING is written. Given the main checkout → Then WORKTREE_WARNING is written.
    Assumptions: The check runs in ${HOOK_CWD:-$PWD}, not bare pwd. Line 1182 already uses that form, and bare pwd is the hook's own directory rather than the command's. Both paths are made absolute before comparing: --git-dir returns .git relative in the main checkout.


    Implementation Plan

    Phase 1: TS resolver + setting (AC-1)

    1. src/lib/settings.ts:
      • Add worktreeRoot?: string to RunSettings, next to defaultBase (:218).
      • Add it to the zod schema (:597), the known-keys list (:811) and the generated docs table (:1826).
    2. src/lib/workflow/worktree-manager.ts:
      • Add an exported pure helper, resolveWorktreeRoot(gitRoot, configured?, env = process.env). Precedence: env, then setting, then default. Relative values are joined to gitRoot, then path.resolved.
      • Use it at :746.
      • Thread worktreeRoot in as a new optional parameter of ensureWorktree / ensureWorktrees / ensureWorktreesChain. run-orchestrator.ts:1199,1206 passes settings.run.worktreeRoot.
    3. Tests go in worktree-manager.test.ts: env beats setting, setting beats default, a relative value resolves against the repo root, an empty env var is ignored.

    Phase 2: Shell script (AC-1, AC-3)

    1. templates/scripts/new-feature.sh, then copy it byte-for-byte to scripts/new-feature.sh:
      • Resolve WORKTREE_ROOT once, after the cd "$MAIN_REPO_DIR" (:109). Order: $SEQUANT_WORKTREE_ROOT, then a JSONC-tolerant text read of worktreeRoot from .sequant/settings.json, then ../worktrees.
      • Apply it at :112 (WORKTREE_DIR), :143 (the git worktree add hint, which is a command users paste) and :417 (CACHE_DIR).
      • Chosen read strategy: a text pattern, not jq. It matches the packageManager precedent at :236 and works in non-Node repos. Lines whose first non-blank characters are // are dropped, then "worktreeRoot"\s*:\s*"([^"]*)" is matched. Env-only was rejected: no TS code runs new-feature.sh (users and skills call it directly), so a TS export would never reach it, and the setting half of AC-1 would silently do nothing.
    2. Add an integration test modelled on __tests__/new-feature-*.integration.test.ts. Run the script with SEQUANT_WORKTREE_ROOT set, then with a JSONC settings file that has a commented-out decoy key followed by the real key. Assert where the worktree lands.

    Phase 3: Detection (AC-2, AC-4)

    1. hooks/pre-tool.sh:1197-1203: replace the grep -qE 'worktrees/feature/' check with a comparison of absolute --git-dir and --git-common-dir from git -C "${HOOK_CWD:-$PWD}". Get absolute paths with cd … && pwd -P, or --absolute-git-dir for the git dir. Copy the file byte-for-byte to .claude/hooks/ and templates/hooks/.
    2. skills/qa/scripts/quality-checks.sh:521: same comparison in place of *"/worktrees/"*. Copy to the .claude/skills/qa/scripts/ and templates/skills/qa/scripts/ mirrors.
    3. Add cases to pre-tool-hook.integration.test.ts: a linked worktree at a custom path (no worktrees/ substring) produces no warning, and the main checkout does. Mutation-verify by restoring the substring check and confirming the custom-root case fails.

    Design Review

    1. Where does this logic belong? Root resolution goes in worktree-manager.ts, which owns the path today, with the key in RunSettings. The shell script resolves on its own because it is a separate producer: two producers, one precedence rule (fix(cli): bare parseInt on run --timeout lets a malformed value abort every phase instantly (sibling of #818) #833/new-feature.sh hardcodes npm ci while the TS path is package-manager aware — pnpm/yarn/bun provisioning fails misleadingly #847 class). Detection stays in each shell script and uses git itself.
    2. What's the simplest correct approach? A pure resolver function with an injected env, so it tests without touching process.env. Settings are passed in as a parameter instead of calling getSettings() inside ensureWorktree: the orchestrator has already loaded them, and that keeps the function I/O-free. Rejected: env-only in the shell, for the reason in step 4.
    3. What existing pattern does this follow? defaultBase for the setting and its resolution priority. The packageManager text read in new-feature.sh for JSONC-safe shell reads. git -C "${HOOK_CWD:-$PWD}" from pre-tool.sh:1182.
    4. What would a senior reviewer challenge?
      • "The shell regex and zod can disagree." A /* */ block-commented key would still match. That's acceptable: it's rare, and it's the same trade-off the packageManager read made. Say so in the PR.
      • "Why not --absolute-git-dir for both?" --git-common-dir has no absolute variant on older git, so normalise that one with cd && pwd -P. pwd -P also handles the /tmp → /private/tmp symlink on macOS, which will otherwise break a naive string compare in tests.

    No ADR: the owner already fixed the design choices in their comment.


    Feature Quality Planning

    The standard checks pass. Items to watch:

    • Mirrors must stay byte-identical: new-feature.sh ×2, pre-tool.sh ×3, quality-checks.sh ×3. Check with cmp before committing.
    • Hook rollout: the plugin-cache pre-tool.sh also fires, so plugin users get AC-2 only after a plugin version bump. Note that in the PR body.
    • Sibling fix inside the edited block: the old check read bare pwd, which is the hook's own directory. HOOK_CWD fixes that as part of AC-2; no scope widening.
    • Follow-up, not in this PR: the hint strings in checkout-lock.ts:182,187, pre-tool.sh:1057, worktree-resolver.ts:5,10,163 (prose/messages only) and the SKILL.md text. List them in the PR as a possible follow-up.

    Derived ACs

    Source Derived AC Priority
    AC-1 evidence gap AC-3: new-feature.sh places the worktree under $SEQUANT_WORKTREE_ROOT, or under a worktreeRoot read from a JSONC settings file, ignoring a //-commented decoy. Evidence: new __tests__/new-feature-worktree-root.integration.test.ts High
    AC-2 evidence gap AC-4: quality-checks.sh's verify_build_against_main finds the main repo from a worktree at a custom root. Evidence: an integration test that sources the function from a custom-root worktree, or a recorded manual run in the PR if sourcing proves impractical Medium

    Open Questions

    1. Expand ~ in worktreeRoot? Recommendation: no. Document the value as absolute or repo-root-relative. Impact if wrong: ~/wt becomes <repo>/~/wt, which is visible immediately and easy to fix.
    2. Should quality-checks.sh also derive main_repo_dir from --git-common-dir, replacing the grep [main] heuristic? Recommendation: no. Keep this PR to the detection condition; it's a possible follow-up. Impact: none for this AC.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: The design is fully specified by the owner, and there are 2 ACs whose tests are concrete cases in named files. Testgen stubs would add a round-trip without adding structure, so I'm overriding the "Unit/Integration → testgen" default. Per the owner's standard block, neither phase runs the full suite: targeted timeout 600 npx vitest run … only.


    Label Review

    Current: enhancement, graph-2026-09-field
    Recommended: + planned
    Quality Loop: won't auto-enable. The scope is OK and there is no complex/refactor label.

  4. added
    planned
    on Sep 30, 2026
  5. admarble commented on Sep 30, 2026

    @admarble
    ContributorAuthor

    QA Verdict: READY_FOR_MERGE

    AC coverage: 4/4 met

  6. admarble commented on Sep 30, 2026

    @admarble
    ContributorAuthor

    Runner verification — PR #1226 (QA verdict READY_FOR_MERGE, no gaps)

    Spot-checked, since a verdict is a claim:

    • Ran the AC suites myself: worktree-manager.test.ts, pre-tool-hook.integration, new-feature-worktree-root.integration, quality-checks-worktree-root.integration. 420 passed. After merging main (fix(#1195): sequant://install recomputes on every read and reports a broken manifest as {error} #1225) they pass again, along with install-resource: 431 passed. tsc is clean.
    • The tests do exercise a custom root: a linked worktree at <sandbox>/custom-root/… for the hook and for quality-checks; the env var winning over the setting for new-feature.sh; and JSONC settings with a //-commented decoy key.
    • Mutation (AC-2): swapped in_linked_worktree's git-dir check back to the old *worktrees/feature/* substring in all three hook copies. Both custom-root cases then failed for every copy (6 failed); restored.
    • Mirrors: the three pre-tool.sh and three quality-checks.sh copies are byte-identical, and scripts/new-feature.sh is a symlink to the template.
    • Rollout caveat, recorded in the PR: the hook change reaches plugin users only after a plugin version bump.
  7. added a commit that references this issue on Sep 30, 2026
  8. admarble commented on Sep 30, 2026

    @admarble
    ContributorAuthor

    Delivered by #1226 (squash 4797ac8c on main). Closed by hand: closingIssuesReferences listed #1199 before the merge, but the merge didn't close it (same as #1195/#1225).

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

    enhancementgraph-2026-09-fieldplanned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions