Skip to content

fix(bootstrap,setup): back up .mcp.json before reformatting; keep a hand-edited governance workflow (#374) - #377

Merged
stackbilt-admin merged 6 commits into
mainfrom
fix/374-mcp-workflow-backup
Oct 18, 2026
Merged

stackbilt-admin merged 6 commits into
mainfrom
fix/374-mcp-workflow-backup

Conversation

@stackbilt-admin

@stackbilt-admin stackbilt-admin commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Closes #374

.mcp.json reformatting

When bootstrap adds mcpServers.charter to an existing .mcp.json that lacks it, it re-serializes the whole file with JSON.stringify(root, null, 2).

  • If the original is not exactly JSON.stringify(parsed, null, 2) + '\n' (a CRLF copy counts as different), .mcp.json is first backed up to .charter/.backup/mcp.json.<ts>.
  • Phase 2 then prints Updated .mcp.json (backup: …), and JSON carries setup.details.mcpBackup.
  • If the backup fails, nothing is written and a warning is shown (exit 0).
  • A canonically formatted file gets no backup.
  • All keys and values are preserved.

Behavior change under --force: if the backup fails while --force is replacing an existing mcpServers.charter entry, .mcp.json is listed in setup.details.notReplaced, the setup step fails, and bootstrap exits 1. On main this case exited 0.

charter-governance.yml

The logic lives in the new module packages/cli/src/commands/github-workflow.ts. It never diffs YAML.

Known templates. KNOWN_WORKFLOWS lists the 9 templates recovered from the history of setup.ts. Each entry records:

  • hash;
  • origin, the first commit that generated it;
  • packageManager;
  • a safe flag;
  • a hand-written refreshChanges.

Only the two current templates (npm and pnpm, from 003ec8d) are safe. All 7 older ones had contents: read and ran on pull_request only, so they carry the same text: contents: read → write; adds push and workflow_dispatch triggers; adds a step that commits and pushes to the repo.

What the current template grants. TEMPLATE_PERMISSIONS is one fixed line: the current Charter template grants contents: write and pull-requests: write, runs on push, pull_request and workflow_dispatch, and has a step that commits and pushes to the repo. A test checks it against the template.

Comparison. Before hashing, the workflow is normalized:

  • a leading BOM is dropped;
  • CRLF becomes LF;
  • trailing whitespace and trailing blank lines are ignored.

A pinned @stackbilt/[email protected] is normalized as well. The only template that pinned one (04b1442) is unsafe, so a pinned workflow is always kept unless --force is passed.

Outcomes (the same for bootstrap --ci github and setup --ci github):

Existing workflow Without --force Under --force
Missing Created Created
Identical Unchanged Unchanged
Same as the template after normalization Refreshed Refreshed
Current template for the other package manager Refreshed, and named: Updated … (package manager npm → pnpm; adds pnpm/action-setup@v4) (packageManagerChange in JSON) Same
Older Charter template Kept. The warning quotes the static refreshChanges (reason: would-widen-permissions, plus changes) Backed up, then replaced: Replaced … (backup: …; changes: …)
Anything else (hand-edited) Kept. Charter makes no claim about what was edited. The warning states TEMPLATE_PERMISSIONS and says to review it before using --force (reason: hand-edited, plus templatePermissions) Backed up, then replaced: Replaced … (backup: …; the current Charter template grants …)

When the backup fails under --force:

  • bootstrap prints Not replaced … (backup failed; see warning), lists the file in setup.details.notReplaced, fails the setup step (status: "partial") and prints Bootstrap finished with failures.;
  • setup prints CI policy gate: not replaced (backup failed) and adds { file, detail, reason } to notReplaced;
  • both exit 1.

The warnings also say what else --force does in each command, and that deleting the workflow and re-running replaces only the workflow.

Changes from main:

  • bootstrap no longer overwrites a hand-edited workflow;
  • bootstrap no longer silently widens a pre-badge workflow;
  • setup --force backs up before replacing;
  • setup --ci github without --force now refreshes the other package manager's current template (with the note above), where it used to leave it alone;
  • bootstrap's summary prints Bootstrap finished with failures. instead of Bootstrap complete. whenever any step failed;
  • bootstrap's governance-gaps check looked for .github/workflows/charter.yml, which Charter never generates. It now accepts charter-governance.yml or charter.yml.

JSON:

Command Fields
bootstrap setup.details.kept lists a kept workflow, using the platform separator like the .charter/ entries.
New: setup.details.replaced: [{ file, backup, reason, note }], setup.details.notReplaced: [{ file, detail, reason? }], setup.details.workflow: { file, action, reason?, changes?, templatePermissions?, backup?, packageManagerChange? }.
setup workflow.kept, notReplaced, reason, changes, templatePermissions, packageManagerChange, backup and warning.
The workflow is also added to top-level replaced and notReplaced, each with reason.

Path separators: kept, created, updated and workflow.path keep the platform separator, as before. The new replaced, notReplaced and workflow fields use /, as backup does.

applyManagedFile is removed; nothing else called it.

Docs

Tests

packages/cli/src/__tests__/workflow-mcp-backup.test.ts has 62 tests. The fixture fixtures/charter-governance-history.json holds all 7 historical templates.

  • Historical templates: each is driven through bootstrap --yes and setup, and each produces the exact static message.
  • Hand-edited workflows: flow-style permissions, job-level permissions, quoted on:, write-all and narrowed contents: read are all kept with the TEMPLATE_PERMISSIONS warning, and nothing in the output claims a widening or x: y → z. Each is also replaced under --force, with the template line in the output.
  • Package-manager swap: both directions, in text and JSON.
  • Exit 1 cases: bootstrap JSON status: "partial" with the setup step failed; setup notReplaced with reason; setup top-level replaced; .mcp.json backup failure under --force.
  • Governance-gaps check.
  • Preset error: the invalid-preset error lists all 6 presets.
  • Guards:
    • every hash is unique, a sha256 and has a safe flag;
    • every unsafe entry has a description;
    • the fixtures match KNOWN_WORKFLOWS one to one, with origin starting at the fixture's commit;
    • TEMPLATE_PERMISSIONS matches the template.

Full suite, run serially: 1691 passed, 3 failed. The 3 are the known Windows-only failures (bootstrap #307, context-refresh, serve-context), which also fail on main.

🤖 Generated with Claude Code

kovermier and others added 5 commits October 17, 2026 17:34
…and-edited governance workflow (#374)

- Adding mcpServers.charter to an existing .mcp.json that would be
  re-serialized differently backs it up to .charter/.backup/mcp.json.<ts>
  first (Updated .mcp.json (backup: ...), setup.details.mcpBackup); a failed
  backup writes nothing and warns. Canonically formatted files need no backup.
- bootstrap/setup --ci github refresh charter-governance.yml only when it
  matches a known Charter template (current npm/pnpm or a historical variant
  recovered from git history, hashed in commands/github-workflow.ts; CRLF,
  trailing whitespace and a pinned CLI version normalized). A hand-edited
  workflow is kept with a warning; --force backs it up to
  .charter/.backup/github-workflows-charter-governance.yml.<ts> and replaces it.
- Docs and CHANGELOG.

Closes #374

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ow refresh (#374 review)

- A known Charter workflow is refreshed without --force only when the refresh
  widens nothing: same normalized permissions and on: triggers, no new git push
  step. KNOWN_WORKFLOWS carries a per-version safe flag (only the current npm
  and pnpm templates); workflowWidening re-checks at run time and names the
  widening. Older contents: read templates are kept in bootstrap and setup with
  a warning (reason would-widen-permissions); --force backs them up and says
  what widened in the Replaced line.
- The --force hint names what else --force does in each command and that
  deleting the workflow and re-running replaces only it.
- A failed workflow backup under --force: not replaced (backup failed),
  listed in notReplaced, exit 1 in both setup and bootstrap.
- JSON: new workflow fields use '/'; bootstrap gains setup.details.workflow.
- Docs: pin/BOM normalization, notReplaced, ci-integration re-run section,
  docs-snippets; CHANGELOG.
- Tests: widening kept/replaced (bootstrap and setup), same-permission refresh,
  BOM, notReplaced and exit codes, CRLF .mcp.json, KNOWN_WORKFLOWS guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…backup

# Conflicts:
#	CHANGELOG.md
#	docs-snippets/charter-cli-reference.md
…iffing (#374 review round 3)

- Drop the run-time workflowWidening diff. KNOWN_WORKFLOWS entries carry a
  package manager, a safe flag and a hand-written refreshChanges; unsafe
  ones (every pre-badge template) are kept unless --force, with that text.
- Hand-edited workflows: no claim about the edit; the warning and the
  Replaced line state what the current template grants (TEMPLATE_PERMISSIONS).
  JSON: reason + templatePermissions (+ changes for older templates).
- npm<->pnpm refresh is named: Updated ... (package manager npm -> pnpm;
  adds pnpm/action-setup@v4), packageManagerChange in JSON.
- A failed --force backup (workflow, or .mcp.json replacing mcpServers.charter)
  fails bootstrap's setup step, status partial, "Bootstrap finished with
  failures.", exit 1. Setup: reason in notReplaced, replace added to replaced.
- Bootstrap kept[] uses the platform separator for the workflow.
- origin labels name the first commit; governance-gaps check accepts
  charter-governance.yml (and charter.yml).
- Docs: pin claim corrected; CHANGELOG, cli-reference, ci-integration, snippets.
- Tests: every historical template via bootstrap and setup (fixture), hand
  edits (flow style, job-level, quoted on, write-all, narrowed), pm swap,
  JSON status on exit 1, setup notReplaced reason, mcp --force exit 1, gaps.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es; precise failure summary (#374)

- An older template whose upgrade also switches npm<->pnpm: the kept
  warning and the Replaced line add "package manager npm -> pnpm (adds
  pnpm/action-setup@v4)"; JSON carries packageManagerChange.
- "Bootstrap finished with failures." only when bootstrap exits 1; a failed
  step that exits 0 (doctor WARN checks) keeps "Bootstrap complete.".
- A setup step failed by a backup: the banner names the cause and files,
  and the next step says to make .charter/.backup writable and re-run.
- Setup wording and comments match the static descriptions.
- Tests: pm switch on upgrade (kept, Replaced, setup JSON), doctor-only
  summary, banner, .mcp.json add-entry backup failure under --force (exit 0),
  old templates' on: has no push/workflow_dispatch.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@stackbilt-admin
stackbilt-admin merged commit 6583976 into main Oct 18, 2026
7 checks passed
@stackbilt-admin
stackbilt-admin deleted the fix/374-mcp-workflow-backup branch October 18, 2026 00:31
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.

bootstrap rewrites .mcp.json formatting and overwrites a hand-edited charter-governance.yml with no backup

2 participants