Skip to content

fix(sync): copy-mode sync leaves existing machine-specific scripts/dev symlinks in place, and doctor's hint loops back to sync #1053

Closed

Description

@admarble

Tier: mechanical Found: 2026-09-11, first field run of v2.15.0 across 12 local projects Related: #990 (AC-5 covers fresh installs only)

What happened

In every project without a local node_modules/sequant, npx [email protected] sync printed

!  scripts/dev templates dir is outside the project tree (npx cache or a sibling checkout) — copying instead of symlinking; pass --no-symlinks to silence
✔ Synced to v2.15.0

but left the pre-existing symlinks exactly as they were:

project scripts/dev/*.sh after sync
vid-gen -> ../../../../.npm/_npx/90a139d9a7555efa/node_modules/sequant/templates/scripts/… (a 2.12.0 npx cache)
pm-projects, varius -> ../../../../.npm/_npx/38ae72183b73fa32/…
ad-motion, matcha-maps -> ../../../sequant/templates/scripts/… (sibling dev checkout)

sequant doctor then reports each link as points outside the project (machine-specific target) - run: sequant sync (re-links scripts/dev …) — and sync does not change it. The two commands point at each other.

Projects with node_modules/sequant (job-scout, sequant-landing, workout, workout-admin, sequant-demo-video) were repointed correctly (-> ../../node_modules/sequant/templates/scripts/…), so AC-4 holds; the gap is the copy branch on an existing link.

Likely cause

copyTemplates' copy mode skips a destination that already exists unless --force, and the existing entry is a symlink; #990 AC-5's test starts from an empty scripts/dev, so it never exercised "foreign link present → copy should replace it".

Release-graph plan — guard-2026-09 (2026-09-20)

Graph: guard-2026-09 Tier: mechanical Wave: 1
May touch: src/lib/templates.ts (copyTemplates copy branch), src/lib/templates.test.ts (append cases only — #1106 deletes a block in this file in the same wave), src/commands/doctor.ts, src/commands/doctor.test.ts, src/commands/sync.ts, src/commands/sync.test.ts, CHANGELOG.md

The npm run build && npm test AC is verified by the runner from a clean shell after the PR opens (PLAN I-1); the agent runs only the test files its ACs name. Mutation markers use failedTest starting with the test file path (I-4). #1097 AC-3 asserts this issue's AC-1 cell inside the writer matrix, so the test name replaces a foreign symlink is a contract.

Acceptance criteria

  • AC-1: in copy mode, an existing scripts/dev/*.sh symlink (any target) is replaced by a copy; an existing regular file is left alone without --force — Verify: npx vitest run src/lib/templates.test.ts -t "replaces a foreign symlink" → temp project seeded with a link into a fake _npx path, copyTemplates in copy mode → isSymlink false, file content equals the template. Mutation: restore the exists-skip for symlinks → fails; restore.
  • AC-2: doctor never recommends a command that will not change the link: on a machine-specific link it says which of sync (local node_modules/sequant present) or npm install sequant --save-dev && sequant sync applies — Verify: npx vitest run src/commands/doctor.test.ts -t "machine-specific" → message names the applicable fix; no --force.
  • AC-3: sync --dry-run lists each foreign link it will replace with a copy (old → (copy)), so I-2 holds for this path — Verify: npx vitest run src/commands/sync.test.ts -t "copy mode preview".
  • AC-4: suite green — npm run build && npm test.

Activity

  1. added
    bug
    on Sep 11, 2026
  2. admarble commented on Sep 20, 2026

    @admarble
    ContributorAuthor

    Runner context — guard-2026-09 launch, 2026-09-20. Base: origin/main at da9ed6e8 (2.16.0 plus the plan docs). These rules override the exec skill's defaults for this run:

    • No full npm test in the worktree. Run only the test files this issue's ACs name. The runner runs the full suite once per PR from a clean shell. Under sequant run the suite is not hermetic against SEQUANT_* (bug(test): suite isn't hermetic against the SEQUANT_* env phases inject — exec sees red tests it didn't cause and opens no PR #1086), so a full run shows red files you did not cause.
    • A named test file red before your change: re-run it as env -u SEQUANT_ORCHESTRATOR -u SEQUANT_WORKTREE -u SEQUANT_PHASE -u SEQUANT_ISSUE -u SEQUANT_RELAY -u SEQUANT_RELAY_FRAME npx vitest run <file>. If it is still red, the PR body names the issue number and the base-vs-head result for that test.
    • Mutation marker (gate-test ACs): <!-- SEQUANT_MUTATION: {"ac":"AC-N","mutation":"<what you deleted>","failedTest":"<test file path> > <describe> > <case>"} --> in the PR body. failedTest MUST start with the test file path, or the qa classifier reports it Failed. Commit before mutating.
    • 30-minute no-progress wall per phase. Commit a WIP checkpoint before any long command; every command foreground, with timeout.
    • Commits: single-line git commit -m "...", no Co-Authored-By. Never rebase or force-push after pushing. One AC per line in the PR body, ids AC-N only.
    • Stop at the PR. Do not merge.

    Issue-specific: In src/lib/templates.test.ts append cases only; #1106 deletes the old side-(b) block in the same file concurrently, so do not reorder or reformat existing blocks. #1097 later asserts your AC-1 cell by the test name replaces a foreign symlink, so keep that name exact.

  3. admarble commented on Sep 20, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 3 directories)

    AC Quality Check

    All four ACs carry a Verify: command and an outcome, so there is no vague, unmeasurable or open-ended wording. Two are precise but rest on wrong or ambiguous premises:

    • AC-1's mutation names a guard that doesn't exist. "restore the exists-skip for symlinks" assumes copyDir skips existing destinations. It doesn't. See Diagnosis.
    • AC-1's "regular file is left alone without --force" conflicts with sync's current behavior. See Open Question 1.
    • AC-2's "npm install … && sequant sync" is stricter than needed once AC-1 lands. See Open Question 2.

    Scope Assessment

    Non-Goals: ⚠️ Not stated in the issue. Proposed: no change to symlink mode (symlinkDir), the #990 target resolver, or the --no-symlinks flag.

    Metric Value Status
    Feature count 1 (copy-branch replace) plus two message and preview follow-ons ✅
    AC items 4 ✅
    Directory spread 3 (src/lib, src/commands, root CHANGELOG.md) ✅

    Verdict: ✅ SCOPE_OK

    Diagnosis (differs from the issue's "Likely cause")

    The issue says copy mode skips an existing destination. It doesn't. copyDir (src/lib/templates.ts:~1000) has no exists-skip for scripts/dev. It reads the template and calls writeFile(destPath, content), which is fsWriteFile and follows symlinks. I checked with a scratch link into a fake cache dir:

    link still symlink: true | cache content: NEW
    

    So in copy mode against a foreign link, sync:

    1. leaves the link in place, which is the reported symptom, and
    2. writes the rendered template through the link into its target, either an evicted or stale _npx cache or the sibling dev checkout's templates/scripts/ (ad-motion and matcha-maps). That second effect is worse than the reported one and is not in the issue.

    The fix is the same: unlink a symlink destination before writing. Both tests must assert the link target was not modified, or the write-through goes unguarded.

    Acceptance Criteria

    AC-1: copy mode replaces a foreign symlink; regular file left alone without --force

    Verification: Unit Test (real fs, src/lib/templates.test.ts, inside the existing scripts/dev symlink target routing (#990) fixture)
    Scenario: Given a temp project with no local node_modules/sequant, a templates dir outside cwd (→ copy mode), and scripts/dev/new-feature.sh symlinked into a fake _npx file → When copyTemplates("generic") runs → Then isSymlink is false, content equals the template, and the fake _npx file is byte-identical to before (write-through guard).
    Test name (contract): replaces a foreign symlink. #1097 AC-3 asserts it, so keep it exact. Append only; #1106 edits the same file concurrently.
    Mutation (corrected): delete the removeFileOrSymlink call added for symlink destinations in copyDir → the test fails; restore. Marker failedTest = src/lib/templates.test.ts > … > replaces a foreign symlink.
    Assumptions: a broken link (target missing) behaves the same. unlink handles it. Add a second case for that in the same describe block.

    AC-2: doctor names the fix that will actually change the link

    Verification: Unit Test (src/commands/doctor.test.ts -t "machine-specific")
    Scenario: Given a foreign, existing link target and no node_modules/sequant/templates/scripts → Then the message contains npm install sequant --save-dev && sequant sync. Given that dir present → Then it says run: sequant sync. Neither contains --force.
    Assumptions: doctor.test.ts mocks fs/promises and existsSync. The "local present" cell needs existsSync stubbed for that path. The existing test at line ~1219 (toContain("run: sequant sync")) must be updated for the no-local case. Both cells go under a describe/it name containing "machine-specific".

    AC-3: sync --dry-run lists each foreign link it will replace as old → (copy)

    Verification: Unit Test (src/commands/sync.test.ts -t "copy mode preview") plus a real-fs unit test on previewScriptsSymlinkTargets (see Derived AC-5)
    Scenario: Given previewScriptsSymlinkTargets returns {path, oldTarget:"../…/_npx/…", newTarget:"(copy)", changed:true} → When syncCommand({dryRun:true}) → Then output contains scripts/dev/x.sh: <old> → (copy).
    Assumptions: sync.test.ts mocks previewScriptsSymlinkTargets, so this test only proves the printer. The real behavior lives in the derived test below.

    AC-4: suite green

    Verification: npm run build && npm test, run by the runner from a clean shell after the PR opens (per the runner-context comment). The agent runs only the three named test files.

    Implementation Plan

    Phase 1: templates.ts (AC-1, AC-3)

    1. In copyDir's write step, before writeFile(destPath, content), lstat the destination. If it is a symlink, removeFileOrSymlink(destPath). isSymlink and removeFileOrSymlink are already imported. Symlink removal is unconditional (matches symlinkDir, "symlinks are always replaced").
    2. Regular-file protection: see Open Question 1. Recommended: applies to the scripts/dev copy call only, gated on options.force. Sync passes force:true, so refresh is unchanged. init passes none, so it leaves existing files alone.
    3. previewScriptsSymlinkTargets: when target.mode === "copy" (and not --no-symlinks/Windows), walk scripts/dev and emit {oldTarget: <link target>, newTarget: "(copy)", changed: true} for each destination that is a symlink. The existing sync printer (${path}: ${old} → ${new}) then renders the AC-3 format with no sync.ts change. Files with no symlink produce no entry.

    Phase 2: doctor.ts (AC-2)

    1. In checkScriptsDevLinks's machine-specific branch, compute hasLocalSequant = existsSync(join(nodeModulesSequant,"templates","scripts")), the same predicate resolveScriptsSymlinkTarget uses. Branch the message. Leave the dead-symlink branch as is: plain sync still fixes it, since the target then resolves.

    Phase 3: tests, changelog

    1. Append the tests (AC-1, AC-1b broken-link, AC-3, derived AC-5). Add a CHANGELOG.md entry under Unreleased.
    2. Mutation-verify AC-1 and AC-5. Commit before mutating (per the runner note).

    sync.ts needs no change unless the printer text must differ. It is in the "may touch" list only as a fallback.

    Rule Touchpoints

    Not triggered. The ACs have no default/always/never/rule/behavior/skip keyword pairs.

    Design Review

    1. Where does this belong? In copyTemplates' copy branch, the one writer for scripts. Doctor and the preview only reflect what that writer does.
    2. Simplest correct approach? Unlink-before-write for symlink destinations. Rejected: writeFile with O_NOFOLLOW, because it would fail instead of replace. Rejected: teaching writeFile in src/lib/fs.ts to unlink symlinks globally, because it is shared by every writer (.claude/settings.json, the version marker) and widens blast radius. Unlinking in copyDir also affects every tree it copies (skills, agents, hooks). That is harmless and arguably correct, since a symlinked skill file would otherwise be written through, but the tests only cover scripts/dev.
    3. Existing pattern? symlinkDir (templates.ts:806) already removes existing symlinks and skips regular files unless force. The copy branch should mirror it.
    4. Senior reviewer challenge? "Why does doctor still recommend npm install when sync alone now fixes it?" See Open Question 2. And "why does the preview say (copy) for a file whose content might already match?" Preview lists every symlink that will be replaced, which is the I-2 contract, so listing all of them is right.

    Feature Quality Planning

    Notable items:

    • Write-through is a second bug. The reported symptom hides that prior copy-mode syncs may already have overwritten sibling dev checkouts' templates/scripts/*.sh with rendered content. For ad-motion and matcha-maps that means git status in the sibling sequant checkout may show modified template scripts. Worth a one-line check before closing.
    • Windows and --no-symlinks hit the same branch, so both are fixed. Symlinks are uncommon there.
    • Idempotence: the second sync sees a regular file and rewrites it as before. Add an assertion in AC-1's test that a second copyTemplates call leaves it a regular file.

    Derived ACs

    Source Derived AC Priority
    Correctness AC-5: real-fs test in templates.test.ts that previewScriptsSymlinkTargets in copy mode returns old → (copy) for a foreign link and nothing for a regular file. AC-3's sync test mocks this function, so nothing else proves it. Mutation: return [] in copy mode → fails. High
    Correctness AC-1 test asserts the link target file is unmodified (write-through guard). High

    Open Questions

    1. AC-1 "an existing regular file is left alone without --force" vs. sync today. copyDir currently overwrites regular scripts/dev files, and sync refreshes stale copies on Windows and --no-symlinks. A literal reading that ignores force would freeze those copies at their install-time version. Recommendation: gate on options.force. Sync always passes force:true, so sync behavior is unchanged, and init and direct callers get the AC's "left alone" behavior. Impact if wrong: copy-mode users stop getting script updates from sync.
    2. AC-2 wording after AC-1. Once AC-1 lands, plain sequant sync also clears the warning without a local install, by replacing the link with a copy. Recommendation: follow the AC as written, but phrase the no-local message as sequant sync (replaces the link with a copy), or npm install sequant --save-dev && sequant sync to keep a link. It must contain the npm install string so the AC-2 test can match it. Impact if wrong: low. It is a message.
    3. Sibling-checkout contamination. Should the PR body tell the owner to git -C ../sequant status in ad-motion and matcha-maps' sibling checkout? Recommendation: yes, as a note in the PR body, not code.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Bug fix, ~30 lines of source, and the ACs have explicit verify commands. testgen is skipped (bug label only, and the ACs name their tests). No UI, no security-sensitive surface (a symlink unlink inside scripts/dev only).

    Label Review

    Current: bug
    Recommended: bug, planned
    Reason: planning complete.
    Quality Loop: won't auto-enable (SCOPE_OK, no complex/refactor/breaking label).

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

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 0/6 met

    Gaps:

    • The copyDir symlink-unlink silently widens to all template trees with no test covering the non-scripts/dev case
    • doctor duplicates half of the resolver's predicate, so in a local checkout it over-prescribes npm install where plain sync would symlink
    • The legacy fix(sync): never rewrite a user-owned AGENTS.md or write a machine-specific scripts/dev symlink (absorbs #991) #990 doctor assertion was weakened and no longer pins the branch it was written for
    • Test fixture creates and recursively removes node_modules/sequant in the real repo, which would delete a contributor's npm-linked install
    • Adjacent unguarded writeFile sites share this PR's root cause; unreachable today because sequant never creates those paths as symlinks
  6. added 3 commits that reference this issue on Sep 20, 2026
  7. added 2 commits that reference this issue on Sep 20, 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