From 50535ac6f454f7d95f5496d919a9b6fccb7e0f54 Mon Sep 17 00:00:00 2001 From: okash1n <48118431+okash1n@users.noreply.github.com> Date: Wed, 7 Oct 2026 09:48:27 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20web-content-extraction=20=E3=81=AE?= =?UTF-8?q?=E4=BE=9D=E5=AD=98=20lock=20=E6=AE=8B=E7=95=99=E6=99=82?= =?UTF-8?q?=E3=81=AB=20update=20=E3=81=8C=E7=84=A1=E8=A8=80=E3=81=A7?= =?UTF-8?q?=E7=B5=82=E4=BA=86=E3=82=B3=E3=83=BC=E3=83=89=2075=20=E3=81=AB?= =?UTF-8?q?=E3=81=AA=E3=82=8B=E5=95=8F=E9=A1=8C=E3=82=92=E4=BF=AE=E6=AD=A3?= =?UTF-8?q?=EF=BC=88v0.79.1=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit install.sh / /update-kit / 自動アップデートが呼ぶ setup.sh --update は、 Step 2/5(CLAUDE.md)の後で web-content-extraction の依存 lock (~/.claude/skills/web-content-extraction/logs/.update.lock)を取得する。 依存 updater(update-deps.mjs)が lock を持ったまま強制終了されると lock が 残り、以後の update は何も表示せず終了コード 75 で止まっていた。しかも backup と settings.json / CLAUDE.md の書き換えは lock 取得より前に走るため、 失敗のたびに snapshot / manifest だけが古い中途半端な状態と 1 回分の バックアップが残っていた。dry-run は一時ディレクトリで模擬するため検出 できず、自動アップデートの失敗を利用者に知らせる経路もなかった。 - setup.sh: update ではバックアップの前、fresh では deploy の前に lock を preflight する。実行中の owner は最大 60 秒待ち、止まるときは kit の ファイルを変更しない - lib/update.sh: lock を取得・解放できないときにパス / owner / 作成時刻 / owner PID とその生存状態 / 状態別の復旧手順を表示する。owner が kit の 書く形式で、その PID が存在せず、owner ファイルが 60 分以上前の lock に 限り、reclaim mutex の下で再照合 → rename で隔離 → 再照合 → 削除する 手順で自動回収する(MSYS / MDM では回収しない)。取得側は owner を noclobber で書き、mutex を見つけたら最大 3 秒待って token 照合付きで 取り下げる。解放も同じ mutex で直列化し、lock token の PID は保持 サブシェルの BASHPID を記録する - lib/update.sh: 失敗で中途半端になった環境の回復時、CLAUDE.md の kit セクションが新 kit と同一なら利用者編集扱いにせず snapshot を更新する - update-deps.mjs: 最新版チェックの間は lock を持たず、更新を適用する間 だけ取得して installed version を読み直す。中断マーカーがあれば 24 時間 の throttle を迂回して復旧する。取得後に canonical owner を再照合する - lib/dryrun.sh / setup.sh: dry-run は実 ~/.claude の lock を読み取りだけで 検査し、自動回収されるか止まるかを警告する - i18n: STR_WCE_LOCK_* を en / ja に追加 - 文書: commands/update-kit.md / update-kit-dry-run.md に終了コード 75 / 74 の意味と復旧手順、skill README に lock の設計、CLAUDE.md に preflight と 回収の規約を追記 - テスト: tests/unit/test-wce-update-lock.sh に 44 件、test-update-refactor.sh に 2 件、skill の node テストに 15 件、シナリオ 3 件を追加 v0.73.0 で廃止した「経過時間だけを根拠にした stale lock の自動奪取」は 復活させていない。回収は owner プロセスの不在を確認できる場合に限る。 自動アップデート失敗の SessionStart 通知は含まない。 レビュー 8 ラウンド(正しさ / セキュリティ・競合 / テスト・文書 / Codex) で計 25 件の指摘を修正し、最終ラウンドは指摘なし。 --- CHANGELOG.md | 17 + CLAUDE.md | 2 + commands/update-kit-dry-run.md | 1 + commands/update-kit.md | 25 + i18n/en/strings.sh | 32 + i18n/ja/strings.sh | 32 + lib/deploy.sh | 19 +- lib/update.sh | 695 +++++++++- setup.sh | 34 +- skills/web-content-extraction/README.md | 45 +- .../scripts/update-deps.mjs | 210 ++- .../test/update-deps-lock.test.mjs | 209 +++ .../test/update-deps-run.test.mjs | 316 +++++ tests/run-scenarios.sh | 116 ++ tests/unit/test-update-refactor.sh | 93 ++ tests/unit/test-wce-update-lock.sh | 1140 ++++++++++++++++- 16 files changed, 2911 insertions(+), 75 deletions(-) create mode 100644 skills/web-content-extraction/test/update-deps-run.test.mjs diff --git a/CHANGELOG.md b/CHANGELOG.md index be25385..3e446bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/), and this project adheres to [Semantic Versioning](https://semver.org/). +## [0.79.1] - 2026-10-07 + +### Fixed +- **`/update-kit` と `setup.sh --update` が Step 2/5(CLAUDE.md)の後に何も表示せず終了コード 75 で止まる問題を修正**: `web-content-extraction` skill の依存 lock(`~/.claude/skills/web-content-extraction/logs/.update.lock`)が残っていると、kit の更新は lock を取得できずに終了コード 75 で終了していた。v0.73.0 で「既存の lock は時刻にかかわらず奪取しない(fail-closed)」に変えた際に、取得できなかったときの表示と回復手段を用意していなかったため、(1) 終了した理由が何も表示されない、(2) lock を確認するのが Step 2 の後なので、`~/.claude` 全体のバックアップ(`~/.claude.backup.`)の作成と `settings.json` / `CLAUDE.md` の書き換えが先に実行され、残りの kit ファイル・snapshot・manifest は旧いまま残る、(3) lock を手で消すまで、実行するたびに同じ箇所で止まってバックアップが 1 つ増える、という状態になっていた。自動アップデート(`auto-update` フック)も同じ `setup.sh --update --non-interactive` を実行するため、同じ箇所で失敗する。対象は skills を導入している非 MDM 環境(Standard / Full など)。lock が残るのは、lock を保持した処理が解放前に強制終了された場合である。`web-content-update` フック(Full のみ既定有効)の依存 updater は、最新版チェック(`npm view`)の間も lock を保持していたため、チェック中に強制終了(`SIGKILL`)されるだけで lock が残った + - **書き込み前に lock を確認**: `setup.sh` は、update ではバックアップの前、fresh / 再セットアップではデプロイを始める前に lock を確認する。ここで止まる場合は kit のファイルを変更せず、バックアップも作らない + - **止まった理由を表示**: lock を取得または解放できずに止まるときは、lock のパス、owner、作成時刻、owner の PID とそのプロセスが存在するか、状態に応じた復旧コマンドを表示する(日本語 / 英語)。書き込み前の確認を通過した後、Step 2 の後で別の処理に lock を取られた場合は、`settings.json` / `CLAUDE.md` が処理済みであることも表示する。kit ファイルの更新が終わった後の `npm ci` で lock を取れなかった場合は、kit ファイルは更新済みで、依存のインストールと、その後の manifest の書き込み・設定の保存・plugin のセットアップが実行されていないことを表示し、表示どおりの終了コード(75 / 74)で終了する(従来はここだけ終了コード 1 だった)。解放に失敗した場合(74)も、manifest の書き込み・設定の保存・plugin のセットアップの前で止まっていることを表示する。どの場合も、lock を解消してから同じコマンドを再実行すれば残りが適用される。skill ディレクトリ・`logs`・lock のパスが symlink やディレクトリ以外の場合は、lock が保持されているとは扱わず、そのパスを表示して `rm` / `rmdir` の案内はしない(symlink 経由で削除させないため) + - **owner のいない lock を自動回収**: owner が kit の書く形式(依存 updater の `:`、setup の `starter-kit-update--…`)で、その PID のプロセスが存在せず、owner ファイルが 60 分以上前のものである場合に限り、書き込み前の確認の中で lock を取り除いてから処理を続ける。回収は専用の mutex(`logs/.update.lock.reclaim`)の下で owner を照合し直し、rename で隔離したものをもう一度照合してから削除する。隔離したものが照合した owner と違えば元の場所に戻して中止する。戻すときは `mkdir` で元の名前を確保してから中身を移し、確保できなければ(その間に別の処理が lock を取った場合)隔離先をそのまま残してパスを表示する。lock を取得する処理(setup と `update-deps.mjs`)は、owner を書いた後にこの mutex があれば、正規パスの owner が自分の token の場合に限り lock を取り下げて(回収で隔離された後に別の処理が取得した lock は消さない)、取得失敗として扱う。取り下げは、回収が終わって mutex が無くなるまで最大 3 秒待ってから行う(回収の途中で owner の照合と rename の間に lock が差し替わり、別の取得者の lock を隔離することを防ぐ)。3 秒待っても mutex が残っている場合は、回収処理が終了したとは判断できないため取り下げず、自分の lock をそのまま残して取得失敗とする(残った lock は owner のプロセスが終了した後、通常の残った lock として表示・回収の対象になる)。また、`mkdir` の後の owner の書き込みは既存の owner ファイルを上書きしない(setup は noclobber、`update-deps.mjs` は `wx`)。その間に回収で隔離され、別の取得者が正規パスを取っていた場合に、その取得者の owner を書き換えて二重に取得することを防ぐ。mutex が無かった場合も、lock の正規パスの owner が自分の token のままかを確認し直し、違えば取得失敗として扱う(回収処理全体が owner の書き込みと mutex の確認の間に完了し、隔離した lock を戻せなかった場合に備える)。これにより、回収中に別の取得が成立して二重に書き込める状態にはならない。mutex だけが残っている(回収処理が途中で強制終了された)場合は、lock が無くても取得できないため、書き込み前の確認でその旨を表示して止まる。lock を取得する処理そのものは、従来どおり既存の lock を奪取しない。それ以外の形式の owner、プロセスの存否を判定できない PID、owner 以外のエントリを含む lock、symlink や通常ファイルは回収しない。Git Bash(MSYS)と MDM 管理下でも回収しない + - **実行中の owner は待つ**: owner のプロセスが実行中なら最大 60 秒待ち、その間に解放されなければ上の表示をして止まる + - **依存 updater が lock を持つ範囲を縮小**: `update-deps.mjs` は最新版チェックの間は lock を持たず、更新を適用する間(`npm install` / `npm test` / ロールバック)だけ取得する。取得後にインストール済みバージョンを読み直し、他の実行が適用済みなら何もしない。チェック中に強制終了されても lock は残らない。中断された更新(`logs/.update-in-progress`)の再テストは従来どおり lock を取得して行う。チェックの間に別の実行の更新が中断されて `logs/.update-in-progress` が残った場合は、更新候補が無くても lock を取得して再テスト(失敗ならロールバック)してから終了し、lock を取れなければ stamp を書かずに終了する。インストール済みの版が最新に見えるだけで 24 時間の stamp を書くと、未テストの依存が次のチェックまでそのまま残るため。また、`logs/.update-in-progress` があれば 24 時間の stamp の期間中でも起動時に lock を取得して再テストする(最新版チェックは stamp どおり行わない)。チェックの直後に別の実行がこのマーカーを残して強制終了され、24 時間の stamp が書かれた場合でも、次の起動で復旧される + - **dry-run で警告**: `--dry-run`(`/update-kit-dry-run`)は一時ディレクトリで模擬するため、lock が残っていても検出できなかった。実際の `~/.claude` の lock を読み取りだけで確認し、保持されていれば、実際の実行で自動回収されるのか止まるのかを警告する(終了コードは 0 のまま) + - **v0.73.0 からの変更点**: v0.73.0 は、経過時間だけを根拠にした自動奪取を廃止し、`SIGKILL` / 電源断で残った lock は「実行中の owner がいないことを確認して明示的に削除する」としていた。今回は、その確認(owner の PID のプロセスが存在しないこと)を kit が自分でできる場合に限って自動化した。経過時間だけでは回収しない点は変わらない。残る制約として、PID は同じ PID 名前空間の中でしか照合できないため、コンテナとホストで `~/.claude` を共有している環境では、別の名前空間で 60 分を超えて実行中の owner の lock を回収し得る。setup が書く owner の PID は、親シェルではなく lock を保持して書き込みを実行するサブシェルのものにした(親だけが強制終了されても、書き込み中の lock は回収されない)。ただし、lock を保持するプロセスだけが強制終了され、その子プロセス(`npm ci` など)が 60 分を超えて動き続けている場合は、回収し得る。このとき `package.json` / `package-lock.json` / `node_modules` への書き込みが重なり得る(`npm ci` で復旧できる) + - **既存インストールへの影響**: lock が残って止まっていた環境は、この版の `setup.sh --update`(`/update-kit`、自動アップデート、`install.sh` の再実行)が実行された時点で、上の条件を満たしていれば lock が回収されて更新が完了する。満たしていなければ理由と復旧コマンドが表示される。失敗のたびに作られた `~/.claude.backup.` は自動では削除されない。`~/.claude/.starter-kit-last-backup` が指すもの以外で不要なものは、中身を確認して手動で削除する。また、止まっていた間の失敗した実行が `CLAUDE.md` の kit セクションをすでに新しい内容へ書き換えていた場合、snapshot は旧いままになる。回復後の update は、現在の kit セクションが新しい kit の kit セクションと同一なら利用者の編集とは扱わず、内容を変えずに snapshot だけを更新する(「CLAUDE.md kit section already matches this version; snapshot refreshed」と表示)。kit セクションが本当に編集されている場合は従来どおり「CLAUDE.md kit section has updates, but your local edits were kept」と表示して現在の内容を保持する。`settings.json` は 3-way merge で従来どおり回復する。この条件は `tests/unit/test-update-refactor.sh` に 2 件のアサーションで拘束した(修正前はどちらも失敗する) + - **手動で復旧する場合**: 依存更新(`update-deps.mjs`)・kit の setup・`npm ci` が動いていないことを確認してから `rm -f ~/.claude/skills/web-content-extraction/logs/.update.lock/owner && rmdir ~/.claude/skills/web-content-extraction/logs/.update.lock` を実行し、更新を再実行する。回収の途中で強制終了されて `logs/.update.lock.reclaim` が残っていると自動回収は行われないため、同じ確認をしてから `rmdir` で取り除く + - **今回含まないもの**: 自動アップデートが失敗したことを次のセッションで通知する仕組みは、この変更に含まない + - **回帰テスト**: `tests/unit/test-wce-update-lock.sh` にアサーションを 42 件追加した(lock の分類、回収する条件としない条件と 60 分の境界、回収中の差し替えと戻せない場合、mutex がある間の取得見送り、owner の書き込みと mutex の確認の間に隔離された場合の取得失敗、取り下げ時に別の取得者の lock を消さないこと、取り下げが回収の終了を待ってから rename すること、待っても mutex が残る場合は rename せずに取得失敗とすること、owner の書き込みが別の取得者の owner を上書きしないこと、owner の PID が lock を保持するプロセスのものであること、同時回収の直列化、実行中 owner の待機、止まった理由の表示と終了後の案内、`npm ci` 段階の終了コード、update / fresh の書き込み前確認、dry-run の警告)。`skills/web-content-extraction/test/update-deps-run.test.mjs` を新設し(11 件)、偽の npm で updater を実行して、npm が呼ばれた時点で lock があったか、stamp の期間中に中断マーカーが再テストされるかを記録する。`test/update-deps-lock.test.mjs` に mutex がある間の取得見送りと、owner の書き込みと mutex の確認の間に隔離された場合の取得失敗、取り下げ時に別の取得者の lock を消さないこと、取り下げが回収の終了を待ってから rename すること、待っても mutex が残る場合は rename せずに取得失敗とすることの 5 件を追加した。`tests/run-scenarios.sh` に 3 シナリオを追加した。修正前のコードでは、unit の 42 件中 38 件、node の 16 件中 14 件、シナリオ 3 件すべてが失敗する(シナリオは終了コード 75、`~/.claude` の書き換え、バックアップの増加を検出する) + ## [0.79.0] - 2026-10-07 Claude Code 2.1.89 未満向けの旧 hook 形状(legacy hook 経路)を退役し、サポート最小 Claude Code を 2.1.89 と宣言する(#136)。 @@ -50,6 +66,7 @@ Claude Code 2.1.89 未満向けの旧 hook 形状(legacy hook 経路)を退 - **既存インストールへの取り込み範囲**: fresh install と、`web-content-update` フックが依存を一度も更新していない(deploy 時の lockfile が snapshot と一致する)インストールは、`setup.sh --update` / 自動アップデートで新しい lockfile に置き換わり、Node.js と npm がある環境では続けて実行される `npm ci` で `node_modules` も入れ直される。フックが一度でも依存を更新したインストールでは lockfile の `.packages` が runtime 所有として保持される(`lib/update.sh` の package pair merge)ため、キットの更新では変わらない。その場合は `cd ~/.claude/skills/web-content-extraction && npm update undici source-map-js` で手動更新できる(いずれも既存の範囲内なので `package.json` は変わらず、以後のアップデートでも保持される)。結果は `npm ls undici source-map-js` と `npm audit` で確認できる。フックが有効な環境でも `source-map-js` はフックの更新では 1.2.1 のまま残るため、同じ手動更新が必要になる(フックと同じ `npm install` を最新版の defuddle 0.19.4 / jsdom 30.1.2 / pdfjs-dist 6.4.299 / undici 8.11.2 で再現して確認)。MDM 管理下は runtime bundle の再配布で更新されるため、利用者側での手動更新は不要 - **MDM runtime bundle の期待 SHA256 ピンを更新**: `package-lock.json` の更新に伴い、`lib/deploy.sh`・`mdm/detect-mdm.sh`・`mdm/install-mdm.sh`・`docs/mdm/README.md`・関連テストの期待 lock ハッシュを v0.78.1 の値から `09fe234e…` へ更新(`package.json` は不変のため package ハッシュ `711c13b0…` は変わらない)。**MDM 配布環境は新しいバンドルパス(`711c13b0…-09fe234e…`)で root-owned runtime bundle を再ビルド・再配布する必要がある**(旧バンドルは検証で拒否される。v0.75.2 / v0.78.1 と同じ経路) + ## [0.78.3] - 2026-10-07 ### Fixed diff --git a/CLAUDE.md b/CLAUDE.md index 1175868..2ed9ff7 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -171,6 +171,8 @@ If `install.sh` sees an existing manifest, it always adds `setup.sh --update`. W Rules to remember: verify fresh install, `setup.sh --update`/`/update-kit`, and saved config reuse for any new `ENABLE_*`; do not overwrite explicit user choices. Legacy `~/.claude/AGENTS.md` is removed during update/uninstall because the kit no longer deploys it. +Web-content-extraction dependency lock (`skills/web-content-extraction/logs/.update.lock`, shared with `update-deps.mjs`; never taken under MDM): `setup_deploy` runs `_wce_runtime_update_lock_preflight` before any write — before `backup_existing` on update, before deployment state on fresh. It waits up to 60s for a running owner, removes a lock only when the owner is a kit-written token whose PID no longer exists and whose owner file is over 60 minutes old (never on MSYS), and otherwise exits 75 with a diagnosis and no file changed. `_wce_runtime_update_lock_acquire` itself never takes over an existing lock — keep reclaim confined to the preflight. Both acquirers (bash and `update-deps.mjs`) withdraw their own lock when `.reclaim` exists after their owner write, which is what makes the reclaimer's re-read under the mutex sufficient; a quarantined lock is put back by claiming the canonical name with `mkdir`, never `mv dir`. A symlink or non-directory on the path is state `unusable` (no lock held, no `rm`/`rmdir` offered). `update-deps.mjs` holds the lock only while applying updates, not during its `npm view` check. + `CLAUDE.md` is marker-managed: only the kit section between `BEGIN/END STARTER-KIT-MANAGED` is snapshotted/updated, while the user section is preserved. `--dry-run` redirects `CLAUDE_DIR` to a temp simulation dir, disables merge prompts, logs external side effects as `[WOULD RUN]`, and compares real `~/.claude` with the simulated result. ### Deploy Targets diff --git a/commands/update-kit-dry-run.md b/commands/update-kit-dry-run.md index bcc57d5..7e3306a 100644 --- a/commands/update-kit-dry-run.md +++ b/commands/update-kit-dry-run.md @@ -165,6 +165,7 @@ fi - **Files to skip**: User-owned files that would be preserved - **External operations**: Actions like plugin installs, shown as `[WOULD RUN]` - **settings.json diff**: Unified diff of what would change in settings + - **Dependency lock warning** (only when present): if the real `~/.claude/skills/web-content-extraction/logs/.update.lock` is held, the preview ends with a warning that names the lock, its owner and PID state, and says whether `/update-kit` would recover it automatically or stop with exit code 75. The preview only reads the lock and still exits 0 3. After showing the summary, let the user know: - If they want to proceed: run `/update-kit` - If they want to cancel: no starter-kit deployment was applied; the selected diff --git a/commands/update-kit.md b/commands/update-kit.md index 0ccbc40..3ecc0ba 100644 --- a/commands/update-kit.md +++ b/commands/update-kit.md @@ -183,6 +183,8 @@ Before running the update: - If successful: show the previous and new version printed by the update block - If the kit is already up to date: report "Already on the latest version" - If it fails: show the error and suggest manual steps + - If it exits with code 75: the web-content-extraction dependency lock (`~/.claude/skills/web-content-extraction/logs/.update.lock`) could not be acquired. `setup.sh` prints the lock path, its owner, its creation time, the owner PID and whether that PID is running, and a recovery command. Relay those lines unchanged, then follow **Recovery**. The closing note says how far the run got (the wording follows the configured language): no starter-kit file was changed (stopped before the backup and before Step 1); settings.json and CLAUDE.md were processed but the remaining kit files were not (stopped after Step 2); or the kit files were updated but the dependency install (`npm ci`) did not run. Whatever the note says, the run stopped before its final steps (writing the install manifest, saving the config, plugin setup), so after resolving the lock rerun the same update block; do not substitute a manual `npm ci` + - If it exits with code 74: the lock could not be released cleanly. The kit files written before that point are in place, but the run stopped before its remaining steps (writing the install manifest, saving the config, plugin setup, and the dependency install if it had not run yet). Relay the printed lines, follow **Recovery** for the lock, then run the same update block again 4. After a successful update, tell the user how to reload the new configuration: - Always suggest `/compact` to refresh the current session cleanly. - Also mention that some changes may require starting a new Claude Code session or opening a new terminal, especially settings/env changes, hook updates, MCP-related changes, or newly added slash commands. @@ -401,6 +403,29 @@ If an update goes wrong: its final setup command changed to `(cd "$kit_repo_physical" && bash setup.sh --update --reset-prefs)` +If the update stops with exit code 75 (web-content-extraction dependency lock): +- `setup.sh --update` checks the lock before the backup. It waits up to 60 + seconds for a running owner, and removes the lock by itself only when the + owner is a token the kit writes, that PID no longer exists, and the lock is + at least 60 minutes old. Every other case stops with exit 75 +- To recover by hand: confirm that the PID `setup.sh` printed does not exist + and that no `update-deps.mjs`, `setup.sh`, or `npm ci` process is running, + get the user's approval, run the printed + `rm -f /owner && rmdir ` command, then rerun the update block +- Do not remove the lock while the printed PID is running one of those + processes. If the PID is running but is an unrelated process (a PID can be + reused), tell the user so and let them decide +- If `setup.sh` also names `.reclaim`, an earlier recovery was + interrupted; remove it with the printed `rmdir` command after the same check. + While it exists, no lock can be acquired even when `` itself is absent +- If `setup.sh` reports that the lock path is a symlink or not a directory, no + lock is held and no `rm`/`rmdir` command is printed. Show the user the path + it names (the skill directory, `logs`, or ``) and let them decide what + to replace or remove; do not delete anything through that path +- If `setup.sh` names a `.stale-*` directory it left for inspection, a + lock taken by another writer during recovery could not be put back. Show it + to the user; remove it only after the same check as for the lock + ### Notes - This updates the checkout recorded as `KIT_REPO` in the manifest-bound active diff --git a/i18n/en/strings.sh b/i18n/en/strings.sh index 74ed574..d69c55c 100644 --- a/i18n/en/strings.sh +++ b/i18n/en/strings.sh @@ -104,6 +104,37 @@ STR_WCE_NPM_INSTALLING="Installing web-content-extraction skill dependencies..." STR_WCE_NPM_DONE="web-content-extraction dependencies installed" STR_WCE_NPM_FAILED="npm ci failed for web-content-extraction; skill scripts may not run until dependencies are installed." +# web-content-extraction dependency lock (logs/.update.lock) diagnosis and recovery +STR_WCE_LOCK_BUSY="Another web-content-extraction dependency update or kit setup is still running, so this run stopped (exit 75)." +STR_WCE_LOCK_STALE="A web-content-extraction dependency lock was left behind by a process that is no longer running, so this run stopped (exit 75)." +STR_WCE_LOCK_UNKNOWN="The web-content-extraction dependency lock is held in a form the kit cannot verify, so it was left untouched and this run stopped (exit 75)." +STR_WCE_LOCK_UNAVAILABLE="The web-content-extraction dependency lock could not be acquired, so this run stopped (exit 75). Nothing holds it right now: run the same command again, and if this repeats, check that its parent is a writable directory." +STR_WCE_LOCK_UNUSABLE="The web-content-extraction dependency lock path cannot be used because it is a symlink or not a directory, so this run stopped (exit 75). No lock is held." +STR_WCE_LOCK_MUTEX_BLOCKED="The web-content-extraction dependency lock could not be acquired because a marker from an interrupted recovery exists, so this run stopped (exit 75)." +STR_WCE_LOCK_RELEASE_FAILED="The web-content-extraction dependency lock could not be released cleanly (exit 74)." +STR_WCE_LOCK_LABEL_PATH="Lock" +STR_WCE_LOCK_LABEL_UNUSABLE="Not a directory" +STR_WCE_LOCK_LABEL_OWNER="Owner" +STR_WCE_LOCK_LABEL_CREATED="Created" +STR_WCE_LOCK_LABEL_PID="Owner PID" +STR_WCE_LOCK_PID_ALIVE="running" +STR_WCE_LOCK_PID_DEAD="not running" +STR_WCE_LOCK_PID_UNKNOWN="cannot be determined" +STR_WCE_LOCK_HINT_BUSY="Wait for it to finish, then run the same command again. Only if that PID is not update-deps.mjs, setup.sh, or npm (a PID can be reused), remove the lock:" +STR_WCE_LOCK_HINT_STALE="setup.sh recovers it automatically once it is 60 minutes old. To recover now, confirm that PID is not running, then run:" +STR_WCE_LOCK_HINT_UNKNOWN="Confirm that no update-deps.mjs, setup.sh, or npm ci process is running, then remove the lock and run the same command again:" +STR_WCE_LOCK_HINT_UNUSABLE="Check what that path is and replace it with a regular directory (or remove it), then run the same command again. Do not remove anything through it." +STR_WCE_LOCK_HINT_MUTEX="An interrupted recovery left a marker that disables automatic recovery. After the same check, remove it too:" +STR_WCE_LOCK_QUARANTINE_LEFT="A lock that was moved aside during recovery could not be put back and was left for inspection:" +STR_WCE_LOCK_WAITING="Waiting for a running web-content-extraction dependency update or kit setup to finish" +STR_WCE_LOCK_RECOVERED="Recovered a web-content-extraction dependency lock left behind by a process that is no longer running." +STR_WCE_LOCK_UNTOUCHED_NOTE="No starter-kit file was changed by this run." +STR_WCE_LOCK_PARTIAL_NOTE="settings.json and CLAUDE.md were already processed by this run; the remaining kit files were not. Resolve the lock and run the same command again to apply the rest." +STR_WCE_LOCK_DEPS_NOTE="The starter-kit files were already updated by this run, but the web-content-extraction dependency install (npm ci) and the remaining steps (the install manifest, saving the settings, and plugin setup) did not run. Resolve the lock and run the same command again." +STR_WCE_LOCK_RELEASE_NOTE="This run stopped before its remaining steps, including the install manifest, saving the settings, and plugin setup. Resolve the lock and run the same command again." +STR_WCE_LOCK_DRYRUN_BLOCKED="The web-content-extraction dependency lock is held. The real run waits up to 60 seconds for a running owner and otherwise stops (exit 75) before changing any file." +STR_WCE_LOCK_DRYRUN_RECOVERABLE="A web-content-extraction dependency lock was left behind by a process that is no longer running. The real run recovers it automatically before updating." + # Step 6: Plugins STR_PLUGINS_TITLE="Select plugins to install (enter numbers separated by spaces):" STR_PLUGINS_NOTE="Note: Plugins are installed inside a Claude Code session after setup." @@ -352,6 +383,7 @@ STR_CLAUDEMD_MIGRATION_PROMPT="[M]erge (add kit section + keep yours) / [S]kip ? STR_CLAUDEMD_MIGRATION_SKIP="CLAUDE.md migration skipped (non-interactive). Run interactively to merge." STR_CLAUDEMD_KIT_UPDATED="CLAUDE.md kit section updated" STR_CLAUDEMD_KIT_UNCHANGED="CLAUDE.md kit section unchanged" +STR_CLAUDEMD_KIT_BASELINE_REFRESHED="CLAUDE.md kit section already matches this version; snapshot refreshed" STR_CLAUDEMD_KIT_CONFLICT="CLAUDE.md kit section: both you and kit have changes" STR_CLAUDEMD_KIT_CONFLICT_PROMPT="[U]se new kit / [K]eep yours / [D]iff ?" STR_CLAUDEMD_KIT_CONFLICT_KEPT="CLAUDE.md kit section has updates, but your local edits were kept (re-run interactively to choose whether to adopt them)" diff --git a/i18n/ja/strings.sh b/i18n/ja/strings.sh index 7265ab7..02fd977 100644 --- a/i18n/ja/strings.sh +++ b/i18n/ja/strings.sh @@ -102,6 +102,37 @@ STR_WCE_NPM_INSTALLING="web-content-extraction スキルの依存をインスト STR_WCE_NPM_DONE="web-content-extraction の依存をインストールしました" STR_WCE_NPM_FAILED="web-content-extraction の npm ci に失敗しました(依存導入までスクリプトは動作しません)" +# web-content-extraction dependency lock (logs/.update.lock) diagnosis and recovery +STR_WCE_LOCK_BUSY="web-content-extraction の依存更新または kit の setup が実行中のため、この実行を中止しました(終了コード 75)。" +STR_WCE_LOCK_STALE="すでに終了したプロセスが web-content-extraction の依存 lock を残しているため、この実行を中止しました(終了コード 75)。" +STR_WCE_LOCK_UNKNOWN="web-content-extraction の依存 lock が kit で確認できない形式で保持されているため、lock には触れずにこの実行を中止しました(終了コード 75)。" +STR_WCE_LOCK_UNAVAILABLE="web-content-extraction の依存 lock を取得できなかったため、この実行を中止しました(終了コード 75)。現在は誰も保持していません。同じコマンドを再実行し、繰り返す場合は親ディレクトリが書き込み可能なディレクトリか確認してください。" +STR_WCE_LOCK_UNUSABLE="web-content-extraction の依存 lock のパスが symlink またはディレクトリ以外のため使用できず、この実行を中止しました(終了コード 75)。lock は保持されていません。" +STR_WCE_LOCK_MUTEX_BLOCKED="中断された回収処理の目印が残っているため web-content-extraction の依存 lock を取得できず、この実行を中止しました(終了コード 75)。" +STR_WCE_LOCK_RELEASE_FAILED="web-content-extraction の依存 lock を正常に解放できませんでした(終了コード 74)。" +STR_WCE_LOCK_LABEL_PATH="lock" +STR_WCE_LOCK_LABEL_UNUSABLE="ディレクトリでないパス" +STR_WCE_LOCK_LABEL_OWNER="owner" +STR_WCE_LOCK_LABEL_CREATED="作成時刻" +STR_WCE_LOCK_LABEL_PID="owner の PID" +STR_WCE_LOCK_PID_ALIVE="実行中" +STR_WCE_LOCK_PID_DEAD="存在しない" +STR_WCE_LOCK_PID_UNKNOWN="判定できない" +STR_WCE_LOCK_HINT_BUSY="終了を待ってから同じコマンドを再実行してください。その PID が update-deps.mjs / setup.sh / npm のいずれでもない場合(PID は再利用されることがあります)に限り、lock を削除してください:" +STR_WCE_LOCK_HINT_STALE="作成から 60 分が経過すると setup.sh が自動で回収します。今すぐ回復する場合は、その PID が存在しないことを確認してから次を実行してください:" +STR_WCE_LOCK_HINT_UNKNOWN="update-deps.mjs / setup.sh / npm ci が動いていないことを確認してから lock を削除し、同じコマンドを再実行してください:" +STR_WCE_LOCK_HINT_UNUSABLE="そのパスが何かを確認し、通常のディレクトリに置き換える(または取り除く)うえで、同じコマンドを再実行してください。そのパスを経由して何かを削除しないでください。" +STR_WCE_LOCK_HINT_MUTEX="中断された回収処理の目印が残っており、自動回収が無効になっています。同じ確認をしたうえで、これも削除してください:" +STR_WCE_LOCK_QUARANTINE_LEFT="回収中に退避した lock を元の場所に戻せなかったため、確認用に残しました:" +STR_WCE_LOCK_WAITING="実行中の web-content-extraction 依存更新または kit の setup の終了を待っています" +STR_WCE_LOCK_RECOVERED="すでに終了したプロセスが残した web-content-extraction の依存 lock を回収しました。" +STR_WCE_LOCK_UNTOUCHED_NOTE="この実行では starter kit のファイルを変更していません。" +STR_WCE_LOCK_PARTIAL_NOTE="settings.json と CLAUDE.md はこの実行で処理済みですが、残りの kit ファイルは未処理です。lock を解消してから同じコマンドを再実行すると残りが適用されます。" +STR_WCE_LOCK_DEPS_NOTE="starter kit のファイルはこの実行で更新済みですが、web-content-extraction の依存インストール(npm ci)と、その後の処理(インストール manifest の書き込み、設定の保存、plugin のセットアップ)は実行されていません。lock を解消してから同じコマンドを再実行してください。" +STR_WCE_LOCK_RELEASE_NOTE="この実行は、インストール manifest の書き込み、設定の保存、plugin のセットアップを含む残りの処理の前で止まりました。lock を解消してから同じコマンドを再実行してください。" +STR_WCE_LOCK_DRYRUN_BLOCKED="web-content-extraction の依存 lock が保持されています。実際の実行では、owner が実行中なら最大 60 秒待ち、それ以外はファイルを変更する前に中止します(終了コード 75)。" +STR_WCE_LOCK_DRYRUN_RECOVERABLE="すでに終了したプロセスが web-content-extraction の依存 lock を残しています。実際の実行では、更新の前に自動で回収します。" + # Step 6: Plugins STR_PLUGINS_TITLE="インストールするプラグインを選択してください(番号をスペース区切りで入力):" STR_PLUGINS_NOTE="注意: プラグインはセットアップ後に Claude Code セッション内でインストールします。" @@ -350,6 +381,7 @@ STR_CLAUDEMD_MIGRATION_PROMPT="[M] マージ(kit セクション追加 + 既 STR_CLAUDEMD_MIGRATION_SKIP="CLAUDE.md のマイグレーションをスキップしました(非対話モード)。対話モードで再実行してください。" STR_CLAUDEMD_KIT_UPDATED="CLAUDE.md の kit セクションを更新しました" STR_CLAUDEMD_KIT_UNCHANGED="CLAUDE.md の kit セクションに変更なし" +STR_CLAUDEMD_KIT_BASELINE_REFRESHED="CLAUDE.md の kit セクションはすでにこの版と一致しています(snapshot を更新)" STR_CLAUDEMD_KIT_CONFLICT="CLAUDE.md の kit セクション: ユーザーと kit の両方に変更があります" STR_CLAUDEMD_KIT_CONFLICT_PROMPT="[U] 新しい kit を使用 / [K] 現在のまま / [D] 差分表示 ?" STR_CLAUDEMD_KIT_CONFLICT_KEPT="CLAUDE.md の kit セクションに更新がありますが、ユーザー編集があるため現状を保持しました(対話モードで再実行すると取り込みを選択できます)" diff --git a/lib/deploy.sh b/lib/deploy.sh index ef6644c..57e90f3 100644 --- a/lib/deploy.sh +++ b/lib/deploy.sh @@ -1820,19 +1820,24 @@ maybe_install_web_content_deps() { return 1 fi local npm_rc=0 + # In update mode this is a second acquisition after run_update released the + # lock, so a writer can take it in between. Every kit file is already in + # place by then, but setup stops on a 74/75 before its final steps + # (manifest, saved config, plugins); the helper's diagnosis appends the + # matching note. + local _WCE_RUNTIME_LOCK_FAILURE_NOTE="${STR_WCE_LOCK_DEPS_NOTE:-The starter-kit files were already updated by this run, but the web-content-extraction dependency install (npm ci) and the remaining steps (the install manifest, saving the settings, and plugin setup) did not run. Resolve the lock and run the same command again.}" + local _WCE_RUNTIME_LOCK_RELEASE_NOTE="${STR_WCE_LOCK_RELEASE_NOTE:-This run stopped before its remaining steps, including the install manifest, saving the settings, and plugin setup. Resolve the lock and run the same command again.}" _wce_with_runtime_update_lock \ "$skill_dir" _wce_run_non_mdm_npm_ci "$skill_dir" || npm_rc=$? case "$npm_rc" in 0) ok "${STR_WCE_NPM_DONE:-web-content-extraction dependencies installed}" ;; - 74) - warn "web-content-extraction dependency lock could not be released; retry setup" - return 1 - ;; - 75) - warn "web-content-extraction dependency update is already active; retry setup after it finishes" - return 1 + 74|75) + # The lock helper already reported the lock path, owner, PID state, and + # recovery steps on stderr, naming this exit code; keep it so the + # process exits with the code the diagnosis announced. + return "$npm_rc" ;; 129|130|143) return "$npm_rc" diff --git a/lib/update.sh b/lib/update.sh index cda96b3..caa8224 100644 --- a/lib/update.sh +++ b/lib/update.sh @@ -271,6 +271,15 @@ _update_claude_md() { return 1 fi + if ! _file_changed "$current_kit_section" "$new_kit_section"; then + # The kit section already holds this version's content while the snapshot + # is older: an earlier run wrote it and stopped before refreshing the + # baseline (for example on the dependency lock after Step 2). That is not + # a user edit. Leave the file as it is and let the caller refresh the + # snapshot so the next update compares against the right baseline. + return 3 + fi + # Both changed → conflict on kit section if [[ "${_MERGE_INTERACTIVE:-true}" != "true" ]]; then # Non-interactive: keep current (non-destructive). Without this warning the @@ -592,14 +601,566 @@ _wce_runtime_update_lock_owner_only() { # [[ "$count" -eq 1 ]] } +# --------------------------------------------------------------------------- +# Lock inspection, diagnosis, and bounded recovery +# +# A writer that is killed while holding the lock (SIGKILL, power loss, an async +# hook torn down with its session) cannot release it. Acquisition never takes +# over an existing lock, so such a lock used to block every later update with +# a bare exit 75. The helpers below (1) classify what is on disk without +# changing it, (2) explain a failure, and (3) let the pre-write preflight — and +# only the preflight — remove a lock whose owner provably no longer exists. +# --------------------------------------------------------------------------- + +# Minimum age of the owner file before an ownerless lock may be recovered. +# Deliberately not overridable from the environment. +_WCE_RUNTIME_LOCK_STALE_MINUTES=60 +# How long an acquisition that found the reclaim mutex waits for the +# reclaimer to finish before withdrawing (update-deps.mjs uses the same). +_WCE_RUNTIME_LOCK_WITHDRAW_WAIT_SECONDS=3 + +_WCE_LOCK_STATE="" # free | busy | stale | unknown | unusable +_WCE_LOCK_OWNER="" # exact token when recognized; display-safe otherwise +_WCE_LOCK_OWNER_PID="" # set only for a recognized token +_WCE_LOCK_PID_STATE="" # alive | dead | unknown +_WCE_LOCK_CREATED="" # owner file mtime, for display +_WCE_LOCK_UNUSABLE="" # the symlink / non-directory that makes the path unusable +_WCE_LOCK_MUTEX=false # whether the reclaim mutex exists beside the lock + +# Git Bash cannot tell whether a lock owner is alive: Windows node.exe and +# MSYS use different PID spaces. +_wce_runtime_update_lock_pid_probe_unsupported() { + if declare -F is_msys >/dev/null 2>&1 && is_msys; then + return 0 + fi + case "$(uname -s 2>/dev/null || true)" in + MSYS_NT*|MINGW*_NT*|CLANG*_NT*|UCRT*_NT*) return 0 ;; + esac + return 1 +} + +_wce_runtime_update_lock_pid_state() { # -> alive | dead | unknown + local pid="$1" ps_out="" ps_rc=0 + case "$pid" in + ""|0*|*[!0123456789]*) printf 'unknown'; return 0 ;; + esac + if _wce_runtime_update_lock_pid_probe_unsupported; then + printf 'unknown' + return 0 + fi + if kill -0 "$pid" 2>/dev/null; then + printf 'alive' + return 0 + fi + # `kill -0` fails the same way for "no such process" and "not permitted" + # (another user's process that reused the PID), so ask the process table. + if [[ -d /proc/self ]]; then + if [[ -e "/proc/$pid" ]]; then + printf 'alive' + else + printf 'dead' + fi + return 0 + fi + ps_out="$(ps -p "$pid" -o pid= 2>/dev/null)" || ps_rc=$? + ps_out="${ps_out//[[:space:]]/}" + if [[ "$ps_rc" -eq 0 && "$ps_out" == "$pid" ]]; then + printf 'alive' + elif [[ "$ps_rc" -eq 1 && -z "$ps_out" ]]; then + printf 'dead' + else + printf 'unknown' + fi + return 0 +} + +_wce_runtime_update_lock_owner_mtime() { # -> "YYYY-MM-DD HH:MM:SS +ZZZZ" + local file="$1" out="" + local stamp_re='^([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})(\.[0-9]+)?( [-+][0-9]{4})?$' + # BSD stat first. GNU stat rejects these operands with a non-zero status, so + # its unrelated stdout is replaced by the fallback assignment. + out="$(stat -f '%Sm' -t '%Y-%m-%d %H:%M:%S %z' "$file" 2>/dev/null)" \ + || out="$(stat -c '%y' "$file" 2>/dev/null)" || out="" + if [[ "$out" =~ $stamp_re ]]; then + printf '%s%s' "${BASH_REMATCH[1]}" "${BASH_REMATCH[3]}" + fi + return 0 +} + +_wce_runtime_update_lock_owner_is_aged() { # + local lock_dir="$1" aged="" + # -mmin +N is "more than N minutes" on BSD, GNU, and BusyBox find. A symlink + # is not followed and never matches -type f. + aged="$(find "$lock_dir/owner" -prune -type f \ + -mmin "+$_WCE_RUNTIME_LOCK_STALE_MINUTES" -print 2>/dev/null)" || return 1 + [[ -n "$aged" ]] +} + +# Classify one lock directory without modifying anything. +# +# free - nothing exists at the path +# busy - recognized owner token whose PID is running +# stale - recognized owner token whose PID does not exist +# unusable - the lock path itself is a symlink or not a directory. No lock +# is held; the recovery commands for a lock must not be offered +# because `rm -f /owner` would resolve through a symlink +# unknown - everything else: a missing or non-regular owner, any entry +# besides `owner`, an owner that is not exactly one LF-terminated +# line of a recognized token, or a PID whose state cannot be +# determined +# +# _WCE_LOCK_MUTEX records whether `.reclaim` exists. Acquisition backs +# off while it does, so a mutex left by a killed reclaimer blocks the lock +# even when nothing holds the canonical name. +# +# Recognized tokens are the two this kit writes: `:` from +# update-deps.mjs and `starter-kit-update---` from setup. +_wce_runtime_update_lock_inspect_dir() { # + local lock_dir="$1" owner_file="$1/owner" + local bytes="" raw="" shown="" rest="" pid="" + local updater_re='^([1-9][0-9]{0,9}):[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$' + local setup_re='^([1-9][0-9]{0,9})-[0-9]+-[0-9]+$' + _WCE_LOCK_STATE=unknown + _WCE_LOCK_OWNER="" + _WCE_LOCK_OWNER_PID="" + _WCE_LOCK_PID_STATE="" + _WCE_LOCK_CREATED="" + _WCE_LOCK_UNUSABLE="" + _WCE_LOCK_MUTEX=false + if [[ -e "${lock_dir}.reclaim" || -L "${lock_dir}.reclaim" ]]; then + _WCE_LOCK_MUTEX=true + fi + + if [[ ! -e "$lock_dir" && ! -L "$lock_dir" ]]; then + _WCE_LOCK_STATE=free + return 0 + fi + if [[ ! -d "$lock_dir" || -L "$lock_dir" ]]; then + _WCE_LOCK_STATE=unusable + _WCE_LOCK_UNUSABLE="$lock_dir" + return 0 + fi + [[ -f "$owner_file" && ! -L "$owner_file" ]] || return 0 + + _WCE_LOCK_CREATED="$(_wce_runtime_update_lock_owner_mtime "$owner_file")" \ + || _WCE_LOCK_CREATED="" + # For display only: first line, bounded, restricted to a safe alphabet so a + # foreign owner file cannot inject terminal control sequences. + { IFS= read -r -n 64 shown < "$owner_file" || true; } 2>/dev/null + _WCE_LOCK_OWNER="${shown//[^A-Za-z0-9._:@+=,-]/?}" + + _wce_runtime_update_lock_owner_only "$lock_dir" || return 0 + bytes="$(LC_ALL=C wc -c < "$owner_file" 2>/dev/null | tr -d '[:space:]')" \ + || return 0 + case "$bytes" in ""|*[!0123456789]*) return 0 ;; esac + [[ "$bytes" -ge 2 && "$bytes" -le 128 ]] || return 0 + # `read` fails without the LF terminator; the byte count rejects a second + # line, NUL bytes, and multibyte content. + { IFS= read -r raw < "$owner_file"; } 2>/dev/null || return 0 + [[ "$bytes" == "$((${#raw} + 1))" ]] || return 0 + + # Explicit character lists first: bracket ranges are locale-dependent. + case "$raw" in + starter-kit-update-*) + rest="${raw#starter-kit-update-}" + case "$rest" in *[!0123456789-]*) return 0 ;; esac + [[ "$rest" =~ $setup_re ]] || return 0 + ;; + *) + case "$raw" in *[!0123456789abcdef:-]*) return 0 ;; esac + [[ "$raw" =~ $updater_re ]] || return 0 + ;; + esac + pid="${BASH_REMATCH[1]}" + + _WCE_LOCK_OWNER="$raw" + _WCE_LOCK_OWNER_PID="$pid" + _WCE_LOCK_PID_STATE="$(_wce_runtime_update_lock_pid_state "$pid")" \ + || _WCE_LOCK_PID_STATE=unknown + case "$_WCE_LOCK_PID_STATE" in + alive) _WCE_LOCK_STATE=busy ;; + dead) _WCE_LOCK_STATE=stale ;; + *) _WCE_LOCK_PID_STATE=unknown ;; + esac + return 0 +} + +# Same classification for the canonical lock of a skill directory. The skill +# and logs directories must be real directories, exactly as acquisition +# requires; anything else is `unusable` so a symlink is never followed and +# no lock-removal command is offered for a lock that does not exist. +_wce_runtime_update_lock_inspect() { # + local current_dir="$1" log_dir="$1/logs" ancestor + for ancestor in "$current_dir" "$log_dir"; do + if [[ -e "$ancestor" || -L "$ancestor" ]] \ + && [[ ! -d "$ancestor" || -L "$ancestor" ]]; then + _WCE_LOCK_STATE=unusable + _WCE_LOCK_OWNER="" + _WCE_LOCK_OWNER_PID="" + _WCE_LOCK_PID_STATE="" + _WCE_LOCK_CREATED="" + _WCE_LOCK_UNUSABLE="$ancestor" + _WCE_LOCK_MUTEX=false + return 0 + fi + done + _wce_runtime_update_lock_inspect_dir "$log_dir/.update.lock" + return 0 +} + +# Recovery is refused wherever "the owner PID does not exist" is not evidence: +# MDM (which never takes this lock) and Git Bash (no shared PID space). +_wce_runtime_update_lock_reclaim_allowed() { + _update_mdm_managed && return 1 + _wce_runtime_update_lock_pid_probe_unsupported && return 1 + return 0 +} + +# True when the lock inspected last meets every recovery condition that does +# not change by waiting: recognized owner, PID gone, old enough, allowed here. +# Read-only. The reclaim mutex is checked by the reclaimer itself. +_wce_runtime_update_lock_reclaim_eligible() { # + [[ "$_WCE_LOCK_STATE" == stale ]] || return 1 + _wce_runtime_update_lock_reclaim_allowed || return 1 + _wce_runtime_update_lock_owner_is_aged "$1/logs/.update.lock" +} + +# Put a quarantined lock directory back under the canonical name. `mv dir +# name` would move the directory *into* a lock that a writer created at that +# name in the meantime, so the name is claimed with an atomic mkdir first and +# the entries are moved one by one. Returns 1 and leaves the quarantine in +# place when the name is taken; the caller reports the path. +_wce_runtime_update_lock_restore_quarantine() { # + local quarantine="$1" lock_dir="$2" entry + (umask 077; mkdir "$lock_dir") 2>/dev/null || return 1 + while IFS= read -r -d '' entry; do + mv "$entry" "$lock_dir/" 2>/dev/null || return 1 + done < <(find "$quarantine" -mindepth 1 -maxdepth 1 -print0 2>/dev/null) + rmdir "$quarantine" 2>/dev/null || return 1 + return 0 +} + +# Runs with HUP/INT/TERM ignored (see the wrapper below). Every exit path after +# the mkdir releases the mutex; nothing here may abort early. +_wce_runtime_update_lock_reclaim_stale_locked() { # + local current_dir="$1" observed="$2" + local lock_dir="$current_dir/logs/.update.lock" + local mutex="${lock_dir}.reclaim" + local quarantine="${lock_dir}.stale-$$-${RANDOM}" + local rc=1 + + # mkdir is atomic, so exactly one reclaimer proceeds. A mutex left behind by + # a killed reclaimer is never removed here: recovery then stays manual. + # Acquisition (bash and update-deps.mjs) fails when it finds this mutex + # after writing its owner (withdrawing its lock once the mutex is gone, or + # leaving it in place if the mutex outlasts its wait), so a writer that + # completed an acquisition after this point is impossible; one that + # completed before it is seen by the inspection below. A release + # (_wce_runtime_update_lock_release) holds the same mutex from its owner + # check through its rename, so it never renames while this runs. + (umask 077; mkdir "$mutex") 2>/dev/null || return 1 + + # Re-read everything under the mutex. The caller's observation may be old: + # another reclaimer can have recovered that lock and a new writer acquired + # the name since. Only the exact owner that was observed may be removed. + _wce_runtime_update_lock_inspect "$current_dir" + if [[ "$_WCE_LOCK_STATE" == stale && "$_WCE_LOCK_OWNER" == "$observed" ]] \ + && _wce_runtime_update_lock_owner_is_aged "$lock_dir" \ + && [[ ! -e "$quarantine" && ! -L "$quarantine" ]] \ + && mv "$lock_dir" "$quarantine" 2>/dev/null; then + # rename(2) moved whichever inode held the name at that instant. Verify + # the quarantined one before deleting anything. + _wce_runtime_update_lock_inspect_dir "$quarantine" + if [[ "$_WCE_LOCK_STATE" == stale && "$_WCE_LOCK_OWNER" == "$observed" ]] \ + && _wce_runtime_update_lock_owner_is_aged "$quarantine"; then + if rm -f "$quarantine/owner" 2>/dev/null \ + && rmdir "$quarantine" 2>/dev/null; then + rc=0 + fi + elif ! _wce_runtime_update_lock_restore_quarantine \ + "$quarantine" "$lock_dir"; then + # A different lock won the read-to-rename race and a third writer has + # taken the canonical name since. Keep the quarantine for inspection. + # Releasing the mutex below stays safe: a writer whose directory this + # is may pass its mutex check afterwards, but acquisition (here and in + # update-deps.mjs) then requires its token on the canonical path, which + # the third writer holds, and fails. + warn "${STR_WCE_LOCK_QUARANTINE_LEFT:-A lock that was moved aside during recovery could not be put back and was left for inspection:}" >&2 + info " $quarantine" >&2 + fi + fi + rmdir "$mutex" 2>/dev/null || rc=1 + return "$rc" +} + +# Remove a lock whose recognized owner no longer exists. Preflight only. +_wce_runtime_update_lock_reclaim_stale() { # + local current_dir="$1" observed="$2" + local reclaim_pid="" reclaim_rc=0 wait_rc=0 + [[ -n "$observed" ]] || return 1 + _wce_runtime_update_lock_reclaim_allowed || return 1 + ( + trap '' HUP INT TERM + _wce_runtime_update_lock_reclaim_stale_locked "$current_dir" "$observed" \ + || exit 1 + exit 0 + ) & + reclaim_pid=$! + # A trapped signal interrupts wait(1), not the signal-ignoring child. + while true; do + wait_rc=0 + wait "$reclaim_pid" 2>/dev/null || wait_rc=$? + case "$wait_rc" in + 129|130|143) continue ;; + *) reclaim_rc="$wait_rc"; break ;; + esac + done + [[ "$reclaim_rc" -eq 0 ]] +} + +# Path / owner / creation time / PID lines for the lock inspected last. stderr. +_wce_runtime_update_lock_print_details() { # + local lock_dir="$1/logs/.update.lock" pid_label="" + info " ${STR_WCE_LOCK_LABEL_PATH:-Lock}: $lock_dir" >&2 + if [[ -n "$_WCE_LOCK_UNUSABLE" ]]; then + info " ${STR_WCE_LOCK_LABEL_UNUSABLE:-Not a directory}: $_WCE_LOCK_UNUSABLE" >&2 + fi + if [[ -n "$_WCE_LOCK_OWNER" ]]; then + info " ${STR_WCE_LOCK_LABEL_OWNER:-Owner}: $_WCE_LOCK_OWNER" >&2 + fi + if [[ -n "$_WCE_LOCK_CREATED" ]]; then + info " ${STR_WCE_LOCK_LABEL_CREATED:-Created}: $_WCE_LOCK_CREATED" >&2 + fi + if [[ -n "$_WCE_LOCK_OWNER_PID" ]]; then + case "$_WCE_LOCK_PID_STATE" in + alive) pid_label="${STR_WCE_LOCK_PID_ALIVE:-running}" ;; + dead) pid_label="${STR_WCE_LOCK_PID_DEAD:-not running}" ;; + *) pid_label="${STR_WCE_LOCK_PID_UNKNOWN:-cannot be determined}" ;; + esac + info " ${STR_WCE_LOCK_LABEL_PID:-Owner PID}: $_WCE_LOCK_OWNER_PID ($pid_label)" >&2 + fi + return 0 +} + +# What to do about the lock inspected last, by state. stderr. +_wce_runtime_update_lock_print_recovery() { # + local lock_dir="$1/logs/.update.lock" + local mutex="${lock_dir}.reclaim" + local quoted_owner="" quoted_lock="" quoted_mutex="" + printf -v quoted_owner '%q' "$lock_dir/owner" + printf -v quoted_lock '%q' "$lock_dir" + printf -v quoted_mutex '%q' "$mutex" + case "$_WCE_LOCK_STATE" in + free) + # Nothing to remove. Only the mutex hint below may apply. + ;; + unusable) + # No lock is held: the path itself is a symlink or not a directory. + # Removing "the lock" would act on whatever that path points to. + info "${STR_WCE_LOCK_HINT_UNUSABLE:-Check what that path is and replace it with a regular directory (or remove it), then run the same command again. Do not remove anything through it.}" >&2 + ;; + busy) + info "${STR_WCE_LOCK_HINT_BUSY:-Wait for it to finish, then run the same command again. Only if that PID is not update-deps.mjs, setup.sh, or npm (a PID can be reused), remove the lock:}" >&2 + info " rm -f $quoted_owner && rmdir $quoted_lock" >&2 + ;; + stale) + info "${STR_WCE_LOCK_HINT_STALE:-setup.sh recovers it automatically once it is 60 minutes old. To recover now, confirm that PID is not running, then run:}" >&2 + info " rm -f $quoted_owner && rmdir $quoted_lock" >&2 + ;; + *) + info "${STR_WCE_LOCK_HINT_UNKNOWN:-Confirm that no update-deps.mjs, setup.sh, or npm ci process is running, then remove the lock and run the same command again:}" >&2 + info " rm -f $quoted_owner && rmdir $quoted_lock" >&2 + ;; + esac + if [[ -e "$mutex" || -L "$mutex" ]]; then + info "${STR_WCE_LOCK_HINT_MUTEX:-An interrupted recovery left a marker that disables automatic recovery. After the same check, remove it too:}" >&2 + info " rmdir $quoted_mutex" >&2 + fi + return 0 +} + +# Explain an exit 75 (could not acquire) or 74 (could not release) from the +# lock inspected last. stderr. +_wce_runtime_update_lock_report_last() { # <74|75> + local current_dir="$1" code="${2:-75}" + if [[ "$code" == 74 ]]; then + error "${STR_WCE_LOCK_RELEASE_FAILED:-The web-content-extraction dependency lock could not be released cleanly (exit 74).}" + else + case "$_WCE_LOCK_STATE" in + busy) + error "${STR_WCE_LOCK_BUSY:-Another web-content-extraction dependency update or kit setup is still running, so this run stopped (exit 75).}" + ;; + stale) + error "${STR_WCE_LOCK_STALE:-A web-content-extraction dependency lock was left behind by a process that is no longer running, so this run stopped (exit 75).}" + ;; + free) + if [[ "$_WCE_LOCK_MUTEX" == true ]]; then + error "${STR_WCE_LOCK_MUTEX_BLOCKED:-The web-content-extraction dependency lock could not be acquired because a marker from an interrupted recovery exists, so this run stopped (exit 75).}" + else + error "${STR_WCE_LOCK_UNAVAILABLE:-The web-content-extraction dependency lock could not be acquired, so this run stopped (exit 75). Nothing holds it right now: run the same command again, and if this repeats, check that its parent is a writable directory.}" + fi + ;; + unusable) + error "${STR_WCE_LOCK_UNUSABLE:-The web-content-extraction dependency lock path cannot be used because it is a symlink or not a directory, so this run stopped (exit 75). No lock is held.}" + ;; + *) + error "${STR_WCE_LOCK_UNKNOWN:-The web-content-extraction dependency lock is held in a form the kit cannot verify, so it was left untouched and this run stopped (exit 75).}" + ;; + esac + fi + _wce_runtime_update_lock_print_details "$current_dir" + _wce_runtime_update_lock_print_recovery "$current_dir" + # The caller's note describes what an acquisition failure left undone. A + # release failure (74) comes after the callback finished, so it gets the + # caller's release note instead: setup stops on a 74 before its remaining + # steps (manifest, saved config, plugins) just as on a 75. + if [[ "$code" == 74 ]]; then + if [[ -n "${_WCE_RUNTIME_LOCK_RELEASE_NOTE:-}" ]]; then + warn "$_WCE_RUNTIME_LOCK_RELEASE_NOTE" + fi + elif [[ -n "${_WCE_RUNTIME_LOCK_FAILURE_NOTE:-}" ]]; then + warn "$_WCE_RUNTIME_LOCK_FAILURE_NOTE" + fi + return 0 +} + +_wce_runtime_update_lock_report() { # <74|75> + _wce_runtime_update_lock_inspect "$1" + _wce_runtime_update_lock_report_last "$1" "${2:-75}" + return 0 +} + +# Settle the lock before the caller writes anything. +# +# free -> 0 +# busy -> wait up to _WCE_RUNTIME_LOCK_WAIT_SECONDS (60) +# stale, old, recoverable -> remove it under the reclaim mutex, then 0 +# anything else -> diagnosis on stderr, 75 +# +# A writer can still take the lock between this check and the caller's own +# acquisition; that later failure is diagnosed by the lock helper itself. +_wce_runtime_update_lock_preflight() { # + local current_dir="$1" + local wait_limit="${_WCE_RUNTIME_LOCK_WAIT_SECONDS:-60}" + local waited=0 relooks=0 changes=0 announced=false seen="" + case "$wait_limit" in ""|*[!0123456789]*) wait_limit=60 ;; esac + while true; do + _wce_runtime_update_lock_inspect "$current_dir" + seen="$_WCE_LOCK_STATE|$_WCE_LOCK_OWNER|$_WCE_LOCK_MUTEX" + case "$_WCE_LOCK_STATE" in + free) + [[ "$_WCE_LOCK_MUTEX" == true ]] || return 0 + # A reclaimer that is finishing holds the mutex for a moment after + # the lock is gone. Acquisition backs off while it exists, so a mutex + # that stays is reported rather than passed through to a later 75. + if [[ "$relooks" -lt 2 && "$waited" -lt "$wait_limit" ]]; then + relooks=$((relooks + 1)) + sleep 1 + waited=$((waited + 1)) + continue + fi + ;; + unusable) + # A symlink or non-directory does not go away by waiting. + ;; + busy) + if [[ "$waited" -lt "$wait_limit" ]]; then + if [[ "$announced" != true ]]; then + announced=true + info "${STR_WCE_LOCK_WAITING:-Waiting for a running web-content-extraction dependency update or kit setup to finish} (PID $_WCE_LOCK_OWNER_PID)..." + fi + sleep 1 + waited=$((waited + 1)) + continue + fi + ;; + stale) + if _wce_runtime_update_lock_reclaim_eligible "$current_dir"; then + if _wce_runtime_update_lock_reclaim_stale \ + "$current_dir" "$_WCE_LOCK_OWNER"; then + warn "${STR_WCE_LOCK_RECOVERED:-Recovered a web-content-extraction dependency lock left behind by a process that is no longer running.}" + _wce_runtime_update_lock_print_details "$current_dir" + return 0 + fi + # A concurrent reclaimer holds the mutex for a moment and then + # frees the lock; losing to it is not a failure. Look again before + # giving up. A mutex that is still there afterwards was left by a + # killed reclaimer and is reported. + if [[ "$relooks" -lt 2 && "$waited" -lt "$wait_limit" ]]; then + relooks=$((relooks + 1)) + sleep 1 + waited=$((waited + 1)) + continue + fi + fi + ;; + *) + # Acquisition is mkdir followed by the owner write, so a writer that + # is acquiring right now looks ownerless for an instant. Look again + # before calling the lock unverifiable. + if [[ "$relooks" -lt 2 && "$waited" -lt "$wait_limit" ]]; then + relooks=$((relooks + 1)) + sleep 1 + waited=$((waited + 1)) + continue + fi + ;; + esac + # Every branch above falls through to here to give up. Do that only for a + # lock that is still the one this pass evaluated: a concurrent reclaimer + # or writer can remove or replace it between the look at the top and the + # checks that followed (the age probe then fails on a path that is gone). + _wce_runtime_update_lock_inspect "$current_dir" + if [[ "$_WCE_LOCK_STATE" == free && "$_WCE_LOCK_MUTEX" != true ]]; then + return 0 + fi + if [[ "$_WCE_LOCK_STATE|$_WCE_LOCK_OWNER|$_WCE_LOCK_MUTEX" != "$seen" \ + && "$changes" -lt 5 ]]; then + changes=$((changes + 1)) + continue + fi + _wce_runtime_update_lock_report_last "$current_dir" 75 + warn "${STR_WCE_LOCK_UNTOUCHED_NOTE:-No starter-kit file was changed by this run.}" + return 75 + done +} + +# Dry-run simulates in a temp dir that has no lock, so the real one is checked +# here, read-only. Warns only; the dry-run exit status is unchanged. +_wce_runtime_update_lock_dryrun_notice() { # + local current_dir="$1/skills/web-content-extraction" + local mutex="$current_dir/logs/.update.lock.reclaim" + _update_mdm_managed && return 0 + _wce_runtime_update_lock_inspect "$current_dir" + if [[ "$_WCE_LOCK_STATE" == free && "$_WCE_LOCK_MUTEX" != true ]]; then + return 0 + fi + if _wce_runtime_update_lock_reclaim_eligible "$current_dir" \ + && [[ ! -e "$mutex" && ! -L "$mutex" ]]; then + warn "${STR_WCE_LOCK_DRYRUN_RECOVERABLE:-A web-content-extraction dependency lock was left behind by a process that is no longer running. The real run recovers it automatically before updating.}" + _wce_runtime_update_lock_print_details "$current_dir" + else + warn "${STR_WCE_LOCK_DRYRUN_BLOCKED:-The web-content-extraction dependency lock is held. The real run waits up to 60 seconds for a running owner and otherwise stops (exit 75) before changing any file.}" + _wce_runtime_update_lock_print_details "$current_dir" + _wce_runtime_update_lock_print_recovery "$current_dir" + fi + return 0 +} + _wce_runtime_update_lock_acquire() { # local current_dir="$1" output_var="$2" local current_parent current_grandparent local log_dir="$current_dir/logs" local lock_file="$log_dir/.update.lock" local generated_token now acquire_pid acquire_rc=0 wait_rc=0 + local withdraw_waited=0 now="$(date +%s)" || return 1 - generated_token="starter-kit-update-$$-${RANDOM}-$now" + # Record the process that holds the lock, not the top-level shell ($$). + # _wce_with_runtime_update_lock calls this from the subshell that runs the + # writer callback, and that subshell outlives a parent killed on its own + # (SIGKILL to setup.sh only). With $$ such a live writer would look + # abandoned and become reclaimable. + generated_token="starter-kit-update-${BASHPID}-${RANDOM}-$now" _WCE_RUNTIME_ACQUIRE_WAITER_PID="$BASHPID" ( @@ -627,13 +1188,54 @@ _wce_runtime_update_lock_acquire() { # fi # mkdir is atomic and never opens an existing FIFO, symlink, or device. - # Never reclaim by age; stale state requires explicit operator recovery. + # Acquisition itself never takes over an existing lock, whatever its age + # or owner. The only recovery path is the pre-write preflight + # (_wce_runtime_update_lock_preflight): recognized owner token, PID + # provably gone, owner file old enough, all rechecked under a mutex. (umask 077; mkdir "$lock_file") 2>/dev/null || exit 1 - if ! (umask 077; printf '%s\n' "$generated_token" \ + # noclobber, like the 'wx' flag in update-deps.mjs: between the mkdir and + # this write a reclaimer can move this directory aside and another writer + # can take the canonical name. A plain > would truncate that writer's + # owner and replace it with this token, and both would hold the lock. + if ! (umask 077; set -C; printf '%s\n' "$generated_token" \ > "$lock_file/owner") 2>/dev/null; then rmdir "$lock_file" 2>/dev/null || true exit 1 fi + # The preflight reclaims an abandoned lock under `.reclaim`. It + # re-reads the lock after taking that mutex, so a lock whose owner was + # written before the mutex appeared is seen and left alone. A lock + # completed after the mutex appeared would not be, so withdraw it: this + # check runs after the owner write, which makes one of the two orderings + # certain. The withdrawal is best effort because the reclaimer may have + # moved this directory aside already; it then puts it back (busy) or + # keeps it for inspection, and this run stops with a diagnosis either way. + # Withdraw through the token-checked release: by now the recovery may + # have finished and another writer may hold the canonical name, and that + # writer's lock must not be removed. + # Wait for the reclaimer to finish first. The release checks the owner + # and then renames; while the reclaimer is active it can move this lock + # aside between those two steps, and after it releases the mutex another + # writer can take the canonical name, which the rename would then move. + # Once the mutex is gone nothing else renames this live, fresh lock. A + # mutex that outlasts the wait does not prove the reclaimer dead (it may + # only be stalled), so the same race stays open: leave this lock in place + # and fail. Its owner names this subshell, so once that exits the lock is + # a recognized abandoned lock that the preflight diagnoses or recovers. + if [[ -e "${lock_file}.reclaim" || -L "${lock_file}.reclaim" ]]; then + withdraw_waited=0 + while [[ -e "${lock_file}.reclaim" || -L "${lock_file}.reclaim" ]] \ + && [[ "$withdraw_waited" -lt "${_WCE_RUNTIME_LOCK_WITHDRAW_WAIT_SECONDS:-3}" ]]; do + sleep 1 + withdraw_waited=$((withdraw_waited + 1)) + done + if [[ -e "${lock_file}.reclaim" || -L "${lock_file}.reclaim" ]]; then + exit 1 + fi + _wce_runtime_update_lock_release "$current_dir" "$generated_token" \ + >/dev/null 2>&1 || true + exit 1 + fi ) & acquire_pid=$! while true; do @@ -649,7 +1251,9 @@ _wce_runtime_update_lock_acquire() { # # removes only a directory it created when its own owner write fails. return 1 fi - # Revalidate the child result before publishing the bearer token. + # Revalidate the child result before publishing the bearer token. This runs + # after the child's mutex check, so it also catches a recovery that moved + # the lock aside and finished between the owner write and that check. _wce_runtime_update_lock_owner_matches \ "$current_dir" "$generated_token" || return 1 _wce_runtime_update_lock_owner_only "$lock_file" || return 1 @@ -657,7 +1261,36 @@ _wce_runtime_update_lock_acquire() { # return 0 } +# Release under the reclaim mutex. The owner check and the rename below are +# two steps, and the release runs in a child of the lock holder whose PID is +# in the token. If only the holder is killed while the child is stalled +# between those steps, the lock is stale; a reclaimer could remove it, writer +# A take the name, the resumed rename move A's lock, and writer B take the +# freed name, leaving A and B both running. The reclaimer only acts under +# this mutex, so holding it from the check through the rename closes that. +# A mutex that outlasts the wait may belong to a stalled reclaimer: fail and +# rename nothing. An acquirer that writes its owner while this is held +# withdraws (it treats the mutex as a reclaimer), which is safe. _wce_runtime_update_lock_release() { # + local current_dir="$1" token="$2" + local mutex="$current_dir/logs/.update.lock.reclaim" + local waited=0 rc=0 + case "$token" in ""|*[!A-Za-z0-9._-]*) return 1 ;; esac + until (umask 077; mkdir "$mutex") 2>/dev/null; do + # mkdir failed for a reason other than an existing mutex (missing or + # unwritable logs directory): nothing to wait for. + [[ -e "$mutex" || -L "$mutex" ]] || return 1 + [[ "$waited" -lt "${_WCE_RUNTIME_LOCK_WITHDRAW_WAIT_SECONDS:-3}" ]] \ + || return 1 + sleep 1 + waited=$((waited + 1)) + done + _wce_runtime_update_lock_release_locked "$current_dir" "$token" || rc=1 + rmdir "$mutex" 2>/dev/null || rc=1 + return "$rc" +} + +_wce_runtime_update_lock_release_locked() { # local current_dir="$1" token="$2" local lock_file="$current_dir/logs/.update.lock" local quarantine="${lock_file}.release-${token}" @@ -678,11 +1311,10 @@ _wce_runtime_update_lock_release() { # "$current_dir" "$token" "$quarantine" \ || ! _wce_runtime_update_lock_owner_only "$quarantine"; then # The inode renamed into quarantine was replaced after our first check. - # Put that foreign directory back when no successor owns the canonical - # name; otherwise retain both paths for manual inspection. - if [[ ! -e "$lock_file" && ! -L "$lock_file" ]]; then - mv "$quarantine" "$lock_file" 2>/dev/null || true - fi + # Put that foreign directory back unless a successor owns the canonical + # name; then retain both paths for manual inspection. + _wce_runtime_update_lock_restore_quarantine "$quarantine" "$lock_file" \ + || true return 1 fi if ! rm -f "$quarantine/owner"; then @@ -741,7 +1373,10 @@ _wce_with_runtime_update_lock() { # [args...] *) release_rc="$wait_rc"; break ;; esac done - [[ "$release_rc" -eq 0 ]] || release_rc=74 + if [[ "$release_rc" -ne 0 ]]; then + release_rc=74 + _wce_runtime_update_lock_report "$current_dir" 74 || true + fi fi trap - HUP INT TERM [[ "$cleanup_pending_signal" -eq 0 ]] || cleanup_rc="$cleanup_pending_signal" @@ -760,6 +1395,9 @@ _wce_with_runtime_update_lock() { # [args...] || acquire_rc=$? if [[ "$acquire_rc" -ne 0 ]]; then [[ "$cleanup_pending_signal" -eq 0 ]] || exit "$cleanup_pending_signal" + # Callers invoke this helper as a simple command under errexit, so the + # exit below ends the whole run. Say why here; nobody else can. + _wce_runtime_update_lock_report "$current_dir" 75 || true exit 75 fi _WCE_RUNTIME_LOCK_TOKEN="$runtime_lock_token" @@ -987,10 +1625,12 @@ _update_auto_managed_wce_package_pair() { # :`、setup の `starter-kit-update--…`)で、その PID のプロセスが存在せず、 + owner ファイルが 60 分以上前のものである場合に限り、回収用 mutex + (`logs/.update.lock.reclaim`)の下で照合し直してから取り除く。経過時間だけを根拠にした + 奪取はしない。Git Bash(MSYS)と MDM 管理下では回収しない。owner のプロセスが実行中なら + 最大 60 秒待つ。PID は同じ PID 名前空間の中でしか照合できないため、コンテナとホストで + `~/.claude` を共有している場合、別の名前空間で実行中の owner は存在しないように見える。 + その場合に誤回収を防ぐのは 60 分の条件だけである。回収中は lock の取得側(setup と + `update-deps.mjs`)が mutex の存在を見て自分の lock を取り下げるため、回収と並行して + 取得が成立することはない。 +- **手動復旧**: 上の条件を満たさない lock(60 分未満、owner が上記以外の形式、PID の生存を + 判定できない、`logs/.update.lock.reclaim` が残っている)は残る。setup / update は lock の + パス・owner・作成時刻・PID の状態を表示し、終了コード 75 で止まる。依存更新・kit setup・ + `npm ci` のプロセスが動いていないことを確認してから次を実行し、setup または手動更新を + 再実行する。実行中プロセスを確認せずに lock を削除してはならない。skill ディレクトリ・ + `logs`・lock のパスが symlink やディレクトリ以外の場合は lock は保持されておらず、そのパスが + 表示される(下のコマンドは案内されない)。そのパスを経由して削除せず、何であるかを確認 + してから置き換える。 + + ```bash + rm -f ~/.claude/skills/web-content-extraction/logs/.update.lock/owner \ + && rmdir ~/.claude/skills/web-content-extraction/logs/.update.lock + ``` > 注: 自動更新ゲートは `npm test` に依存する。テストが実抽出(HTML/PDF)を検証しているため > ゲートは実効的だが、サイト固有の挙動変化までは捕捉できない。重要更新後は実URLでの確認を推奨。 diff --git a/skills/web-content-extraction/scripts/update-deps.mjs b/skills/web-content-extraction/scripts/update-deps.mjs index 7b044c8..c1223e8 100644 --- a/skills/web-content-extraction/scripts/update-deps.mjs +++ b/skills/web-content-extraction/scripts/update-deps.mjs @@ -8,7 +8,10 @@ // // Safety: // - Throttled to once per 24h (override with --force). -// - Lock file prevents concurrent runs (e.g. multiple sessions starting). +// - A lock serializes the apply phase (npm install / test / rollback) with +// other sessions and with kit setup / update / npm ci. The registry check +// (`npm view`) runs without it, so a run killed while checking leaves no +// lock behind. // - package.json + package-lock.json are backed up and restored on test fail. // - All output is appended to logs/update.log; never writes to stdout JSON. // @@ -132,13 +135,68 @@ export function acquireLock(lockFile = LOCK_FILE) { flag: 'wx', mode: 0o600, }) - return token } catch (error) { // The directory is ours because this call created it. Remove only the // expected empty/owner-only shape; any foreign addition fails closed. try { rmdirSync(lockFile) } catch { /* leave residue for inspection */ } throw error } + // The kit's setup recovers an abandoned lock under `.reclaim`. It + // re-reads the lock after creating that mutex, so an owner written before + // the mutex appeared is seen and left alone; one completed after it would + // not be. This check runs after the owner write, which makes one of the two + // orderings certain: withdraw the lock when the mutex is there. The + // reclaimer may already have moved this directory aside, in which case the + // removals fail and it puts the directory back or keeps it for inspection. + // Withdraw through the token-checked release: by now the recovery may have + // finished and another writer may hold the canonical name, and that + // writer's lock must not be removed. + // Wait for the reclaimer to finish first. The release checks the owner and + // then renames; while the reclaimer is active it can move this lock aside + // between those two steps, and after it releases the mutex another writer + // can take the canonical name, which the rename would then move. Once the + // mutex is gone nothing else renames this live, fresh lock. A mutex that + // outlasts the wait does not prove the reclaimer dead (it may only be + // stalled), so the same race stays open: leave this lock in place and + // fail. Its owner names this process, so once it exits the lock is a + // recognized abandoned lock that the kit's preflight diagnoses or recovers. + if (reclaimMutexExists(lockFile)) { + if (!waitForReclaimMutex(lockFile)) return null + releaseLock(token, lockFile) + return null + } + // A whole recovery can also start and finish between the owner write and + // the mutex check above: the reclaimer moves this directory aside in a + // read-to-rename race and then cannot put it back because another writer + // took the canonical name in the meantime. The mutex is gone again, so only + // the canonical path tells which writer holds the lock. Succeed only while + // it still carries this token; otherwise leave everything where the + // reclaimer left it (it names that path) and report the lock as held. + const owned = openOwnedLock(token, lockFile) + const ours = owned !== null && lockOwnerOnly(lockFile) + closeOwnedLock(owned) + return ours ? token : null +} + +// Same bound as _WCE_RUNTIME_LOCK_WITHDRAW_WAIT_SECONDS in the kit's setup. +const RECLAIM_WITHDRAW_WAIT_MS = 3000 + +function waitForReclaimMutex(lockFile) { + const pause = new Int32Array(new SharedArrayBuffer(4)) + const deadline = Date.now() + RECLAIM_WITHDRAW_WAIT_MS + while (reclaimMutexExists(lockFile) && Date.now() < deadline) { + Atomics.wait(pause, 0, 0, 50) + } + return !reclaimMutexExists(lockFile) +} + +function reclaimMutexExists(lockFile) { + try { + lstatSync(`${lockFile}.reclaim`) + return true + } catch { + return false + } } function openOwnedLock(token, lockDirectory) { @@ -359,45 +417,92 @@ function main() { log('skip: dependency versions are pinned by MDM expected state') return 0 } - if (throttled()) return 0 + // The marker of an interrupted apply phase bypasses the throttle. A run that + // found no marker can still stamp 24h after another run's apply phase left + // one (that run can be killed between this run's marker check and its + // stamp). Checking the marker first makes the next start recover it anyway. + const isThrottled = throttled() + if (isThrottled && !existsSync(IN_PROGRESS_FILE)) return 0 let lockToken = null const removeLockSignalHandlers = installLockSignalHandlers(() => lockToken) + const dropLock = () => { + if (!lockToken) return + if (!releaseLock(lockToken)) { + log('lock release skipped: lock owner token changed or lock is missing') + } + lockToken = null + } let outcome = 'ok' // becomes 'failed' on any check/install/test failure -> short backoff + // True once this run reached a result of its own. A run that only lost the + // lock to another writer leaves the timer to that writer. + let completed = false try { + // An apply phase that died mid-way left its marker. Re-testing (and a + // possible rollback) mutates the package pair and node_modules, so it + // runs under the lock, which is dropped again before the registry check. + if (existsSync(IN_PROGRESS_FILE)) { + lockToken = acquireLock() + if (!lockToken) { + log('skip: another update run is active (lock held)') + return 0 + } + completed = true + if (!recoverInterruptedUpdate()) { + outcome = 'failed' + return 1 + } + dropLock() + if (isThrottled) { + // Only the recovery was due; the registry check keeps its timer. A + // failed recovery above still stamps the short backoff. + log('done: registry check throttled') + completed = false + return 0 + } + } + if (isThrottled) return 0 + + // The registry check holds no lock. This process is started by an async + // SessionStart hook and can be killed at any point, and the HUP/INT/TERM + // handlers cannot run while execFileSync blocks on npm. A lock held across + // `npm view` stayed behind when the process was killed there, and then + // blocked every later kit update. + log('check start') + const check = checkForUpdates() + if (check.failed) outcome = 'failed' + completed = true + // Another run's apply phase may have been interrupted while this check + // ran: the installed versions then look current, but they are untested + // and the marker is still there. Stamping 24h here would hide it from + // every later run, so such a run goes through the locked path below. + if (check.updates.length === 0 && !existsSync(IN_PROGRESS_FILE)) { + log('done: all dependencies up-to-date') + return 0 + } + + // Only the apply phase needs exclusion from kit setup / update / npm ci. lockToken = acquireLock() if (!lockToken) { log('skip: another update run is active (lock held)') + completed = false return 0 } if (!recoverInterruptedUpdate()) { outcome = 'failed' return 1 } - log('check start') - + // The check ran unlocked, so another run may have applied these updates + // (or a kit update may have replaced node_modules) since. Decide again + // from what is installed now. const updates = [] - for (const pkg of TARGETS) { - const installed = installedVersion(pkg) - if (!installed) { - log(`${pkg}: not installed, skipping`) - continue - } - let latest - try { - latest = latestVersion(pkg) - } catch (error) { - log(`${pkg}: 最新版の取得に失敗 (${error?.message ?? error}) — skip`) - outcome = 'failed' // a target could not be checked -> retry sooner - continue - } - if (compareVersions(latest, installed) > 0) { - log(`${pkg} ${installed} -> ${latest} (update available)`) - updates.push({ pkg, installed, latest }) + for (const candidate of check.updates) { + const installed = installedVersion(candidate.pkg) + if (installed && compareVersions(candidate.latest, installed) > 0) { + updates.push({ ...candidate, installed }) } else { - log(`${pkg} up-to-date (${installed})`) + log(`${candidate.pkg} no longer needs ${candidate.latest} (installed: ${installed ?? 'none'})`) } } - if (updates.length === 0) { log('done: all dependencies up-to-date') return 0 @@ -451,21 +556,21 @@ function main() { return 0 } finally { // Stamp AFTER a completed run: 24h on a clean run, 1h backoff on failure, so - // transient npm/network failures retry sooner. Runs skipped by - // throttle/lock do not own a token and therefore do not reset the timer. - if (lockToken) { - log(`run outcome: ${outcome}`) - try { - stampOutcome(outcome) - } catch (error) { - // The stamp is advisory. Preserve main's original result while still - // guaranteeing release of the token-bound writer lock. - log(`outcome stamp failed: ${error?.message ?? error}`) - } finally { - if (!releaseLock(lockToken)) { - log('lock release skipped: lock owner token changed or lock is missing') + // transient npm/network failures retry sooner. Runs skipped by throttle, + // or stopped by another writer's lock, do not reset the timer. + try { + if (completed) { + log(`run outcome: ${outcome}`) + try { + stampOutcome(outcome) + } catch (error) { + // The stamp is advisory. Preserve main's original result while still + // guaranteeing release of the token-bound writer lock. + log(`outcome stamp failed: ${error?.message ?? error}`) } } + } finally { + dropLock() } // Keep signal cleanup installed through one event-loop turn so a signal // queued during a synchronous release syscall can publish status 128+n. @@ -473,6 +578,37 @@ function main() { } } +/** + * Compare installed versions with the registry. Takes no lock: it only reads + * node_modules and runs `npm view`. + */ +function checkForUpdates() { + const updates = [] + let failed = false + for (const pkg of TARGETS) { + const installed = installedVersion(pkg) + if (!installed) { + log(`${pkg}: not installed, skipping`) + continue + } + let latest + try { + latest = latestVersion(pkg) + } catch (error) { + log(`${pkg}: 最新版の取得に失敗 (${error?.message ?? error}) — skip`) + failed = true // a target could not be checked -> retry sooner + continue + } + if (compareVersions(latest, installed) > 0) { + log(`${pkg} ${installed} -> ${latest} (update available)`) + updates.push({ pkg, installed, latest }) + } else { + log(`${pkg} up-to-date (${installed})`) + } + } + return { updates, failed } +} + function rollback(backups) { for (const f of backups) { try { diff --git a/skills/web-content-extraction/test/update-deps-lock.test.mjs b/skills/web-content-extraction/test/update-deps-lock.test.mjs index 5621f84..79c922e 100644 --- a/skills/web-content-extraction/test/update-deps-lock.test.mjs +++ b/skills/web-content-extraction/test/update-deps-lock.test.mjs @@ -1,5 +1,7 @@ import { spawn, spawnSync } from 'node:child_process' import { once } from 'node:events' +import fs from 'node:fs' +import { syncBuiltinESMExports } from 'node:module' import { existsSync, copyFileSync, @@ -7,6 +9,7 @@ import { mkdirSync, mkdtempSync, readFileSync, + readdirSync, rmSync, symlinkSync, utimesSync, @@ -47,6 +50,212 @@ test('dependency lock never reclaims an existing stale inode', (t) => { assert.equal(existsSync(lockFile), false) }) +test('dependency lock backs off while the kit reclaim mutex exists', (t) => { + // setup.sh recovers an abandoned lock under `.reclaim` and re-reads + // the lock only after creating that mutex. An acquisition completed after + // the mutex appeared would go unseen, so acquireLock checks for the mutex + // after writing its owner and fails when it is there. A mutex that outlasts + // the withdrawal wait (here one left by a killed reclaimer) does not prove + // the reclaimer dead, so the lock is left in place rather than renamed. + const root = mkdtempSync(join(tmpdir(), 'wce-update-lock-mutex-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + const logs = join(root, 'logs') + const lockFile = join(logs, '.update.lock') + const mutex = `${lockFile}.reclaim` + mkdirSync(logs) + mkdirSync(mutex) + + assert.equal(acquireLock(lockFile), null) + assert.match(readFileSync(join(lockFile, 'owner'), 'utf8'), /^\d+:[0-9a-f-]{36}\n$/) + assert.equal(lstatSync(mutex).isDirectory(), true) + assert.equal(acquireLock(lockFile), null) + + rmSync(mutex, { recursive: true }) + rmSync(lockFile, { recursive: true }) + const token = acquireLock(lockFile) + assert.equal(typeof token, 'string') + assert.equal(releaseLock(token, lockFile), true) +}) + +test('dependency lock fails when a recovery displaced it before the mutex check', (t) => { + // A whole kit recovery can run between the owner write and the mutex + // check: it moves this directory aside (read-to-rename race), cannot put it + // back because writer B took the canonical name, and releases the mutex. + // The mutex check then passes, so acquisition must verify the canonical + // owner or both writers would proceed. + const root = mkdtempSync(join(tmpdir(), 'wce-update-lock-displaced-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + const lockFile = join(root, 'logs', '.update.lock') + const quarantine = `${lockFile}.stale-test` + const writeFile = fs.writeFileSync + t.after(() => { + fs.writeFileSync = writeFile + syncBuiltinESMExports() + }) + let displaced = false + fs.writeFileSync = function (path, ...rest) { + const result = writeFile.call(this, path, ...rest) + if (!displaced && path === join(lockFile, 'owner')) { + displaced = true + fs.renameSync(lockFile, quarantine) + fs.mkdirSync(lockFile) + writeFile(join(lockFile, 'owner'), 'writer-b\n') + } + return result + } + syncBuiltinESMExports() + + assert.equal(acquireLock(lockFile), null) + fs.writeFileSync = writeFile + syncBuiltinESMExports() + assert.equal(displaced, true) + assert.equal(readFileSync(join(lockFile, 'owner'), 'utf8'), 'writer-b\n') + // The displaced directory stays where the reclaimer left it. + assert.match(readFileSync(join(quarantine, 'owner'), 'utf8'), /^\d+:[0-9a-f-]{36}\n$/) +}) + +test('dependency lock withdrawal never removes another writer\'s lock', (t) => { + // The mutex check sees the recovery, but before the withdrawal runs the + // recovery moves this directory aside and writer B takes the canonical + // name. Withdrawing must check the token and leave B's lock in place. + const root = mkdtempSync(join(tmpdir(), 'wce-update-lock-withdraw-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + const lockFile = join(root, 'logs', '.update.lock') + const quarantine = `${lockFile}.stale-test` + const writeFile = fs.writeFileSync + t.after(() => { + fs.writeFileSync = writeFile + syncBuiltinESMExports() + }) + let displaced = false + fs.writeFileSync = function (path, ...rest) { + const result = writeFile.call(this, path, ...rest) + if (!displaced && path === join(lockFile, 'owner')) { + displaced = true + fs.mkdirSync(`${lockFile}.reclaim`) + fs.renameSync(lockFile, quarantine) + fs.mkdirSync(lockFile) + writeFile(join(lockFile, 'owner'), 'writer-b\n') + } + return result + } + syncBuiltinESMExports() + + assert.equal(acquireLock(lockFile), null) + fs.writeFileSync = writeFile + syncBuiltinESMExports() + assert.equal(displaced, true) + assert.equal(readFileSync(join(lockFile, 'owner'), 'utf8'), 'writer-b\n') + assert.deepEqual(readdirSync(lockFile), ['owner']) + assert.match(readFileSync(join(quarantine, 'owner'), 'utf8'), /^\d+:[0-9a-f-]{36}\n$/) +}) + +test('dependency lock withdrawal waits for the reclaimer before renaming', (t) => { + // The reclaimer is active when the mutex check runs. If the withdrawal + // checked its owner and renamed while the reclaimer still worked, the + // reclaimer could move this lock aside between the two steps, writer B + // could take the canonical name after the mutex is released, and the + // rename would move B's lock (writer C then takes the free name). Waiting + // for the mutex first leaves the withdrawal nothing to rename. + const root = mkdtempSync(join(tmpdir(), 'wce-update-lock-withdraw-wait-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + const lockFile = join(root, 'logs', '.update.lock') + const mutex = `${lockFile}.reclaim` + const quarantine = `${lockFile}.stale-test` + const writeFile = fs.writeFileSync + const lstat = fs.lstatSync + const rename = fs.renameSync + const restore = () => { + fs.writeFileSync = writeFile + fs.lstatSync = lstat + fs.renameSync = rename + syncBuiltinESMExports() + } + t.after(restore) + let mutexLooks = 0 + let recovered = false + // The reclaimer moves this lock aside and finishes; writer B acquires. + const finishRecoveryThenB = () => { + if (recovered) return + recovered = true + rename(lockFile, quarantine) + fs.rmdirSync(mutex) + fs.mkdirSync(lockFile) + writeFile(join(lockFile, 'owner'), 'writer-b\n') + } + fs.writeFileSync = function (path, ...rest) { + const result = writeFile.call(this, path, ...rest) + if (path === join(lockFile, 'owner') && !existsSync(mutex)) { + fs.mkdirSync(mutex) + } + return result + } + fs.lstatSync = function (path, ...rest) { + // The first look is the mutex check; later looks happen while waiting. + if (path === mutex && ++mutexLooks > 1) finishRecoveryThenB() + return lstat.call(this, path, ...rest) + } + fs.renameSync = function (from, to) { + if (from === lockFile && String(to).startsWith(`${lockFile}.release-`)) { + finishRecoveryThenB() + } else if (to === lockFile && !existsSync(lockFile)) { + fs.mkdirSync(lockFile) + writeFile(join(lockFile, 'owner'), 'writer-c\n') + } + return rename.call(this, from, to) + } + syncBuiltinESMExports() + + assert.equal(acquireLock(lockFile), null) + restore() + assert.equal(recovered, true) + assert.equal(readFileSync(join(lockFile, 'owner'), 'utf8'), 'writer-b\n') + assert.deepEqual(readdirSync(lockFile), ['owner']) + assert.match(readFileSync(join(quarantine, 'owner'), 'utf8'), /^\d+:[0-9a-f-]{36}\n$/) + assert.deepEqual(readdirSync(join(root, 'logs')).sort(), + ['.update.lock', '.update.lock.stale-test']) +}) + +test('dependency lock fails without renaming when the mutex outlasts the wait', (t) => { + // A mutex still there after the wait does not prove the reclaimer dead: it + // may be stalled between its own read and rename, and the token-checked + // release is not atomic. Withdrawing now could move a successor's lock, so + // the acquisition must fail and leave its lock and the mutex in place. + const root = mkdtempSync(join(tmpdir(), 'wce-update-lock-withdraw-stalled-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + const lockFile = join(root, 'logs', '.update.lock') + const mutex = `${lockFile}.reclaim` + const writeFile = fs.writeFileSync + const rename = fs.renameSync + const restore = () => { + fs.writeFileSync = writeFile + fs.renameSync = rename + syncBuiltinESMExports() + } + t.after(restore) + const renames = [] + fs.writeFileSync = function (path, ...rest) { + const result = writeFile.call(this, path, ...rest) + if (path === join(lockFile, 'owner') && !existsSync(mutex)) { + fs.mkdirSync(mutex) + } + return result + } + fs.renameSync = function (from, to) { + renames.push([from, to]) + return rename.call(this, from, to) + } + syncBuiltinESMExports() + + assert.equal(acquireLock(lockFile), null) + restore() + assert.deepEqual(renames, []) + assert.equal(existsSync(mutex), true) + assert.match(readFileSync(join(lockFile, 'owner'), 'utf8'), /^\d+:[0-9a-f-]{36}\n$/) + assert.deepEqual(readdirSync(join(root, 'logs')).sort(), + ['.update.lock', '.update.lock.reclaim']) +}) + test('dependency lock rejects non-directories and exact-owner violations', { skip: process.platform === 'win32', }, (t) => { diff --git a/skills/web-content-extraction/test/update-deps-run.test.mjs b/skills/web-content-extraction/test/update-deps-run.test.mjs new file mode 100644 index 0000000..1583761 --- /dev/null +++ b/skills/web-content-extraction/test/update-deps-run.test.mjs @@ -0,0 +1,316 @@ +// End-to-end runs of update-deps.mjs against an offline npm stand-in. +// +// The lock serializes the phase that mutates package.json / package-lock.json +// / node_modules. The registry check (`npm view`) must run without it: the +// updater is started from an async SessionStart hook and can be killed at any +// point, and a kill during a lock-holding `npm view` used to leave the lock +// behind forever. +import { spawn, spawnSync } from 'node:child_process' +import { once } from 'node:events' +import { + chmodSync, + copyFileSync, + existsSync, + mkdirSync, + mkdtempSync, + readFileSync, + rmSync, + writeFileSync, +} from 'node:fs' +import { tmpdir } from 'node:os' +import { delimiter, dirname, join } from 'node:path' +import { fileURLToPath } from 'node:url' +import assert from 'node:assert/strict' +import { test } from 'node:test' + +const updaterPath = fileURLToPath(new URL('../scripts/update-deps.mjs', import.meta.url)) +const TARGETS = ['defuddle', 'jsdom', 'pdfjs-dist', 'undici'] + +// The npm stand-in is a POSIX shell script executed from the temp directory. +// This suite is also the updater's own test gate on user machines, so where +// that cannot work (Windows, a noexec temp mount) the tests are skipped +// instead of failing the gate. +function standInUnavailable() { + if (process.platform === 'win32') return 'needs a POSIX shell' + const dir = mkdtempSync(join(tmpdir(), 'wce-update-probe-')) + try { + const probe = join(dir, 'probe') + writeFileSync(probe, '#!/bin/sh\nexit 0\n') + chmodSync(probe, 0o755) + const result = spawnSync(probe, [], { timeout: 10000 }) + return result.status === 0 ? false : 'cannot execute scripts from the temp directory' + } catch { + return 'cannot execute scripts from the temp directory' + } finally { + rmSync(dir, { recursive: true, force: true }) + } +} +const skip = standInUnavailable() + +// Records " " for every call, so each test can +// assert whether the lock directory existed while npm was running. +const FAKE_NPM = `#!/bin/sh +skill="$FAKE_NPM_SKILL" +if [ -d "$skill/logs/.update.lock" ]; then state=locked; else state=unlocked; fi +printf '%s %s\\n' "$1" "$state" >> "$FAKE_NPM_TRACE" +case "$1" in + view) + if [ -n "\${FAKE_NPM_VIEW_BLOCK:-}" ]; then exec sleep 60; fi + if [ -n "\${FAKE_NPM_BUMP_ON_VIEW:-}" ]; then + printf '{"version":"%s"}\\n' "$FAKE_NPM_LATEST" \\ + > "$skill/node_modules/$2/package.json" + fi + if [ -n "\${FAKE_NPM_MARK_ON_VIEW:-}" ]; then + # Another run's apply phase was interrupted while this check ran. + mkdir -p "$skill/logs" + printf '{"pid":1,"specs":[]}' > "$skill/logs/.update-in-progress" + fi + printf '%s\\n' "$FAKE_NPM_LATEST" + ;; + test) + exit "\${FAKE_NPM_TEST_RC:-0}" + ;; +esac +exit 0 +` + +function makeFixture(t, { installed = '1.0.0' } = {}) { + const root = mkdtempSync(join(tmpdir(), 'wce-update-run-')) + t.after(() => rmSync(root, { recursive: true, force: true })) + // Mirror ~/.claude/skills// so the updater's manifest lookup + // (two levels above the skill) stays inside the fixture. + const skill = join(root, 'claude', 'skills', 'web-content-extraction') + const scripts = join(skill, 'scripts') + const bin = join(root, 'bin') + mkdirSync(scripts, { recursive: true }) + mkdirSync(bin) + const updater = join(scripts, 'update-deps.mjs') + copyFileSync(updaterPath, updater) + for (const pkg of TARGETS) { + const dir = join(skill, 'node_modules', pkg) + mkdirSync(dir, { recursive: true }) + writeFileSync(join(dir, 'package.json'), `${JSON.stringify({ version: installed })}\n`) + } + writeFileSync(join(skill, 'package.json'), '{"name":"fixture"}\n') + writeFileSync(join(skill, 'package-lock.json'), '{"lockfileVersion":3}\n') + const npm = join(bin, 'npm') + writeFileSync(npm, FAKE_NPM) + chmodSync(npm, 0o755) + const trace = join(root, 'npm.trace') + const logs = join(skill, 'logs') + return { + skill, + updater, + trace, + logs, + lock: join(logs, '.update.lock'), + stamp: join(logs, '.last-update-check'), + marker: join(logs, '.update-in-progress'), + env(extra = {}) { + return { + ...process.env, + PATH: [bin, dirname(process.execPath), process.env.PATH].join(delimiter), + FAKE_NPM_SKILL: skill, + FAKE_NPM_TRACE: trace, + FAKE_NPM_LATEST: '1.0.0', + ...extra, + } + }, + traceLines() { + return existsSync(trace) + ? readFileSync(trace, 'utf8').split('\n').filter(Boolean) + : [] + }, + log() { + return readFileSync(join(logs, 'update.log'), 'utf8') + }, + } +} + +// Each run finishes in well under a second when the machine is idle. The +// timeout only guards against a hang: when it fires, the updater's SIGTERM +// handler exits 143 and the test fails, and on a user machine that failure +// rolls back a dependency update that was fine. So it stays far above what a +// heavily loaded host needs, inside the updater's 300s gate for `npm test`. +const RUN_TIMEOUT_MS = 120000 + +function run(fixture, extraEnv) { + return spawnSync(process.execPath, [fixture.updater, '--force'], { + encoding: 'utf8', + env: fixture.env(extraEnv), + timeout: RUN_TIMEOUT_MS, + }) +} + +test('an up-to-date check never creates the lock', { skip }, (t) => { + const fixture = makeFixture(t) + const result = run(fixture) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), TARGETS.map(() => 'view unlocked')) + assert.match(fixture.log(), /done: all dependencies up-to-date/) + assert.equal(existsSync(fixture.stamp), true) + assert.equal(existsSync(fixture.lock), false) +}) + +test('SIGKILL during the registry check leaves no lock behind', { skip }, async (t) => { + const fixture = makeFixture(t) + // Own process group, so the blocked npm stand-in dies with the updater. + const child = spawn(process.execPath, [fixture.updater, '--force'], { + env: fixture.env({ FAKE_NPM_VIEW_BLOCK: '1' }), + stdio: 'ignore', + detached: true, + }) + const exited = once(child, 'exit') + const killGroup = () => { + try { process.kill(-child.pid, 'SIGKILL') } catch { /* already gone */ } + } + t.after(killGroup) + const deadline = Date.now() + RUN_TIMEOUT_MS + while (fixture.traceLines().length === 0) { + assert.ok(Date.now() < deadline, 'npm view was never started') + assert.equal(child.exitCode, null, 'updater exited before npm view') + await new Promise((resolve) => setTimeout(resolve, 20)) + } + killGroup() + const [code, signal] = await exited + assert.equal(code, null) + assert.equal(signal, 'SIGKILL') + assert.deepEqual(fixture.traceLines(), ['view unlocked']) + assert.equal(existsSync(fixture.lock), false) +}) + +test('the lock covers install and the test gate, and is released', { skip }, (t) => { + const fixture = makeFixture(t) + const result = run(fixture, { FAKE_NPM_LATEST: '2.0.0' }) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), [ + ...TARGETS.map(() => 'view unlocked'), + 'install locked', + 'test locked', + ]) + assert.match(fixture.log(), /DONE updated \(tests pass\)/) + assert.equal(existsSync(fixture.stamp), true) + assert.equal(existsSync(fixture.marker), false) + assert.equal(existsSync(fixture.lock), false) +}) + +test('a held lock still blocks the apply phase and is never taken over', { skip }, (t) => { + const fixture = makeFixture(t) + mkdirSync(fixture.lock, { recursive: true }) + writeFileSync(join(fixture.lock, 'owner'), 'foreign-owner\n') + const result = run(fixture, { FAKE_NPM_LATEST: '2.0.0' }) + assert.equal(result.status, 0, result.stderr) + assert.match(fixture.log(), /skip: another update run is active \(lock held\)/) + assert.equal(fixture.traceLines().some((line) => !line.startsWith('view ')), false) + assert.equal(readFileSync(join(fixture.lock, 'owner'), 'utf8'), 'foreign-owner\n') + // The run that owns the lock stamps the outcome; a skipped one must not. + assert.equal(existsSync(fixture.stamp), false) +}) + +test('updates applied by another run while checking are not re-applied', { skip }, (t) => { + const fixture = makeFixture(t) + const result = run(fixture, { FAKE_NPM_LATEST: '2.0.0', FAKE_NPM_BUMP_ON_VIEW: '1' }) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), TARGETS.map(() => 'view unlocked')) + assert.match(fixture.log(), /done: all dependencies up-to-date/) + assert.equal(existsSync(fixture.stamp), true) + assert.equal(existsSync(fixture.lock), false) +}) + +test('an interrupted update is re-tested under the lock before checking', { skip }, (t) => { + const fixture = makeFixture(t) + mkdirSync(fixture.logs, { recursive: true }) + writeFileSync(fixture.marker, '{"pid":1,"specs":[]}') + const result = run(fixture) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), [ + 'test locked', + ...TARGETS.map(() => 'view unlocked'), + ]) + assert.match(fixture.log(), /interrupted update accepted: tests pass/) + assert.equal(existsSync(fixture.marker), false) + assert.equal(existsSync(fixture.lock), false) +}) + +test('an update interrupted during the check is re-tested before stamping', { skip }, (t) => { + const fixture = makeFixture(t) + // No marker when the run starts; another run's apply phase leaves one + // (installed versions already current) while `npm view` is running. + const result = run(fixture, { FAKE_NPM_BUMP_ON_VIEW: '1', FAKE_NPM_MARK_ON_VIEW: '1' }) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), [ + ...TARGETS.map(() => 'view unlocked'), + 'test locked', + ]) + assert.match(fixture.log(), /interrupted update accepted: tests pass/) + assert.equal(existsSync(fixture.marker), false) + assert.equal(existsSync(fixture.lock), false) + assert.equal(existsSync(fixture.stamp), true) +}) + +test('an update interrupted during the check is not stamped while its lock is held', { skip }, (t) => { + const fixture = makeFixture(t) + mkdirSync(fixture.lock, { recursive: true }) + writeFileSync(join(fixture.lock, 'owner'), 'foreign-owner\n') + const result = run(fixture, { FAKE_NPM_MARK_ON_VIEW: '1' }) + assert.equal(result.status, 0, result.stderr) + assert.equal(fixture.traceLines().length, TARGETS.length) + assert.equal(fixture.traceLines().some((line) => !line.startsWith('view ')), false) + assert.match(fixture.log(), /skip: another update run is active \(lock held\)/) + // The untested state belongs to the lock holder; a 24h stamp here would + // hide the marker from every later run. + assert.equal(existsSync(fixture.stamp), false) + assert.equal(existsSync(fixture.marker), true) + assert.equal(readFileSync(join(fixture.lock, 'owner'), 'utf8'), 'foreign-owner\n') +}) + +test('a failed test gate rolls back under the lock and releases it', { skip }, (t) => { + const fixture = makeFixture(t) + const result = run(fixture, { FAKE_NPM_LATEST: '2.0.0', FAKE_NPM_TEST_RC: '1' }) + assert.equal(result.status, 1, result.stderr) + assert.deepEqual(fixture.traceLines(), [ + ...TARGETS.map(() => 'view unlocked'), + 'install locked', + 'test locked', + 'ci locked', + ]) + assert.match(fixture.log(), /npm test FAIL after update/) + assert.equal(existsSync(fixture.marker), false) + assert.equal(existsSync(fixture.lock), false) +}) + +test('an interrupted update bypasses the 24h throttle on the next start', { skip }, (t) => { + // A run that saw no marker can stamp 24h just after another run's apply + // phase left one and was killed. The next (throttled) start must still + // re-test it, without running the throttled registry check. + const fixture = makeFixture(t) + mkdirSync(fixture.logs, { recursive: true }) + const nextAllowed = String(Date.now() + 24 * 60 * 60 * 1000) + writeFileSync(fixture.stamp, nextAllowed) + writeFileSync(fixture.marker, '{"pid":1,"specs":[]}') + const result = spawnSync(process.execPath, [fixture.updater], { + encoding: 'utf8', + env: fixture.env(), + timeout: RUN_TIMEOUT_MS, + }) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), ['test locked']) + assert.match(fixture.log(), /interrupted update accepted: tests pass/) + assert.equal(existsSync(fixture.marker), false) + assert.equal(existsSync(fixture.lock), false) + assert.equal(readFileSync(fixture.stamp, 'utf8'), nextAllowed) +}) + +test('a throttled start without a marker does nothing', { skip }, (t) => { + const fixture = makeFixture(t) + mkdirSync(fixture.logs, { recursive: true }) + writeFileSync(fixture.stamp, String(Date.now() + 24 * 60 * 60 * 1000)) + const result = spawnSync(process.execPath, [fixture.updater], { + encoding: 'utf8', + env: fixture.env(), + timeout: RUN_TIMEOUT_MS, + }) + assert.equal(result.status, 0, result.stderr) + assert.deepEqual(fixture.traceLines(), []) + assert.equal(existsSync(fixture.lock), false) +}) diff --git a/tests/run-scenarios.sh b/tests/run-scenarios.sh index 0d8b347..f8cec53 100644 --- a/tests/run-scenarios.sh +++ b/tests/run-scenarios.sh @@ -1009,6 +1009,119 @@ test_update_progress_output() { teardown_test_env } +# --- 30b-30d. web-content-extraction dependency lock --- +# Shared fixture: a standard install (skills enabled) whose live and snapshot +# settings.json carry the same marker the current kit does not produce, and +# whose manifest claims an older kit. A completed update removes the marker +# from both and rewrites the manifest; an update that stops before writing +# leaves the whole tree byte-identical. +setup_wce_lock_scenario() { + run_setup --profile=standard --fonts=false --ghostty=false >/dev/null 2>&1 \ + || return 1 + local snapshot_settings="$CLAUDE_DIR/.starter-kit-snapshot/settings.json" + local manifest="$CLAUDE_DIR/.starter-kit-manifest.json" + jq '.old_kit_marker = true' "$snapshot_settings" > "$snapshot_settings.tmp" \ + && mv "$snapshot_settings.tmp" "$snapshot_settings" || return 1 + cp "$snapshot_settings" "$CLAUDE_DIR/settings.json" || return 1 + jq '.kit_version = "v0.0.0-wce-lock-fixture"' "$manifest" > "$manifest.tmp" \ + && mv "$manifest.tmp" "$manifest" || return 1 + mkdir -p "$CLAUDE_DIR/skills/web-content-extraction/logs/.update.lock" +} + +count_claude_backups() { + find "$HOME" -maxdepth 1 -name '.claude.backup.*' -print | wc -l | tr -d '[:space:]' +} + +# The owner is not a token the kit recognizes, so nothing may reclaim it. The +# update must stop before the backup and before Step 1 instead of rewriting +# settings.json / CLAUDE.md and then exiting 75 without a word. +test_update_blocked_wce_lock() { + setup_test_env + setup_wce_lock_scenario \ + || { fail "update-blocked-wce-lock (setup failed)"; teardown_test_env; return; } + local lock="$CLAUDE_DIR/skills/web-content-extraction/logs/.update.lock" + printf 'foreign-updater\n' > "$lock/owner" + + local before after backups_before backups_after output rc=0 + before="$(snapshot_dir_checksum "$CLAUDE_DIR")" + backups_before="$(count_claude_backups)" + output="$(run_setup_update 2>&1)" || rc=$? + after="$(snapshot_dir_checksum "$CLAUDE_DIR")" + backups_after="$(count_claude_backups)" + + if [[ $rc -eq 75 ]] \ + && grep -qF "$lock" < <(printf '%s\n' "$output") \ + && ! grep -q "Step 1/5" < <(printf '%s\n' "$output") \ + && [[ "$before" == "$after" ]] \ + && [[ "$backups_before" == "$backups_after" ]] \ + && [[ "$(cat "$lock/owner")" == "foreign-updater" ]]; then + pass "update-blocked-wce-lock" + else + fail "update-blocked-wce-lock (rc=$rc, changed=$([[ "$before" != "$after" ]] && echo yes || echo no), backups=$backups_before->$backups_after)" + fi + + teardown_test_env +} + +# The owner is a dependency-updater token whose process no longer exists and +# the lock is old: the update recovers it and then runs to completion. +test_update_recovers_stale_wce_lock() { + setup_test_env + setup_wce_lock_scenario \ + || { fail "update-recovers-stale-wce-lock (setup failed)"; teardown_test_env; return; } + local logs="$CLAUDE_DIR/skills/web-content-extraction/logs" + local lock="$logs/.update.lock" dead_pid + sh -c 'exit 0' & + dead_pid=$! + wait "$dead_pid" 2>/dev/null || true + printf '%s:33613976-dc7b-422d-aa54-8c4cb5ab84fb\n' "$dead_pid" > "$lock/owner" + touch -t 200001010000 "$lock/owner" + + local output rc=0 residue + output="$(run_setup_update 2>&1)" || rc=$? + residue="$(find "$logs" -maxdepth 1 -name '.update.lock*' -print | wc -l | tr -d '[:space:]')" + + if [[ $rc -eq 0 ]] \ + && grep -qF "$lock" < <(printf '%s\n' "$output") \ + && grep -q "Step 5/5:" < <(printf '%s\n' "$output") \ + && [[ "$residue" == "0" ]] \ + && ! jq -e '.old_kit_marker' "$CLAUDE_DIR/settings.json" >/dev/null 2>&1 \ + && ! jq -e '.old_kit_marker' "$CLAUDE_DIR/.starter-kit-snapshot/settings.json" >/dev/null 2>&1 \ + && [[ "$(jq -r '.kit_version' "$CLAUDE_DIR/.starter-kit-manifest.json")" != "v0.0.0-wce-lock-fixture" ]]; then + pass "update-recovers-stale-wce-lock" + else + fail "update-recovers-stale-wce-lock (rc=$rc, residue=$residue)" + fi + + teardown_test_env +} + +# Dry-run simulates in a temp dir that has no lock, so it used to promise a +# clean update the real run could not deliver. It must report the real lock +# without touching it, and still exit 0. +test_update_dry_run_warns_wce_lock() { + setup_test_env + setup_wce_lock_scenario \ + || { fail "update-dry-run-warns-wce-lock (setup failed)"; teardown_test_env; return; } + local lock="$CLAUDE_DIR/skills/web-content-extraction/logs/.update.lock" + printf 'foreign-updater\n' > "$lock/owner" + + local before after output rc=0 + before="$(snapshot_dir_checksum "$CLAUDE_DIR")" + output="$(run_setup_update --dry-run 2>&1)" || rc=$? + after="$(snapshot_dir_checksum "$CLAUDE_DIR")" + + if [[ $rc -eq 0 ]] \ + && grep -qF "$lock" < <(printf '%s\n' "$output") \ + && [[ "$before" == "$after" ]]; then + pass "update-dry-run-warns-wce-lock" + else + fail "update-dry-run-warns-wce-lock (rc=$rc, changed=$([[ "$before" != "$after" ]] && echo yes || echo no))" + fi + + teardown_test_env +} + # --- 32. dry-run-progress-output --- test_dry_run_progress_output() { setup_test_env @@ -1584,6 +1697,9 @@ run_scenario update test_update_from_no_manifest run_scenario update test_update_v019_to_latest_direct run_scenario update test_update_partial_failure_recovery run_scenario update test_update_progress_output +run_scenario update test_update_blocked_wce_lock +run_scenario update test_update_recovers_stale_wce_lock +run_scenario update test_update_dry_run_warns_wce_lock run_scenario update test_auto_update_session_hooks run_scenario update test_auto_update_old_claude_same_hooks run_scenario update test_auto_update_preserves_custom_config_binding diff --git a/tests/unit/test-update-refactor.sh b/tests/unit/test-update-refactor.sh index 5ff04ba..bd93928 100644 --- a/tests/unit/test-update-refactor.sh +++ b/tests/unit/test-update-refactor.sh @@ -393,6 +393,99 @@ source "$PROJECT_DIR/lib/update.sh" fi } +{ + # A run that rewrote the CLAUDE.md kit section and then stopped before the + # snapshot phase (the dependency lock after Step 2) leaves current == new + # kit with an older snapshot. That is not a user edit: the next update must + # keep the content and refresh the baseline, not warn and keep a stale + # snapshot forever. A kit section that really differs still warns. + test_name="update: CLAUDE.md kit section already at the new content refreshes the snapshot" + _ucm_tmp="$(mktemp -d)" + _SETUP_TMP_FILES+=("$_ucm_tmp") + _ucm_write() { # [user-body] + { + printf '\n%s\n\n' "$2" + printf '\n# User Settings\n\n%s\n' "${3:-}" + } > "$1" + } + _ucm_write "$_ucm_tmp/new-kit.md" "kit v2" + _ucm_write "$_ucm_tmp/old-kit.md" "kit v1" + _extract_kit_section "$_ucm_tmp/old-kit.md" > "$_ucm_tmp/snapshot-kit" + _ucm_write "$_ucm_tmp/current.md" "kit v2" "my rule" + cp "$_ucm_tmp/current.md" "$_ucm_tmp/current.saved" + _ucm_write "$_ucm_tmp/edited.md" "kit v1 with my edit" "my rule" + cp "$_ucm_tmp/edited.md" "$_ucm_tmp/edited.saved" + _ucm_run() { # + local rc=0 + ( + # shellcheck disable=SC2034 # consumed indirectly by the function under test + KIT_MDM_MANAGED=false _MERGE_INTERACTIVE=false + # shellcheck disable=SC2034 # consumed indirectly by the function under test + STR_CLAUDEMD_KIT_CONFLICT_KEPT=conflict-kept-marker + _update_claude_md "$1" "$_ucm_tmp/snapshot-kit" "$_ucm_tmp/new-kit.md" + ) > "$2" 2>&1 || rc=$? + printf '%s' "$rc" + } + _ucm_rc="$(_ucm_run "$_ucm_tmp/current.md" "$_ucm_tmp/current.out")" + _ucm_edited_rc="$(_ucm_run "$_ucm_tmp/edited.md" "$_ucm_tmp/edited.out")" + if [[ "$_ucm_rc" == 3 ]] \ + && cmp -s "$_ucm_tmp/current.md" "$_ucm_tmp/current.saved" \ + && ! grep -q conflict-kept-marker "$_ucm_tmp/current.out" \ + && [[ "$_ucm_edited_rc" == 1 ]] \ + && cmp -s "$_ucm_tmp/edited.md" "$_ucm_tmp/edited.saved" \ + && grep -q conflict-kept-marker "$_ucm_tmp/edited.out"; then + pass "$test_name" + else + fail "$test_name (current rc=$_ucm_rc edited rc=$_ucm_edited_rc)" + fi + + # Through the phases: no "updated" or conflict message, content untouched, + # and the snapshot now holds the new kit section. + test_name="update: CLAUDE.md baseline-only refresh reaches the snapshot phase" + _ucm_claude="$_ucm_tmp/claude" + mkdir -p "$_ucm_claude/.starter-kit-snapshot" + cp "$_ucm_tmp/current.md" "$_ucm_claude/CLAUDE.md" + cp "$_ucm_tmp/snapshot-kit" "$_ucm_claude/.starter-kit-snapshot/CLAUDE.md" + _extract_kit_section "$_ucm_tmp/new-kit.md" > "$_ucm_tmp/expected-snapshot" + _ucm_phase_rc=0 + ( + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + KIT_MDM_MANAGED=false _MERGE_INTERACTIVE=false DRY_RUN=false + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_UPDATE_CLAUDEMD="claude-md" STR_UPDATE_SNAPSHOT="snapshot" + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_UPDATE_SNAPSHOT_DONE="snapshot-done" + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_CLAUDEMD_KIT_UPDATED="kit-updated-marker" + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_CLAUDEMD_KIT_UNCHANGED="kit-unchanged-marker" + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_CLAUDEMD_KIT_CONFLICT_KEPT="conflict-kept-marker" + # shellcheck disable=SC2034 # consumed indirectly by the phases under test + STR_CLAUDEMD_KIT_BASELINE_REFRESHED="baseline-refreshed-marker" + _progress_step() { :; } + build_claude_md_to_file() { cp "$_ucm_tmp/new-kit.md" "$1"; } + _claude_md_user_section_has_content() { return 1; } + _UPDATE_ALL_UPDATED_FILES=() + _UPDATE_ALL_SKIPPED_FILES=() + _update_phase_claude_md "$_ucm_claude" "$_ucm_claude/.starter-kit-snapshot" + _update_phase_snapshot "$_ucm_claude" "$_ucm_claude/.starter-kit-snapshot" + ) > "$_ucm_tmp/phase.out" 2>&1 || _ucm_phase_rc=$? + if [[ "$_ucm_phase_rc" -eq 0 ]] \ + && cmp -s "$_ucm_claude/CLAUDE.md" "$_ucm_tmp/current.saved" \ + && cmp -s "$_ucm_claude/.starter-kit-snapshot/CLAUDE.md" \ + "$_ucm_tmp/expected-snapshot" \ + && grep -q baseline-refreshed-marker "$_ucm_tmp/phase.out" \ + && ! grep -q kit-updated-marker "$_ucm_tmp/phase.out" \ + && ! grep -q conflict-kept-marker "$_ucm_tmp/phase.out"; then + pass "$test_name" + else + echo " rc=$_ucm_phase_rc" >&2 + sed 's/^/ | /' "$_ucm_tmp/phase.out" >&2 || true + fail "$test_name" + fi +} + { test_name="update: user-section content detection drives the rules/user-* tip" _ust_tmp="$(mktemp -d)" diff --git a/tests/unit/test-wce-update-lock.sh b/tests/unit/test-wce-update-lock.sh index a1aecd0..44f8288 100644 --- a/tests/unit/test-wce-update-lock.sh +++ b/tests/unit/test-wce-update-lock.sh @@ -144,9 +144,10 @@ _wul_outer_callback() { printf '%s:%s\n' "$direct_rc" "$child_rc" > "$2" } _wul_reentry_rc=0 +# The child attempt is expected to lose; its contention report is not asserted. _wce_with_runtime_update_lock "$_wul_reentry" \ _wul_outer_callback "$_wul_reentry" "$_wul_reentry_result" \ - || _wul_reentry_rc=$? + 2>/dev/null || _wul_reentry_rc=$? if [[ "$_wul_reentry_rc" -eq 0 \ && "$(< "$_wul_reentry_result")" == 0:75 \ && ! -e "$_wul_reentry/logs/.update.lock" ]]; then @@ -242,7 +243,7 @@ _RESET_MERGE_PREFS=false STR_UPDATE_TITLE="Update" _wul_update_rc=0 run_update "$_wul_update_project" "$_wul_update_home" \ - >/dev/null 2>&1 || _wul_update_rc=$? + > "$_wul_tmp/update-contention.out" 2>&1 || _wul_update_rc=$? eval "$_wul_phase_settings_saved" eval "$_wul_phase_claude_saved" eval "$_wul_tail_saved" @@ -258,6 +259,16 @@ if [[ "$_wul_update_rc" -eq 75 ]] \ else fail "WCE update: contention exposed a partial subtree transaction" fi +# Exit 75 at this point used to be completely silent. It must name the lock and +# say that settings.json / CLAUDE.md were already processed by this run. +if grep -Fq \ + "$_wul_update_home/skills/web-content-extraction/logs/.update.lock" \ + "$_wul_tmp/update-contention.out" \ + && grep -q 'settings\.json' "$_wul_tmp/update-contention.out"; then + pass "WCE update: tail contention names the lock and the partial state" +else + fail "WCE update: tail contention exited 75 without a diagnosis" +fi # Fresh/full and merge-aware deployment must contend before backup or any live # or snapshot write. Exercise both branch selectors with the same production @@ -688,4 +699,1129 @@ else fail "WCE package pair: staged no-op left a stale valid baseline" fi +# --------------------------------------------------------------------------- +# Diagnosis, pre-write preflight, and bounded recovery of an abandoned lock. +# +# A dependency updater killed while holding the lock used to leave it forever: +# every later update wrote settings.json / CLAUDE.md and a full backup, then +# exited 75 without a word. The low-level acquire must still refuse every +# existing lock; only the preflight may recover one, and only when its owner +# is a recognized token whose process is provably gone and the lock is old. +# --------------------------------------------------------------------------- +_wul_uuid="33613976-dc7b-422d-aa54-8c4cb5ab84fb" +_wul_uuid_other="0f0e0d0c-0b0a-4908-8706-050403020100" + +_wul_dead_pid() { # prints the PID of a process that has already exited + local pid + sh -c 'exit 0' & + pid=$! + wait "$pid" 2>/dev/null || true + printf '%s' "$pid" +} + +_wul_make_lock() { # [old] + mkdir -p "$1/logs/.update.lock" + printf '%s\n' "$2" > "$1/logs/.update.lock/owner" + if [[ "${3:-}" == old ]]; then + touch -t 200001010000 "$1/logs/.update.lock/owner" + fi +} + +_wul_lock_state() { # + ( + _WCE_LOCK_STATE="" + _wce_runtime_update_lock_inspect "$1" >/dev/null 2>&1 + printf '%s' "$_WCE_LOCK_STATE" + ) 2>/dev/null || true +} + +_wul_lock_residue() { # : true while reclaim scaffolding remains + compgen -G "$1/logs/.update.lock.*" >/dev/null +} + +_wul_preflight() { # [wait-seconds] [msys|mdm] + local rc=0 + ( + _WCE_RUNTIME_LOCK_WAIT_SECONDS="${3:-0}" + KIT_MDM_MANAGED=false + case "${4:-}" in + msys) is_msys() { return 0; } ;; + mdm) KIT_MDM_MANAGED=true ;; + esac + _wce_runtime_update_lock_preflight "$1" + ) > "$2" 2>&1 || rc=$? + printf '%s' "$rc" +} + +sleep 120 >/dev/null 2>&1 & +_wul_live_pid=$! +_wul_dead="$(_wul_dead_pid)" +_wul_dead_other="$(_wul_dead_pid)" + +# --- Classification: free / busy / stale / unknown --------------------------- +_wul_inspect="$_wul_tmp/inspect" +mkdir -p "$_wul_inspect/free" +_wul_make_lock "$_wul_inspect/node-live" "$_wul_live_pid:$_wul_uuid" +_wul_make_lock "$_wul_inspect/node-dead" "$_wul_dead:$_wul_uuid" +_wul_make_lock "$_wul_inspect/kit-live" \ + "starter-kit-update-$_wul_live_pid-1-1700000000" +_wul_make_lock "$_wul_inspect/kit-dead" \ + "starter-kit-update-$_wul_dead-1-1700000000" +# PID 1 always exists but belongs to another user: `kill -0` fails for it +# exactly as it does for a missing PID, so it must not be classified as gone. +_wul_make_lock "$_wul_inspect/pid1" "1:$_wul_uuid" +_wul_make_lock "$_wul_inspect/foreign" foreign-updater +_wul_make_lock "$_wul_inspect/zero-pid" "0:$_wul_uuid" +_wul_make_lock "$_wul_inspect/uppercase-uuid" \ + "$_wul_dead:33613976-DC7B-422D-AA54-8C4CB5AB84FB" +mkdir -p "$_wul_inspect/empty/logs/.update.lock" +_wul_make_lock "$_wul_inspect/two-lines" "$_wul_dead:$_wul_uuid" +printf 'second-line\n' >> "$_wul_inspect/two-lines/logs/.update.lock/owner" +mkdir -p "$_wul_inspect/no-newline/logs/.update.lock" +printf '%s' "$_wul_dead:$_wul_uuid" \ + > "$_wul_inspect/no-newline/logs/.update.lock/owner" +mkdir -p "$_wul_inspect/oversized/logs/.update.lock" +printf '%0129d\n' 0 > "$_wul_inspect/oversized/logs/.update.lock/owner" +_wul_make_lock "$_wul_inspect/extra-entry" "$_wul_dead:$_wul_uuid" +: > "$_wul_inspect/extra-entry/logs/.update.lock/npm-in-flight" +mkdir -p "$_wul_inspect/symlink-lock/logs" "$_wul_inspect/symlink-target" +printf '%s\n' "$_wul_dead:$_wul_uuid" > "$_wul_inspect/symlink-target/owner" +ln -s "$_wul_inspect/symlink-target" \ + "$_wul_inspect/symlink-lock/logs/.update.lock" +mkdir -p "$_wul_inspect/symlink-owner/logs/.update.lock" +ln -s "$_wul_inspect/symlink-target/owner" \ + "$_wul_inspect/symlink-owner/logs/.update.lock/owner" +mkdir -p "$_wul_inspect/regular-file/logs" +printf '%s\n' "$_wul_dead:$_wul_uuid" \ + > "$_wul_inspect/regular-file/logs/.update.lock" +mkdir -p "$_wul_inspect/symlink-logs" "$_wul_inspect/symlink-logs-target" +ln -s "$_wul_inspect/symlink-logs-target" "$_wul_inspect/symlink-logs/logs" + +_wul_states="" +for _wul_case in free node-live node-dead kit-live kit-dead pid1 foreign \ + zero-pid uppercase-uuid empty two-lines no-newline oversized extra-entry \ + symlink-lock symlink-owner regular-file symlink-logs; do + _wul_states+="$_wul_case=$(_wul_lock_state "$_wul_inspect/$_wul_case") " +done +_wul_states_expected="free=free node-live=busy node-dead=stale kit-live=busy" +_wul_states_expected+=" kit-dead=stale pid1=busy foreign=unknown" +_wul_states_expected+=" zero-pid=unknown uppercase-uuid=unknown empty=unknown" +_wul_states_expected+=" two-lines=unknown no-newline=unknown oversized=unknown" +_wul_states_expected+=" extra-entry=unknown symlink-lock=unusable" +_wul_states_expected+=" symlink-owner=unknown regular-file=unusable" +_wul_states_expected+=" symlink-logs=unusable " +if [[ "$_wul_states" == "$_wul_states_expected" ]]; then + pass "WCE lock inspect: only a recognized owner with a missing PID is stale" +else + echo " expected: $_wul_states_expected" >&2 + echo " got: $_wul_states" >&2 + fail "WCE lock inspect: a lock shape or owner was misclassified" +fi + +# A symlinked or non-directory path holds no lock. The diagnosis must name +# that path and must not offer `rm -f /owner && rmdir `: through a +# symlinked lock, that command would delete the link target's owner file. +_wul_unusable_ok=true +for _wul_case in symlink-lock symlink-logs regular-file; do + _wul_unusable_out="$_wul_tmp/unusable-$_wul_case.out" + _wul_unusable_rc="$(_wul_preflight "$_wul_inspect/$_wul_case" \ + "$_wul_unusable_out")" + case "$_wul_case" in + symlink-lock|regular-file) + _wul_unusable_path="$_wul_inspect/$_wul_case/logs/.update.lock" ;; + symlink-logs) + _wul_unusable_path="$_wul_inspect/$_wul_case/logs" ;; + esac + [[ "$_wul_unusable_rc" == 75 ]] || _wul_unusable_ok=false + grep -Fq "$_wul_unusable_path" "$_wul_unusable_out" || _wul_unusable_ok=false + if grep -Eq 'rm -f .*(&&|;) *rmdir' "$_wul_unusable_out"; then + _wul_unusable_ok=false + fi + if [[ "$_wul_unusable_ok" != true ]]; then + echo " case=$_wul_case rc=$_wul_unusable_rc" >&2 + sed 's/^/ | /' "$_wul_unusable_out" >&2 || true + break + fi +done +if [[ "$_wul_unusable_ok" == true ]] \ + && [[ -L "$_wul_inspect/symlink-lock/logs/.update.lock" ]] \ + && grep -qx "$_wul_dead:$_wul_uuid" "$_wul_inspect/symlink-target/owner"; then + pass "WCE preflight: a symlink or non-directory path is named, with no lock removal offered" +else + fail "WCE preflight: an unusable path was reported as a held lock, or a removal command was offered" +fi + +# --- Preflight recovers a provably abandoned, old lock ------------------------ +for _wul_case in node kit; do + _wul_reclaim="$_wul_tmp/reclaim-$_wul_case" + if [[ "$_wul_case" == node ]]; then + _wul_make_lock "$_wul_reclaim" "$_wul_dead:$_wul_uuid" old + else + _wul_make_lock "$_wul_reclaim" \ + "starter-kit-update-$_wul_dead-1-1700000000" old + fi + _wul_reclaim_rc="$(_wul_preflight "$_wul_reclaim" "$_wul_reclaim.out")" + if [[ "$_wul_reclaim_rc" == 0 ]] \ + && [[ ! -e "$_wul_reclaim/logs/.update.lock" \ + && ! -L "$_wul_reclaim/logs/.update.lock" ]] \ + && ! _wul_lock_residue "$_wul_reclaim" \ + && grep -Fq "$_wul_dead" "$_wul_reclaim.out"; then + pass "WCE preflight: an old $_wul_case lock whose owner is gone is recovered" + else + echo " rc=$_wul_reclaim_rc" >&2 + sed 's/^/ | /' "$_wul_reclaim.out" >&2 || true + fail "WCE preflight: an abandoned $_wul_case lock was not recovered cleanly" + fi +done + +# --- Preflight refuses everything else, byte for byte ------------------------- +_wul_check_blocked() { # [needle...] + local name="$1" dir="$2" mode="$3" needle ok=true rc + local out="$_wul_tmp/blocked-$1.out" saved="$_wul_tmp/blocked-$1.owner" + shift 3 + cp -p "$dir/logs/.update.lock/owner" "$saved" + rc="$(_wul_preflight "$dir" "$out" 0 "$mode")" + [[ "$rc" == 75 ]] || ok=false + cmp -s "$dir/logs/.update.lock/owner" "$saved" || ok=false + [[ ! "$dir/logs/.update.lock/owner" -nt "$saved" ]] || ok=false + grep -Fq "$dir/logs/.update.lock" "$out" || ok=false + for needle in "$@"; do + grep -Fq -- "$needle" "$out" || ok=false + done + compgen -G "$dir/logs/.update.lock.stale-*" >/dev/null && ok=false + if [[ "$ok" == true ]]; then + pass "WCE preflight: $name stops with a diagnosis and is left untouched" + else + echo " rc=$rc" >&2 + sed 's/^/ | /' "$out" >&2 || true + fail "WCE preflight: $name was reclaimed, changed, or reported silently" + fi +} + +_wul_make_lock "$_wul_tmp/blocked-recent" "$_wul_dead:$_wul_uuid" +_wul_check_blocked "a recently abandoned lock" "$_wul_tmp/blocked-recent" "" \ + "$_wul_dead" rmdir + +# The age threshold is 60 minutes (_WCE_RUNTIME_LOCK_STALE_MINUTES). Bind it +# from both sides: 59 minutes is kept, 61 minutes is recovered. BSD find +# rounds the age up and GNU find truncates it, so both sides stay a full +# minute away from the threshold. +_wul_touch_minutes_ago() { # + local stamp="" + stamp="$(date -v "-$2M" '+%Y%m%d%H%M.%S' 2>/dev/null)" \ + || stamp="$(date -d "$2 minutes ago" '+%Y%m%d%H%M.%S' 2>/dev/null)" \ + || return 1 + touch -t "$stamp" "$1" +} +_wul_make_lock "$_wul_tmp/blocked-59min" "$_wul_dead:$_wul_uuid" +if _wul_touch_minutes_ago "$_wul_tmp/blocked-59min/logs/.update.lock/owner" 59; then + _wul_check_blocked "an abandoned lock 59 minutes old" \ + "$_wul_tmp/blocked-59min" "" "$_wul_dead" rmdir +else + fail "WCE preflight: could not set a 59-minute-old owner mtime" +fi +_wul_edge="$_wul_tmp/reclaim-61min" +_wul_make_lock "$_wul_edge" "$_wul_dead:$_wul_uuid" +_wul_edge_rc=1 +if _wul_touch_minutes_ago "$_wul_edge/logs/.update.lock/owner" 61; then + _wul_edge_rc="$(_wul_preflight "$_wul_edge" "$_wul_edge.out")" +fi +if [[ "$_wul_edge_rc" == 0 ]] \ + && [[ ! -e "$_wul_edge/logs/.update.lock" ]] \ + && ! _wul_lock_residue "$_wul_edge" \ + && grep -Fq "$_wul_dead" "$_wul_edge.out"; then + pass "WCE preflight: an abandoned lock 61 minutes old is recovered" +else + echo " rc=$_wul_edge_rc" >&2 + sed 's/^/ | /' "$_wul_edge.out" >&2 || true + fail "WCE preflight: an abandoned lock just past the 60-minute threshold was kept" +fi + +_wul_make_lock "$_wul_tmp/blocked-foreign" foreign-updater old +_wul_check_blocked "an unverifiable owner" "$_wul_tmp/blocked-foreign" "" \ + foreign-updater rmdir + +_wul_make_lock "$_wul_tmp/blocked-live" "$_wul_live_pid:$_wul_uuid" old +_wul_check_blocked "a running owner" "$_wul_tmp/blocked-live" "" \ + "$_wul_live_pid" + +_wul_make_lock "$_wul_tmp/blocked-pid1" "1:$_wul_uuid" old +_wul_check_blocked "a PID that cannot be signalled" "$_wul_tmp/blocked-pid1" "" + +_wul_make_lock "$_wul_tmp/blocked-msys" "$_wul_dead:$_wul_uuid" old +_wul_check_blocked "a lock on MSYS" "$_wul_tmp/blocked-msys" msys rmdir + +_wul_make_lock "$_wul_tmp/blocked-mdm" "$_wul_dead:$_wul_uuid" old +_wul_check_blocked "a lock under MDM" "$_wul_tmp/blocked-mdm" mdm + +_wul_make_lock "$_wul_tmp/blocked-mutex" "$_wul_dead:$_wul_uuid" old +mkdir "$_wul_tmp/blocked-mutex/logs/.update.lock.reclaim" +_wul_check_blocked "a lock with a leftover reclaim mutex" \ + "$_wul_tmp/blocked-mutex" "" ".update.lock.reclaim" +if [[ -d "$_wul_tmp/blocked-mutex/logs/.update.lock.reclaim" ]]; then + pass "WCE preflight: a reclaim mutex it does not own is never removed" +else + fail "WCE preflight: a foreign reclaim mutex was removed" +fi + +# Acquisition fails while the reclaim mutex exists (see the reclaimer: only +# then can it be sure no writer completed an acquisition after it took the +# mutex). A mutex left by a killed reclaimer therefore blocks the lock even +# with the canonical name free. It also outlasts the withdrawal wait, which +# does not prove the reclaimer dead, so the attempt leaves its own lock in +# place rather than renaming anything. +_wul_mutex_acq="$_wul_tmp/mutex-only-acquire" +mkdir -p "$_wul_mutex_acq/logs/.update.lock.reclaim" +_wul_mutex_acq_rc=0 +_wce_with_runtime_update_lock "$_wul_mutex_acq" true \ + >/dev/null 2> "$_wul_mutex_acq.err" || _wul_mutex_acq_rc=$? +if [[ "$_wul_mutex_acq_rc" -eq 75 ]] \ + && [[ -d "$_wul_mutex_acq/logs/.update.lock.reclaim" ]] \ + && grep -Eqx 'starter-kit-update-[0-9]+-[0-9]+-[0-9]+' \ + "$_wul_mutex_acq/logs/.update.lock/owner" \ + && [[ "$(command ls -A "$_wul_mutex_acq/logs" | LC_ALL=C sort | tr '\n' ' ')" \ + == ".update.lock .update.lock.reclaim " ]] \ + && grep -Fq ".update.lock.reclaim" "$_wul_mutex_acq.err"; then + pass "WCE lock: acquisition backs off while a reclaim mutex exists" +else + echo " rc=$_wul_mutex_acq_rc" >&2 + sed 's/^/ | /' "$_wul_mutex_acq.err" >&2 || true + fail "WCE lock: a lock was taken beside a reclaim mutex, or the mutex was not named" +fi + +# The preflight runs before any acquisition and must report a leftover mutex +# instead of passing a run through to a later 75 after the backup. +_wul_mutex_only="$_wul_tmp/mutex-only" +mkdir -p "$_wul_mutex_only/logs/.update.lock.reclaim" +_wul_mutex_only_pre_rc="$(_wul_preflight "$_wul_mutex_only" "$_wul_mutex_only.out")" +if [[ "$_wul_mutex_only_pre_rc" == 75 ]] \ + && grep -Fq ".update.lock.reclaim" "$_wul_mutex_only.out" \ + && ! grep -Eq 'rm -f .*(&&|;) *rmdir' "$_wul_mutex_only.out" \ + && [[ -d "$_wul_mutex_only/logs/.update.lock.reclaim" ]]; then + pass "WCE preflight: a leftover reclaim mutex with no lock is reported, not passed" +else + echo " rc=$_wul_mutex_only_pre_rc" >&2 + sed 's/^/ | /' "$_wul_mutex_only.out" >&2 || true + fail "WCE preflight: a leftover reclaim mutex was passed through or misreported" +fi + +# A whole recovery can run between the owner write and the mutex check: the +# reclaimer moves this lock aside, cannot put it back because writer B took +# the canonical name, and releases the mutex. The mutex check then passes, so +# acquisition must still fail on the canonical owner (B's), and must leave +# both B's lock and the displaced directory alone. +_wul_displaced="$_wul_tmp/acquire-displaced" +_wul_displaced_lock="$_wul_displaced/logs/.update.lock" +mkdir -p "$_wul_displaced/logs" +_wul_displaced_rc=0 +( + printf() { + builtin printf "$@" + if [[ "${1:-}" == '%s\n' && "${2:-}" == starter-kit-update-* \ + && ! -e "$_wul_displaced/injected" ]]; then + : > "$_wul_displaced/injected" + command mv "$_wul_displaced_lock" "$_wul_displaced_lock.stale-test" + command mkdir "$_wul_displaced_lock" + builtin printf 'writer-b\n' > "$_wul_displaced_lock/owner" + fi + } + _wul_displaced_token="" + _wce_runtime_update_lock_acquire "$_wul_displaced" _wul_displaced_token \ + || exit 1 + [[ -z "$_wul_displaced_token" ]] || exit 2 + exit 0 +) >/dev/null 2>&1 || _wul_displaced_rc=$? +if [[ "$_wul_displaced_rc" -eq 1 && -e "$_wul_displaced/injected" ]] \ + && grep -qx writer-b "$_wul_displaced_lock/owner" \ + && grep -Eqx 'starter-kit-update-[0-9]+-[0-9]+-[0-9]+' \ + "$_wul_displaced_lock.stale-test/owner"; then + pass "WCE lock: acquisition fails when a recovery displaced it before the mutex check" +else + echo " rc=$_wul_displaced_rc" >&2 + fail "WCE lock: a displaced acquisition succeeded or disturbed the canonical owner" +fi + +# The mutex check sees the recovery, but before the withdrawal runs the +# recovery moves this lock aside and writer B takes the canonical name. The +# withdrawal must check the token and leave B's lock in place. +_wul_withdraw="$_wul_tmp/acquire-withdraw" +_wul_withdraw_lock="$_wul_withdraw/logs/.update.lock" +mkdir -p "$_wul_withdraw/logs" +_wul_withdraw_rc=0 +( + printf() { + builtin printf "$@" + if [[ "${1:-}" == '%s\n' && "${2:-}" == starter-kit-update-* \ + && ! -e "$_wul_withdraw/injected" ]]; then + : > "$_wul_withdraw/injected" + command mkdir "$_wul_withdraw_lock.reclaim" + command mv "$_wul_withdraw_lock" "$_wul_withdraw_lock.stale-test" + command mkdir "$_wul_withdraw_lock" + builtin printf 'writer-b\n' > "$_wul_withdraw_lock/owner" + fi + } + _wul_withdraw_token="" + _wce_runtime_update_lock_acquire "$_wul_withdraw" _wul_withdraw_token \ + || exit 1 + exit 0 +) >/dev/null 2>&1 || _wul_withdraw_rc=$? +if [[ "$_wul_withdraw_rc" -eq 1 && -e "$_wul_withdraw/injected" ]] \ + && grep -qx writer-b "$_wul_withdraw_lock/owner" \ + && [[ "$(command ls -A "$_wul_withdraw_lock")" == owner ]] \ + && grep -Eqx 'starter-kit-update-[0-9]+-[0-9]+-[0-9]+' \ + "$_wul_withdraw_lock.stale-test/owner"; then + pass "WCE lock: withdrawing beside a reclaim mutex never removes another writer's lock" +else + echo " rc=$_wul_withdraw_rc" >&2 + fail "WCE lock: withdrawing beside a reclaim mutex removed another writer's lock" +fi + +# The reclaimer is still active when the mutex check runs. A withdrawal that +# checked its owner and renamed at once could have this lock moved aside +# between the two steps; writer B then takes the canonical name after the +# mutex is released, the rename moves B's lock, and writer C takes the free +# name. The withdrawal must wait for the mutex, then find B and rename nothing. +_wul_wwait="$_wul_tmp/acquire-withdraw-wait" +_wul_wwait_lock="$_wul_wwait/logs/.update.lock" +mkdir -p "$_wul_wwait/logs" +_wul_wwait_rc=0 +( + _wul_wwait_recover() { # reclaimer moves this lock aside, finishes; B acquires + [[ ! -e "$_wul_wwait/recovered" ]] || return 0 + : > "$_wul_wwait/recovered" + command mv "$_wul_wwait_lock" "$_wul_wwait_lock.stale-test" + command rmdir "$_wul_wwait_lock.reclaim" + command mkdir "$_wul_wwait_lock" + builtin printf 'writer-b\n' > "$_wul_wwait_lock/owner" + } + printf() { + builtin printf "$@" + if [[ "${1:-}" == '%s\n' && "${2:-}" == starter-kit-update-* \ + && ! -e "$_wul_wwait/injected" ]]; then + : > "$_wul_wwait/injected" + command mkdir "$_wul_wwait_lock.reclaim" + fi + } + sleep() { _wul_wwait_recover; } + mv() { + local mv_rc=0 + if [[ "${1:-}" == "$_wul_wwait_lock" \ + && "${2:-}" == "$_wul_wwait_lock".release-* ]]; then + _wul_wwait_recover + command mv "$@" || mv_rc=$? + command mkdir "$_wul_wwait_lock" 2>/dev/null \ + && builtin printf 'writer-c\n' > "$_wul_wwait_lock/owner" + return "$mv_rc" + fi + command mv "$@" + } + _wul_wwait_token="" + _wce_runtime_update_lock_acquire "$_wul_wwait" _wul_wwait_token \ + || exit 1 + exit 0 +) >/dev/null 2>&1 || _wul_wwait_rc=$? +if [[ "$_wul_wwait_rc" -eq 1 && -e "$_wul_wwait/recovered" ]] \ + && grep -qx writer-b "$_wul_wwait_lock/owner" \ + && [[ "$(command ls -A "$_wul_wwait_lock")" == owner ]] \ + && grep -Eqx 'starter-kit-update-[0-9]+-[0-9]+-[0-9]+' \ + "$_wul_wwait_lock.stale-test/owner" \ + && [[ "$(command ls -A "$_wul_wwait/logs" | LC_ALL=C sort | tr '\n' ' ')" \ + == ".update.lock .update.lock.stale-test " ]]; then + pass "WCE lock: withdrawal waits for the reclaimer before renaming anything" +else + echo " rc=$_wul_wwait_rc" >&2 + command ls -A "$_wul_wwait/logs" | sed 's/^/ | /' >&2 || true + fail "WCE lock: withdrawal renamed while a reclaimer could still move locks" +fi + +# The token names the process that holds the lock. A parent killed on its own +# leaves that subshell running, and a token naming the parent would make the +# live writer look abandoned (stale) and therefore reclaimable. +_wul_holder="$_wul_tmp/holder-pid" +mkdir -p "$_wul_holder" +_wul_holder_cb() { + builtin printf '%s\n' "$BASHPID" > "$_wul_holder/holder" + _wce_runtime_update_lock_inspect "$_wul_holder" + builtin printf '%s\n' "$_WCE_LOCK_OWNER_PID" > "$_wul_holder/token-pid" +} +_wul_holder_rc=0 +_wce_with_runtime_update_lock "$_wul_holder" _wul_holder_cb \ + >/dev/null 2>&1 || _wul_holder_rc=$? +if [[ "$_wul_holder_rc" -eq 0 && -s "$_wul_holder/holder" ]] \ + && [[ "$(cat "$_wul_holder/token-pid")" == "$(cat "$_wul_holder/holder")" ]] \ + && [[ "$(cat "$_wul_holder/holder")" != "$$" ]]; then + pass "WCE lock: the owner token names the lock-holding process, not the parent" +else + echo " rc=$_wul_holder_rc holder=$(cat "$_wul_holder/holder" 2>/dev/null) token=$(cat "$_wul_holder/token-pid" 2>/dev/null) parent=$$" >&2 + fail "WCE lock: the owner token names a process other than the lock holder" +fi + +# A mutex that outlasts the withdrawal wait does not prove the reclaimer dead; +# it may be stalled between its own read and rename. The token-checked release +# is not atomic either, so withdrawing now could move a successor's lock. The +# acquisition must fail and leave its own lock and the mutex untouched. +_wul_wstall="$_wul_tmp/acquire-withdraw-stalled" +_wul_wstall_lock="$_wul_wstall/logs/.update.lock" +mkdir -p "$_wul_wstall/logs" +_wul_wstall_rc=0 +( + printf() { + builtin printf "$@" + if [[ "${1:-}" == '%s\n' && "${2:-}" == starter-kit-update-* \ + && ! -e "$_wul_wstall/injected" ]]; then + : > "$_wul_wstall/injected" + command mkdir "$_wul_wstall_lock.reclaim" + fi + } + sleep() { :; } + mv() { + builtin printf '%s\n' "$*" >> "$_wul_wstall/mv-calls" + command mv "$@" + } + _wul_wstall_token="" + _wce_runtime_update_lock_acquire "$_wul_wstall" _wul_wstall_token \ + || exit 1 + exit 0 +) >/dev/null 2>&1 || _wul_wstall_rc=$? +if [[ "$_wul_wstall_rc" -eq 1 && -e "$_wul_wstall/injected" ]] \ + && [[ ! -e "$_wul_wstall/mv-calls" ]] \ + && [[ -d "$_wul_wstall_lock.reclaim" ]] \ + && grep -Eqx 'starter-kit-update-[0-9]+-[0-9]+-[0-9]+' \ + "$_wul_wstall_lock/owner" \ + && [[ "$(command ls -A "$_wul_wstall/logs" | LC_ALL=C sort | tr '\n' ' ')" \ + == ".update.lock .update.lock.reclaim " ]]; then + pass "WCE lock: a mutex outlasting the wait fails without renaming anything" +else + echo " rc=$_wul_wstall_rc" >&2 + command ls -A "$_wul_wstall/logs" | sed 's/^/ | /' >&2 || true + fail "WCE lock: withdrew while a stalled reclaimer could still move locks" +fi + +# Between the mkdir and the owner write a reclaimer can move this directory +# aside and writer B can take the canonical name. The owner write must not +# truncate B's owner (noclobber, like 'wx' in update-deps.mjs); otherwise both +# writers pass their final token check and hold the lock at once. +_wul_clob="$_wul_tmp/acquire-owner-noclobber" +_wul_clob_lock="$_wul_clob/logs/.update.lock" +mkdir -p "$_wul_clob/logs" +_wul_clob_rc=0 +( + mkdir() { + command mkdir "$@" || return + if [[ "${!#}" == "$_wul_clob_lock" ]]; then + # This directory is swapped for writer B's completed lock. + builtin printf 'writer-b\n' > "$_wul_clob_lock/owner" + fi + } + _wul_clob_token="" + _wce_runtime_update_lock_acquire "$_wul_clob" _wul_clob_token \ + || exit 1 + exit 0 +) >/dev/null 2>&1 || _wul_clob_rc=$? +if [[ "$_wul_clob_rc" -eq 1 ]] \ + && grep -qx writer-b "$_wul_clob_lock/owner" \ + && [[ "$(command ls -A "$_wul_clob_lock")" == owner ]]; then + pass "WCE lock: the owner write never overwrites another writer's owner" +else + echo " rc=$_wul_clob_rc owner=$(cat "$_wul_clob_lock/owner" 2>/dev/null)" >&2 + fail "WCE lock: the owner write replaced another writer's owner" +fi + +# --- A stale observation never authorizes removing a successor ---------------- +_wul_observed="$_wul_tmp/reclaim-observed" +_wul_make_lock "$_wul_observed" "$_wul_dead_other:$_wul_uuid_other" old +cp -p "$_wul_observed/logs/.update.lock/owner" "$_wul_observed.owner" +_wul_observed_rc=0 +( + # The mismatch must be caught under the mutex, before any rename: moving a + # successor's lock aside, even briefly, would let a third writer in. + mv() { + : > "$_wul_observed.mv-called" + command mv "$@" + } + _wce_runtime_update_lock_reclaim_stale \ + "$_wul_observed" "$_wul_dead:$_wul_uuid" +) >/dev/null 2>&1 || _wul_observed_rc=$? +if [[ "$_wul_observed_rc" -ne 0 && "$_wul_observed_rc" -ne 127 ]] \ + && [[ ! -e "$_wul_observed.mv-called" ]] \ + && cmp -s "$_wul_observed/logs/.update.lock/owner" "$_wul_observed.owner" \ + && ! _wul_lock_residue "$_wul_observed"; then + pass "WCE reclaim: a different owner than the one observed is not removed" +else + fail "WCE reclaim: an outdated observation removed a successor lock" +fi + +# Replace the canonical directory between the mutex-held recheck and the +# rename. The quarantined inode no longer carries the observed owner, so it +# must be put back and the mutex released. +_wul_swap="$_wul_tmp/reclaim-swap" +_wul_swap_lock="$_wul_swap/logs/.update.lock" +_wul_make_lock "$_wul_swap" "$_wul_dead:$_wul_uuid" old +_wul_swap_rc=0 +( + mv() { + if [[ "$1" == "$_wul_swap_lock" && ! -e "$_wul_swap/injected" ]]; then + : > "$_wul_swap/injected" + command rm -rf "$_wul_swap_lock" + command mkdir "$_wul_swap_lock" + printf 'successor-owner\n' > "$_wul_swap_lock/owner" + fi + command mv "$@" + } + _WCE_RUNTIME_LOCK_WAIT_SECONDS=0 + KIT_MDM_MANAGED=false + _wce_runtime_update_lock_preflight "$_wul_swap" +) > "$_wul_swap.out" 2>&1 || _wul_swap_rc=$? +if [[ "$_wul_swap_rc" -eq 75 && -e "$_wul_swap/injected" ]] \ + && grep -qx successor-owner "$_wul_swap_lock/owner" \ + && ! _wul_lock_residue "$_wul_swap"; then + pass "WCE reclaim: a read-to-rename successor is restored, not deleted" +else + echo " rc=$_wul_swap_rc" >&2 + sed 's/^/ | /' "$_wul_swap.out" >&2 || true + fail "WCE reclaim: a successor lock was deleted or stranded in quarantine" +fi + +# Same replacement, but a third writer takes the canonical name before the +# successor can be put back. The restore claims the name with mkdir; when +# that fails it must leave the successor in quarantine and say where, never +# move it into the third writer's lock directory. +_wul_third="$_wul_tmp/reclaim-third" +_wul_third_lock="$_wul_third/logs/.update.lock" +_wul_make_lock "$_wul_third" "$_wul_dead:$_wul_uuid" old +_wul_third_rc=0 +( + mv() { + if [[ "$1" == "$_wul_third_lock" && ! -e "$_wul_third/injected" ]]; then + : > "$_wul_third/injected" + command rm -rf "$_wul_third_lock" + command mkdir "$_wul_third_lock" + printf 'successor-owner\n' > "$_wul_third_lock/owner" + fi + command mv "$@" + } + mkdir() { + # The restore's claim of the canonical name loses to a third writer. + if [[ "${*: -1}" == "$_wul_third_lock" && ! -e "$_wul_third/third" ]]; then + : > "$_wul_third/third" + command mkdir "$_wul_third_lock" + printf 'third-owner\n' > "$_wul_third_lock/owner" + return 1 + fi + command mkdir "$@" + } + _WCE_RUNTIME_LOCK_WAIT_SECONDS=0 + KIT_MDM_MANAGED=false + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + STR_WCE_LOCK_QUARANTINE_LEFT="quarantine-left-marker" + _wce_runtime_update_lock_preflight "$_wul_third" +) > "$_wul_third.out" 2>&1 || _wul_third_rc=$? +_wul_third_quarantine="$(compgen -G "$_wul_third/logs/.update.lock.stale-*" || true)" +if [[ "$_wul_third_rc" -eq 75 && -e "$_wul_third/third" ]] \ + && grep -qx third-owner "$_wul_third_lock/owner" \ + && [[ -n "$_wul_third_quarantine" ]] \ + && grep -qx successor-owner "$_wul_third_quarantine/owner" \ + && [[ -z "$(find "$_wul_third_lock" -mindepth 1 -type d -print 2>/dev/null)" ]] \ + && [[ ! -e "$_wul_third/logs/.update.lock.reclaim" ]] \ + && grep -Fq quarantine-left-marker "$_wul_third.out" \ + && grep -Fq "$_wul_third_quarantine" "$_wul_third.out"; then + pass "WCE reclaim: a successor that cannot be put back is kept aside and named" +else + echo " rc=$_wul_third_rc quarantine=$_wul_third_quarantine" >&2 + sed 's/^/ | /' "$_wul_third.out" >&2 || true + fail "WCE reclaim: a successor was moved into a third writer's lock or lost silently" +fi + +# The release runs in a child of the lock holder. If only the holder is killed +# (its PID is in the token) while the child is stalled between its owner check +# and its rename, the lock is stale; once old enough a reclaimer removes it, +# writer A takes the name, the resumed rename moves A's lock, and writer B +# takes the free name: A and B both run. The release must hold the reclaim +# mutex from the owner check to the rename, so the reclaimer cannot act then. +_wul_rel="$_wul_tmp/release-vs-reclaim" +_wul_rel_lock="$_wul_rel/logs/.update.lock" +_wul_rel_token="starter-kit-update-$_wul_dead-1-1" +_wul_make_lock "$_wul_rel" "$_wul_rel_token" old +_wul_rel_rc=0 +( + mv() { + if [[ "${1:-}" == "$_wul_rel_lock" \ + && "${2:-}" == "$_wul_rel_lock".release-* \ + && ! -e "$_wul_rel/injected" ]]; then + : > "$_wul_rel/injected" + # The stalled window: a reclaimer runs, then writer A tries to acquire. + if _wce_runtime_update_lock_reclaim_stale_locked \ + "$_wul_rel" "$_wul_rel_token"; then + : > "$_wul_rel/reclaimed" + fi + if command mkdir "$_wul_rel_lock" 2>/dev/null; then + builtin printf 'writer-a\n' > "$_wul_rel_lock/owner" + : > "$_wul_rel/a-acquired" + fi + command mv "$@" || return + # Writer B takes the name the rename freed. + if command mkdir "$_wul_rel_lock" 2>/dev/null; then + builtin printf 'writer-b\n' > "$_wul_rel_lock/owner" + fi + return 0 + fi + command mv "$@" + } + KIT_MDM_MANAGED=false + _wce_runtime_update_lock_release "$_wul_rel" "$_wul_rel_token" +) > "$_wul_rel.out" 2>&1 || _wul_rel_rc=$? +if [[ "$_wul_rel_rc" -eq 0 && -e "$_wul_rel/injected" ]] \ + && [[ ! -e "$_wul_rel/reclaimed" && ! -e "$_wul_rel/a-acquired" ]] \ + && grep -qx writer-b "$_wul_rel_lock/owner" \ + && [[ "$(command ls -A "$_wul_rel/logs")" == .update.lock ]]; then + pass "WCE lock: a release holds the reclaim mutex from owner check to rename" +else + echo " rc=$_wul_rel_rc" >&2 + command ls -A "$_wul_rel" "$_wul_rel/logs" | sed 's/^/ | /' >&2 || true + sed 's/^/ | /' "$_wul_rel.out" >&2 || true + fail "WCE lock: a reclaim during a stalled release let two writers hold the lock" +fi + +# A release that finds the reclaim mutex waits for it; one that outlasts the +# wait (a stalled reclaimer) fails without renaming anything. +_wul_relw="$_wul_tmp/release-mutex-stalled" +_wul_relw_lock="$_wul_relw/logs/.update.lock" +_wul_relw_token="starter-kit-update-$_wul_dead-2-2" +_wul_make_lock "$_wul_relw" "$_wul_relw_token" +command mkdir "$_wul_relw_lock.reclaim" +_wul_relw_rc=0 +( + sleep() { :; } + mv() { : > "$_wul_relw/mv-called"; command mv "$@"; } + _wce_runtime_update_lock_release "$_wul_relw" "$_wul_relw_token" +) >/dev/null 2>&1 || _wul_relw_rc=$? +if [[ "$_wul_relw_rc" -ne 0 && ! -e "$_wul_relw/mv-called" ]] \ + && grep -qx "$_wul_relw_token" "$_wul_relw_lock/owner" \ + && [[ -d "$_wul_relw_lock.reclaim" ]]; then + pass "WCE lock: a release never renames while a reclaimer holds the mutex" +else + echo " rc=$_wul_relw_rc" >&2 + fail "WCE lock: a release renamed while a reclaimer held the mutex" +fi + +# Several reclaimers racing for one abandoned lock are serialized by the +# mutex: exactly one performs the recovery, nobody fails, nothing is left. +_wul_conc="$_wul_tmp/reclaim-concurrent" +_wul_make_lock "$_wul_conc" "$_wul_dead:$_wul_uuid" old +_wul_conc_pids=() +for _wul_case in 1 2 3 4 5 6 7 8; do + ( + KIT_MDM_MANAGED=false + _wce_runtime_update_lock_preflight "$_wul_conc" + ) > "$_wul_conc.$_wul_case.out" 2>&1 & + _wul_conc_pids+=("$!") +done +_wul_conc_failed=0 +for _wul_case in "${_wul_conc_pids[@]}"; do + wait "$_wul_case" || _wul_conc_failed=$((_wul_conc_failed + 1)) +done +_wul_conc_recovered="$({ grep -Fl "$_wul_dead" "$_wul_conc".*.out \ + 2>/dev/null || true; } | wc -l | tr -d '[:space:]')" +if [[ "$_wul_conc_failed" -eq 0 && "$_wul_conc_recovered" == 1 ]] \ + && [[ ! -e "$_wul_conc/logs/.update.lock" ]] \ + && ! _wul_lock_residue "$_wul_conc"; then + pass "WCE reclaim: concurrent reclaimers are serialized to one recovery" +else + echo " failed=$_wul_conc_failed recovered=$_wul_conc_recovered" >&2 + fail "WCE reclaim: concurrent reclaimers failed, repeated, or left residue" +fi + +# The same serialization, made deterministic: while another reclaimer holds +# the mutex, a second preflight must look again instead of failing. The mutex +# and the lock disappear together when that reclaimer finishes. +_wul_peer="$_wul_tmp/reclaim-peer" +_wul_make_lock "$_wul_peer" "$_wul_dead:$_wul_uuid" old +mkdir "$_wul_peer/logs/.update.lock.reclaim" +( + sleep 1 + rm -rf "$_wul_peer/logs/.update.lock" + rmdir "$_wul_peer/logs/.update.lock.reclaim" +) & +_wul_peer_bg=$! +_wul_peer_rc="$(_wul_preflight "$_wul_peer" "$_wul_peer.out" 20)" +wait "$_wul_peer_bg" 2>/dev/null || true +if [[ "$_wul_peer_rc" == 0 ]] && ! _wul_lock_residue "$_wul_peer"; then + pass "WCE reclaim: a reclaim in progress elsewhere is waited for, not failed" +else + echo " rc=$_wul_peer_rc" >&2 + sed 's/^/ | /' "$_wul_peer.out" >&2 || true + fail "WCE reclaim: losing the mutex to a live reclaimer failed the preflight" +fi + +# A concurrent reclaimer can remove the lock between this preflight's look and +# its own age probe, which then fails on a path that is gone. That is not a +# reason to stop: the verdict must come from what is there afterwards. +_wul_vanish="$_wul_tmp/preflight-vanish" +_wul_make_lock "$_wul_vanish" "$_wul_dead:$_wul_uuid" old +_wul_vanish_rc=0 +( + find() { + if [[ "$1" == "$_wul_vanish/logs/.update.lock/owner" ]]; then + command rm -rf "$_wul_vanish/logs/.update.lock" + fi + command find "$@" + } + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + _WCE_RUNTIME_LOCK_WAIT_SECONDS=0 KIT_MDM_MANAGED=false + _wce_runtime_update_lock_preflight "$_wul_vanish" +) > "$_wul_vanish.out" 2>&1 || _wul_vanish_rc=$? +if [[ "$_wul_vanish_rc" -eq 0 && ! -s "$_wul_vanish.out" ]]; then + pass "WCE preflight: a lock removed mid-evaluation is not reported as held" +else + echo " rc=$_wul_vanish_rc" >&2 + sed 's/^/ | /' "$_wul_vanish.out" >&2 || true + fail "WCE preflight: a lock that had already gone still stopped the run" +fi + +# --- A running owner is waited for, within a bound ---------------------------- +_wul_wait="$_wul_tmp/preflight-wait" +_wul_make_lock "$_wul_wait" "$_wul_live_pid:$_wul_uuid" +( sleep 1; rm -rf "$_wul_wait/logs/.update.lock" ) & +_wul_wait_bg=$! +_wul_wait_rc="$(_wul_preflight "$_wul_wait" "$_wul_wait.out" 20)" +wait "$_wul_wait_bg" 2>/dev/null || true +if [[ "$_wul_wait_rc" == 0 ]] \ + && grep -Fq "$_wul_live_pid" "$_wul_wait.out"; then + pass "WCE preflight: a running owner is waited for until it releases" +else + echo " rc=$_wul_wait_rc" >&2 + fail "WCE preflight: a running owner was not waited for" +fi + +# Acquisition is mkdir followed by the owner write. A preflight that looks in +# between must not report a writer that is merely starting as unverifiable. +_wul_starting="$_wul_tmp/preflight-starting" +mkdir -p "$_wul_starting/logs/.update.lock" +( + sleep 1 + printf '%s\n' "$_wul_live_pid:$_wul_uuid" \ + > "$_wul_starting/logs/.update.lock/owner" + sleep 2 + rm -rf "$_wul_starting/logs/.update.lock" +) & +_wul_starting_bg=$! +_wul_starting_rc="$(_wul_preflight "$_wul_starting" "$_wul_starting.out" 20)" +wait "$_wul_starting_bg" 2>/dev/null || true +if [[ "$_wul_starting_rc" == 0 ]]; then + pass "WCE preflight: a writer between mkdir and owner write is waited for" +else + echo " rc=$_wul_starting_rc" >&2 + sed 's/^/ | /' "$_wul_starting.out" >&2 || true + fail "WCE preflight: a starting writer was reported as an unverifiable lock" +fi + +# --- The low-level helper reports, and still never reclaims ------------------- +_wul_diag="$_wul_tmp/diagnostic" +_wul_make_lock "$_wul_diag" foreign-updater old +_wul_diag_rc=0 +_WCE_RUNTIME_LOCK_FAILURE_NOTE="caller-note-marker" \ + _wce_with_runtime_update_lock "$_wul_diag" true \ + > "$_wul_diag.out" 2> "$_wul_diag.err" || _wul_diag_rc=$? +if [[ "$_wul_diag_rc" -eq 75 ]] \ + && grep -Fq "$_wul_diag/logs/.update.lock" "$_wul_diag.err" \ + && grep -Fq caller-note-marker "$_wul_diag.err" \ + && [[ ! -s "$_wul_diag.out" ]] \ + && grep -qx foreign-updater "$_wul_diag/logs/.update.lock/owner"; then + pass "WCE lock: contention is diagnosed on stderr with the caller's note" +else + fail "WCE lock: contention exited 75 without naming the lock" +fi + +_wul_lowlevel="$_wul_tmp/lowlevel-stale" +_wul_make_lock "$_wul_lowlevel" "$_wul_dead:$_wul_uuid" old +cp -p "$_wul_lowlevel/logs/.update.lock/owner" "$_wul_lowlevel.owner" +_wul_lowlevel_rc=0 +_wce_with_runtime_update_lock "$_wul_lowlevel" true \ + >/dev/null 2> "$_wul_lowlevel.err" || _wul_lowlevel_rc=$? +if [[ "$_wul_lowlevel_rc" -eq 75 ]] \ + && cmp -s "$_wul_lowlevel/logs/.update.lock/owner" "$_wul_lowlevel.owner" \ + && ! _wul_lock_residue "$_wul_lowlevel" \ + && grep -Fq "$_wul_dead" "$_wul_lowlevel.err"; then + pass "WCE lock: acquisition itself never reclaims, even a reclaimable lock" +else + fail "WCE lock: the low-level acquire path reclaimed an existing lock" +fi + +_wul_unreleased="$_wul_tmp/release-failure" +mkdir -p "$_wul_unreleased" +_wul_break_release() { printf 'intruder\n' > "$1/logs/.update.lock/owner"; } +_wul_unreleased_rc=0 +# The caller's note describes work an acquisition failure left undone. A +# release failure comes after the callback finished, so it must not appear; +# the caller's release note (setup stops before its final steps) must. +_WCE_RUNTIME_LOCK_FAILURE_NOTE="caller-note-marker" \ +_WCE_RUNTIME_LOCK_RELEASE_NOTE="release-note-marker" \ + _wce_with_runtime_update_lock "$_wul_unreleased" \ + _wul_break_release "$_wul_unreleased" \ + >/dev/null 2> "$_wul_unreleased.err" || _wul_unreleased_rc=$? +if [[ "$_wul_unreleased_rc" -eq 74 ]] \ + && grep -Fq "$_wul_unreleased/logs/.update.lock" "$_wul_unreleased.err" \ + && ! grep -Fq caller-note-marker "$_wul_unreleased.err" \ + && grep -Fq release-note-marker "$_wul_unreleased.err" \ + && grep -qx intruder "$_wul_unreleased/logs/.update.lock/owner"; then + pass "WCE lock: a failed release is diagnosed without the acquisition note" +else + sed 's/^/ | /' "$_wul_unreleased.err" >&2 || true + fail "WCE lock: a failed release exited 74 without naming the lock, or claimed work was left undone" +fi + +# --- npm ci after update: the announced exit code is the real one ------------- +# maybe_install_web_content_deps acquires the lock again after run_update +# released it. The helper's diagnosis says "exit 75" / "exit 74"; the function +# must return that code (not 1) and, on 75, note that the kit files are done. +_wul_npm_check() { # <75|74> + local code="$1" root="$_wul_tmp/npm-$1" rc=0 + local skill="$root/.claude/skills/web-content-extraction" + mkdir -p "$skill/logs" + printf '{}\n' > "$skill/package.json" + if [[ "$code" == 75 ]]; then + _wul_make_lock "$skill" foreign-updater old + fi + ( + CLAUDE_DIR="$root/.claude" + # shellcheck disable=SC2034 # consumed indirectly by the sourced function + INSTALL_SKILLS=true DRY_RUN=false KIT_MDM_MANAGED=false + unset WCE_SKIP_NPM_INSTALL + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + STR_WCE_LOCK_DEPS_NOTE="deps-note-marker" + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + STR_WCE_LOCK_RELEASE_NOTE="release-note-marker" + _deploy_mdm_managed() { return 1; } + _fresh_wce_pair_is_skipped() { return 1; } + # Never run npm here; stand in for the callback. + if [[ "$code" == 74 ]]; then + _wce_run_non_mdm_npm_ci() { _wul_break_release "$1"; } + else + _wce_run_non_mdm_npm_ci() { :; } + fi + maybe_install_web_content_deps + ) > "$root.out" 2>&1 || rc=$? + if [[ "$rc" -eq "$code" ]] \ + && grep -Fq "$skill/logs/.update.lock" "$root.out" \ + && { [[ "$code" == 74 ]] || grep -Fq deps-note-marker "$root.out"; } \ + && { [[ "$code" == 75 ]] || ! grep -Fq deps-note-marker "$root.out"; } \ + && { [[ "$code" == 74 ]] || ! grep -Fq release-note-marker "$root.out"; } \ + && { [[ "$code" == 75 ]] || grep -Fq release-note-marker "$root.out"; }; then + pass "WCE npm ci: lock exit $code is returned as announced" + else + echo " rc=$rc" >&2 + sed 's/^/ | /' "$root.out" >&2 || true + fail "WCE npm ci: lock exit $code was returned as a different code or misnoted" + fi +} +# After a late 75/74, setup stops before the manifest, saved config, and +# plugin setup, so rerunning the same command is the only complete recovery. +# The shipped notes must not offer a bare `npm ci` as an equal alternative. +_wul_notes_ok=true +for _wul_lang in en ja; do + _wul_notes="$( + # shellcheck disable=SC1090 + source "$PROJECT_DIR/i18n/$_wul_lang/strings.sh" >/dev/null 2>&1 + builtin printf '%s\n%s\n' "${STR_WCE_LOCK_DEPS_NOTE:-}" \ + "${STR_WCE_LOCK_RELEASE_NOTE:-}" + )" + if [[ "$_wul_notes" == *"npm ci --omit=dev"* ]] \ + || [[ "$(grep -c manifest <<< "$_wul_notes")" -ne 2 ]]; then + _wul_notes_ok=false + fi +done +if [[ "$_wul_notes_ok" == true ]]; then + pass "WCE lock notes: late 75/74 notes name the skipped final steps and only offer a rerun" +else + fail "WCE lock notes: a late 75/74 note offers npm ci alone or omits the skipped final steps" +fi + +if command -v node >/dev/null 2>&1 && command -v npm >/dev/null 2>&1; then + _wul_npm_check 75 + _wul_npm_check 74 +else + skip "WCE npm ci: node/npm not on PATH" +fi + +# --- setup_deploy (update) checks the lock before backup or any write --------- +_wul_check_update_preflight() { # + local mode="$1" + local case_root="$_wul_tmp/update-preflight-$1" + local case_home="$case_root/home" + local case_claude="$case_home/.claude" + local case_skill="$case_claude/skills/web-content-extraction" + local case_rc=0 backup_count ok=true + mkdir -p "$case_skill" "$case_claude/.starter-kit-snapshot" + printf '{"marker":"before"}\n' > "$case_claude/settings.json" + printf 'claude-md-before\n' > "$case_claude/CLAUDE.md" + printf '{}\n' > "$case_claude/.starter-kit-manifest.json" + if [[ "$mode" == stale ]]; then + _wul_make_lock "$case_skill" "$_wul_dead:$_wul_uuid" old + else + _wul_make_lock "$case_skill" foreign-updater old + fi + cp -p "$case_claude/settings.json" "$case_root/settings.saved" + cp -p "$case_claude/CLAUDE.md" "$case_root/claude-md.saved" + + ( + HOME="$case_home" + # shellcheck source=setup.sh + source "$PROJECT_DIR/setup.sh" + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + UPDATE_MODE=true DRY_RUN=false KIT_MDM_MANAGED=false + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + [[ "$mode" != mdm ]] || KIT_MDM_MANAGED=true + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + INSTALL_SKILLS=true WIZARD_NONINTERACTIVE=true + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + _WCE_RUNTIME_LOCK_WAIT_SECONDS=0 _CONFIG_ALLOWED_KEYS="" + unset KIT_MDM_OUTER_TRANSACTION KIT_MDM_OUTER_TRANSACTION_BACKUP + _prepare_mdm_claude_root() { :; } + _has_user_customizations() { return 1; } + backup_existing() { : > "$case_root/backup-called"; } + _snapshot_exists() { return 0; } + _validate_dismissed_features() { :; } + _validate_plugin_csv() { :; } + run_update() { : > "$case_root/update-called"; } + _detect_and_write_pending_features() { :; } + _detect_and_offer_new_plugins() { :; } + maybe_install_web_content_deps() { :; } + setup_deploy + ) > "$case_root/out" 2>&1 || case_rc=$? + backup_count="$(find "$case_home" -maxdepth 1 \ + -name '.claude.backup.*' -print | wc -l | tr -d '[:space:]')" + + cmp -s "$case_claude/settings.json" "$case_root/settings.saved" || ok=false + cmp -s "$case_claude/CLAUDE.md" "$case_root/claude-md.saved" || ok=false + [[ "$backup_count" == 0 ]] || ok=false + case "$mode" in + blocked) + [[ "$case_rc" -eq 75 ]] || ok=false + [[ ! -e "$case_root/backup-called" \ + && ! -e "$case_root/update-called" ]] || ok=false + grep -Fq "$case_skill/logs/.update.lock" "$case_root/out" || ok=false + grep -qx foreign-updater "$case_skill/logs/.update.lock/owner" \ + || ok=false + ;; + stale) + [[ "$case_rc" -eq 0 ]] || ok=false + [[ -e "$case_root/backup-called" \ + && -e "$case_root/update-called" ]] || ok=false + [[ ! -e "$case_skill/logs/.update.lock" ]] || ok=false + _wul_lock_residue "$case_skill" && ok=false + grep -Fq "$_wul_dead" "$case_root/out" || ok=false + ;; + mdm) + # MDM never takes this lock, so the preflight must not run either. + [[ "$case_rc" -eq 0 ]] || ok=false + [[ -e "$case_root/backup-called" \ + && -e "$case_root/update-called" ]] || ok=false + grep -qx foreign-updater "$case_skill/logs/.update.lock/owner" \ + || ok=false + ;; + esac + if [[ "$ok" == true ]]; then + pass "WCE update preflight ($mode): the lock is settled before backup and update" + else + echo " rc=$case_rc backups=$backup_count" >&2 + sed 's/^/ | /' "$case_root/out" >&2 || true + fail "WCE update preflight ($mode): backup or update ran in the wrong lock state" + fi +} +_wul_check_update_preflight blocked +_wul_check_update_preflight stale +_wul_check_update_preflight mdm + +# The fresh / full re-setup branch recovers the same abandoned lock before it +# creates any deployment state. +_wul_fresh_stale="$_wul_tmp/fresh-stale" +_wul_fresh_stale_skill="$_wul_fresh_stale/home/.claude/skills/web-content-extraction" +_wul_make_lock "$_wul_fresh_stale_skill" "$_wul_dead:$_wul_uuid" old +printf '{}\n' > "$_wul_fresh_stale/home/.claude/settings.json" +printf '{}\n' > "$_wul_fresh_stale/home/.claude/.starter-kit-manifest.json" +_wul_fresh_stale_rc=0 +( + HOME="$_wul_fresh_stale/home" + # shellcheck source=setup.sh + source "$PROJECT_DIR/setup.sh" + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + UPDATE_MODE=false DRY_RUN=false KIT_MDM_MANAGED=false + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + INSTALL_AGENTS=false INSTALL_RULES=false INSTALL_COMMANDS=false + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + INSTALL_SKILLS=true WIZARD_NONINTERACTIVE=true + # shellcheck disable=SC2034 # consumed indirectly by sourced setup_deploy + _WCE_RUNTIME_LOCK_WAIT_SECONDS=0 + unset KIT_MDM_OUTER_TRANSACTION KIT_MDM_OUTER_TRANSACTION_BACKUP + section() { :; } + warn_existing_claude_reconfigure() { :; } + _setup_deploy_fresh_body() { + _wce_runtime_update_lock_owner_matches \ + "$CLAUDE_DIR/skills/web-content-extraction" \ + "$_WCE_RUNTIME_LOCK_TOKEN" || return 1 + : > "$_wul_fresh_stale/body-called" + _setup_write_fresh_deploy_state "$7" + } + setup_deploy +) > "$_wul_fresh_stale/out" 2>&1 || _wul_fresh_stale_rc=$? +if [[ "$_wul_fresh_stale_rc" -eq 0 && -e "$_wul_fresh_stale/body-called" ]] \ + && [[ ! -e "$_wul_fresh_stale_skill/logs/.update.lock" ]] \ + && ! _wul_lock_residue "$_wul_fresh_stale_skill" \ + && grep -Fq "$_wul_dead" "$_wul_fresh_stale/out"; then + pass "WCE fresh preflight: an abandoned lock is recovered before deployment" +else + echo " rc=$_wul_fresh_stale_rc" >&2 + sed 's/^/ | /' "$_wul_fresh_stale/out" >&2 || true + fail "WCE fresh preflight: an abandoned lock still blocked re-setup" +fi + +# --- Dry-run inspects the real lock read-only and only warns ------------------ +_wul_dry_notice() { # + local rc=0 + ( + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + KIT_MDM_MANAGED=false + # Fixed markers: another test file may have loaded either language. + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + STR_WCE_LOCK_DRYRUN_BLOCKED="dry-run-blocked-marker" + # shellcheck disable=SC2034 # consumed indirectly by the sourced helper + STR_WCE_LOCK_DRYRUN_RECOVERABLE="dry-run-recoverable-marker" + _wce_runtime_update_lock_dryrun_notice "$1" + ) > "$2" 2>&1 || rc=$? + printf '%s' "$rc" +} +_wul_dry="$_wul_tmp/dryrun-notice" +_wul_dry_skill="$_wul_dry/blocked/.claude/skills/web-content-extraction" +_wul_make_lock "$_wul_dry_skill" foreign-updater old +cp -p "$_wul_dry_skill/logs/.update.lock/owner" "$_wul_dry/blocked.owner" +_wul_dry_blocked_rc="$(_wul_dry_notice "$_wul_dry/blocked/.claude" \ + "$_wul_dry/blocked.out")" +_wul_dry_stale_skill="$_wul_dry/stale/.claude/skills/web-content-extraction" +_wul_make_lock "$_wul_dry_stale_skill" "$_wul_dead:$_wul_uuid" old +cp -p "$_wul_dry_stale_skill/logs/.update.lock/owner" "$_wul_dry/stale.owner" +_wul_dry_stale_rc="$(_wul_dry_notice "$_wul_dry/stale/.claude" \ + "$_wul_dry/stale.out")" +mkdir -p "$_wul_dry/free/.claude/skills/web-content-extraction" +_wul_dry_free_rc="$(_wul_dry_notice "$_wul_dry/free/.claude" \ + "$_wul_dry/free.out")" +if [[ "$_wul_dry_blocked_rc" == 0 && "$_wul_dry_stale_rc" == 0 \ + && "$_wul_dry_free_rc" == 0 ]] \ + && grep -Fq dry-run-blocked-marker "$_wul_dry/blocked.out" \ + && grep -Fq "$_wul_dry_skill/logs/.update.lock" "$_wul_dry/blocked.out" \ + && grep -Fq dry-run-recoverable-marker "$_wul_dry/stale.out" \ + && grep -Fq "$_wul_dry_stale_skill/logs/.update.lock" "$_wul_dry/stale.out" \ + && [[ ! -s "$_wul_dry/free.out" ]] \ + && cmp -s "$_wul_dry_skill/logs/.update.lock/owner" "$_wul_dry/blocked.owner" \ + && cmp -s "$_wul_dry_stale_skill/logs/.update.lock/owner" \ + "$_wul_dry/stale.owner" \ + && ! _wul_lock_residue "$_wul_dry_stale_skill"; then + pass "WCE dry-run: a held lock is reported without being touched" +else + echo " rc=$_wul_dry_blocked_rc/$_wul_dry_stale_rc/$_wul_dry_free_rc" >&2 + fail "WCE dry-run: the real lock was not reported, or was modified" +fi + +kill "$_wul_live_pid" 2>/dev/null || true +wait "$_wul_live_pid" 2>/dev/null || true + rm -rf "$_wul_tmp"