Skip to content

fix(sync): scripts/dev never converges when node_modules holds an older sequant — drift check and dry-run compare against the running bundle, not the symlink target #1159

Closed

Description

@admarble

Observed

When a project has sequant installed in node_modules at an older version and a newer sequant runs sync, scripts/dev/*.sh never settles. resolveScriptsSymlinkTarget (src/lib/templates.ts) prefers <project>/node_modules/sequant/templates/scripts (#991), so sync rebuilds each symlink pointing at the installed copy. But sync --dry-run and the version-current drift check (sync.ts:821, version-preflight.ts:145) compare that file with the running CLI's bundled template. The dry-run predicts an overwrite that the apply never makes, and every later sync exits 1 with Version current, but 1 file(s) differ from bundled content.

It stayed hidden because templates/scripts/*.sh hadn't changed since 2.16.0. The first PR to change one (#1145, PR #1156) turns the required canary check red. From now on, every change to a script template is blocked from merging until this is fixed.

Repro (run 2026-09-25)

mkdir p && cd p && git init -q && echo '{"name":"p","version":"1.0.0"}' > package.json
npm install [email protected] && node_modules/.bin/sequant init --yes --skip-setup
node <PR-with-a-changed-templates/scripts/new-feature.sh>/dist/bin/cli.js sync
readlink scripts/dev/new-feature.sh   # ../../node_modules/sequant/templates/scripts/new-feature.sh  (the 2.16 copy)
cmp "$(readlink -f scripts/dev/new-feature.sh)" <PR>/templates/scripts/new-feature.sh   # differ

Canary on PR #1156 at f2f9dbf: npx vitest run --project canary → 2 failed | 2 passed (dry-run equals apply predicts scripts/dev/new-feature.sh, and customizations survive… fails on the drift exit 1). The same happens with #1122's branch merged in, so this is not #1122.

Fix direction

Owner decision (2026-09-25): option 1. Keep the #991 targeting (node_modules first). Fix the dry-run and the drift check so they compare a scripts/dev symlink against resolveScriptsSymlinkTarget(), not the running bundle. Options 2 and 3 are rejected: don't change which target sync links to, and don't change only the canary.

Options considered:

  1. The drift check and the dry-run treat a scripts/dev symlink that already points at resolveScriptsSymlinkTarget() as current, and compare against that target, not the running bundle. This keeps the fix(templates): scripts/dev symlinks target whichever sequant package ran the command — npx-cache targets get evicted, npm-link targets are machine-specific #991 design.
  2. sync targets the running CLI's templates whenever its version differs from the one in node_modules. This partly reverses fix(templates): scripts/dev symlinks target whichever sequant package ran the command — npx-cache targets get evicted, npm-link targets are machine-specific #991.
  3. Change only the canary, so it installs the PR tarball into node_modules before sync. This hides a real user-facing loop, so it's not recommended alone.

Found by the graph 2026-09-w3 run (docs/internal/graph-2026-09-w3/issue-graph.md). Blocks #1145.

Activity

  1. added
    bug
    on Sep 25, 2026
  2. added a commit that references this issue on Sep 25, 2026
  3. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner context (graph 2026-09-w3, docs/internal/graph-2026-09-w3/issue-graph.md, PR #1154) — these rules apply to the spec, exec AND qa phases:

    • qa phase: do not run the full npm test. Verify each AC with its own verify command and run only the named test files. The runner runs the full suite once per PR, serially — three phase agents share this machine and a full-suite call inside a phase can hit the 30-minute no-progress wall.
    • exec phase: commit WIP before any long command; run the full suite at most once, in the foreground, with a timeout; never run_in_background.
    • Gate-test ACs: the PR body's <!-- SEQUANT_MUTATION: {...} --> marker needs failedTest to start with the test file path (src/.../x.test.ts > describe > case), or /qa §6i floors it to AC_NOT_MET.
    • CHANGELOG: add exactly one [Unreleased] line.
    • Stop at the PR — do not merge.
  4. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner note on verifying AC-1/AC-2. This branch doesn't change templates/scripts/new-feature.sh itself, so on its own the canary is green before and after. To exercise the bug, merge origin/feature/1145-bug-worktree-new-feature-sh-and-sequant-run-derive (PR #1156) into a scratch worktree on top of this branch, build, and run npx vitest run --project canary. On main + #1156 alone that gives 2 failed | 2 passed, and with this fix it should give 4 passed. Record the result in the PR body. Better still, also add a permanent unit/integration case: a project whose node_modules/sequant/templates/scripts/x.sh differs from the bundled one; sync twice; the second exits 0 and the dry-run predicts nothing for scripts/dev/x.sh.

  5. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 1 directory — src/lib + src/lib/templates.test.ts)

    AC Quality Check

    lintAcceptanceCriteria: 3/3 passed, 0 flagged. All three parse cleanly on one line each.

    One gap worth fixing at exec time: all three ACs use Verify:, not the house Evidence: field, so extractAcceptanceCriteria resolves verificationMethod: "manual" for every one of them and AC.evidence is unset. The verify commands are concrete, so this is cosmetic for a human, but it means no AC declares a test as its evidence — see derived AC-4.

    Scope Assessment

    Non-Goals: not present as a section, but the issue states them as rejected options: don't change which target sync links to (option 2), don't change only the canary (option 3). Treat those as binding.

    Metric Value Status
    Feature count 1 (one comparison site) ✅
    AC items 3 (+2 derived) ✅
    Directory spread 1 (src/lib/) ✅

    Verdict: ✅ SCOPE_OK

    Root cause — one site, not two

    The issue names sync.ts:821 and version-preflight.ts:145. Both are message sites; neither compares anything. Every symptom funnels through a single function:

    computeTemplateChanges (src/lib/templates.ts:614) walks the running CLI's bundled templates, maps templates/scripts/x.sh → scripts/dev/x.sh via TEMPLATE_ROUTES (templates.ts:527), then readFile(localPath) — which follows the symlink into node_modules/sequant/templates/scripts/x.sh (the older copy) — and compares it to getTemplateContent(templatePath) from the running bundle. The apply path never writes that file: copyTemplates (templates.ts:1093) relinks it to resolveScriptsSymlinkTarget().scriptsDir, which is the node_modules dir. So the diff reports modified forever.

    Four consumers read that count — sync's version-current fast path (sync.ts:419, sets exitCode = 1), sync --dry-run (sync.ts:476, the Modified files: and overwrite: lines), checkAndWarnSkillsOutdated (sync.ts:756, every command's pre-flight) and getSkillsInstallStatus (version-preflight.ts:145, serve + sequant://install). One edit fixes all four. previewScriptsSymlinkTargets (templates.ts:929) is already correct — it reads resolveScriptsSymlinkTarget and needs no edit.

    In the canary (__tests__/canary/downstream.integration.test.ts) snapshot() stores a symlink as link:<target>, so the apply leaves the entry identical: predicted contains scripts/dev/new-feature.sh, changed does not (dry-run equals apply fails), and the second sync exits 1 (customizations survive… fails). The fixture installs the previous minor into node_modules for real, so it is the repro.

    Acceptance Criteria

    AC-1: a second sync exits 0 with no differ from bundled content line

    Verification: Integration Test (canary)
    Scenario: Given the canary fixture (previous minor in node_modules) on a branch that changes templates/scripts/new-feature.sh → When npx vitest run --project canary → Then 4 passed (today: 2 failed | 2 passed)
    Assumptions: the canary needs the npm registry + npm pack; #1156's branch must be merged into a scratch worktree to change a script template.

    AC-2: sync --dry-run lists scripts/dev/<name>.sh only when the apply changes it

    Verification: Integration Test (canary dry-run equals apply)
    Scenario: Given the same fixture → When sync --dry-run then sync → Then the non-preserved predicted set equals the set of files whose snapshot entry changed.
    Assumptions: satisfied by the same edit as AC-1 — both read computeTemplateChanges.

    AC-3: resolveScriptsSymlinkTarget still prefers node_modules/sequant/templates/scripts

    Verification: Unit Test + manual readlink
    Scenario: Given node_modules/sequant/templates/scripts exists → When sync → Then readlink scripts/dev/new-feature.sh still resolves into node_modules/sequant.
    Assumptions: resolveScriptsSymlinkTarget and copyTemplates are not edited at all. The existing test at templates.test.ts:1140 already asserts the resolver; the new assertion is that the link on disk is unchanged after the fix.

    AC-4 (derived): a permanent test reproduces the skew without the registry

    Verification: Unit Test
    Scenario: Given a temp cwd with SEQUANT_TEMPLATES_DIR pointing at a bundled scripts/x.sh of content A and node_modules/sequant/templates/scripts/x.sh of content B → When copyTemplates() then computeTemplateChanges("generic") → Then scripts/dev/x.sh is unchanged and the link points into node_modules/sequant.
    Assumptions: use real process.chdir + SEQUANT_TEMPLATES_DIR, the pattern already in the computeTemplateChanges describe (templates.test.ts:560). Never spy process.cwd — it leaks across forks (#870).

    AC-5 (derived): a scripts/dev symlink pointing anywhere else is still reported as drift

    Verification: Unit Test
    Scenario: Given scripts/dev/x.sh → …/_npx/deadbeef/…/scripts/x.sh while resolveScriptsSymlinkTarget() resolves to node_modules/sequant/… → When computeTemplateChanges("generic") → Then scripts/dev/x.sh is modified (the apply relinks it).
    Assumptions: this is the guard against the cheap wrong fix. Blanket-skipping scripts/dev/ in the diff passes AC-1, AC-2 and AC-3 while silently killing stale-link detection — the #1053 npx-cache shape.

    Implementation Plan

    Phase 1: one comparison site

    1. src/lib/templates.ts, computeTemplateChanges: hoist const scriptsTarget = resolveScriptsSymlinkTarget(getTemplatesDir()) above the loop (one call per diff, not per file).
    2. For any localPath starting with scripts/dev/, resolve expectedSource = join(scriptsTarget.scriptsDir, basename(localPath)) and branch:
      • destination absent → new;
      • destination is a symlink → unchanged iff its resolved target is expectedSource, else modified (AC-5);
      • destination is a regular file (Windows / --no-symlinks / copy mode) → compare its bytes against expectedSource's bytes, not getTemplateContent(templatePath). copyDir copies from symlinkTarget.scriptsDir in that mode too, so the same rule is correct for both.
    3. Don't run processTemplate on the scripts/dev/ branch — neither symlinkDir nor copyDir renders scripts, so rendering them at diff time is a latent false positive the moment a script template contains a {{TOKEN}}.
    4. No edits to resolveScriptsSymlinkTarget, copyTemplates, previewScriptsSymlinkTargets, sync.ts or version-preflight.ts (AC-3, and options 2/3 are rejected).

    Phase 2: durable tests (AC-4, AC-5)

    1. Add cases to the computeTemplateChanges describe in src/lib/templates.test.ts (unit project, 5s default — keep them filesystem-only, no subprocess): skew-converges, stale-link-still-modified, copy-mode-compares-target.
    2. Mutation-verify each: revert the one line it gates, confirm exactly that test fails, restore. Record <!-- SEQUANT_MUTATION: {...} --> in the PR body with failedTest starting at the file path (src/lib/templates.test.ts > … > …), or /qa §6i floors the AC. Start from a clean git status --short.

    Phase 3: canary proof (AC-1, AC-2)

    1. Scratch worktree off this branch, merge origin/feature/1145-bug-worktree-new-feature-sh-and-sequant-run-derive, npm run build, then a single foreground timeout 1500 npx vitest run --project canary. Never background it (Nested phase claude that hangs after its turn is killed at ~106s, taking down the parent sequant run #856 kills the group at ~106s). Record before (2 failed | 2 passed on main + fix(#1145): new-feature.sh derives the branch name with the slugify() rule #1156) and after (4 passed) in the PR body.
    2. CHANGELOG: exactly one [Unreleased] line.

    Design Review

    1. Where does this logic belong? In computeTemplateChanges, beside the .local/ twin check that already special-cases scripts/dev/ paths (templates.ts:672). The diff is the single source of truth fix(update/sync): sync reports 'up to date' on content drift; update overwrites in-place customizations (constitution) #708/fix(#708): content-truth for update/sync; protect in-place customizations #710/fix(sync/init/update): one ownership rule for project files — the clobbering class fixed file-by-file in #708/#814/#990/#1042/#1071/#1078 #1090 built; putting the rule anywhere else re-splits the preview from the apply.
    2. Simplest correct approach? Make the scripts/dev/ branch read the same source the writer reads. Rejected: a scripts/dev/ skip-list in the diff (breaks AC-5 / fix(sync): copy-mode sync leaves existing machine-specific scripts/dev symlinks in place, and doctor's hint loops back to sync #1053); declaring scripts/dev user-owned (turns drift into preserved, which is a different and wrong verb for a sequant-owned tree).
    3. Existing pattern? previewScriptsSymlinkTargets already resolves the same target for the same destination — this brings the content diff onto the resolver the preview has used since fix(sync): never rewrite a user-owned AGENTS.md or write a machine-specific scripts/dev symlink (absorbs #991) #990.
    4. What would a senior reviewer challenge? "Why not just compare content on both sides?" — in symlink mode the content is identical by construction (same inode), so content can never distinguish a correct link from a stale one; the target path is the only signal. Second challenge: the copy-mode arm is in scope deliberately — a Windows or --no-symlinks project with skewed node_modules hits the identical loop and the canary does not cover it.

    Conflict Risk Analysis

    No in-flight worktree touches src/lib/templates.ts (#1141 → test files, #1153 → .github/workflows/, #1145 → templates/scripts/new-feature.sh). Only CHANGELOG.md is shared — one [Unreleased] line each, a routine squash conflict. The existing feature/1159-… worktree sits at 104f324e (main with #1122 merged), so #1122 is already in the base.

    Dependency (outbound): this blocks #1145 / PR #1156 — the canary stays red there until this lands.

    Open Questions

    1. Include the copy-mode arm (step 2, third bullet)? — Recommendation: yes, same function, ~3 lines. Impact if skipped: Windows and --no-symlinks projects keep the permanent N file(s) differ loop with no test covering it, and the next report reads as a regression of this fix.
    2. Drop processTemplate on the scripts/dev/ branch (step 3)? — Recommendation: yes. Impact if wrong: none today (no script template contains a token); it prevents a future token in a .sh from re-opening this exact loop.
    3. Also make the canary fixture perturb a script template permanently? — Recommendation: no. fix(#1145): new-feature.sh derives the branch name with the slugify() rule #1156 lands that change for real; a fixture that fakes it would be the mock the canary exists to avoid. The AC-4/AC-5 unit tests are the durable gate.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • Regression surface: computeTemplateChanges feeds sync, update, doctor, serve's install status and the per-command pre-flight. The change must be inert when resolveScriptsSymlinkTarget().scriptsDir is the bundled dir — the sequant repo itself has no node_modules/sequant, so local sync behaviour must not move.
    • Drift cache: computeDriftFingerprint / .claude/.sequant/ drift cache (sync.ts:156) stats inputs. Confirm the fingerprint covers the symlink target, or a cached count can outlive a relink on the cache: true pre-flight path.
    • No security surface. No new I/O outside the project tree; readlink on a project-relative path only.

    Derived ACs

    Source Derived AC Priority
    Verifiability (AC-1/AC-2 need a foreign branch) AC-4: registry-free unit reproduction High
    Adversarial re-read (#1053 regression) AC-5: stale link still reported as drift High

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: SCOPE_OK, one function edited, five ACs. testgen is skipped — bug label only, and the two test ACs are specific enough that stubs would be churn (/testgen leaves stubs behind). No UI, no security surface, so no test or security-review phase. Per the runner note: qa verifies each AC with its own named test files, never the full npm test.

    Label Review

    Current: bug
    Recommended: bug (unchanged)
    Reason: one-function fix with a clear repro; no complex/refactor/breaking signal.
    Quality Loop: won't auto-enable — SCOPE_OK.

  6. added
    planned
    on Sep 25, 2026
  7. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_NOT_MET

    AC coverage: 0/6 met

    Gaps:

    • The new scripts/dev classification makes non-convergent in symlink mode — the same defect class fix(sync): scripts/dev never converges when node_modules holds an older sequant — drift check and dry-run compare against the running bundle, not the symlink target #1159 fixes for sync, relocated to update; exclude scripts/dev/ from update's applySet.
    • No test covers update.ts's consumption of scripts/dev changes, which is why the non-convergence regression shipped through a green suite.
    • Zero CI checks have ever run on PR fix(#1159): diff scripts/dev against the install target, not the running bundle #1161 because GitHub holds it as conflicting and never created the merge ref, so the required context cannot report and the PR cannot merge.
    • A comment states an unconditional invariant the callee does not provide, leaving an undocumented dependency on sync.ts passing force: true.
    • The scripts template prefix is a second literal alongside the TEMPLATE_ROUTES source of truth.
    • The update non-convergence regression above — blocking.
    • classifyScriptsDevChange's closing comment ("A regular file here … is a real write: symlinkDir unlinks it and puts expectedLink in its place") is only true under force. symlinkDir:972-982 skips a regular file without it. It happens to be correct for every current consumer because sync.ts:632 hardcodes force: true, but the comment states an unconditional invariant the callee does not provide — exactly the "comments state intent, not effect" trap, and the coupling is undocumented at the symlinkDir end.
    • The symlink arm returns modified with no diff field, so update --dry-run (update.ts:271) prints the path with no diff body for these entries. Minor, and arguably right (there is no content diff to show), but it is an asymmetry with the copy arm.

    Suggestions:

    • SCRIPTS_TEMPLATE_PREFIX = "templates/scripts/" duplicates the TEMPLATE_ROUTES entry at templates.ts:536-540. The JSDoc points at the route, but nothing fails if the two drift. Deriving the prefix from the route entry would make drift impossible.
    • Consider asserting the force coupling at symlinkDir's definition, so a future change to its skip-without-force branch surfaces the dependency.
  8. added 2 commits that reference this issue on Sep 25, 2026
  9. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner, after QA round 1: I fixed QA's update non-convergence finding myself in c8c3732a and b3777a63; details in the PR #1161 body under Runner fixes. Checks: full npm run build && npm test → 338 files and 8399 tests passed. Canary on a scratch merge of this branch with #1156 → 4 passed. Real [email protected] probe: update and sync both converge on the second run. qa phase: don't run the full suite; the runner already has.

  10. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner, qa phase: the last qa run hit the 30-minute no-progress wall and returned no verdict. For this run: don't run npm test or npm run build (the runner ran both at b3777a63: 8399 passed, and CI at that SHA shows test, canary and typecheck success). Don't repeat the real-npm install probe either (the runner's result is in the PR body). Verify with the named test files only (src/lib/templates.test.ts, src/commands/update.integration.test.ts, src/lib/__tests__/writer-state-matrix.test.ts) and keep every command under ~5 minutes.

  11. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    Gaps:

    • After the linkTarget filter, a plain reports everything up to date while symlink-mode scripts/dev drift is real and deferred to sync, with no message telling the user so
    • The scripts template prefix is a second literal alongside the TEMPLATE_ROUTES source of truth and nothing fails if the two drift
    • The cached drift fingerprint is blind to a scripts/dev relink that preserves the target mtime, and no test pins the interaction
    • A bundle-only script is reported rather than a status meaning not-installed, because the TemplateChange enum has no honest value for it

    Suggestions:

    • SCRIPTS_TEMPLATE_PREFIX = "templates/scripts/" (templates.ts:625) duplicates the TEMPLATE_ROUTES entry at templates.ts:541-545. The JSDoc points at the route, but nothing fails if the two drift. Deriving the prefix from the route entry would make drift impossible.
    • Consider asserting the force coupling at symlinkDir's own definition so a future change to its skip-without-force branch surfaces the sync.ts-side dependency.
  12. added 2 commits that reference this issue on Sep 25, 2026
  13. added a commit that references this issue on Sep 25, 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