Skip to content

fix(wrappers): borrow a projection in the openspec wrapper for a linked worktree - #204

Merged
TornquistK merged 4 commits into
mainfrom
workset/openspec-wrapper-borrows-in-worktree
Nov 30, 2026
Merged

TornquistK merged 4 commits into
mainfrom
workset/openspec-wrapper-borrows-in-worktree

Conversation

@TornquistK

@TornquistK TornquistK commented Nov 30, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The openspec wrapper refused with "no mate context here" in a linked worktree that mate wrap never ran in, for example one made by mate working worktree whose wrap was skipped. It only walked upward for a projection of its own. The TypeScript resolver (borrowedProjection in src/runtime/projection.ts) borrows in that case, and the specs require the same of wrapper scripts.

 resolve context (packages/mate-core/wrappers/bin/openspec)
   launch environment set            -> use it
+  PWD is no absolute path (folder was deleted) -> skip the walk
   walk up for an own projection.env -> use it
+  none found, nearest Checkout is a linked worktree -> borrow:
+    main worktree's projection.env  (only when git reports core.bare as false; core.worktree when set)
+    else each linked worktree's, by record name:
+      its folder carries a projection.env, and its .git points back to the record
+    never the worktree itself
+    repository path = the borrowing worktree's root
   nothing resolves                  -> refuse
-    "no mate context here — run `mate wrap` in this repository"
+    in a Companion Repository: "no mate context in a Companion Repository — run openspec
+      from a Working Repository checkout wrapped with `mate wrap`"
+    elsewhere: unchanged

The borrow is a fallback only: a resolution that worked before is unchanged. A Companion Repository still resolves no context without a launch.

Fixes friction reports 43c9f44a, 809b4f56, 0f9c6f0f.

OpenSpec change: openspec-wrapper-borrows-in-worktree (schema mate-minimal, capability cli-wrapper-packaging, two added requirements). It is not submitted yet. It has no scenario for a deleted folder.

Not in this PR: wrappers/bin/graphify has the same walk and is left untouched. #188 removes graphify from mate-core, so a change there would be a modify/delete conflict.

Second commit: review round

  • The main worktree lends only when git itself reports core.bare as false. A git that cannot run no longer makes the folder beside the shared Git folder count as the main worktree.
  • A record folder without a gitdir file, or a stray file under worktrees/, no longer prints a raw bash error.
  • Two tests cover the safety conditions (see Evidence).

Third commit: finishing round

  • Deleted folder. The wrapper looped forever when it started in a folder that was deleted. main already loops there when the environment carries no PWD: bash leaves PWD empty, and the upward walk never reaches /. The first two commits added the same loop twice (pwd -P with an empty result, and the Companion Repository check). All three walks now end, and the wrapper refuses.
  • Cheap test first. The record loop tests a record's folder for projection.env before the point-back check, which starts several processes per record. A record without a projection now starts no process.
  • One name for the main worktree rule. The rule moved into _mate_git_confirmed_main_worktree.
  • Tests. The Git fixtures come from test/git-checkouts.ts, which also gives relative worktree paths where Git supports them. A new group pins the wrapper to resolveProjection: the wrapper may refuse or agree, and never picks another lender. The fixture now writes projection.yaml and projection.env together, as mate wrap does.

Known gaps, not fixed here

  • A worktree placed inside another wrapped checkout resolves that checkout. The wrapper's upward walk does not stop at a linked worktree's root, as the resolver's does. So acme/.claude/worktrees/fix resolves acme as the repository, and a worktree inside another project's checkout resolves that project's companion. main behaves the same. A follow-up change is agreed.
  • A copied .mate/ is used, not borrowed. The resolver borrows instead. Unchanged from main.
  • A lender is judged by projection.env alone. The resolver reads projection.yaml. Mate writes both files as one unit. When they disagree, the wrapper refuses where the resolver lends (env file names no companion), or lends where the resolver skips (no valid projection.yaml).
  • A config without a core.bare key lends nothing from the main worktree. git init and git clone always write the key.
  • A gitdir record with a trailing slash lends nothing. Git writes none.
  • Git Bash on Windows. A gitdir: C:/... path is not read as absolute, so nothing is borrowed. Derived from the code, not run.
  • The "different mate install" refusal for a borrowed projection says "this repository". The spec asks for the same refusal as for an own projection.

Evidence

  • Before: the three tests of the first commit fail against the main wrapper (49 pass, 3 fail):

    openspec wrapper in a linked worktree > borrows the main worktree's projection and serves the worktree
      received ""   (expected cwd=<acme-companion>)
    openspec wrapper in a linked worktree > another linked worktree lends when the main worktree carries no projection
      received ""
    openspec wrapper in a Companion Repository > refuses, pointing to a Working Repository checkout
      received "openspec: no mate context here — run `mate wrap` in this repository"
    

    After: bun run test -- src/wrappers.test.ts → 56 pass, 0 fail.

  • Each guard has a test that goes red alone. One line broken in the wrapper, the other 55 tests stay green:

    core.bare check replaced by `true`              -> red: a bare clone lends nothing from the folder that holds it
    "points back" line removed                      -> red: a worktree record that does not point back lends nothing
    lender must name a companion in projection.env  -> red: lends no wider than resolveProjection > main's shell projection names no companion
    "never the worktree itself" check removed       -> red: lends no wider than resolveProjection > the borrowing worktree carries a projection without a registry
    
  • In the real CLI, isolated run with an acme-app fixture linked to acme-companion, wrapped with mate wrap, wrapper started with no Mate launch environment:

    Where main (fc74b09) This branch
    linked worktree acme-fix, openspec templates exit 1, no mate context here exit 0, four template paths under acme-companion
    linked worktree acme-fix, openspec list exit 1, no mate context here exit 0, No active changes found., plus the existing store pointer hint
    acme-companion, openspec list exit 1, run mate wrap in this repository exit 1, names the Companion Repository and a Working Repository checkout
    deleted folder, PWD set exit 1 at once exit 1 at once (the second commit looped here)
    deleted folder, no PWD loops, stopped after 5 s exit 1 at once
  • Other files that run the wrappers (unmanaged-session-guardrail, openspec-store.integration, package-packaging, capabilities/graphify, capabilities/graphify-secrets): 82 pass, 0 fail.

  • bun run typecheck, bun run lint and bun run format:check in packages/mate-core pass. Lint prints one warning in src/runtime/skill-prefix.ts, which this PR does not touch.

  • Speed of the record loop, measured by a reviewer on a scratch fixture with 26 worktree records and an equivalent edit: 5.24 s → 0.38 s per call.

  • Not exercised: the wrapper inside a real agent session in a linked worktree; a missing git and a set core.worktree after the move into _mate_git_confirmed_main_worktree; the "different mate install" refusal for a borrowed projection; the speed of the final script; the full packages/mate-core suite locally; Windows.

  • Second opinion: 0 blockers found, 0 fixed without review, 0 open

Merge Danger

Door: two-way

Reverting the commits restores the old refusal. Nothing is written to disk: the wrapper copies no projection into the worktree.

Blast Radius: narrow

Only the openspec wrapper. Two things change for a call that worked or refused before:

  • In a linked worktree without a store pointer, a root-resolving command now runs against the companion and prints the existing one-line "no OpenSpec store pointer" hint on stderr.
  • Every call now tests that PWD is an absolute path before the upward walk. A shell that started the wrapper with a relative PWD would now get the refusal; bash sets an absolute PWD itself.

The borrow path starts git config twice to read core.bare and core.worktree from the shared Git folder; without a working git only a linked worktree can lend. The new tests need git in /usr/bin or /bin, because the test harness gives the wrapper that PATH.

…ed worktree

The openspec wrapper only walked upward for a projection of its own, so a linked worktree that mate wrap never ran in ended at the refusal. It now borrows as the TypeScript resolver does: from the main worktree first, then from each linked worktree that points back to its record, and it uses the worktree's root as the repository.

In a Companion Repository the refusal now points to a wrapped Working Repository checkout instead of telling the operator to run mate wrap there.
…nfirm

The openspec wrapper took the folder beside the shared Git folder as the main worktree whenever git could not read the config, so a bare clone or one with core.worktree set could lend where the TypeScript resolver returns null. It now lends from the main worktree only when git itself reports core.bare as false.

A record folder without a gitdir file, or a stray file under worktrees/, no longer prints a bash error. Two tests cover the bare check and the record that does not point back, and the test helper fails on a Git error and drops an inherited GIT_DIR.
The openspec wrapper looped forever when it started in a folder that was deleted. bash leaves PWD empty there, so the two upward walks never reached "/", and pwd -P gave the borrow path an empty root. All three now end, and the wrapper refuses as it does without a Mate context.

The borrow path now tests a worktree record's folder for a projection before the point-back check, which starts several processes per record. The main worktree rule moved into _mate_git_confirmed_main_worktree.

The tests take their Git fixtures from test/git-checkouts.ts, which also gives them relative worktree paths. A new group pins the wrapper to resolveProjection: it may refuse or agree, and never picks another lender.
@TornquistK
TornquistK merged commit 7821aeb into main Nov 30, 2026
1 check passed
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