Skip to content

feat(workspace): reconcile a preserved pre-commit config on upgrade - #1665

Merged
c-vigo merged 4 commits into
devfrom
feature/1654-reconcile-preserved-hook-config
Sep 21, 2026
Merged

c-vigo merged 4 commits into
devfrom
feature/1654-reconcile-preserved-hook-config

Conversation

@c-vigo

@c-vigo c-vigo commented Sep 21, 2026

Copy link
Copy Markdown
Contributor

What

One reconciliation pass over a preserved .pre-commit-config.yaml, run as a
post-copy render in assets/init-workspace.sh. Two cases, two evidence gates,
one report channel.

Case 1 — fold a retired block. A block the template retired because it
breaks
is replaced by the current template's hook for the same id, gated on a
byte-identical match against the historical template text. First row: the
pre-#1170 jackdewinter/pymarkdown block, whose language: python venv skews
against the flake interpreter and fails the devkit-upgrade.yml commit step
itself — so those repos never reach a PR where #1652's warning could be read.

Case 2 — insert a hook the consumer never received. A hook an older tree
never got lands at its template position, gated on PREVIOUS_PIN predating the
release that first shipped it (the mirror of retired_prune_paths(), #1348) and
on DEVKIT_FEATURES_DISABLED. First row: actionlint (#1660).

Everything else in the consumer's file is untouched: global and per-hook
exclude: patterns, ordering, comments, and the file's own mode.

Field evidence

Run against the real consumer configs on the maintainer's machine — this is what
settled the strictness question:

repo block outcome
two repos the 0.3.0–1.3.x block byte-for-byte folded
one repo same block, exclude: extended with vendored-doc paths declined, #1652 warning stands

Folding the third would have deleted the consumer's exception list. That is the
whole reason the gate is a byte-match and not a repo:-line match.

The resulting hunk on a real config (fold + insert in one upgrade):

+  # >>> devkit:actionlint — excised when the actionlint feature is disabled (#1660)
+  - repo: local
+    hooks:
+      - id: actionlint
+        …
+  # <<< devkit:actionlint
+
   # Markdown Linting (excludes auto-generated docs)   <- their comment, kept
-  - repo: https://github.com/jackdewinter/pymarkdown
-    rev: f93643d339dfee2a1022e7b05e8b5a281bfac553  # v0.9.23
+  - repo: local
     hooks:
       - id: pymarkdown
+        entry: pymarkdown
+        language: system

The six open questions

Full rationale in the ## Design comment on #1654; the decisions:

1. Opt-in knob, or default-on for byte-exact matches only? — Default-on, no
new knob
, both cases. Case 1's byte-match is the gate, and a knob would have
to be set by the repos that cannot run the upgrade far enough to read the notice.
Case 2's durable opt-out already exists (DEVKIT_FEATURES_DISABLED, #1660);
a second knob would split the opt-out surface.

2. init-workspace.sh or devkit-upgrade.yml? — init-workspace.sh, as a
sixth member of the render family that already seds this preserved file
(#1642, #1282/#1633, #1431, #1432, #1660). Decisive: devkit-upgrade.yml commits
in the project shell with the hooks running, so the repair must be in the
tree before the commit step; and every other upgrade path funnels through the
same script. The workflow only lifts the report lines.

3. Case 2: is the version gate enough? — Yes, because the ambiguity the
issue names cannot actually arise and the gate is paired with a one-shot
property. A repo pinned before the introducing release cannot have deleted a
hook devkit never shipped it, so "skipped several versions" is exactly the "never
had it" case. The insert fires at most once — the same run advances the pin
past that release — so a later hand-deletion is permanent (#1651 holds). The one
residual shape (hand-added then removed, still pinned low) is covered by
DEVKIT_FEATURES_DISABLED. No --adopt-hooks flag: the population furthest
behind is the automated lane, which runs no flags. Five gates in all: semver-shaped
pin present → pin predates the release → feature group not disabled → hook absent
from the file → template ships the block and the anchor is locatable.

4. Where does the block come from? — The rendered template, never
re-derived, through one extractor (hook_block_range) used for both cases and
both files. It prefers the # >>> devkit:<id> / # <<< devkit:<id> range #1660
introduced and falls back to the structural - repo: entry when a block carries
no sentinels. Preferring the sentinels is load-bearing beyond exactness: an
inserted actionlint block without them would be invisible to
render_actionlint_optout, silently outliving the opt-out. The pass is the first
.pre-commit-config.yaml touch of the upgrade, so the knob renders below apply
to whatever it writes.

5. Insert position? — Defined position, never append. After the end of the
repo entry holding the block's template predecessor hook, derived from the
template (so a template reorder moves it), because hook order is observable for
formatter hooks that rewrite files. No anchor in the consumer's file ⇒ skip with
a notice
, not a guess; the #878 diff stays the fallback.

6. One pass or two PRs? — One pass, one PR, two gates, two markers. They
share the extractor, the write path, the report channel and the tests. What stays
separate is the reporting, because the two ask for different actions:

preserved-hook-fold: pymarkdown-pre-1170 in .pre-commit-config.yaml
preserved-hook-insert: actionlint in .pre-commit-config.yaml

devkit-upgrade.yml carries them under "the upgrade rewrote your preserved
config — review the hunk", distinct from the existing preserved-hook-drift:
section ("the upgrade could not deliver this").

Two calls beyond the six

  • --preview honesty. The pass has a plan mode: the preview reports the
    planned fold/insert (like the DEVKIT_LICENSE and trunk sections already do)
    and suppresses the preserved-hook-drift: line for a block the run
    repairs — otherwise the preview would tell the consumer to hand-fold something
    the upgrade fixes for them.
  • File mode. The splice copies the rebuilt content back over the original
    instead of renaming a temp file onto it; a rename would hand the consumer
    mktemp's 0600. Caught by its own failing test before the fix.

Table seeds and their limits

Verification

uv run pytest -q --deselect tests/test_image.py --deselect tests/test_integration.py
  1672 passed, 1 skipped, 192 deselected in 424.46s

just lint          ruff check . — All checks passed!
just precommit     exit 0, 53 hooks Passed (shellcheck, actionlint, pymarkdown,
                   yamllint, sync-manifest, typos, bandit … all green)
just test-bats     596 passing, 0 failing

test_image / test_integration are excluded locally (lagging local :dev
image, not this branch) and left to CI.

The bats actionlint gate caught a real SC2016 on the new PR-body printf
(backticks as Markdown code spans) that just precommit does not see — fixed
with the same # shellcheck disable the sibling line carries.

Tests

tests/test_scaffold_preserved_hooks.py (new, 21 cases) drives the real
init-workspace.sh against seeded consumer trees: the fold fires on the exact
block and declines on a customized exclude: and a Renovate-bumped rev:; the
insert honours the version gate, the feature opt-out, an already-present hook, a
missing pin and a missing anchor; the block lands at its template position and
keeps its sentinels; the preview reports and writes nothing; a stock upgrade
reconciles nothing. Plus two workflow-shape cases in
tests/test_workflow_devkit_upgrade.py.

Out of scope

Refs: #1654

Failing tests for the two divergences a preserved .pre-commit-config.yaml
cannot heal on its own: a retired block that breaks (the pre-#1170
pymarkdown hook) and a hook the consumer never received (actionlint,
#1660).

Case 1 pins the byte-exact gate from both sides -- the historical block
folds, while a consumer-customized exclude and a Renovate-bumped rev fall
through to the #1652 warning untouched. Case 2 pins the PREVIOUS_PIN
version gate, the DEVKIT_FEATURES_DISABLED opt-out, the
already-present/no-pin/no-anchor refusals, the template position and the
opt-out sentinels the inserted block must carry.

Also pins the preview contract (report the rewrite, suppress the
hand-fold notice for a block the run repairs) and the devkit-upgrade.yml
channel that carries the report into the adoption PR.

Refs: #1654
The reconciliation writes through a temp file; renaming it over the target
(what sed -i does) hands the consumer mktemp's 0600 and silently
de-group-reads a tracked file.

Refs: #1654
A preserved .pre-commit-config.yaml (#878) protects the consumer's
customizations and blocks template evolution with them. #1652 made the two
resulting divergences visible; this repairs them, with a separate evidence
gate per case and nothing else in the file touched.

Fold: a block the template retired BECAUSE it breaks is replaced by the
current template's hook for the same id, gated on a byte-identical match
against the historical template text -- the only proof that the bytes being
overwritten are devkit's own output. An extended exclude: or a
Renovate-bumped rev: declines the fold and keeps the #1652 warning. First
row: the pre-#1170 jackdewinter/pymarkdown block, which breaks the
devkit-upgrade commit step itself, so those repos never reach a PR where
the warning could be read.

Insert: a hook an older tree never received lands at its template position,
gated the way retired_paths gates a prune (#1348) -- only when PREVIOUS_PIN
predates the release that first shipped it, so the consumer never had the
chance to decline. It fires at most once per repo, which keeps a later
hand-deletion durable (#1651), and DEVKIT_FEATURES_DISABLED is honoured
first. First row: actionlint (#1660).

Both read the block from the RENDERED template, preferring the
sentinel range so an inserted block stays excisable by its feature opt-out;
both report on the machine channel #1652 opened, which devkit-upgrade.yml
lifts into the step summary and its own adoption-PR section; and --preview
reports the planned rewrite instead of telling the consumer to hand-fold
what the run repairs.

Refs: #1654
Both branches appended a render function to the same region of
assets/init-workspace.sh, so the conflict was adjacency, not semantics:
reconcile_preserved_hooks and render_release_optout are independent and
both are kept whole, with their call sites intact.

Refs: #1654
@c-vigo
c-vigo merged commit 3c15154 into dev Sep 21, 2026
12 checks passed
@c-vigo
c-vigo deleted the feature/1654-reconcile-preserved-hook-config branch September 21, 2026 14:04
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.

1 participant