Skip to content

fix(#870): resolve worktree package manager from lockfile when manifest is absent - #874

Merged
admarble merged 6 commits into
mainfrom
feature/870-worktree-manager-s-bare-npm-fallback-disagrees
Jul 30, 2026
Merged

admarble merged 6 commits into
mainfrom
feature/870-worktree-manager-s-bare-npm-fallback-disagrees

Conversation

@admarble

@admarble admarble commented Jul 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Both package-manager resolution sites in worktree-manager.ts (installWorktreeDeps, reinstallIfLockfileChanged) now resolve through a new resolvePackageManager(declared, root) in stacks.ts, which falls back to detectPackageManagerSync(worktreePath) instead of the bare literal "npm" when the manifest records no manager.
  • This makes the TypeScript provisioning path agree with templates/scripts/new-feature.sh (PM-aware since new-feature.sh hardcodes npm ci while the TS path is package-manager aware — pnpm/yarn/bun provisioning fails misleadingly #847) on manifest-less pnpm/yarn/bun projects, closing the dual-producer drift described in the issue.
  • sequant update's three sibling sites are converted through the same resolver, so no live || "npm" fallback remains anywhere in src/.
  • An own-property membership test also retires the unchecked as keyof typeof PM_CONFIG cast, which turned an unrecognized manifest value into a TypeError on the next property access. The test is hasOwnProperty, not in — see the QA finding below.

AC Verification

AC Description Status Evidence
AC-1 Both sites resolve via detectPackageManagerSync(worktreePath) when the manifest value is absent ✅ Implemented worktree-manager.ts:544 and :1228 — both now PM_CONFIG[resolvePackageManager(packageManager, worktreePath)]. Neither function needed a signature change; both already held worktreePath.
AC-2 A manifest-less pnpm worktree provisions with pnpm's frozen install, matching new-feature.sh ✅ Implemented worktree-manager.frozen-install.test.ts — "installWorktreeDeps runs pnpm's frozen install on a manifest-less pnpm worktree" asserts the spawned command is pnpm install --frozen-lockfile, byte-identical to the pnpm) branch of pm_frozen_install in new-feature.sh:211. Both read PM_CONFIG.pnpm.ciInstall.
AC-3 A unit test pins the manifest-absent case for a non-npm PM, so a revert to || "npm" fails ✅ Implemented 11 gate tests across the two files (6 for the resolution fallback, 5 for the prototype-key hole); both mutation results recorded below.

Defect found by the review pass, fixed in this PR

The first commit used declared in PM_CONFIG. in walks the prototype chain, so "toString", "constructor", "valueOf", "hasOwnProperty", and "__proto__" all passed the membership test and were returned as if they named a package manager. Indexing PM_CONFIG with one yields an inherited function (or Object.prototype) whose ciInstall is undefined. Confirmed by running it:

resolved pm = toString | typeof cfg = function | cfg.ciInstall = undefined
DOWNSTREAM CRASH: Cannot read properties of undefined (reading 'split')

An uncaught TypeError during worktree provisioning — the exact crash class this guard exists to close, reintroduced one layer down, while the comment, commit message, and CHANGELOG all claimed it was closed. Fixed in 8165b530 with Object.prototype.hasOwnProperty.call, a table-driven gate test over all five inherited keys, and corrected wording in all three places.

A second, smaller gap: docs/features/package-manager-detection.md:157 still told users "Sequant defaults to npm" when the manifest field is missing, which this change makes false for the provisioning path. Scoped rather than deleted in b510669a, since it remains true for sequant update.

Mutation verification (CLAUDE.md gate-test rule)

Two independent mutations. Committed first each time, then reverted resolvePackageManager's body to the bare fallback (return (declared as PackageManager) || "npm";) and re-ran both test files:

× resolvePackageManager > detects from the lockfile when no manager is declared
× resolvePackageManager > detects from the lockfile when the declared value is empty
× resolvePackageManager > detects rather than crashing when the declared value is not a known manager
× resolvePackageManager > resolves against the given root, not the process cwd
× manifest-less worktrees detect the package manager (#870) > installWorktreeDeps runs pnpm's frozen install on a manifest-less pnpm worktree
× manifest-less worktrees detect the package manager (#870) > reinstallIfLockfileChanged runs yarn's frozen install on a manifest-less yarn worktree

Tests  6 failed | 11 passed (17)

Mutation 2 — revert hasOwnProperty back to in:

× routes the inherited Object.prototype key "toString" to detection
× routes the inherited Object.prototype key "constructor" to detection
× routes the inherited Object.prototype key "valueOf" to detection
× routes the inherited Object.prototype key "hasOwnProperty" to detection
× routes the inherited Object.prototype key "__proto__" to detection

Tests  5 failed | 17 passed (22)

In both cases exactly the intended tests fail and every pre-existing test still passes, so the coverage is real and scoped. git checkout src/lib/stacks.ts restored each time; 22/22 green.

Behavior change surface

Deliberately narrow:

  • Manifest-less npm projects: unchanged (detection returns npm, same as the old literal).
  • Manifest-less pnpm/yarn/bun projects: changed from broken to correct — pinned by the two call-site tests.
  • Lockfile-less worktrees: unchanged — detectPackageManagerSync still defaults to npm, and a failing npm ci is surfaced by the ensureWorktree swallows the provisioning install exit status — npm ci failure yields a silent dependency-less worktree #846 warn-and-continue path. Pinned by "a manifest-less worktree with no lockfile still falls back to npm".
  • Declared manager present: still wins over detection, including Python managers (pip/poetry/uv), which are PM_CONFIG keys but absent from LOCKFILE_PRIORITY and so would be silently rewritten to npm by an always-detect implementation. Pinned by two tests.

Test plan

  • npm run build — clean
  • npm run lint — clean (--max-warnings 0)
  • npm test — green in CI (build (22.x) runs npm test, ci.yml:36); all 5 checks pass
  • Mutation verification (both mutations, above)

One local full-suite run failed mcp-e2e … SIGTERM (4107 ms). Measured rather than assumed: it passes in isolation on this branch, the branch's first full run was clean, and a full run on main under identical conditions failed 4 different tests while that one passed. Pre-existing load-dependent integration flakes on this machine, not a regression.

Files changed

File Change
src/lib/stacks.ts New resolvePackageManager export beside detectPackageManagerSync
src/lib/workflow/worktree-manager.ts Both call sites converted; import updated
src/lib/stacks.resolve-package-manager.test.ts New — 12 direct resolver tests
src/lib/workflow/worktree-manager.frozen-install.test.ts New #870 describe block — 4 call-site tests
CHANGELOG.md [Unreleased] → Fixed entry
docs/features/package-manager-detection.md Corrected the now-stale manifest-less claim

sequant update's three sites (originally deferred, now included)

Open Question 1 in the plan proposed leaving src/commands/update.ts:113/:128/:296 — the byte-identical pattern — to a follow-up. They are now converted in this PR instead.

They mattered more than the deferral assumed: the manifest field is absent precisely on pre-1.3.0 installs, which is exactly the population the PM_RUN backfill at :113 exists to serve. So the site most likely to run with an absent manifest value was the one hardcoding npm — writing an npm PM_RUN token into pnpm/yarn/bun projects and running npm install at :296 when package.json changed.

All three now consume a single const pm resolved right after the manifest loads. Resolving once rather than converting each site in place is deliberate: the defect was one rule copied to three places, and three copies of the new call would have preserved that shape. The root is process.cwd(), matching what getManifest and syncSequantMcpPin already read.

Coverage: 4 behavioral tests drive updateCommand with a redirected process.cwd() and a real temp-dir lockfile — pnpm backfill, yarn first-run config, the unchanged no-lockfile npm default, and manifest-wins-over-lockfile.

Mutation 3 — revert that resolution to (manifest.packageManager as keyof typeof PM_CONFIG) || "npm":

× backfills PM_RUN from the lockfile, not npm, on a legacy pnpm install
× writes a yarn PM_RUN on first-time config setup for a yarn project

Tests  2 failed | 7 passed (9)

Exactly the two lockfile-detection tests fail. The other two new tests correctly keep passing under the mutation — the no-lockfile and manifest-present cases have the same expected result under both implementations, so they are boundary guards rather than gates, and claiming otherwise would overstate the coverage.

Branch state

origin/main is merged in (33405d16), so the branch is 0 commits behind. The merge brought only the dependabot hono bump (package-lock.json). The post-merge install rewrote that lockfile by stripping libc fields a newer npm had committed — the exact normalization installWorktreeDeps' comment documents — so the rewrite was discarded rather than committed.

Fixes #870

admarble added 3 commits July 29, 2026 22:27
…st is absent

`installWorktreeDeps` and `reinstallIfLockfileChanged` both resolved the
package manager as `(packageManager as keyof typeof PM_CONFIG) || "npm"`.
That value comes from the worktree manifest — a snapshot taken at
`sequant init` — so it is absent on manifest-less or pre-init worktrees,
and the bare literal then assumed npm. `new-feature.sh` has detected live
from the lockfile since #847, so on a pnpm/yarn/bun project without a
manifest the two provisioning paths ran different frozen installs for the
same repo. Same dual-producer drift class as #833.

Both sites now call a new `resolvePackageManager(declared, root)` in
`stacks.ts` — the module new-feature.sh already names as its source of
truth — which keeps a declared manager and otherwise falls back to
`detectPackageManagerSync`, the helper #487 added for exactly this. This
is the continuation of #487's conversion pass, whose inventory covered
`version-check.ts` and `bin/cli.ts` but never `worktree-manager.ts`.

The `in PM_CONFIG` membership test also retires the unchecked cast: a
declared value outside `PackageManager` used to index `PM_CONFIG` to
`undefined` and throw on the next property access, and now routes to
detection. Python managers stay declared-wins, since the sync detector is
JS-only. Manifest-less npm projects and lockfile-less worktrees are
unaffected.

AC-1: both sites converted (worktree-manager.ts:541, :1223).
AC-2: pnpm case asserts `pnpm install --frozen-lockfile`, byte-identical
      to new-feature.sh's `pm_frozen_install` pnpm branch.
AC-3: gate tests added; mutation result recorded in the PR body.

Tests: 229 files, 4707 passed, 1 expected fail, 2 skipped. Build + lint clean.
… test

Adversarial QA pass on the previous commit: `in` walks the prototype
chain, so `"toString"`, `"constructor"`, `"valueOf"`, `"hasOwnProperty"`,
and `"__proto__"` all passed the membership test and were returned as if
they named a package manager. `PM_CONFIG["toString"]` then yields the
inherited function (and `PM_CONFIG["__proto__"]` yields Object.prototype),
whose `ciInstall` is undefined, so the caller crashed on `.split(" ")`:

  TypeError: Cannot read properties of undefined (reading 'split')

That is the exact failure the guard was added to prevent, reintroduced one
layer down — and the doc comment, commit message, and CHANGELOG all claimed
it was closed. `Object.prototype.hasOwnProperty.call` closes it for real.

Adds a table-driven gate test over the five inherited keys, and corrects
the CHANGELOG wording to name the `hasOwnProperty`-vs-`in` choice.
`docs/features/package-manager-detection.md` told users that a missing
`packageManager` field means "Sequant defaults to npm". That is now stale
for the path this issue changed: worktree provisioning detects from the
worktree's lockfile, so a manifest-less pnpm/yarn/bun project gets that
manager's frozen install, and npm applies only when no lockfile is present.

The sentence is scoped rather than deleted, because it is still accurate
for `sequant update`, whose three sibling sites keep the bare npm fallback
pending the follow-up noted in the PR body.
admarble added 3 commits July 29, 2026 23:45
Closes the scope gap flagged in QA. `update.ts:113`, `:128`, and `:296`
carried the byte-identical `(manifest.packageManager as keyof typeof
PM_CONFIG) || "npm"` spelling. The manifest field is absent precisely on
pre-1.3.0 installs — the population the PM_RUN backfill exists to serve —
so `update` wrote an npm PM_RUN token into pnpm/yarn/bun projects and ran
`npm install` there when package.json changed.

All three now consume one `const pm` resolved right after the manifest
loads, from `resolvePackageManager(manifest.packageManager, process.cwd())`.
Resolving once rather than converting each site in place is the point: the
defect was one rule copied to three places, and three copies of the new
call would have preserved that shape. The cwd is the correct root because
`update` operates on the project in place, the same directory `getManifest`
and `syncSequantMcpPin` read.

Repo-wide grep confirms no live `|| "npm"` fallback remains; only the
comments explaining the old spelling mention it.

Tests: 4 behavioral cases driving `updateCommand` with a redirected cwd and
a real temp-dir lockfile — pnpm backfill, yarn first-run config, the
unchanged no-lockfile npm default, and manifest-wins-over-lockfile.
Mutation result recorded in the PR body.

Docs and CHANGELOG updated: the previous pass scoped the docs sentence to
"`sequant update` still assumes npm", which this commit makes false.
… runtime

The #870 update tests used `vi.spyOn(process, "cwd").mockReturnValue(dir)`
so the resolver would read a temp-dir lockfile. `process.cwd` is a process
global and `pool: "forks"` reuses a worker across test files, so the patch
escaped the file that set it.

Measured on this machine, holding directory and node_modules constant and
varying only the source:

  main's code in the worktree          205s   0 failures
  branch, update.test.ts removed       211s   0 failures
  branch, only the cwd spy disabled    230s   2 failures (its own)
  branch as committed                  695s   4 failures (semgrep,
                                              mcp-serve, App.tsx,
                                              lock-manager — all unrelated)

One line accounted for ~465s and every unrelated failure: the poisoned cwd
made other files in the reused fork slow enough to blow their timeouts. The
file itself ran in 11s, which is why per-file runs never showed it.

Faking a process global to control one function argument was the wrong
lever. The resolver's lockfile semantics are already covered directly, with
a real temp dir, in stacks.resolve-package-manager.test.ts (12 cases,
mutation-verified). What update.test.ts needs to pin is the *wiring* — that
update consults the resolver, passes the manifest value and project root,
and lets the answer drive PM_RUN — which needs no filesystem at all. So
`resolvePackageManager` is mocked (everything else in stacks.js stays real)
and the assertions moved to call arguments and the resulting PM_RUN.

The rewrite also gates harder: 4 of 5 tests now fail when update.ts is
reverted to the bare `|| "npm"`, against 2 of 4 before.

Second defect fixed here: `updateCommand` calls `syncSequantMcpPin` before
the template short-circuit, so the non-dry-run cases were rewriting the
repo's real `.mcp.json` on every test run (observed twice as an unexplained
dirty file). The pre-existing tests never hit it because they all pass
`--dry-run`. `mcp-config.js` is now mocked.

Full suite after the fix: 229 files, 4717 passed, 0 failures, 190s — below
main's own 205s on the same machine.
@admarble
admarble merged commit fe46858 into main Jul 30, 2026
5 checks 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.

worktree-manager's bare || "npm" fallback disagrees with the PM-aware shell path on manifest-less projects

1 participant