Skip to content

fix(sync): dry-run lists files apply never writes — the template walk maps templates/opencode/** to .claude/opencode/** (phantom drift nag in serve; update plants unrendered {{PHASE}} files) #1030

Closed

Description

@admarble

Epic: W2 Blocked by: #990 Tier: mechanical Doc: W2 PLAN §4 (template sync seam), §5 I-2, §7 D14

Why

computeTemplateChanges (src/lib/templates.ts:194-225) walks the entire templates tree with no skip list, so templates/opencode/** is mapped 1:1 to .claude/opencode/**. But the only real producer of the opencode shim is init (src/commands/init.ts:145-175, :300-330), which renders templates/opencode/command.md ({{PHASE}}) into .opencode/commands/<phase>.md and writes the plugin to .opencode/plugin/. Nothing in src/, templates/ or bin/ reads .claude/opencode/. Meanwhile sync's apply path is copyTemplates (templates.ts:600-665) with a fixed directory list — skills, agents, hooks, memory, scripts, settings.json — that does not include opencode at all.

One root cause, four symptoms:

Symptom Effect
sync --dry-run lists files that sync never writes dry-run ≠ apply — a direct violation of the ownership invariant (I-2: every decision visible in --dry-run before the write)
serve / version-preflight warns N file(s) differ from bundled content on every project that ran init before the shim existed permanent nag on every downstream project since 2.13
update "fixes" the nag by writing unrendered templates ({{PHASE}} ×4) to a path nothing reads junk under .claude/opencode/
sync never refreshes the real .opencode/ shim for projects that use --agent opencode shim drifts silently across releases

Motivating example (verbatim, downstream project on 2.11.0 → 2.14.0, 2026-09-10; prompts stripped)

$ sequant sync --dry-run
Summary (dry-run):
  New files: 4
  Modified: 11
  Customizable (preserved): 1
New files:
  .claude/hooks/capture-tokens.sh
  .claude/opencode/command.md
  .claude/opencode/plugins/lib/sequant-hooks-core.ts
  .claude/opencode/plugins/sequant-hooks.ts

$ sequant sync
  preserved: .claude/memory/constitution.md — run `sync --force` to replace
✔ Synced to v2.14.0

$ npx sequant@latest serve
!  Version current, but 3 file(s) differ from bundled content
   Run: npx sequant sync --force (or npx sequant update)

$ npx sequant update
Summary:
  New files: 3
  Modified: 0
  ✓ Unchanged: 56
New files:
  .claude/opencode/command.md
  .claude/opencode/plugins/lib/sequant-hooks-core.ts
  .claude/opencode/plugins/sequant-hooks.ts
✔ Updated 3 files

After this, grep -c '{{' .claude/opencode/command.md → 4 in the downstream project. The three files were removed by hand.

Scope

  • may touch: src/lib/templates.ts (computeTemplateChanges, copyTemplates), src/commands/sync.ts, src/commands/update.ts, src/commands/init.ts (extract the shim renderer so one producer owns .opencode/), src/commands/version-preflight.ts; tests: src/lib/templates.test.ts, src/commands/sync.test.ts, src/commands/update.test.ts (new if absent), src/commands/sync-source-invocation.integration.test.ts.
  • not in scope: opencode driver behavior; the .opencode/ layout itself; fix(sync): never rewrite a user-owned AGENTS.md or write a machine-specific scripts/dev symlink (absorbs #991) #990's AGENTS.md / symlink work (this issue is blocked by it because both edit templates.ts and sync.ts — land after, rebase onto its merge).
  • design call for the spec: either (a) sync/update reuse init's renderer for .opencode/ when the project already has .opencode/, or (b) templates/opencode is excluded from drift with a one-line reason and the shim is init-only. Pick one; both satisfy the ACs.

Acceptance criteria

  • AC-1: what sync --dry-run lists is exactly what sync then writes (same set of paths, customizable files excluded on both sides) — Verify: npx vitest run src/commands/sync.test.ts -t "dry-run matches apply" → on a temp project, the dry-run change set equals the post-sync diff of the tree. Mutation: add a template dir that copyTemplates skips → fails; restore.
  • AC-2: no path under .claude/opencode/ is ever produced by computeTemplateChanges, sync or update — Verify: npx vitest run src/lib/templates.test.ts -t "opencode" → change set contains no .claude/opencode entry; on a temp project after update, test ! -e .claude/opencode.
  • AC-3: the shim has one producer — on a project with .opencode/, sync and update refresh .opencode/commands/<phase>.md rendered (no {{PHASE}}) and .opencode/plugin/**; on a project without .opencode/, they write nothing there — Verify: npx vitest run src/commands/sync.test.ts -t "opencode shim" → both cases; grep -rc '{{' <tmp>/.opencode/commands/ → 0 for every file.
  • AC-4: the version-preflight "differ from bundled content" count only counts files the apply path can write — a fresh init project (no opencode) followed by serve prints no differ warning — Verify: npx vitest run src/commands/sync-source-invocation.integration.test.ts -t "no phantom drift" → stderr has no differ from bundled content. Mutation: re-add the unfiltered walk → fails; restore.
  • AC-5: no file written by init, sync or update contains an unrendered {{…}} placeholder — Verify: npx vitest run src/lib/templates.test.ts -t "no unrendered placeholders" → walks the temp project after each command, zero matches for \{\{[A-Z_]+\}\}.
  • AC-6: suite green — Verify: npm run build && npm test → pass.

Done when

--dry-run lists exactly what sync/update write, nothing ever lands under .claude/opencode/, and the opencode shim has exactly one producer that renders it.

Activity

  1. added
    bug
    cli
    graph-2026-09-w2
    on Sep 10, 2026
  2. admarble commented on Sep 10, 2026

    @admarble
    ContributorAuthor

    Scope addition from #990 (runner, 2026-09-10, W2 D16). #990's AC-6 originally required sequant update --dry-run to print the AGENTS.md decision and scripts/dev link-target changes. update's apply path never regenerates AGENTS.md and never re-links scripts/dev (it writes rendered template files only), so a dry-run that printed those decisions would itself violate I-2 (dry-run ≠ apply) — this issue's subject. The clause therefore lands here: once update's apply path is reconciled with sync (design call (a) or (b) in the body), update --dry-run must show the same AGENTS.md / scripts/dev decisions sync --dry-run shows (#990 AC-6, decideAgentsMdSync + previewScriptsSymlinkTargets in src/commands/sync.ts), or explicitly state that update does not manage those files.

  3. admarble commented on Sep 10, 2026

    @admarble
    ContributorAuthor

    Runner context — W2 wave 2 launch (2026-09-10). Node 09, mechanical tier. Base: main at 6783356 (v2.14.0 + wave 1: #980/#982/#990/#1032 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> (CLAUDE.md; a describe title alone is rejected). Foreground only: run every check in the foreground with a timeout (timeout 600 npm test 2>&1 | tail -80); the pre-tool hook now blocks Monitor and run_in_background under the orchestrator (#1032) — 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]: append one bullet under the matching ### heading, never a second heading of the same name.

  4. admarble commented on Sep 10, 2026

    @admarble
    ContributorAuthor

    Complexity: Standard (6 ACs, 2 directories)

    AC Quality Check

    All 6 AC declare explicit Evidence: verify commands (vitest target + assertion); AC-1/AC-4 carry Mutation: clauses. No vague/unmeasurable/open-ended language, no title/body tension.


    Scope Assessment

    Non-Goals (from issue body): opencode driver behavior; the .opencode/ layout itself; #990's AGENTS.md/symlink work (blocked-by, now merged).

    Metric Value Status
    Feature count 1 (single root cause) ✅
    AC items 6 ⚠️ (yellow line)
    Directory spread 2 (src/lib, src/commands) ✅
    File estimate ~7 (3 source + 4 test files) ✅

    Verdict: ✅ SCOPE_OK — every AC maps to the same fix, not independent features; AC-count sits at the yellow line but the issue's own Scope section already bounds it tightly.

    Dependencies: #990 merged (6783356a) — not a live blocker; decideAgentsMdSync/previewScriptsSymlinkTargets now exist as the reuse pattern below. Worktrees for #1024/#1025 exist but carry no diff yet; CHANGELOG.md is the only shared file across the wave (append-only, low risk).


    Root Cause

    templateDestination() (src/lib/templates.ts:318) already routes two paths to null — untracked by the generic diff/copy engine: templates/mcp.json, templates/relay/**. It has no case for templates/opencode/** (the only gap — every other top-level templates/ dir is either copied by copyTemplates or already excluded), so it falls through to the generic rule and gets diffed/reported despite copyTemplates never writing it.

    The real shim already has one set of producers — writeOpencodeCommands/Agents/Plugin/McpConfig (src/commands/init.ts:153,276,309,338), exported, called only from init --agent opencode. writeOpencodeCommands renders templates/opencode/command.md per-phase ({{PHASE}} substituted in a loop) — the generic single-pass renderer can never do this correctly, which is why unrendered {{PHASE}} landed under .claude/opencode/ in the motivating example. update.ts:194 writes applySet directly (no copyTemplates call), so it needs its own call site, not a shared one. Version-preflight's drift count (computeContentDrift → computeTemplateChanges) is fully derived from the same diff — AC-4 falls out for free once AC-2 lands.


    Acceptance Criteria

    AC-1: dry-run lists exactly what sync then writes

    Verification: Integration Test
    Scenario: Given a temp project mid-upgrade → When sync --dry-run then sync run in sequence → Then the reported path set equals the post-apply diff, including the opencode-shim decision when .opencode/ exists.
    Assumptions: The opencode-shim preview (Phase 2) is wired into the same dry-run block as AGENTS.md/scripts, or this AC stays false for --agent opencode projects even after AC-2/AC-3.

    AC-2: no .claude/opencode/ path produced by the generic engine

    Verification: Unit Test
    Scenario: Given templateDestination("templates/opencode/command.md") → When called → Then returns null; computeTemplateChanges output has zero .claude/opencode entries; post-update temp project has no .claude/opencode path.

    AC-3: the shim has exactly one producer

    Verification: Integration Test
    Scenario: Given a project with .opencode/ → When sync/update runs → Then .opencode/commands/<phase>.md (rendered) and .opencode/plugin/** refresh via the same functions init uses. Given no .opencode/ → When sync/update runs → Then nothing is written there.
    Assumptions: Presence detected via existsSync(".opencode") — sync/update don't know the project's driver choice, only the directory's existence (design (a), see Open Questions).

    AC-4: no phantom "differ from bundled content" warning

    Verification: Integration Test
    Scenario: Given a fresh init project (no --agent opencode) → When serve runs preflight → Then no "differ from bundled content" line.
    Assumptions: Falls out of AC-2; this test guards the regression per its own mutation clause.

    AC-5: no unrendered {{...}} placeholder anywhere

    Verification: Unit Test
    Scenario: Given init (± --agent opencode), then sync, then update → When walking every written file → Then zero matches for \{\{[A-Z_]+\}\}.

    AC-6: suite green

    Verification: Integration Test
    Scenario: Given the full change set → When npm run build && npm test → Then it passes.


    Implementation Plan

    Phase 1: Exclude templates/opencode/** from the generic engine

    1. src/lib/templates.ts — add a templates/opencode/ case to templateDestination() returning null, alongside mcp.json/relay/; extend the doc comment with the rationale (bespoke per-phase render, not a flat copy).
    2. No change to copyTemplates() (never had an opencode path) or version-preflight.ts (derives through computeTemplateChanges).

    Phase 2: One producer, refreshed by sync/update when .opencode/ exists

    1. src/commands/sync.ts apply path — after copyTemplates(), guarded by existsSync(join(process.cwd(), ".opencode")), call the four writeOpencode* functions to refresh the shim.
    2. src/commands/sync.ts dry-run path — add decideOpencodeShimSync() (shape of decideAgentsMdSync) returning "none"/"refresh"; report it next to the AGENTS.md/scripts preview lines so AC-1 holds for opencode projects.
    3. src/commands/update.ts — mirror both: guarded refresh after the applySet write loop, and the same decision under --dry-run.

    Phase 3: Tests

    1. src/lib/templates.test.ts — exclusion cases (AC-2); no-unrendered-placeholder sweep (AC-5).
    2. src/commands/sync.test.ts — "dry-run matches apply" (AC-1); "opencode shim" with/without .opencode/ (AC-3).
    3. src/commands/update.test.ts (new) — same two cases for update.
    4. src/commands/sync-source-invocation.integration.test.ts — "no phantom drift" (AC-4).
    5. npm run build && npm test (AC-6).

    Design Review

    1. Where does this logic belong? templateDestination already owns "does the generic walk touch this path" (2 precedents); the shim's render logic stays in init.ts's writeOpencode* functions — sync/update become a second caller, not a second implementation.
    2. Simplest correct approach? Exclude + reuse. Rejected: teaching the generic engine per-phase substitution just to diff/copy a 3-file directory that already has a working renderer.
    3. Existing pattern? The fix(sync): never rewrite a user-owned AGENTS.md or write a machine-specific scripts/dev symlink (absorbs #991) #990 shape exactly — null-routing exclusion (like mcp.json/relay/) plus a preview-before-write decision function (like decideAgentsMdSync) surfaced in --dry-run before apply.
    4. Senior reviewer challenge? "Why not delete templates/opencode/ and inline strings in init.ts?" — because sync/update need to re-render the same content later (AC-3); a shared template file is the right shape, only the generic walk needs to skip it.

    Notable: the four writeOpencode* functions unconditionally ensureDir+write — the sync/update call sites must gate on existsSync(".opencode") themselves, or a plain sync would create the directory on a project that never opted in. init.ts's post-write reminder (git add .opencode && git commit) should apply to the refresh path too.


    Open Questions

    1. Design (a) vs (b) — recommending (a). AC-3's wording ("refresh ... rendered") only holds under (a) (reuse init's renderer when .opencode/ exists); (b) (exclude-only, shim is init-only forever) leaves the issue's own symptom 4 ("shim drifts silently") unresolved. Impact if wrong: Phase 2 (steps 3-5) and AC-3's refresh assertions drop out; AC-3's test becomes "never touched either way" — smaller change, but the drift symptom stays open.
    2. Where should the shared refresh call live? Importing 4 functions from commands/init.ts into sync.ts/update.ts is a commands→commands import. Recommendation: import directly unless it creates a cycle; if so, hoist to src/lib/opencode-shim.ts. Impact if wrong: minor structural churn only.

    Recommended Workflow

    Phases: spec → testgen → exec → qa
    Quality Loop: disabled
    Reasoning: Every AC declares vitest evidence (unit/integration) — testgen stubs warranted despite the bug label, since declared evidence outranks the label heuristic (#938). Mechanical tier, single root cause, mutation-verified ACs already specify their own failure mode.


    Label Review

    Current: bug, cli, graph-2026-09-w2
    Recommended: no changes beyond planned.
    Reason: existing labels already describe the change; no UI surface, no complexity beyond what's scoped.
    Quality Loop: Won't auto-enable — SCOPE_OK, mechanical tier, self-verifying ACs.

    → gh issue edit 1030 --add-label planned

  5. added
    planned
    on Sep 10, 2026
  6. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

    AC coverage: 0/1 met

    Gaps:

    • now fails non-interactively on opencode projects that have nothing to update, because the presence-based shim decision can never resolve to 'nothing to do'
    • exits non-zero permanently on any project with .opencode/, breaking the CI-gating contract and leaving a now-false code comment in place
    • A second independent templates-to-.claude mapping survives outside templateDestination, the same root-cause pattern as this bug; harmless today but will silently diverge on the next exclusion
    • The test AC-1 names does not gate AC-1's own declared mutation because it mocks computeTemplateChanges and copyTemplates; the invariant is gated by the AC-4 integration test instead, and no SEQUANT_MUTATION record exists for AC-1 or AC-4

    Suggestions:

    • sync.ts calls decideOpencodeShimSync() twice (lines 396, 580) on mutually exclusive paths; update.ts computes once and reuses. Harmless, but the update form is cleaner.
    • update's ✔ Updated 0 files after Refreshed opencode shim undercounts — the shim files aren't in the tally.
  7. added 2 commits that reference this issue on Sep 11, 2026
  8. admarble commented on Sep 11, 2026

    @admarble
    ContributorAuthor

    QA Verdict: AC_MET_BUT_NOT_A_PLUS

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

    bugcligraph-2026-09-w2planned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions