Skip to content

fix(sync): plain sync overwrites CUSTOMIZABLE_FILES (constitution) — copyDir ignores force and never consults isCustomizableFile #814

Closed

Description

@admarble

Summary

sequant sync overwrites .claude/memory/constitution.md with the rendered template, destroying in-place customizations. No --force is required — plain sequant sync does it.

This is not a #708 regression. #708 fixed update and left sync destructive, with the divergence recorded in a code comment (src/commands/sync.ts:328-330). This issue is about closing that gap, because the reasoning that justified leaving it open no longer holds.

Root cause

isCustomizableFile has exactly one consumer, and it is on the diff path:

$ grep -rn "isCustomizableFile\|CUSTOMIZABLE_FILES" src bin | grep -v "\.test\."
src/lib/templates.ts:104:export const CUSTOMIZABLE_FILES = [".claude/memory/constitution.md"];
src/lib/templates.ts:109:export function isCustomizableFile(localPath: string): boolean {
src/lib/templates.ts:112:  return CUSTOMIZABLE_FILES.includes(localPath.replace(/\\/g, "/"));
src/lib/templates.ts:234:    if (hasLocalOverride || isCustomizableFile(localPath)) {

templates.ts:234 is inside computeTemplateChanges. The write path never consults it. copyTemplates calls an inner copyDir that unconditionally writes every template file:

// src/lib/templates.ts:412-428
async function copyDir(srcDir: string, destDir: string): Promise<void> {
  // ...
  let content = await readFile(srcPath);
  content = processTemplate(content, variables);
  await writeFile(destPath, content);

Note what is absent: copyDir never reads options.force. It is not "force overwrites customizations" — the copy overwrites unconditionally, and CopyTemplatesOptions.force only reaches the scripts symlink path (templates.ts:474-477). .claude/memory is copied at templates.ts:453 with no options argument at all.

Meanwhile sync sets force: true unconditionally on every apply, independent of the user's --force flag:

// src/commands/sync.ts:386-389
const copyOptions: CopyTemplatesOptions = {
  force: true, // Always overwrite when syncing
};

Why this matters more than it did at #708 time

sync is the recovery path for a missing skills install. A user whose .claude/skills/ has gone missing is told to run sequant sync — that is the correct and documented fix, and doctor points at it. So the remedy for one failure silently destroys the constitution as a side effect. Observed exactly this way in a downstream project on 2026-07-26.

sync is the automation-recommended path. #708's own text: "the path we point CI and scripts at is the one that declares success while leaving drift in place." The same path now also silently eats the one file sequant explicitly designates as user-owned.

The dry-run already knows. sync --dry-run reports Local overrides (will be overwritten by sync): N and lists them (sync.ts:344-368). The apply path prints nothing. A preview that warns and an apply that is silent is the divergence shape #722 was about.

The design question

Two distinct notions of "force" are conflated:

  • "overwrite the skills tree" — what sync always wants, and what it sets force: true for
  • "overwrite files the user owns" — what the user asks for with an explicit --force

They should not be one flag. The fix is to separate them, so sync's always-on tree overwrite stops implying consent to clobber CUSTOMIZABLE_FILES.

Acceptance criteria

  • AC-1: Plain sequant sync does not overwrite files in CUSTOMIZABLE_FILES that exist and differ from the rendered template; the skills/agents/hooks trees are still fully refreshed.
  • AC-2: copyTemplates gains an explicit opt-in for overwriting customizable files, separate from CopyTemplatesOptions.force, so sync's always-on tree overwrite no longer implies consent to clobber user-owned files.
  • AC-3: An explicit user sequant sync --force still overwrites them, and says so on stdout before writing.
  • AC-4: When plain sync preserves a customizable file, it reports it (e.g. preserved: .claude/memory/constitution.md — run \sync --force` to replace), so the apply path's output matches what --dry-run` already promises.
  • AC-5: A missing customizable file is still created on a fresh install — preservation applies only to files that already exist.
  • AC-6: A test pins the round trip: write a customized constitution, run plain sync, assert the content survives; run sync --force, assert it is replaced.
  • AC-7: docs/guides/customization.md no longer implies in-place constitution edits are unsafe under sync, and states the actual --force semantics.

Note on scope

.claude/memory/constitution.md is the only current entry in CUSTOMIZABLE_FILES, but the fix should be written against the list, not the single path — the whole point of the constant is that the set can grow.

Activity

  1. added
    bug
    cli
    on Jul 26, 2026
  2. admarble commented on Jul 26, 2026

    @admarble
    ContributorAuthor

    Related: #813. sequant sync is the documented remedy for the missing-skills failure in #813, so the two compound — recovering from #813 currently costs you your constitution. Landing #813's pre-flight will point more users at sync, which raises the priority of this one.

  3. admarble commented on Jul 26, 2026

    @admarble
    ContributorAuthor

    Plan (spec) — #814

    Complexity: Standard (7 ACs, ~3 directories: src/lib, src/commands, docs)

    AC Quality Check

    All 7 ACs are concrete and measurable. AC-6 is itself a test requirement (good — it pins the round trip). No vague/unmeasurable/open-ended patterns. No title/body verb tension.

    Scope Assessment

    • Non-Goals: No explicit ## Non-Goals, but a ## Note on scope fixes the boundary well: write against CUSTOMIZABLE_FILES (the list), not the single constitution path.
    • Feature count: 1 (separate two notions of "force" on the write path) ✅
    • AC items: 7 ✅
    • Directory spread: 3 ✅
    • Verdict: ✅ SCOPE_OK — focused, single-root-cause bug.

    Root cause (confirmed against source)

    Two write paths implement the "protect in-place customizations" rule, and only one honors it:

    • update.ts is correct: computeTemplateChanges → filters status === "local-override" → writes them only under --force (update.ts:189-206,233,279).
    • sync.ts bypasses that: it calls copyTemplates({ force: true }) unconditionally (sync.ts:388-391), and copyTemplates' inner copyDir writes every file with no existence/diff/customizable check (templates.ts:412-434). copyDir never reads options.force at all — force only reaches the scripts symlink path (templates.ts:465). .claude/memory is copied with no options (templates.ts:453).

    So isCustomizableFile has one consumer, on the diff path (templates.ts:234); the write path never consults it. Issue's diagnosis is accurate.

    Acceptance Criteria + verification

    • AC-1 — plain sync preserves differing CUSTOMIZABLE_FILES, trees still refresh. Integration Test. Given a customized constitution.md → When sequant sync → Then constitution content survives and .claude/skills is rewritten.
    • AC-2 — copyTemplates gains an opt-in separate from CopyTemplatesOptions.force. Unit Test. Given overwriteCustomizable unset → When copyTemplates over a differing customizable file → Then file preserved.
    • AC-3 — sync --force overwrites and says so on stdout before writing. Integration Test. Given customized constitution → When sync --force → Then stdout announces overwrite AND file replaced.
    • AC-4 — plain sync reports each preserved file (preserved: … — run \sync --force` to replace). **Integration Test.** Assert stdout matches the --dry-run` promise.
    • AC-5 — missing customizable file still created on fresh install. Integration Test. Given no constitution → When sync → Then rendered constitution written.
    • AC-6 — round-trip test: customize → plain sync survives → sync --force replaces. Integration Test (this AC is the test).
    • AC-7 — docs/guides/customization.md states real --force semantics; no longer implies in-place edits are unsafe under sync. Manual/Docs verification.

    Implementation Plan

    Phase 1 — Write-path opt-in (src/lib/templates.ts)

    1. Add overwriteCustomizable?: boolean to CopyTemplatesOptions (distinct from force).
    2. In copyDir, before writeFile: if isCustomizableFile(destPath) (normalize separators) AND the dest exists AND rendered content differs AND !overwriteCustomizable → skip the write and record the path as preserved. Otherwise write as today (this keeps AC-5: missing file → written; identical file → harmless rewrite).
    3. Return the preserved set from copyTemplates (extend the return object, e.g. preservedCustomizable: string[]) so the caller can report authoritatively without a second diff pass.

    Phase 2 — Sync wiring + reporting (src/commands/sync.ts)
    4. Keep force: true for the tree refresh; set overwriteCustomizable only from the user's --force flag (options.force).
    5. On --force: announce which customizable files will be overwritten before calling copyTemplates (AC-3) — compute via the existing computeTemplateChanges local-override set, matching the dry-run branch.
    6. On plain sync: after copy, print each preserved file from the returned set (AC-4), format preserved: <path> — run \sync --force` to replace`.

    Phase 3 — Tests + docs
    7. Round-trip integration test (AC-6) + unit test for copyTemplates opt-in (AC-2) + fresh-install create (AC-5). Add to src/commands/sync.test.ts / src/lib/templates.test.ts.
    8. Update docs/guides/customization.md: state that in-place constitution.md edits survive plain sync/update and are only overwritten by --force.

    Design Review

    1. Where does this logic belong? The skip belongs in copyDir (the write path) — the single place that actually mutates files — mirroring how update filters local-override before writeFile. Reporting belongs in sync.ts.
    2. Simplest correct approach? One new boolean option + a per-file guard in copyDir reusing existing isCustomizableFile + processTemplate. Rejected: routing sync through update's computeTemplateChanges+per-file-write path — larger blast radius, changes sync's whole-tree copy semantics.
    3. Existing pattern? Follows update.ts's --force-gates-local-override pattern. Confirmed it fits.
    4. What a reviewer would challenge? "Why not reuse computeTemplateChanges for the skip too?" — because the write path is a bulk copyDir, not a change-list; a per-file check keeps it O(1) and local. Report AC-3/AC-4 from the authoritative preserved-set, not a re-diff, to avoid drift.

    Open Questions

    1. .local-twin parity. update/computeTemplateChanges protect a managed file when it has a .claude/.local/ twin or is customizable (templates.ts:234). The ACs scope only to CUSTOMIZABLE_FILES. Recommendation: implement isCustomizableFile only (matches the ACs); note that a managed file with a .local twin but not in the list is out of scope here. Impact if this matters later: a follow-up to unify write-path protection with the diff path. (User decides — don't silently widen.)
    2. Reporting source for AC-3. Recommendation: announce the force-overwrite set from computeTemplateChanges (as the dry-run branch already does) to guarantee "before writing"; report AC-4 preservation from copyTemplates' return value. Impact if wrong: minor stdout ordering.

    Recommended Workflow

    Phases: spec → testgen → exec → qa
    Quality Loop: disabled (focused bug, SCOPE_OK)
    Reasoning: ACs carry Unit + Integration verification (AC-6 is an explicit test) → testgen stubs the round-trip before exec. No UI → no test phase. Not security-sensitive.

    Label Review

    Current: bug, cli
    Recommended: add planned. No complexity label needed (Standard tier).
    Quality Loop: won't auto-enable (SCOPE_OK).

  4. added
    planned
    on Jul 26, 2026
  5. admarble commented on Jul 26, 2026

    @admarble
    ContributorAuthor

    Progress Update — /exec (PR #815)

    Implemented via PR #815. All 7 ACs addressed.

    AC Coverage

    AC Status Evidence
    AC-1 — plain sync preserves differing CUSTOMIZABLE_FILES; trees still refresh ✅ copyDir per-file guard (src/lib/templates.ts)
    AC-2 — copyTemplates opt-in separate from force ✅ new overwriteCustomizable option, distinct from force
    AC-3 — sync --force overwrites and says so on stdout before writing ✅ force-announce block in syncCommand before copyTemplates
    AC-4 — plain sync reports each preserved file ✅ preserved: … — run \sync --force` to replace` from the returned set
    AC-5 — missing customizable file still created on fresh install ✅ guard only skips when the file exists + differs
    AC-6 — round-trip test ✅ pins the round trip in templates.test.ts
    AC-7 — docs state real --force semantics ✅ docs/guides/customization.md "Edit it in place (safe)" section

    Design

    Two conflated notions of "force" separated on the write path (mirroring how update already gates local-override behind --force):

    • force → refresh the managed skills/agents/hooks trees (unchanged, always on for sync)
    • overwriteCustomizable → consent to clobber user-owned CUSTOMIZABLE_FILES (set only from the user's --force)

    copyDir now skips an existing, differing customizable file unless opted in, and copyTemplates returns preservedCustomizable so the caller reports authoritatively without a second diff pass. --dry-run was updated to mirror the preserved/overwritten split so the preview can't diverge from the apply path (#722). Scoped to the CUSTOMIZABLE_FILES list, not the single constitution path. The .local-twin-on-a-non-customizable-managed-file case (Open Question 1) is left out of scope as recommended.

    Checks

    • npm run build ✅ · npm run lint ✅
    • npx vitest run src/lib/templates.test.ts src/commands/sync.test.ts → 57 passed (5 new write-path tests + 4 new/updated sync tests)
    • Broader src/lib src/commands sweep: green except a pre-existing, unrelated prompt-wait.test.ts timer flake (relay/inbox subsystem — verified it fails on the base branch too, no path to this change).

    Files changed

    • src/lib/templates.ts — overwriteCustomizable option, copyDir preservation, preservedCustomizable return
    • src/commands/sync.ts — wire overwriteCustomizable from --force, force-announce (AC-3), preserved report (AC-4), force-aware dry-run
    • src/lib/templates.test.ts, src/commands/sync.test.ts — new/updated tests
    • docs/guides/customization.md (AC-7), CHANGELOG.md
  6. admarble commented on Jul 26, 2026

    @admarble
    ContributorAuthor

    QA Review for Issue #814 (PR #815)

    AC Coverage

    AC Source Description Status Notes
    AC-1 Original Plain sync preserves differing CUSTOMIZABLE_FILES; trees still refresh ✅ MET copyDir skips a customizable file that exists+differs when !overwriteCustomizable (templates.ts); non-customizable files bypass the guard so skills/agents/hooks trees are still written unconditionally. Test: preserves an in-place customized constitution.
    AC-2 Original copyTemplates opt-in separate from force ✅ MET New overwriteCustomizable field is the only gate on the skip; force still drives the tree/symlink refresh. Test explicitly pins force:true alone does NOT overwrite: overwrites … only when overwriteCustomizable is set — not force.
    AC-3 Original sync --force overwrites + says so on stdout before writing ✅ MET Announce block runs before copyTemplates; ordering asserted via sentinel in announces customizable overwrites on stdout before copying under --force.
    AC-4 Original Plain sync reports each preserved file ✅ MET preserved: <path> — run \sync --force` to replaceprinted from the returnedpreservedCustomizableset. Test:reports preserved customizable files on stdout under a plain sync`.
    AC-5 Original Missing customizable file still created on fresh install ✅ MET Guard requires fileExists(destPath); a missing file falls through to writeFile. Test: creates a missing constitution on a fresh install.
    AC-6 Original Round-trip test (customize → sync survives → --force replaces) ✅ MET pins the round trip: plain copy preserves, opt-in replaces runs the real copyTemplates against a real fs.
    AC-7 Original Docs state real --force semantics; no longer imply in-place edits unsafe ✅ MET docs/guides/customization.md adds "Edit it in place (safe)". Cross-checked the update/update --force claims against update.ts — accurate (#708 protection + forced-overwrite listing before write).

    Coverage: 7/7 AC items fully met. No derived ACs in the spec.


    CI Status

    Check State Impact
    build (22.x) pass (2m38s) ✅ MET
    typecheck pass ✅ MET
    node-floor-guard pass ✅ MET
    validate-plugin pass ✅ MET
    validate-skills pass ✅ MET

    CI Summary: 5 passed, 0 pending, 0 failed. (Diff is not markdown-only, so no relaxation applied — build genuinely green.)


    Quality Metrics

    Metric Value Status
    Type issues (any) 0 OK
    Deleted tests 0 OK
    Files changed 6 OK
    Lines added +421 -
    Lines deleted −36 -
    Build / Lint / Tests (local) pass / pass / 57 passed OK

    Test Coverage Analysis

    Changed File Tier Has Tests?
    src/lib/templates.ts Standard ✅ src/lib/templates.test.ts (new preservation block, 5 tests, real-fs)
    src/commands/sync.ts Standard ✅ src/commands/sync.test.ts (new/updated apply + dry-run tests)

    Coverage: 2/2 changed source files have corresponding tests. No critical (auth/payment/security) paths.


    Test Quality Review

    Category Status Notes
    Behavior vs Implementation ✅ OK Asserts on-disk file content + returned preservedCustomizable, not internals. AC-3 test verifies announce-before-copy ordering.
    Coverage Depth ✅ OK Covers preserve / overwrite / fresh-install / identical-rewrite / round-trip; dry-run force vs plain split.
    Mock Hygiene ✅ OK sync.test mocks fs/manifest/templates/config (command unit test); isCustomizableFile mock mirrors the real allow-list, and templates.test exercises the real function — so mock drift can't hide a regression.
    Test Reliability ✅ OK Real tmpdirs + chdir, cleaned up in afterEach; deterministic PROJECT_NAME. No timing deps.

    Anti-Pattern Detection

    No any, no eval/innerHTML, no hardcoded secrets, no SQL concatenation. No package.json change (dependency audit N/A). The awaits inside copyDir's loop are the pre-existing sequential-copy pattern, unchanged in intent.


    Execution Evidence

    src/commands/sync.ts (CLI command logic) changed; scripts/ unchanged. End-to-end behavior is exercised by the real-fs round-trip in templates.test.ts (not mocked), plus the command-level unit tests and green CI. Status: Complete (integration test is the execution evidence).


    CLI Registration Verification

    CopyTemplatesOptions gained overwriteCustomizable, but it is internal — set programmatically from the existing options.force, not a new user flag. sync -f, --force is already registered in bin/cli.ts:206. Status: N/A (no new user-facing flag required).


    Risk Assessment

    • Likely failure mode: A future CUSTOMIZABLE_FILES entry copied via a copyDir whose destPath doesn't normalize to the exact allow-list string (e.g. an absolute destDir) would silently not be protected. Today all copyDir call sites use relative dest dirs (.claude/memory, …), and isCustomizableFile normalizes separators, so this holds — but it's an invariant worth keeping if the copy call sites change.
    • Not tested: AC-1's "trees still refresh" clause is guaranteed by construction (the guard only affects customizable files) but has no direct assertion that a non-customizable tree file is (over)written in the same call that preserves the constitution. Low risk — that write path is unchanged from pre-fix(sync): plain sync overwrites CUSTOMIZABLE_FILES (constitution) — copyDir ignores force and never consults isCustomizableFile #814 and covered by existing init/copy tests.
    • Sibling sites considered: none — cross-file. copyTemplates/copyDir is the single bulk write path; symlinkDir (scripts) already has its own force/skip logic and scripts aren't customizable; update.ts already protects overrides via computeTemplateChanges (fix(update/sync): sync reports 'up to date' on content drift; update overwrites in-place customizations (constitution) #708). No other file shares the "write path ignores isCustomizableFile" root cause.
    • Sibling-line audit: intra-file — the other unconditional writes in copyTemplates (.claude/settings.json, SKILLS_VERSION_PATH) are correct as-is: neither is in CUSTOMIZABLE_FILES (settings is customized via settings.local.json), so they must always be rewritten.

    Adversarial Re-Read

    Findings: No blocking gaps. (1) Verbatim motivating example (plain sequant sync eating the constitution) is pinned by the AC-6 round-trip against real fs. (2) The exec "evidence" is genuine post-fix validation (content survives plain copy), not a pre-fix repro. (3) Process state clean — only a pre-existing package-lock.json (−12 lines, present at session start) is uncommitted, correctly excluded from the commit. (4) Dry-run/apply parity verified by hand: computeTemplateChanges only classifies existing+differing files as local-override, and the dry-run partitions that set by isCustomizableFile exactly as copyDir does — so preview and apply cannot diverge (preserves #722's invariant), including the .local-twin-non-customizable case (both report "overwritten"). (5) No Non-Goal creep: scoped to CUSTOMIZABLE_FILES, and the .local-twin-write-path protection was deliberately left out per the spec's Open Question 1.

    Status: Clean


    Verdict: AC_MET_BUT_NOT_A_PLUS

    All 7 ACs are literally met, both changed source files are tested (including a real-fs round trip), local build/lint/tests and all 5 CI checks are green, and dry-run/apply parity is preserved. One recommended polish keeps this from A+:

    Recommendation (non-blocking): sync's --force help text is "Sync even if versions match" (bin/cli.ts:206). Since #814, --force also gates overwriting user-owned customizable files (the constitution) — its most consequential effect. Consider expanding the help text, e.g. "Sync even if versions match; also overwrites in-place customizations (e.g. the constitution)", so --help reflects the now-more-destructive semantics. The runtime announce (AC-3) already prevents a silent clobber, so this is polish, not a correctness gap.

    Optional: add one assertion that a non-customizable tree file is (over)written in the same copyTemplates call that preserves the constitution, to lock AC-1's "trees still refresh" clause explicitly.

    Documentation

    Next Steps

    1. (Optional, non-blocking) Expand the sync --force help text to mention customizable-file overwrite.
    2. Mergeable now — the recommendation can land in this PR or be waived.
  7. added
    needs-improvement
    on Jul 26, 2026
  8. added 2 commits that reference this issue on Jul 26, 2026
  9. admarble commented on Jul 26, 2026

    @admarble
    ContributorAuthor

    QA Follow-up for Issue #814 (PR #815)

    The two non-blocking gaps from the prior QA review (verdict AC_MET_BUT_NOT_A_PLUS at ac0addd) are now addressed in 5ee6d66:

    1. sync --force help text now names its destructive semantics: "Sync even if versions match; also overwrites in-place customizations (e.g. the constitution)" (bin/cli.ts). Verified via npx tsx bin/cli.ts sync --help.
    2. AC-1 "trees still refresh" clause is now pinned by a direct assertion: the preservation test seeds a stale .claude/skills/exec/SKILL.md and asserts the same copyTemplates call that preserves the customized constitution overwrites the tree file with the rendered template (src/lib/templates.test.ts).

    Additionally, origin/main was merged into the branch (3179fc0) to resolve a CHANGELOG conflict — the PR had gone CONFLICTING after #816 landed. The conflict resolution also repairs a pre-existing CHANGELOG corruption introduced on main by e6d160e, where the #816 bullet had overwritten the #808 entry's header and absorbed its body mid-sentence; both entries are now separate, complete bullets (and #816's bullet gains its issue ref).

    Validation: targeted suites green post-merge (templates.test.ts + sync.test.ts: 57 passed), tsc --noEmit clean, all 5 CI checks pass (build 22.x, typecheck, node-floor-guard, validate-plugin, validate-skills), PR is MERGEABLE.

    Verdict: READY_FOR_MERGE

    All 7 ACs met (see prior review for the full table), the only recommended polish has landed, and no new gaps were introduced.

  10. added
    ready-for-review
    and removed
    needs-improvement
    on Jul 26, 2026
  11. added a commit that references this issue on Jul 26, 2026
  12. added a commit that references this issue on Aug 9, 2026
  13. added 2 commits that reference this issue on Sep 9, 2026
  14. added a commit that references this issue on Sep 19, 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

    bugcliplannedready-for-review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions