spec-kit icon indicating copy to clipboard operation
spec-kit copied to clipboard

fix(presets): seed constitution from preset constitution-template (#3272)

Open BenBtg opened this issue 5 months ago • 6 comments

Summary

Fixes #3272. A preset that ships a type: template constitution-template entry (e.g. strategy: replace with a ratified constitution) was never applied to .specify/memory/constitution.md. The constitution is the only template materialized to a live file rather than resolved on demand, yet ensure_constitution_from_template hardcoded a copy from the core template and bypassed PresetResolver. Combined with init seeding the constitution before preset installation, a preset's constitution could never go live.

This is a provenance fix (resolve through the same priority stack every other template uses), not a copy-into-core fix.

Changes

  • commands/init.py — ensure_constitution_from_template now resolves constitution-template through PresetResolver (project overrides → installed presets → extensions → core). Core remains the fallback when nothing overrides it. Skip-if-exists / not-found behavior preserved.
  • commands/init.py — reordered so the constitution is seeded after preset installation, so specify init --preset seeds from the resolved stack. No-op for non-preset init.
  • presets/__init__.py — install_from_directory re-seeds .specify/memory/constitution.md from the resolved preset template, only when the memory file is missing or still contains generic placeholder tokens ([PROJECT_NAME] / [PRINCIPLE_1_NAME]). Authored constitutions are never overwritten. Covers specify preset add and install_from_zip (which delegates here).
  • Tests — preset seeds memory; placeholder memory re-seeded; authored constitution preserved; CORE_TEMPLATE_NAMES override resolves constitution-template to the preset file; resolver-aware init seeding (core fallback, preset override, existing-file preservation).

Key guard

Placeholder-token detection (_constitution_is_placeholder) prevents clobbering legitimately authored constitutions during preset add on an existing project.

Testing

  • pytest tests/test_presets.py — 333 passed (7 new)
  • Full suite — 3853 passed, 4 skipped

This PR was authored autonomously by GitHub Copilot (model: Claude Opus 4.8). Each commit carries an Assisted-by: trailer.

BenBtg avatar Jun 30 '26 18:06 BenBtg

+1 — we hit this independently. We maintain a spec-kit harness for a team with a fixed, ratified constitution authored upstream in a canonical repo and installed verbatim downstream. /speckit.constitution treating .specify/memory/constitution.md as a template to (re-)author from placeholders is exactly the hazard #3272 names: for us an AI-authored variant is a governance violation, not a convenience.

We read #3276's diff against our use case, and it handles our edge cases correctly:

  • Deferred-value markers are preserved. Our authored constitution carries a few intentional spec-kit-style TODO(FIELD) deferred values (e.g. TODO(RATIFICATION_DATE)) that resolve at a later milestone. Because _constitution_is_placeholder keys on the stock template's specific sentinels ([PROJECT_NAME], [PRINCIPLE_1_NAME]) rather than any generic TODO( / [...], such a constitution is correctly classified authored and preserved. 👍 (That was our one worry from the summary; the diff resolves it.)
  • Byte-faithful install. shutil.copy2(resolved, memory_constitution) materializes the preset's replace template unchanged — exactly what a ratified constitution needs (no reformatting, token-fill, or version recompute).

One small maintainability suggestion: _CONSTITUTION_PLACEHOLDER_TOKENS is a fixed pair — worth a test pinning it to the actual [ALL_CAPS] tokens in the stock constitution-template.md, so the guard can't silently drift out of sync if that template's placeholders ever change.

Our current stopgap is cp canonical → diff (confirm byte-identical) → run the command report-only plus disabling the auto-authoring path; #3276's preset-aware replace path would let us ship the constitution as a preset template and retire that entirely. Thanks for picking this up.

(Disclosure, per CONTRIBUTING: this comment was drafted with the assistance of Claude Code (an AI coding agent) and reviewed by a human before posting.)

gglachant avatar Jul 01 '26 08:07 gglachant

Addressed review feedback in f954e30.\n\n- Fixed constitution seeding to handle composing strategies (//) in both paths ( and ): we now materialize composed content via whenever the winning layer is non-.\n- Added regression tests that verify wrap composition is materialized (no unresolved ) in both seeding paths.\n- Added a drift-guard test that pins to placeholders present in the core , per gglachant’s maintainability suggestion.\n\nPosted on behalf of @benbtg by GitHub Copilot (model: gpt-5.3-codex).

BenBtg avatar Jul 01 '26 18:07 BenBtg

Correction to my previous comment (shell quoting ate formatting). Addressed review feedback in f954e30.

  • Fixed constitution seeding to handle composing strategies (prepend/append/wrap) in both paths: ensure_constitution_from_template and PresetManager._seed_constitution_from_preset now materialize composed content via PresetResolver.resolve_content() whenever the winning layer is non-replace.
  • Added regression tests that verify wrap composition is materialized with no unresolved {CORE_TEMPLATE} in both seeding paths.
  • Added a drift-guard test that pins _CONSTITUTION_PLACEHOLDER_TOKENS to placeholders present in the core templates/constitution-template.md, per gglachant’s maintainability suggestion.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.3-codex).

BenBtg avatar Jul 01 '26 18:07 BenBtg

Addressed the latest Copilot feedback in aa09272.

  • Moved resolver/layer collection behind the existing-memory fast path in ensure_constitution_from_template, so we avoid unnecessary I/O when memory already exists.
  • Updated tracker output to distinguish copied vs composed constitution materialization.
  • Deduplicated constitution materialization logic used by both init seeding and preset install re-seeding into a shared helper (_materialize_constitution_template) to keep behavior in sync.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.3-codex).

BenBtg avatar Jul 01 '26 18:07 BenBtg

Resynced this PR with current main and marked it ready for review.

  • Rebased cleanly with no conflicts; branch is now 0 commits behind main.
  • Restored the required shutil import in init.py (commit e03fb69), fixing the prior ruff failure.
  • Local validation: ruff passed; 4,040 tests passed and 4 skipped.
  • All GitHub checks are now passing across lint, CodeQL, and the Python 3.13/3.14 OS matrix.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.6-sol).

BenBtg avatar Jul 13 '26 15:07 BenBtg

Addressed the outstanding review batch in commit 116ab2c.

  • Routed constitution and provenance writes through checked atomic helpers and rejected symlinked parents and destinations.
  • Replaced placeholder substring heuristics with hash/source provenance, retaining exact core-template comparison for legacy projects.
  • Made unchanged generated constitutions follow resolver priority while preserving edited or authored files.
  • Made expected post-install materialization failures non-fatal warnings.
  • Confirmed the earlier composition, fast-path, tracker, and shared-helper feedback remains addressed.

Validation: ruff passed; 343 focused preset tests passed; full suite passed with 4,045 tests and 4 skips.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.6-sol).

BenBtg avatar Jul 13 '26 16:07 BenBtg

Addressed the latest review round in commit 8685bcc.

  • Preset removal now reconciles unchanged generated constitutions from the winning remaining resolver layer.
  • Edited or authored constitutions remain preserved through provenance verification.
  • Updated the PR body to describe SHA-256 provenance, exact legacy core-template comparison, safe writes, composition, and install/removal reconciliation.

Validation: ruff passed; 344 focused preset tests passed; full suite passed with 4,046 tests and 4 skips.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.6-sol).

BenBtg avatar Jul 14 '26 10:07 BenBtg

Addressed the latest review round in commit d52098a.

  • Legacy projects now establish generated provenance only by exact comparison with the immutable bundled/source core constitution template.
  • Mutable project template copies and preset IDs beginning with core are no longer trusted as core provenance.
  • Preset removal now detects convention-based constitution-template paths and reconciles unchanged generated content to the remaining resolver layer.

Validation: ruff passed; 348 focused preset tests passed; full suite passed with 4,050 tests and 4 skips.

Posted on behalf of @benbtg by GitHub Copilot (model: gpt-5.6-sol).

BenBtg avatar Jul 14 '26 17:07 BenBtg