Skip to content

fix(mcp-config): .mcp.json writes still follow an existing symlink (#1122 follow-up) #1160

Closed

Description

@admarble

Observed

#1122 (PR #1157) routes template writers through a writeFile that replaces an existing symlink instead of following it. src/lib/mcp-config.ts:243,356,471 still write .mcp.json with fs.writeFileSync directly, so a .mcp.json symlink is still written through to its target. This is the same data-loss class as #1053 and #1122, and PR #1157's QA flagged it (risk_gap). #1122 scoped it out deliberately, and no issue tracked it.

  • Tier: mechanical
  • AC-1: no writer writes through a .mcp.json symlink. The matrix pins it today: mcpJson localSymlink/foreignSymlink are M for init, init --yes via the before.target === after.target arm of classify (the link survives and its target is merged into). The fix re-pins those cells to a replaced-link letter, with the target byte-identical — Evidence: npx vitest run src/lib/__tests__/writer-state-matrix.test.ts green with the re-pinned mcpJson rows
  • AC-2: a named case writes .mcp.json as a symlink to a file outside the project, runs the writer, and asserts the outside file's bytes are unchanged — Evidence: npx vitest run src/lib/mcp-config.test.ts -t "1160" passes, mutation-verified per AC-3
  • AC-3: AC-2's case is mutation-verified: restoring a direct fs.writeFileSync at the config write fails exactly that case — Evidence: SEQUANT_MUTATION marker for AC-2 in the PR body, with failedTest starting src/lib/mcp-config.test.ts >
  • AC-4: mcp-config.ts no longer writes the config with fs.writeFileSync (the three sites at :243, :356, :471 on 2026-09-25) — Evidence: grep -n "fs.writeFileSync" src/lib/mcp-config.ts prints nothing

Non-Goals

Activity

  1. added
    bug
    on Sep 25, 2026
  2. added 2 commits that reference this issue on Sep 25, 2026
  3. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner context (2026-09-25): these rules apply to the spec, exec AND qa phases.

  4. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (4 ACs, 3 directories)

    AC Quality Check

    All four ACs are single-line, carry Evidence:, and are measurable. Notes:

    • AC-3's failedTest must start with the file path src/lib/mcp-config.test.ts >, per the runner context.
    • AC-1 says "a replaced-link letter" without naming it. The plan pins T: the link is replaced by a regular file, and classify maps that to T only if the bytes equal the owned template. A merged .mcp.json is not template bytes, so it may classify as M (after.kind === "file", dest.merged). Exec should read the actual classify result and pin whatever it reports, provided the link is gone and the target is byte-identical (no V, no W).

    Scope Assessment

    Non-Goals: present (other merged-policy destinations, merge semantics, directory-at-destination).

    Metric Value Status
    Feature count 1 ✅
    AC items 4 ✅
    Directory spread 3 (src/lib, src/lib/__tests__, CHANGELOG) ✅

    Verdict: ✅ SCOPE_OK

    Acceptance Criteria

    AC-1: Matrix re-pinned

    Verification: Integration Test (writer-state-matrix.test.ts)
    Scenario: Given .mcp.json is a local or foreign symlink → When init / init --yes run → Then the link is replaced by a regular file and the target is byte-identical; the mcpJson localSymlink/foreignSymlink cells stop being link-preserving.
    Assumptions: the exact letter follows classify output (see above). Only the two mcpJson rows change. The gitignore rows at ~line 342 are also M M and are out of scope per Non-Goals.

    AC-2: Named 1160 case

    Verification: Unit Test (src/lib/mcp-config.test.ts, test title contains 1160)
    Scenario: Given .mcp.json is a symlink to a file outside the project → When createProjectMcpJson and syncSequantMcpPin run → Then the outside file's bytes are unchanged and .mcp.json is a regular file.

    AC-3: Mutation verification

    Verification: Manual (recorded in PR body)
    Scenario: Restore a direct fs.writeFileSync at the config write → only AC-2's case fails → restore. Record the SEQUANT_MUTATION marker with failedTest = src/lib/mcp-config.test.ts > <describe> > <case>. Commit before mutating.

    AC-4: No fs.writeFileSync in mcp-config.ts

    Verification: Unit Test / grep (grep -n "fs.writeFileSync" src/lib/mcp-config.ts prints nothing)

    Implementation Plan

    Phase 1: Sync-safe symlink-aware write

    1. writeFile in src/lib/fs.ts is async, but addSequantToMcpConfig (:243), createProjectMcpJson (:356) and syncSequantMcpPin (:471) are sync, with callers in commands/init.ts:1192,1251, update.ts:184 and sync.ts:436. Add writeFileSync to fs.ts beside writeFile, with the same rule: lstatSync for a symlink, unlinkSync it, then write. Route the async writeFile and the new sync one through one small shared predicate so the rule lives in one place (this is the fix(writers): update/init/sync write through an existing symlink at a sequant-owned destination (#1053 class) #1122 "rule lives here" intent).
    2. Replace the three fs.writeFileSync calls in mcp-config.ts with the new helper. Keep mkdir behaviour: addSequantToMcpConfig may write to a client config path whose parent may not exist. Check that the current code creates the dir before writing, and have the helper mkdirSync recursive only if the existing code does.

    Phase 2: Tests (parallel-safe after Phase 1)

    1. Add the 1160 case(s) to src/lib/mcp-config.test.ts: symlink to an outside temp file, run the writer, assert the outside bytes are unchanged and .mcp.json is no longer a link.
    2. Re-pin the mcpJson localSymlink/foreignSymlink rows in writer-state-matrix.test.ts. Run it first to see the new classify letters, then pin them. Add a unit test for the sync helper in the fs tests.
    3. Mutation-verify AC-2 (commit first), record the marker.
    4. One CHANGELOG [Unreleased] line.

    Design Review

    1. Where does this belong? src/lib/fs.ts, next to the fix(writers): update/init/sync write through an existing symlink at a sequant-owned destination (#1053 class) #1122 guard, not a second mechanism in mcp-config.ts.
    2. Simplest correct approach? Add a sync twin. Rejected: making the three functions async, which ripples into init/update/sync and their tests for no gain. Also rejected: a local lstat/unlink inline in mcp-config.ts, which is the second mechanism the runner context forbids.
    3. Existing pattern? fix(writers): update/init/sync write through an existing symlink at a sequant-owned destination (#1053 class) #1122's writeFile replace-a-link rule and fix(sync): copy-mode sync leaves existing machine-specific scripts/dev symlinks in place, and doctor's hint loops back to sync #1053's copyDir.
    4. Senior reviewer challenge? "Why not async?" (see above). "Line 243 is a client config, not .mcp.json." AC-4 covers it, and replacing a symlink at e.g. ~/.claude.json is a behaviour change for dotfile-managed users. Flagged as an open question.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • Sibling scan: fs.ts async writeFile and mcp-config.ts are the only writers of this class in scope. .gitignore and settings.json are excluded by Non-Goals. No sibling fs.writeFileSync sites elsewhere in mcp-config.ts beyond the three.
    • Rule Touchpoints: no AC triggers the behaviour-rule heuristic, so the section is omitted.
    • No dependencies or in-flight overlap were detected in the issue. I did not run the worktree diff scan.

    Open Questions

    1. Should addSequantToMcpConfig (:243, user-level client configs) also replace a symlink? Recommendation: yes. AC-4 requires it, and a link to a dotfiles repo gets its target rewritten today. Impact if wrong: a dotfiles-managed user's link becomes a plain file and the dotfiles copy stops updating. Consider a note in the CHANGELOG line.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: AC-1 and AC-2 declare npx vitest run evidence (unit/integration), so testgen could apply, but they are small and mechanical, so stubs add nothing. Tier is mechanical with 4 ACs.

    Label Review

    Current: bug
    Recommended: bug (no change)
    Quality Loop: won't auto-enable (SCOPE_OK)

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

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    Gaps:

    • AC-1's declared evidence (the writer-state matrix) does not gate the fix — the matrix passes with the fix fully reverted, so the mcpJson symlink cells pin nothing about replaced-link vs write-through.
    • The full-suite check on PR fix(#1160): mcp-config writes replace a .mcp.json symlink #1168 has not reported yet; the issue's runner note delegates the suite to CI, so it is the only unverified gate.
    • A dotfiles-managed client MCP config that is a symlink is silently converted to a regular file with no runtime notice; the behavior change is recorded only in the CHANGELOG.
    • The writer-state matrix no longer gates this fix (verified empirically). I reverted the three writeFileSync calls back to fs.writeFileSync and re-ran npx vitest run src/lib/__tests__/writer-state-matrix.test.ts: 649/649 passed. The mcpJson symlink rows read M M = = = = whether the writer replaces the link or writes through it, because classify returns M from two different arms — the file arm (mcp-config.ts:698-707) and the write-through symlink arm (:726-731). The diff added a comment asserting the M now comes from the file arm, but nothing tests that claim. By CLAUDE.md's own standing rule ("delete the thing it asserts, confirm exactly that test fails"), AC-1's declared evidence does not count as coverage for AC-1's claim. The fix is still protected — by AC-2's test, which does fail under the same mutation — but the matrix's header prose ("a regression that reintroduces either fails the invariant test rather than quietly re-pinning itself") is false for this cell.
    • Related, broader: W (write-through) only fires when after.bytes === owned, so a write-through at any merge destination collapses to M. The W regression guard has never covered merge destinations. Out of scope per Non-Goals, but the letter fix would incidentally close it.

    Suggestions:

    • Add a distinct legend letter for "symlink replaced, merged content, target intact" in the file arm of classify (when before.kind === "symlink"), re-pin the two mcpJson rows to it, and update the invariant counts. That is what AC-1 asked for and it makes the mutation fail. This touches a shared matrix file's legend, so it is worth a deliberate decision rather than an unattended loop fix.
  7. added a commit that references this issue on Sep 25, 2026
  8. admarble commented on Sep 25, 2026

    @admarble
    ContributorAuthor

    Runner verification (PR #1168): QA returned AC_MET_BUT_NOT_A_PLUS. Its fix-now gap was that AC-1's matrix evidence passed with the fix reverted. Fixed in 0addb27e: a merge written through a link now classifies as W, and reverting the fix fails exactly the four .mcp.json symlink cells. The mutation record is in the PR body. CI at 0addb27e is green (test, canary). The remaining QA notes (the symlink-to-file conversion is silent at runtime; W coverage for other merge destinations) are documented and not changed here.

  9. 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