Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions components/hoxay/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ log "building $name ($commit)"

# `mise install --locked` must see only the checkout's own mise.toml /
# mise.lock. On managed machines the user-global config
# (~/.config/mise/config.toml, symlinked to pc-setup's mise.global.toml)
# (~/.config/mise/config.toml, a copy of pc-setup's mise.global.toml)
# is also discovered as an ancestor config and pins machine-global tools
# that are absent from the repo lockfile, failing --locked installs
# (rebuildup/pc-setup#245). Point the global config at an empty file and
Expand All @@ -80,10 +80,12 @@ log "building $name ($commit)"
MISE_EMPTY_GLOBAL="$STATE_DIR/mise-empty-global.toml"
: > "$MISE_EMPTY_GLOBAL"
# mise compares the CANONICAL path of a discovered config file against the
# ignore list, so a config that is symlinked into the user config dir
# (e.g. ~/.config/mise/config.toml -> pc-setup's mise.global.toml) has to be
# ignored by its real path too — excluding the directory alone would not
# keep the global pins out of the build (rebuildup/pc-setup#245).
# ignore list. The global config is a copy of the repository's config, so the
# directory exclusion is enough for a managed machine; a config that is
# symlinked into the user config dir (a machine that still uses the old
# symlink layout) has to be ignored by its real path too — excluding the
# directory alone would not keep the global pins out of the build
# (rebuildup/pc-setup#245).
# ~/.tool-versions is read by mise's ancestor discovery as well, and the
# managed state dir lives under the home directory — a tool pinned there
# that the repo lockfile does not know would fail `--locked` too.
Expand Down
28 changes: 25 additions & 3 deletions docs/adr/ADR-0016.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,29 @@ converge は dotfiles の端末定義も毎 tick で各端末へ反映する。I
- 作業チェックアウト(`~/src/pc-setup` など)は配備に使わない。作業ブランチが何であっても、配備される内容は main に限定される。
- `~/.local/libexec/pc-setup` は実行ファイルや補助バイナリの置き場として使われてきた。チェックアウト(ソースツリー)を置くとバイナリ置き場と混ざるため使わない。
- `~/.local/state/pc-setup` は状態・ログ・ロックの置き場にする。ソースツリーは置かない。
- `apply-global-mise.sh` は自身の位置から repo root を決めるため、converge は必ず `$DEPLOY/scripts/apply-global-mise.sh` を呼ぶ。これで `~/.config/mise/config.toml` と `mise.lock` は配備用チェックアウトを指す。
- `apply-global-mise.sh` は自身の位置から repo root を決めるため、converge は必ず `$DEPLOY/scripts/apply-global-mise.sh` を呼ぶ。これで `~/.config/mise/config.toml` と `mise.lock` は配備用チェックアウトの**内容**と一致する。

### 2a. machine-global config は symlink ではなく copy

`~/.config/mise/config.toml` と `~/.config/mise/mise.lock` は、配備用チェックアウト内の tracked file への **symlink ではなく copy** として置く(Windows 版 `apply-global-mise.ps1` と同じ方式)。

symlink にしていた consequence は次のとおりで、いずれも「配備用チェックアウトが dirty になる → converge が dirty を拒否して恒久的に skip する → 端末が main を取り込み続ける」という連鎖发生过。

- `mise use -g <tool>` は global config に tool を書き込む。symlink だと実体 = 配備用チェックアウトの `mise.global.toml` が変更される(実測: `yq = "4.54.3"` の追加)。
- `MISE_LOCKED` を効かせない `mise install` は global lock を書き換える。symlink だと実体 = 配備用チェックアウトの `mise.global.lock` が変更される(実測: 約 700 行の差分)。
- 配備用チェックアウトが dirty になると §3 の規則で converge が skipped になる。

copy にすると、端末側の mise の書き込みは machine 自身の copy に留まり、配備用チェックアウトは dirty にならない。write は一時ファイルへ書いてから置換し、途中で中断されても truncated な config は残らない。copy の内容は常に配備用チェックアウトの main と一致し、次の tick で更新される。

同じ理由で、`~/.config/mise/.mise/locks/...` の sidecar も lock と同じ directory へ copy する(lock が参照する path が lock file からの相対 path であるため)。

### 2b. 移行

旧 symlink で運用されている machine では、次の順で copy 方式へ移行する。

1. `~/.config/mise/config.toml` と `mise.lock` の symlink 先が配備用チェックアウト(またはその中の `mise.global.toml` / `mise.global.lock`)である場合だけ移行対象とする。別の場所を指す symlink は user の設定なので触らない。
2. 配備用チェックアウトの dirty な tracked file は、差分を確認したうえで `git restore` する。ADR-0016 §3 の「dirty は拒否してスキップ」は人が判断する差分を自動で消さないため、この修復は明示的な操作として行う。
3. `apply-global-mise.sh` を実行すると、symlink が copy に置き換わる(symlink の中身が managed marker を持つ場合のみ)。以降の書き込みは copy に留まる。

### 3. 安全性

Expand All @@ -46,7 +68,7 @@ converge は dotfiles の端末定義も毎 tick で各端末へ反映する。I
- **冪等**:2 回目の実行は変更なしで exit 0 になる。
- **同時実行ガード**:`~/.local/state/pc-setup/converge.lock/` を mkdir で取る(macOS に flock はない)。pid を書き、pid が死んでいれば stale として退ける。退ける時は rename してから削除し、別プロセスが新しく取った lock を消さないようにする。
- **再実行の境界**:同期後に配備済みの `converge.sh` へ `exec` する。bash はスクリプトを逐次読むため、実行中に自分の本体が差し替わると壊れる。`exec` は pid を保つので、同じ lock を引き継げる。
- **巻き戻しをしない理由**:`apply-global-mise.sh` が config のリンクを張り替えた後で install が失敗しても、ロールバックはしない。mise の install は追加的で、旧バージョンはディスクに残る。巻き戻すと、この処理が取り除こうとしている dirty な作業チェックアウトへのリンクが戻ってしまう。欠けているツールは status に載るので、次の実行で再試行される。
- **巻き戻しをしない理由**:`apply-global-mise.sh` が config を書き換えた後で install が失敗しても、ロールバックはしない。mise の install は追加的で、旧バージョンはディスクに残る。欠けているツールは status に載るので、次の実行で再試行される。
- **終了コード**:0 は全ステップ成功、1 は失敗あり、3 は skipped または pending あり、75 は lock 競合。

### 4. スケジューラ:macOS LaunchAgent(StartInterval 3600、RunAtLoad)
Expand Down Expand Up @@ -124,7 +146,7 @@ dsh は未マージの PR #154 にしか宣言がない。配備先が main に
- macOS では、ターミナルを開かなくても定期的に main へ追従する。
- 失敗は status に残り、次の実行で再試行される。
- 旧バイナリを保持する古いプロセスは検出されるが、止めはしない。止めるかどうかは人が決める。
- `apply-global-mise.sh` の config 張り替えは `rm` の後に `ln -s` を行うため、ごく短い間は config が存在しない。converge の側ではこの窓を塞げない。別途改善が必要である。
- `apply-global-mise.sh` の config 書き込みは一時ファイルへ書いてから置換する(§2a)。対象は旧内容か新内容のどちらかで、truncated な config は残らない。
- WSL2 と Windows のスケジューラは実装した。ただし各 OS の実機での発火は未検証(後続の cross-OS 検証で確認する)。ADR の方針(採用は train、配備は converge、シェルは activate のみ)は両 OS に適用する。

## Re-evaluation triggers
Expand Down
107 changes: 86 additions & 21 deletions scripts/apply-global-mise.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,94 @@ fi

mkdir -p "$(dirname "$target_config")"

if [[ -L "$target_config" ]]; then
rm "$target_config"
elif [[ -e "$target_config" ]]; then
if ! head -n 1 "$target_config" | grep -Fxq "$managed_marker"; then
printf 'refusing to overwrite unmanaged mise global config: %s\n' "$target_config" >&2
exit 1
# The machine-global config is a *copy* of the repository's config, not a
# symlink into it.
#
# A symlink made the deployment checkout part of the machine's mutable mise
# state: `mise use -g <tool>` writes into the global config, and an install
# that runs without MISE_LOCKED rewrites the global lock. Through a symlink
# both writes landed in the deployment checkout's tracked files, which made the
# checkout dirty, and a dirty deployment checkout is refused by converge
# (ADR-0016 §3). The result was a machine that stopped adopting `main` and
# stayed on whatever state it had. Copying keeps the checkout pristine and
# gives the machine its own writable copy, which is what Windows already did.
#
# The rules for the target are the same as on Windows
# (scripts/mise-config-target.ps1): a config that holds content is only
# overwritten when this repository wrote it, a zero-length config is adopted,
# and the write replaces the target as a whole.
config_state() {
local path="$1" first_line
if [[ ! -e "$path" ]]; then
printf 'missing'
return 0
fi
if [[ ! -s "$path" ]]; then
printf 'empty'
return 0
fi
rm "$target_config"
first_line="$(head -n 1 "$path" 2>/dev/null || true)"
if [[ "$first_line" == "$managed_marker" ]]; then
printf 'managed'
return 0
fi
printf 'unmanaged'
}

config_state_value="$(config_state "$target_config")"
if [[ "$config_state_value" == "unmanaged" ]]; then
printf 'refusing to overwrite unmanaged mise global config: %s\n' "$target_config" >&2
exit 1
fi

if [[ -e "$target_config" ]] && ! cmp -s "$source_config" "$target_config"; then
config_backup="${target_config}.pc-setup-backup-$(date +%Y%m%d-%H%M%S)"
cp -p "$target_config" "$config_backup"
printf 'backup %s\n' "$config_backup"
fi

ln -s "$source_config" "$target_config"
printf 'linked %s -> %s\n' "$target_config" "$source_config"
# Replace the target as a whole: a truncated config would leave every mise shim
# unusable, and the managed marker guard above would refuse to repair it.
config_tmp="${target_config}.pc-setup-tmp.$$"
cp "$source_config" "$config_tmp"
mv -f "$config_tmp" "$target_config"
printf 'applied %s <- %s\n' "$target_config" "$source_config"

if [[ -f "$source_lock" ]]; then
if [[ -L "$target_lock" ]]; then
rm "$target_lock"
elif [[ -e "$target_lock" ]]; then
printf 'refusing to overwrite unmanaged mise global lock: %s\n' "$target_lock" >&2
exit 1
# The backup copies above are kept per content change, which made the config
# directory grow without bound. Keep the most recent ones and drop the rest.
config_backup_keep="${PC_SETUP_MISE_CONFIG_BACKUP_KEEP:-5}"
# macOS ships bash 3.2, which has no mapfile, so the list is read with a loop.
config_backup_index=0
while IFS= read -r stale_backup; do
[[ -n "$stale_backup" ]] || continue
config_backup_index=$((config_backup_index + 1))
if [[ "$config_backup_index" -gt "$config_backup_keep" ]]; then
rm -f "$stale_backup"
printf 'pruned %s\n' "$stale_backup"
fi
done < <(find "$(dirname "$target_config")" -maxdepth 1 -name "$(basename "$target_config").pc-setup-backup-*" -type f 2>/dev/null | sort -r)

ln -s "$source_lock" "$target_lock"
printf 'linked %s -> %s\n' "$target_lock" "$source_lock"
elif [[ -L "$target_lock" && "$(readlink "$target_lock")" == "$source_lock" ]]; then
if [[ -f "$source_lock" ]]; then
lock_tmp="${target_lock}.pc-setup-tmp.$$"
cp "$source_lock" "$lock_tmp"
mv -f "$lock_tmp" "$target_lock"
printf 'applied %s <- %s\n' "$target_lock" "$source_lock"

# The lock references its dependency sidecars by a path relative to the lock
# file (`aube = { path = ".mise/locks/mise.global/<tool>/<version>" }`), so
# the sidecar tree has to sit next to the lock. The lock is a copy now, so
# mise resolves those paths against the config directory.
source_sidecars="$repo_root/.mise"
target_sidecars="$(dirname "$target_lock")/.mise"
if [[ -d "$source_sidecars" ]]; then
sidecars_tmp="${target_sidecars}.pc-setup-tmp.$$"
rm -rf "$sidecars_tmp"
cp -R "$source_sidecars" "$sidecars_tmp"
rm -rf "$target_sidecars"
mv -f "$sidecars_tmp" "$target_sidecars"
printf 'applied %s <- %s\n' "$target_sidecars" "$source_sidecars"
fi
elif [[ -L "$target_lock" ]]; then
rm "$target_lock"
printf 'removed stale managed mise global lock link: %s\n' "$target_lock"
fi
Expand All @@ -62,9 +126,10 @@ export MISE_HTTP_RETRIES="${MISE_HTTP_RETRIES:-5}"
# it installs from, so the unlocked `mise install gh` bootstrap below replaces the
# committed lock before `--locked` is ever consulted (observed: 38393 -> 75443
# bytes) and the machine installs a different tool set than the repository pins.
# On POSIX the lock is a symlink into the checkout, so that rewrite also dirties
# the work tree. Lock reads for the rest of the run when a committed lock exists;
# a machine with no committed lock must still be able to create one.
# The lock the machine reads is a copy of the repository's lock, so that rewrite
# now lands in the copy and the repository's lock stays untouched. Lock reads for
# the rest of the run when a committed lock exists; a machine with no committed
# lock must still be able to create one.
if [[ -f "$source_lock" ]]; then
export MISE_LOCKED=1
fi
Expand Down
8 changes: 8 additions & 0 deletions scripts/ci.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ if stale:
raise SystemExit(f"ADR index references absent files: {', '.join(stale)}")
PY

printf 'Checking the machine-global mise apply rules...\n'
# scripts/apply-global-mise.sh has to write copies of the repository config and
# lock, never symlinks into the checkout. A symlink let `mise use -g` and an
# unlocked install modify the deployment checkout, and a dirty deployment
# checkout is refused by converge, so the machine stopped adopting `main`
# (rebuildup/pc-setup#282).
bash scripts/test-apply-global-mise.sh

printf 'Checking macOS container runtime contract...\n'
grep -Fq "bash \"\$repo_root/platforms/macos/containers/install.sh\"" scripts/apply-platform.sh
grep -Fq "mise -C \"\$HOME\" bootstrap packages apply" scripts/apply-global-mise.sh
Expand Down
32 changes: 19 additions & 13 deletions scripts/converge.sh
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,32 @@ apply_mise() {
return 0
fi

local previous_target="" rc=0 lock_before="" lock_after=""
previous_target="$(readlink "$mise_config_path" 2>/dev/null || true)"
local previous_config="" rc=0 lock_before="" lock_after=""
# The machine-global config is a copy of the repository's config, so the
# previous content has to be read before the apply replaces it. It is what
# the dsh-declaration check below compares against the deployed config.
if [[ -f "$mise_config_path" ]]; then
previous_config="$(cat "$mise_config_path" 2>/dev/null || true)"
fi
lock_before="$(git -C "$deploy_dir" hash-object mise.global.lock 2>/dev/null || true)"

# apply-global-mise.sh runs one unlocked `mise install gh` before its auth check.
# Without MISE_LOCKED=1, mise writes lock-time provenance into the linked
# mise.global.lock (observed: ~650 added lines in the deployment checkout).
# MISE_LOCKED=1 keeps that write from happening. The lock hash is compared
# afterwards as a safety net; the script reports a change and never restores it.
# Without MISE_LOCKED=1, mise writes lock-time provenance into the machine's
# lock (observed: ~650 added lines in the deployment checkout while the lock
# was a symlink into it). The lock is a copy now, so that rewrite lands in the
# copy; MISE_LOCKED=1 keeps it from happening at all. The lock hash is
# compared afterwards as a safety net; the script reports a change and never
# restores it.
#
# apply-global-mise.sh links the config into the deployment checkout and then
# apply-global-mise.sh copies the config from the deployment checkout and then
# runs `mise install --locked`. A failed install is not rolled back: mise
# installs are additive, the previous versions stay on disk, and the status
# report lists what is missing. Rolling the link back would reintroduce the
# dirty work checkout that this step exists to remove.
# report lists what is missing.
if MISE_LOCKED=1 "$deploy_dir/scripts/apply-global-mise.sh" >>"$log_file" 2>&1; then
if grep -Fq 'using committed mise global lock' "$log_file"; then
record mise-apply ok "config linked to deploy checkout; locked install ok"
record mise-apply ok "config applied from deploy checkout; locked install ok"
else
record mise-apply ok "config linked to deploy checkout; install ok without a committed lock"
record mise-apply ok "config applied from deploy checkout; install ok without a committed lock"
fi
else
rc=$?
Expand All @@ -242,8 +248,8 @@ apply_mise() {
fi

# Known caveat (ADR-0016): npm:@deepseek-ai/dsh exists only in unmerged PR #154.
if [[ -n "$previous_target" && -f "$previous_target" ]] \
&& grep -Fq 'deepseek-ai/dsh' "$previous_target" \
if [[ -n "$previous_config" ]] \
&& grep -Fq 'deepseek-ai/dsh' <<<"$previous_config" \
&& ! grep -Fq 'deepseek-ai/dsh' "$deploy_dir/mise.global.toml"; then
record dsh-declaration pending "npm:@deepseek-ai/dsh is declared only in unmerged PR #154; dsh is no longer managed by main"
fi
Expand Down
Loading
Loading