Skip to content

refactor(sync): fold computeDriftFingerprint's templates→.claude mapping into templateDestination; document the retired-file limitation #1045

Closed

Description

@admarble

Tier: mechanical Found by: QA on #1030 (PR #1042), 2026-09-11 Blocked by: #1030

Why

src/commands/sync.ts (computeDriftFingerprint, ~:130) builds the .claude/<relpath> install path for its stat-only cache key with its own string replacement, independently of templateDestination in src/lib/templates.ts — the function that now owns the templates→.claude mapping and its exclusions (templates/opencode/** → not a copy target, #1030). Harmless today (the cache key only needs to be stable), but it is the same root-cause pattern #1030 fixed: the next exclusion or relocation added to templateDestination silently diverges from the fingerprint.

QA also noted a limitation worth documenting rather than silently carrying: a template file that is retired from the bundle leaves its installed copy in place and invisible to drift detection (the walk is template-driven).

Scope

  • may touch: src/commands/sync.ts (computeDriftFingerprint), src/lib/templates.ts (export or reuse templateDestination), src/commands/sync.test.ts, docs/reference/ (one paragraph on retired files).
  • not in scope: any behaviour change to what sync/update write.

Acceptance criteria

  • AC-1: computeDriftFingerprint derives install paths via templateDestination (or a shared helper) and has no private templates/ → .claude/ replacement — Verify: grep -c 'replace("templates/", ".claude/")' src/commands/sync.ts → 0 (runner amendment 2026-09-11: the original grep also matched an unrelated prefix-strip replace("templates/", "") that is not a mapping); npx vitest run src/commands/sync.test.ts -t "fingerprint" → green, including a case where an excluded template (templates/opencode/**) contributes nothing to the key.
  • AC-2: the retired-file limitation is documented in one sentence where sync drift detection is described — Verify: grep -rc "retired" docs/reference/*.md ≥ 1.
  • AC-3: suite green — Verify: npm run build && npm test.

Activity

  1. added
    enhancement
    on Sep 11, 2026
  2. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    Runner context — launch (2026-09-11). Base: main at ac9afc3 (wave 1 + wave 2 merged). Stop-at-PR: open the PR, do not merge. Gate tests need Mutation-verified: records in the PR body with failedTest as <test file path> > <test name>. Foreground only: run every check in the foreground with a timeout (timeout 600 npm test 2>&1 | tail -80), once — the pre-tool hook blocks Monitor and run_in_background under the orchestrator and nothing will wake you; the phase ends when your turn ends. Commit a WIP within 5 minutes and every ~10 minutes; every phase has a hard 30-minute wall. CHANGELOG.md [Unreleased]: one bullet under the matching existing ### heading.

  3. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (3 ACs, 3 directories)

    AC Quality Check

    All 3 AC pass the linter — each has a concrete Verify: clause (grep or command), no vague/unmeasurable/open-ended language, no title/body tension.


    Scope Assessment

    Non-Goals: Informal — the issue's ## Scope section has a not in scope: bullet ("any behaviour change to what sync/update write") rather than a dedicated ## Non-Goals heading. Sufficient for this size of change; no warning raised.

    Metric Value Status
    Feature count 1 ✅
    AC items 3 ✅
    Directory spread 3 (src/commands, src/lib, docs/reference) ✅

    Verdict: ✅ SCOPE_OK (skipped as trivial by the assessor: 3 AC, single feature)


    Dependencies

    Blocked by: #1030 — merged (main is at ac9afc34, the squash of PR #1042 that shipped #1030's templateDestination exclusion). Unblocked, safe to start.

    In-flight overlap: worktree feature/1044-... is active in parallel but touches src/lib/workflow/pr-status.ts / state-cleanup.ts only — confirmed via git diff --name-only origin/main...HEAD, no overlap with this issue's files.


    Acceptance Criteria

    AC-1: computeDriftFingerprint derives install paths via templateDestination, no private templates/→.claude/ replace

    Verification: Unit Test
    Scenario: Given the template tree includes an excluded path (templates/opencode/**) → When computeDriftFingerprint builds its cache-key lines → Then it resolves each install path via the exported templateDestination (not a private .replace("templates/", ".claude/")), and excluded templates contribute zero t:/l:/o: lines to the key (matching the skip computeTemplateChanges already does at templates.ts:356-359).
    Assumptions: templateDestination stays exported from src/lib/templates.ts (it is, templates.ts:323). sync.test.ts's vi.mock("../lib/templates.js", ...) spreads ...actual, so the real templateDestination runs in tests rather than a stub — no new mock wiring needed for the happy path, only for asserting the excluded-path case.

    AC-2: Retired-file limitation documented in one sentence

    Verification: Manual Test (doc content), gated by an automated grep
    Scenario: Given docs/reference/cheat-sheet.md's sync/drift-check description (the "Automation" callout under "When to use which", ~line 43) → When a reader reads it → Then one sentence states that a template file retired from the bundle leaves its previously-installed copy in place, invisible to sync's drift check (the walk is template-driven, not install-driven).
    Assumptions: cheat-sheet.md is the right home — it's the only doc that currently describes the sync/drift-check use case at all (no dedicated sync.md exists). If a more specific doc is added later this sentence should move with it.

    AC-3: Suite green

    Verification: Integration Test (build + full suite)
    Scenario: Given the full repo after the above changes → When npm run build && npm test runs → Then it exits 0.
    Assumptions: none beyond the current green baseline on main.


    Implementation Plan

    Phase 1: Fold the mapping into templateDestination

    1. In src/commands/sync.ts, import templateDestination from ../lib/templates.js.
    2. Replace the private mapping at sync.ts:130 (normalized.replace("templates/", ".claude/")) with a call to templateDestination(normalized). When it returns null (excluded: templates/opencode/**, templates/relay/**, templates/mcp.json), continue the loop for that template — don't emit t:/l:/o: lines for it, mirroring computeTemplateChanges's own skip at templates.ts:356-359.
    3. Leave sync.ts:134 (normalized.replace("templates/", ""), used only to build the on-disk template path via join(templatesDir, ...)) untouched — it's unrelated to the install-path mapping and isn't a duplicate of templateDestination. Confirmed by repo-wide grep: sync.ts:130 is the only stray reimplementation of the templates→.claude mapping outside templates.ts itself.
    4. templateDestination can also return a scripts/dev/... path (for templates/scripts/**) which has no .local override concept — guard the existing overridePath = localPath.replace(".claude/", ".claude/.local/") line with localPath.startsWith(".claude/") before computing/checking it, else skip the o: line for that entry.

    Phase 2: Tests

    1. Extend the fingerprint describe block in src/commands/sync.test.ts: assert that an excluded template (e.g. templates/opencode/foo.json) contributes nothing to the fingerprint — its mtime changing must not flip the cache result, and getFileStats should not be called for its .claude/-mapped ghost path.
    2. Regression-check the existing 4 fingerprint tests still pass unchanged (they use .claude/skills/... paths, unaffected by the fix).
    3. grep -n 'replace("templates/"' src/commands/sync.ts → should show only the unrelated on-disk-path strip (step 3), not the install-path mapping.

    Phase 3: Docs

    1. Add the one-sentence retired-file caveat to docs/reference/cheat-sheet.md.

    Phase 4: Verify

    1. npm run build && npm test.

    Design Review

    1. Where does this logic belong? Install-path derivation belongs entirely in templates.ts (templateDestination already owns it and its exclusions); sync.ts should be a consumer, not a second source of truth.
    2. Simplest correct approach? Call the existing exported function and skip on null, matching the skip pattern computeTemplateChanges already uses. No new abstraction, no signature changes.
    3. What existing pattern does this follow? The null-skip idiom at templates.ts:356-359.
    4. What would a senior reviewer challenge? "Why does the fingerprint walk excluded/customizable files at all instead of filtering listTemplateFiles itself?" — Because the fingerprint's job is cheap cache-invalidation (path+mtime hashing), not content diffing; walking everything and skipping at the mapping step keeps it consistent with the real write path (computeTemplateChanges) without touching the shared walker other callers rely on.

    Feature Quality Planning

    All standard checks pass. Notable items:

    • Cache format is unchanged (still a sha1 hex string) — a stale on-disk drift cache from before this fix mismatches once, forcing one extra rescan, then self-heals. No migration needed.
    • No new derived ACs.

    Open Questions

    1. Should the retired-file caveat get more than one sentence (e.g. its own "Known limitations" subsection)? — Recommendation: one sentence, per AC-2's literal ask. Impact if wrong: cheap to expand into a subsection in a follow-up docs pass if a reviewer wants more.

    Recommended Workflow

    Phases: exec → qa
    Quality Loop: disabled
    Reasoning: Single-file mechanical refactor (per the issue's own Tier: mechanical) of already-tested code — sync.test.ts already has a dedicated fingerprint describe block to extend directly, so testgen stubs would add process overhead without value. Not UI, not security-sensitive, scope is SCOPE_OK/trivial, so no quality loop.


    Label Review

    Current: enhancement
    Recommended: no change
    Reason: Scope is SCOPE_OK and correctly labeled already; no UI/security/complexity signal to add.
    Quality Loop: Won't auto-enable — scope trivial, single-file fix.


    --- DRAFT GITHUB ISSUE COMMENT (PLAN) ---

    Plan for #1045

    • AC-1: computeDriftFingerprint derives install paths via templateDestination, no private templates/→.claude/ replace — Unit Test
    • AC-2: Retired-file limitation documented in one sentence — Manual/doc, grep-gated
    • AC-3: Suite green — Integration (build+test)

    Implementation: Replace sync.ts:130's private templates/→.claude/ mapping with a call to the already-exported templateDestination (templates.ts:323), skipping (continue) when it returns null for excluded templates (opencode/, relay/, mcp.json) — mirrors the skip computeTemplateChanges already does. Guard the .local override-path check with localPath.startsWith(".claude/") since templateDestination can also return non-.claude/ paths (scripts/dev/...). Leave sync.ts:134 (on-disk template path strip) alone — it's not a duplicate of the mapping. Add a fingerprint test proving an excluded template contributes nothing to the cache key. Add one sentence to docs/reference/cheat-sheet.md on the retired-file blind spot.

    Key decisions: No testgen (existing test file covers this function already); no quality loop (trivial scope); docs sentence goes in cheat-sheet.md (only doc describing sync/drift-check today).

    Open questions: Whether the retired-file caveat deserves more than one sentence — recommend keeping it to one per AC-2, expandable later if wanted.

    Recommended workflow: exec → qa (quality loop disabled).

  4. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 3/9 met

    Gaps:

    • AC-1's grep proxy is over-broad relative to its own requirement clause, so the AC's verbatim verify command reads as a failure even though the requirement is met
    • The two behavior branches introduced beyond the opencode case, including the one that fixes the latent scripts/dev cache-staleness bug, ship untested

    Suggestions:

    • Add one fingerprint case asserting a templates/scripts/x.sh template contributes l:scripts/dev/x.sh and no o: line.
  5. added 2 commits that reference this issue on Sep 11, 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

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions