Skip to content

fix(install): render kit.toml via temp file, never onto itself - #358

Closed
hieuphq wants to merge 1 commit into
masterfrom
fix/install-toml-self-clobber
Closed

hieuphq wants to merge 1 commit into
masterfrom
fix/install-toml-self-clobber

Conversation

@hieuphq

@hieuphq hieuphq commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Problem

install.sh silently reduces the kit-root kit.toml from 160 lines / 9 sections to a 12-line header stub — on the layout README Option 2 documents.

kit_render_install_toml "$KIT_DIR/kit.toml" "$KIT_TOML" (L672) gets the same path twice when the kit is cloned to ~/.claude/dwarves-kit and installed from there: KIT_DIR is $CLAUDE_DIR/dwarves-kit. The shell truncates > "$dst" before awk opens src; echo writes the header into the now-empty file; awk opens the same path, reads back that header, and passes it through its catch-all { print } — which is why the stub carries the header twice from a single run.

Fallout: every non-[modules] key stops resolving. kit_config_get mega.wave_cap → empty instead of 2, gate.understanding_gate → empty instead of true. Nothing errors, because each caller passes its own default — the config layer just quietly stops existing.

SB=$(mktemp -d); mkdir -p "$SB/home/.claude"
git clone -q https://github.com/dwarvesf/dwarves-kit.git "$SB/home/.claude/dwarves-kit"
wc -l "$SB/home/.claude/dwarves-kit/kit.toml"            # 160
cd "$SB/home/.claude/dwarves-kit"
env HOME="$SB/home" CLAUDE_DIR="$SB/home/.claude" bash install.sh >/dev/null 2>&1
wc -l "$SB/home/.claude/dwarves-kit/kit.toml"            # 12

Fix

Render to $dst.tmp.$$, then mv into place, so truncation cannot precede the read. The comment above the function says why the indirection is load-bearing, so nobody "tidies" it back.

Why CI missed it

Every existing case in tests/test-install-modules.sh installs from this checkout into a temp HOME:

HOME="$H1" bash "$KIT_DIR/install.sh"

so src != dst and the truncation is harmless. The one shape never exercised was the shape the README tells users to use.

This PR adds an NC in-place layout case that copies the kit into $H5/.claude/dwarves-kit and installs from there, asserting:

  • every section ([modules] [ledger] [mega] [gate] [features] [sync]) survives
  • the file did not shrink
  • the render header appears exactly once (the self-read signature)
  • kit_config_get mega.wave_cap still resolves to 2
  • [modules] is still recomputed from this run's --with, not merely copied through

Verification

Full run-table in docs/verification/install-toml-self-clobber.md.

Check Result
tests/test-install-modules.sh with fix 43 passed, 0 failed
same, with install.sh stashed to the buggy version 37 passed, 6 failed
test-install-compat.sh / test-install-contract.sh / test-install-clis.sh PASS / PASS=4 FAIL=0 / all 20 passed
test-config.sh / test-reserved-config-guard.sh / test-sync-cron-install.sh PASS / 9 passed / 29/29

The negative control is the load-bearing row: against the unfixed install.sh all 6 new checks fail with the exact clobber signature (160 -> 12 lines, header ... got 2, mega.wave_cap=<unset>), so the test cannot pass vacuously.

tests/test-config-registry.sh reports 17/19 both with and without this change (0 orphans on the live tree, TIER4_CLOSE strips the annotation) — pre-existing on a clean 1f7209c, not touched here.

Note for anyone already affected

kit.toml is tracked, so a clobbered install repairs without a reinstall:

git -C ~/.claude/dwarves-kit checkout -- kit.toml

Found on macOS, kit 892d25c (v2.0.0); reproduced on 1f7209c.

🤖 Generated with Claude Code

kit_render_install_toml was handed src == dst under README Option 2 (clone
to ~/.claude/dwarves-kit, install from there). The shell truncated dst before
awk opened src, so one install run reduced a 160-line, 9-section kit.toml to a
12-line header stub -- awk reading back the header echo had just written into
the same file. Every non-[modules] key then fell through to caller defaults,
silently: no error, no warning.

Render to $dst.tmp.$$ and mv into place.

Every existing case in test-install-modules.sh installs from the checkout into
a temp HOME, so src != dst and the bug was invisible to CI. Adds an NC in-place
layout case (6 checks) that reproduces the documented user layout; verified to
fail 6/6 against the unfixed install.sh.

Proof: docs/verification/install-toml-self-clobber.md

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@hieuphq

hieuphq commented Aug 7, 2026

Copy link
Copy Markdown
Contributor Author

Context: this PR fixes bug 1 of the three reported in #359 — all three share the same src == dst / plugin-unaware root cause. Bugs 2 (compat mode self-symlinks AGENTS.md/WORKFLOW.md, which kills adopt.sh) and 3 (adopt.sh re-registers hooks the plugin already provides) are unfixed and documented there with repros and suggested patches.

@tieubao tieubao closed this Aug 13, 2026
@tieubao
tieubao deleted the fix/install-toml-self-clobber branch August 13, 2026 17:15
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.

2 participants