shutil.copy2 and shutil.copytree both preserve source-file mode bits.
When bundled skills are sourced from a read-only filesystem — the Nix
store (mode 0444/0555), squashfs, or an OCI image layer — the user copy
in ~/.hermes/skills/ inherits those bits. Later edits via skill_manage,
the curator, or even the shutil.rmtree(*.bak) cleanup at the end of an
update then fail with PermissionError (silently swallowed by
ignore_errors=True, so stale *.bak directories accumulate).
Three helpers fix the root cause at the copy boundary:
- _ensure_owner_writable(path) — adds the owner-write bit on a single
file or directory without touching other mode bits (skips symlinks:
os.chmod follows them by default, and a copied symlink pointing
outside the profile/tree must not have its external target mutated).
- _copy_file_writable(src, dst) — drop-in shutil.copy2 replacement,
also used as copytree's copy_function.
- _copytree_writable(src, dst) — drop-in shutil.copytree replacement;
sweeps the destination tree afterwards since copytree reapplies
source-directory metadata after file copies.
Wired into every copy site that may read from a read-only bundled
source: tools/skills_sync.py (sync_skills new + update paths,
restore_official_optional_skill, the DESCRIPTION.md copy, and the
reset_bundled_skill rmtree), hermes_cli/profiles.py (--clone and
--clone-all), and hermes_cli/profile_distribution.py
(apply_distribution).
Also repairs two states the copy-boundary fix alone doesn't reach:
- Migration for existing installs: a user copy that predates this fix
can be hash-identical to the bundled source, so sync_skills takes
the "unchanged" no-op path and would never repair it. Both the
v1-migration branch and the "bundled unchanged, user unchanged"
branch now sweep _make_tree_owner_writable(dest) unconditionally.
- Symlink safety in the profile-clone repair sweep: profiles.py clones
skills with shutil.copytree(..., symlinks=True, ...), so a skill
that is itself a symlink to a shared/vendored directory outside the
profile reaches the writable-mode repair as a real symlink entry.
_ensure_owner_writable skips symlinks rather than chmod-ing through
them into whatever they still point at.
Salvages closed PR #20135 (closed by author, not maintainer-rejected)
and extends its coverage. Complements #34860 (83a7d0b60 / 8ae0802d5),
which fixed the removal side (_rmtree_writable making read-only trees
removable) — this fixes the copy side (preventing read-only trees from
being created in the first place).