Skip to content

fix(workspace): render knobs unconditionally and refuse symlinks - #1641

Merged
c-vigo merged 2 commits into
devfrom
bugfix/1640-render-knobs-idempotently
Sep 16, 2026
Merged

c-vigo merged 2 commits into
devfrom
bugfix/1640-render-knobs-idempotently

Conversation

@c-vigo

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

Copy link
Copy Markdown
Contributor

Description

.pre-commit-config.yaml is preserved across upgrades (#878 — the consumer owns
its hook excludes), so it accumulates past renders. The three knob renders
early-returned on an empty manifest key, which means "don't touch it", not
"restore the default". A consumer who set a knob and later cleared it kept
the previous render locally, while CI — which re-derives from .vig-os on every
run with no memory — resolved the default. The local hook and
validate-commit-range then disagreed about the same gate.

The scaffold-drift gate (#1295) could not catch it: it re-runs the scaffold and
git diffs, so it hits the same early return, reproduces the stale file
byte-identically, and sees no drift.

Type of Change

  • fix -- Bug fix

Modifiers

  • Breaking change (!) -- This change breaks backward compatibility

Changes Made

All in assets/init-workspace.sh.

1. The three renders are unconditional and idempotent. render_commit_types,
render_branch_types and render_refs_policy now write the resolved value on
every run. For an unset knob that rewrites exactly what the template already
ships, so a default scaffold stays byte-identical and the drift baseline is
untouched; for a cleared knob it restores the default instead of stranding the
previous render.

2. render_branch_types gained a generic anchor. It was anchored on the
literal stock alternation, which by construction stops matching the moment a
custom set has been rendered — so making it unconditional alone would not have
fixed it, because the stock set could never be written back. The anchor is now
the (?!^( prefix plus the )/[0-9] suffix, which together pin exactly this one
alternation: the (chore)/[a-z0-9] clause has the wrong suffix, and the
renovate/worktree clauses have no inner group. Verified to round-trip
byte-identically: stock → custom → stock reproduces the template line exactly.
With the stock literal gone, the old ordering constraint (this render had to run
before anything else that rewrites the line) goes with it.

3. New precommit_render_target helper refuses a symlinked config. This is
what makes the rest safe. A flake-hooks consumer's .pre-commit-config.yaml is a
gitignored /nix/store symlink (#883/#1167). [[ -f ]] is true for it, and GNU
sed -i writes a temp file and renames it over the path — replacing the symlink
with a regular file, which shadows the generated config with a frozen copy and
silently drops the consumer's hooks/hooksExcludes customizations. Making the
renders unconditional multiplies the chances of hitting that, so every in-place
render of this file now goes through one resolver that refuses both an absent and
a symlinked target. render_workflow_model's .pre-commit-config.yaml block was
carrying the same latent hazard and now uses it too.

Skipping costs a flake-hooks consumer nothing: their knobs reach the hook through
mkProjectShell, which reads the same .vig-os keys at eval time (#1434).

Changelog Entry

### Fixed

- **Clearing a scaffold knob now restores the default render**
  ([#1640](https://github.com/vig-os/devkit/issues/1640))
  - `.pre-commit-config.yaml` is preserved across upgrades, so it accumulates
    past renders. `render_commit_types`, `render_branch_types` and
    `render_refs_policy` early-returned on an empty manifest key, which meant
    "don't touch it" rather than "restore the default": a consumer who set
    `DEVKIT_COMMIT_TYPES`, `DEVKIT_BRANCH_TYPES`, `DEVKIT_REFS_OPTIONAL_TYPES`
    or `DEVKIT_REFS_POLICY` and later CLEARED it kept the previous render
    locally while CI, which re-derives from `.vig-os` on every run, resolved
    the default — the local hook and `validate-commit-range` then disagreed.
    The scaffold-drift gate could not catch it either: it re-runs the scaffold
    and hits the same early return, so the stale file reproduces identically
  - All three renders are now unconditional and idempotent, writing the
    resolved value every run. An unset knob rewrites exactly what the template
    already ships, so a default scaffold stays byte-identical
  - `render_branch_types` gained a generic anchor. It was anchored on the
    literal stock alternation, which by construction stops matching once a
    custom set has been rendered, so the stock set could never be restored
  - New `precommit_render_target` helper refuses a **symlinked**
    `.pre-commit-config.yaml` for every in-place render, `render_workflow_model`
    included. A flake-hooks consumer's config is a `/nix/store` symlink that
    `[[ -f ]]` accepts and `sed -i` would replace with a regular file, shadowing
    the generated config with a frozen copy. Skipping costs that consumer
    nothing: their knobs reach the hook through `mkProjectShell`, which reads
    the same keys at eval time

Testing

  • Tests pass locally (just test)
  • Manual testing performed (describe below)

Manual Testing Details

Five new bats cases in tests/bats/init-workspace.bats, committed failing first
(b4963195 — all five red, the symlink one red after a successful upgrade,
i.e. for the right reason):

  • clearing DEVKIT_COMMIT_TYPES restores the stock 11 --types
  • clearing DEVKIT_BRANCH_TYPES restores the stock alternation (the case the
    generic anchor exists for)
  • clearing DEVKIT_REFS_OPTIONAL_TYPES restores chore
  • clearing DEVKIT_REFS_POLICY restores chore
  • a symlinked .pre-commit-config.yaml survives an upgrade with all three knobs
    set: still a symlink, target byte-identical by sha256

Full runs, all green:

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have updated the documentation accordingly (edit docs/templates/, then run just docs)
  • I have updated CHANGELOG.md in the [Unreleased] section (and pasted the entry above)
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Additional Notes

render_workflow_model's gitflow → trunk transform has the same one-way shape
— switching a consumer back cannot restore the dropped (?!dev$) clause or the
prose substitutions — and is deliberately not fixed here. It is a set of
anchored prose/marker substitutions across many files rather than a single value
slot, so it needs its own design rather than this pattern. It does get the
symlink guard, since that hazard is identical.

Refs: #1640

@c-vigo
c-vigo enabled auto-merge September 16, 2026 18:29
@c-vigo
c-vigo merged commit 4914a5f into dev Sep 16, 2026
12 checks passed
@c-vigo
c-vigo deleted the bugfix/1640-render-knobs-idempotently branch September 16, 2026 18:35
c-vigo added a commit that referenced this pull request Sep 17, 2026
…ways (#1643)

## Description

`render_workflow_model` applies the `gitflow → trunk` retarget one way —
it
early-returns unless the resolved model is `trunk`. For every
**managed** file it
touches that is harmless: the template overwrite restores the gitflow
shape on the
next upgrade and the render simply does not re-run.
`.pre-commit-config.yaml` is
**preserved**, so the trunk edits stayed in it forever.

A consumer switching back to gitflow therefore lost the dev-branch guard
silently:
`no-commit-to-branch` stopped blocking direct commits to `dev`, on a
repo whose
`.vig-os` says `gitflow`. No error, no notice.

Last instance of the class fixed in #1640.

## Type of Change

- [x] `fix` -- Bug fix

### Modifiers

- [ ] Breaking change (`!`) -- This change breaks backward compatibility

## Changes Made

I scoped this out of #1641 believing it spanned many files and needed
its own
design. That premise was wrong, and measuring it first is what made the
fix small:
every other target self-heals, so the entire stale surface is two lines
of one file.

| Target | Managed / preserved | After a trunk → gitflow round-trip |
|---|---|---|
| `ci.yml`, `codeql.yml`, `sync-issues.yml`, `prepare-release.yml`,
`promote-release.yml` | managed | restored |
| `.github/renovate-default.json` | managed | restored (`["dev"]`) |
| `.devcontainer/justfile.gh` | managed | restored |
| `.claude/skills/branch-naming/SKILL.md` | managed | restored |
| **`.pre-commit-config.yaml`** | **preserved** | **STALE** |

So the `.pre-commit-config.yaml` edits move out of
`render_workflow_model` into a
new `render_branch_guard_model`, which runs for **both** models and
renders the dev
clause *from* the resolved model instead of only subtracting it under
trunk. It
goes through the `precommit_render_target` resolver added in #1640,
keeping the
symlink and absent-file refusals in one place. Each direction's anchors
stop
matching once applied, so a re-run is a no-op and a default gitflow
scaffold stays
byte-identical to the template.

**One trap worth recording, because it silently produced a no-op fix.**
GNU sed
4.10 treats a *mid-pattern* `$` in a basic regular expression as an end
anchor, not
a literal. So the obvious insert pattern

```sed
s|(?!main$)(?!^(chore)|(?!main$)(?!dev$)(?!^(chore)|
```

matches nothing at all — and fails silently, leaving the file untouched.
The
existing `(?!dev$)` strip only works because its `$)` sits at the very
end of the
pattern. Both metacharacters are escaped in the insert pattern for that
reason;
the replacement side needs no escaping, since sed gives `$`/`^` no
meaning there.
The comment in the source records this so the next person does not
rediscover it.

Flake-hooks consumers were never affected: their guard comes from
`mkProjectShell`,
which reads `DEVKIT_WORKFLOW` at eval time (#1224/#1434) and was already
correct in
both directions. It was only the scaffolded YAML that was one-way.

## Changelog Entry

```md
### Fixed

- **Switching `trunk` back to `gitflow` restores the dev-branch guard**
  ([#1642](#1642))
  - `render_workflow_model` applied the `gitflow -> trunk` retarget one way. For
    every managed file it touches that is harmless — the template overwrite
    restores the gitflow shape on the next upgrade — but `.pre-commit-config.yaml`
    is preserved, so a consumer switching back kept the trunk edits and lost the
    `(?!dev$)` clause: `no-commit-to-branch` silently stopped blocking direct
    commits to `dev` on a repo whose manifest said `gitflow`
  - The dev clause now renders from the resolved model in BOTH directions, in a
    new `render_branch_guard_model` that runs for either model and goes through
    the `precommit_render_target` resolver. Each direction's anchors stop
    matching once applied, so a re-run is a no-op and a default gitflow scaffold
    stays byte-identical to the template
  - Flake-hooks consumers were never affected: their guard comes from
    `mkProjectShell`, which reads `DEVKIT_WORKFLOW` at eval time and was already
    correct in both directions
```

## Testing

- [x] Tests pass locally (`just test`)
- [x] Manual testing performed (describe below)

### Manual Testing Details

Four new bats cases, committed first (`5c33a7ca`) with the two
behavioural ones
red:

- trunk → gitflow restores both the `(?!dev$)` clause and its comment
(red)
- the restored file matches the template **byte for byte**, the way a
managed file
  does — this is the assertion that caught the silent no-op sed (red)
- the gitflow render is idempotent: a second upgrade does not
double-insert
  (green before, pins the new code path)
- the render refuses a symlinked config (green before via #1640's
resolver; pins
  that the refactor keeps it)

Full runs, all green:

- `bats tests/bats/init-workspace.bats` — 270 passed, 0 failed.
Deliberately
includes the #1205 workflow-switch suite, the #1255 flake-hooks
fixtures, the
#1282/#1431/#1432/#1633/#1640 knob suites and the trunk-render
composition tests.
- `pytest` (minus the two suites needing a freshly built local image) —
1070
passed, 1 skipped. `tests/test_flake_checks.py` caught a `typos`
violation in a
  source comment on the first run; reworded rather than allowlisted.
- `prek run --all-files` — green, no reformats.

## Checklist

- [x] My code follows the project's style guidelines
- [x] I have performed a self-review of my code
- [x] I have commented my code, particularly in hard-to-understand areas
- [x] I have updated the documentation accordingly (edit
`docs/templates/`, then run `just docs`)
- [x] I have updated `CHANGELOG.md` in the `[Unreleased]` section (and
pasted the entry above)
- [x] My changes generate no new warnings or errors
- [x] I have added tests that prove my fix is effective or that my
feature works
- [x] New and existing unit tests pass locally with my changes
- [x] Any dependent changes have been merged and published

## Additional Notes

The contradiction guard still refuses a `--workflow` flag that
contradicts the
persisted `DEVKIT_WORKFLOW`; this changes nothing there. The switch path
this fixes
is the guard's own documented step 3 — set `DEVKIT_WORKFLOW` in
`.vig-os` on a
dedicated branch and re-run the upgrade.

Refs: #1642
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