Skip to content

fix(#1090): one ownership rule for project files across init, sync and update - #1105

Merged
admarble merged 11 commits into
mainfrom
feature/1090-fix-sync-init-update-one-ownership-rule-for-projec
Sep 20, 2026
Merged

admarble merged 11 commits into
mainfrom
feature/1090-fix-sync-init-update-one-ownership-rule-for-projec

Conversation

@admarble

@admarble admarble commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

One declared ownership policy per destination, consulted by every writer.

Pre-PR AC Verification

AC Source Description Status Evidence
AC-1 Original Single ownershipPolicy(), sole source for init/sync/update ✅ Implemented evidence grep prints 0 (baseline 9). src/lib/templates.ts:239-395
AC-2 Original Two-sided gate test, both sides mutation-verified ✅ Implemented Tests 2 passed. src/lib/templates.test.ts:186-330
AC-3 Original .sequant/settings.json declared user-owned; init guard reads the policy ✅ Implemented grep -c ownershipPolicy src/lib/settings.ts → 2; -t "1071" → Tests 14 passed
AC-4 Original .claude/settings.json sequant-owned, settings.local.json named; dry-run prints overwrite ✅ Implemented grep settings.local.json → 2; AC-5 parity case asserts preview.overwrite contains the path
AC-5 Original Per-file decision printed; applied set == printed overwrite+merged ✅ Implemented npx vitest run src/commands -t "1090" → Tests 4 passed
AC-6 Derived ownershipPolicy normalizes \ → /; gate asserts it ✅ Implemented assertion (a3) inside side (a) — every routed destination's backslash form must resolve identically
AC-7 Derived update --force reports the same protected set ✅ Implemented npx vitest run src/commands/update*.test.ts -t "1090" → Tests 2 passed
AC-8 Original npm run build && npm test green ✅ Implemented npm run build ✅, npm run lint ✅, npm test 6522 passed / 3 failed, all three the known SEQUANT_* env leak (#1086) — see the test plan.

Test plan

Check Command Result
Build npm run build ✅ pass
Lint npm run lint ✅ pass (--max-warnings 0)
Templates npx vitest run src/lib/templates.test.ts ✅ 59 passed
Sync npx vitest run src/commands/sync.test.ts ✅ 46 passed
Init npx vitest run src/commands/init.test.ts ✅ 53 passed
Update npx vitest run src/commands/update.test.ts src/commands/update.integration.test.ts ✅ 23 passed
Settings + agents-md + codex init npx vitest run src/lib/settings.*.test.ts src/lib/agents-md.test.ts src/commands/init.codex.integration.test.ts ✅ 70 passed
Settings-adjacent (doctor/ready/preflight) npx vitest run src/commands/doctor.test.ts src/commands/ready.test.ts src/commands/run-preflight-exit.test.ts src/lib/workflow/ready-gate-run.test.ts src/commands/ready.phase-policy.test.ts ✅ 105 passed
Full suite npm test ✅ 6522 passed, 3 failed — all three are #1086 (below), not this diff
Import-time regression npx vitest run src/lib/worktree-isolation.test.ts src/commands/doctor.test.ts src/lib/workflow/log-rotation.test.ts src/lib/workflow/token-utils.test.ts ✅ 131 passed (was red on CI)

End-to-end smoke (real filesystem, not mocks)

A scratch project was init-ed from this branch, its constitution hand-edited and its exec SKILL.md locally tweaked — the exact AC-5 fixture. sync --dry-run printed:

File ownership decisions:
  preserved: .claude/memory/constitution.md
  overwrite: .claude/skills/exec/SKILL.md

sync then printed the identical block, and after it ran the constitution's sha1 was byte-identical (118e7688… before and after, hand-edit intact) while the skill's local tweak was gone. Under --force both files moved to overwrite.

Mutation verification (AC-2 gate test)

Both sides mutated from a committed, clean tree (git status --short → 0 lines), restored to clean after each.

Mutation-verified: AC-2 — added templates/stray-gate-probe.md, a file under templates/ with no entry in TEMPLATE_ROUTES; src/lib/templates.test.ts > templates > ownership policy covers every destination sequant writes > ownership policy covers every bundled template (side a) failed; restored.

Mutation-verified: AC-2 — removed the .codex/config.toml entry from NON_TEMPLATE_DESTINATIONS; src/lib/templates.test.ts > templates > ownership policy covers every destination sequant writes > ownership policy covers every non-template destination (side b) failed; restored.

Failure messages named the path in both cases:

  • side (a): undeclared template route(s) — add them to TEMPLATE_ROUTES in src/lib/templates.ts: templates/stray-gate-probe.md
  • side (b): init.ts#writeCodexConfig writes .codex/config.toml, which is not in NON_TEMPLATE_DESTINATIONS

Third probe — the recurrence path itself. Beyond the two the AC requires, a brand-new undeclared writer (async function writeProbeArtifact() in sync.ts, writing .project-owned-file) was added and side (b) failed with unregistered writeFile site(s) … sync.ts#writeProbeArtifact (line 174). That is the case that has reopened this class four times out of six, so it is the one worth knowing the gate catches. Restored to clean.

Notes for review

  • .gitignore is declared merge, not sequant-owned — init appends its .sequant/ entries only when absent and never rewrites existing lines. It is not one of the six defects, but it is a writeFile target in init.ts and therefore has to be declared.
  • Two mock gaps were fixed, not worked around. init.test.ts stubbed ../lib/templates.js with a bare factory, so createDefaultSettings reading ownershipPolicy threw; it now uses importOriginal and deliberately leaves ownershipPolicy real — a stub would make the bug(init): init --agent <name> on an initialized repo overwrites .sequant/settings.json with defaults #1071 preservation cases assert against the stub rather than the declared table. sync.test.ts needed PROJECT_MCP_JSON on its mcp-config mock and a default [] for computeTemplateChanges.
  • The apply path now runs one template diff pass to resolve decisions before writing. It is skipped entirely under --quiet, where neither the decision report nor the --force announcement is printed.
  • The parity assertion is not circular. The test's copyTemplates mock models the write path's own guard (ownershipPolicy(path) === "user-owned" && !overwriteCustomizable) and the applied set is derived from that, not from sync's printer.
  • templateDestination keeps its .claude/<relpath> fallback for unrouted paths, so behaviour is unchanged for everything already shipping; the gate is what forces a new tree onto the table before it can reach a release.

Second round — the five QA findings on this PR

1. Blocking: the import-time regression (CI build (22.x) red) — fixed

settings.ts → templates.ts → manifest.ts, whose top level ran const pkg = JSON.parse(findPackageJson()). Every suite that stubs fs and then imports settings.js threw at import. CI named it exactly: src/lib/worktree-isolation.test.ts > settings integration > isolateParallel defaults to false, Error: Could not find sequant package.json.

Fixed at the root, not the symptom: the read is lazy and memoized behind getPackageVersion(), and the two internal PACKAGE_VERSION uses now call it. No module-level fs side effect remains — the class that broke ESM import order in #734.

Causally verified, not assumed. With only manifest.ts reverted to the module-scope read, that exact test fails at src/lib/manifest.ts:34:24 ← src/lib/templates.ts:20:1; with the fix in place the four affected suites are 131 passed.

2. Shipped docs named a deleted symbol — fixed

templates/memory/constitution.md:131 (ships to every project), its memory/constitution.md twin and README.md:225 all said CUSTOMIZABLE_FILES. All three now name the policy (user-owned). The two constitution copies are byte-identical (diff clean). The remaining hits in the repo are historical by design: past CHANGELOG entries describing the old mechanism, docs/internal/ lab notes, a __tests__/fixtures/ transcript, and one comment in update.test.ts that explains what the characterization case maps from.

3. Apply-path cost — evaluated, measured, not changed

The finding's premise does not hold in this tree: there is one computeTemplateChanges pass on the apply path (sync.ts), and the --force announcement already reuses its result rather than computing a second. The four call sites in sync.ts are on mutually exclusive paths — the pre-flight helper computeContentDrift, the version fast path (returns), the dry-run path (returns), and the apply path.

What is real is the remaining single pass on a non-force apply, which main did not run. It cannot be removed without giving up AC-5's apply-side report, and printing after the copy instead would break the invariant the report exists for (#1030 I-2: the decision must be visible before the write). So I measured it rather than argue it:

  • computeTemplateChanges over the 63 bundled templates: 35.8 ms, yielding 57 changes.
  • That line is only reached when force is set or the version marker mismatches. A version-current plain sync returns at the report-only fast path above it, which runs its own diff and never gets there.

So the added pass lands only on runs that then render and write the entire tree — ~36 ms against that. The analysis is recorded as a comment at the call site so the next reader does not re-derive it. Happy to reopen if the measurement is the part you disagree with.

4. Duplicate preserved: output — fixed, and turned into a parity check

The decision line now carries the — run sync --force to replace hint that #814's separate post-copy loop used to add, so a preserved file is announced exactly once. What remains after the copy is the check the decision block cannot make for itself: the decisions come from the diff pass, preservedCustomizable comes from the write path that actually ran, so a file the writer preserved but the preview never predicted is precisely the #722 divergence — now reported loudly instead of printed as a silent duplicate.

The AC-5 parity test's stdout parser is scoped to the File ownership decisions: block rather than grepping whole stdout. That was not cosmetic: unscoped, it silently folded the #990 preserved: AGENTS.md line — printed outside the block — into the decision set, which is the failure mode the repo's gate-test rule warns about.

Mutation-verified: AC-5 print-once — removed the if (announced.has(file)) continue; guard in sync.ts so the post-copy loop prints every preserved file again; src/commands/sync.test.ts > sync command > ownership decision parity between --dry-run and apply (#1090) > prints a policy decision per file and applies exactly that set (#1090 AC-5) failed (expected [ …(2) ] to have a length of 1 but got 2); restored.

5. The .opencode/opencode.json merge rule — now enforced

The rule is real: writeOpencodeMcpConfig reads the existing config, merges in only mcp.sequant, and keeps every other entry. It was the gate that was weak — the write site was mapped to the .opencode/ prefix, so side (b) asserted the tree's sequant-owned rule and never touched the exact merge rule. A declared policy nothing enforces is the class this issue exists to end, so:

  • init.ts#writeOpencodeMcpConfig now maps to .opencode/opencode.json, and that exact path is declared in NON_TEMPLATE_DESTINATIONS alongside the prefix.
  • New assertion (b4) pins the policy value for every destination whose defect was decided (constitution.md/settings.json/AGENTS.md → user-owned, .mcp.json/.opencode/opencode.json → merge, .claude/settings.json → sequant-owned). The existing POLICIES.toContain(...) check passes for all three values, so it would have let a silent re-classification ship.

Mutation-verified: AC-2 — flipped .opencode/opencode.json's policy from merge to sequant-owned in OWNERSHIP_RULES; src/lib/templates.test.ts > templates > ownership policy covers every destination sequant writes > ownership policy covers every non-template destination (side b) failed (.opencode/opencode.json is declared merge; changing it reopens the defect it closed: expected 'sequant-owned' to be 'merge'); restored.

Full suite (AC-8)

npm test → 6522 passed, 3 failed, 1 expected-fail, 3 skipped across 322 files.

The three failures are src/commands/__tests__/prompt-wait.test.ts, src/lib/workflow/batch-executor.test.ts and __tests__/integration/relay-hook.integration.test.ts — the known SEQUANT_* environment leak (#1086) from running the suite inside a sequant run phase. Verified as such rather than assumed: re-running exactly those three files with SEQUANT_ORCHESTRATOR/PHASE/ISSUE/WORKTREE/RELAY/ISOLATE_PARALLEL stripped gives 3 files, 106 tests, all passed. None of the three touches templates.ts, sync.ts, init.ts, settings.ts or manifest.ts. CI does not set those variables.

Still open, by the runner's own note

Side-(b) granularity — per-write-site destinations rather than per-enclosing-function, non-awaited writeFile(, arrow-const writers — is deferred to a follow-up. merged: .mcp.json prints after syncSequantMcpPin has already written on the apply path; noted as accepted.

Closes #1090

🤖 Generated with Claude Code

Full suite — runner result (2026-09-19, ef43f6fd)

Run once in this worktree, foreground, from a shell with no SEQUANT_* variables: Test Files 322 passed (322), Tests 6525 passed | 1 expected fail | 3 skipped, 348s, exit 0. The four fs-stubbing files that crashed at import on the first pass (doctor.test.ts, worktree-isolation.test.ts, token-utils.test.ts, log-rotation.test.ts) pass 131/131 after the lazy getPackageVersion().

…tings through it

Entire-Checkpoint: 0907cea0c707
…nt/settings.json

Entire-Checkpoint: 072f6425aa17
…has no fs side effect

settings.ts -> templates.ts -> manifest.ts made every suite that stubs fs and imports settings.js throw at import time (CI build (22.x) red on worktree-isolation.test.ts). Memoize the read behind getPackageVersion() instead of running it at module scope.

Entire-Checkpoint: 30050aeb21c1
…CUSTOMIZABLE_FILES symbol

templates/memory/constitution.md ships to every project; keep it byte-identical to its memory/ twin. README's constitution section reworded the same way.

Entire-Checkpoint: 2edb1476e042
…port a parity check

The decisions block and the post-copy #814 report were fed by two different sources and both printed. The decisions line now carries the repair hint; what remains after the copy fires only when copyTemplates preserved a file the diff pass did not predict — the #722 divergence, which is worth a loud line. Test parser scoped to the decisions block so sync's other preserved: lines cannot leak into the parity set.

Entire-Checkpoint: 377a23651d63
… decided policy value

The gate mapped writeOpencodeMcpConfig to the .opencode/ prefix, so the exact merge rule describing the one file under that tree sequant merges was declared but never asserted — the same shape as the class this issue ends. Map the site to the exact path, declare it, and assert the policy VALUE for each decided destination; POLICIES.toContain passes for all three values and would let a silent re-classification ship.

Entire-Checkpoint: fcf2dff887a6
… and the lazy version read

Also records the apply-path diff cost at the call site: the pass only runs when force is set or the version marker mismatches, measured ~36 ms against 63 templates on a run that renders and writes the whole tree.

Entire-Checkpoint: 0541015c854d
@admarble
admarble merged commit d1d1b57 into main Sep 20, 2026
9 checks passed
@admarble
admarble deleted the feature/1090-fix-sync-init-update-one-ownership-rule-for-projec branch September 20, 2026 02:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(sync/init/update): one ownership rule for project files — the clobbering class fixed file-by-file in #708/#814/#990/#1042/#1071/#1078

1 participant