profiles_initialization.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. """Initial profile files, written on the staging tree before publication."""
  2. import contextlib
  3. import os
  4. import shutil
  5. from pathlib import Path
  6. from typing import Optional
  7. def clone_staging_dir(profile_dir: Path) -> Path:
  8. """Fresh ``profiles/.<name>.staging-<pid>`` beside the final dir (same filesystem, so the publish
  9. rename is atomic). A leftover from a crashed create is discarded."""
  10. staging = profile_dir.parent / f".{profile_dir.name}.staging-{os.getpid()}"
  11. profile_dir.parent.mkdir(parents=True, exist_ok=True)
  12. if staging.is_symlink() or staging.is_file():
  13. staging.unlink()
  14. elif staging.is_dir():
  15. shutil.rmtree(staging, ignore_errors=True)
  16. return staging
  17. def discard_unpublished_vault_key(ref: Optional[dict]) -> None:
  18. """A create that fails after a --clone-all copy got its own keychain entry (``ref``, returned by
  19. ``VaultStore.own_copied_key``) deletes exactly that entry; never one inferred from the source."""
  20. if ref is not None:
  21. from agent.vault_keychain import delete_key
  22. delete_key(ref)
  23. def finish_profile_layout(profile_dir: Path, *, no_skills: bool, clone_all: bool,
  24. description: Optional[str], soul: Optional[str] = None) -> None:
  25. """Seed files a fresh profile owns from day one; runs on the staging tree before publish."""
  26. from hermes_cli.profiles import (
  27. _seed_file_if_missing, _PLACEHOLDER_ENV, NO_BUNDLED_SKILLS_MARKER,
  28. _migrate_profile_config_if_outdated, write_profile_meta,
  29. )
  30. if soul is not None:
  31. (profile_dir / "SOUL.md").write_text(soul, encoding="utf-8")
  32. # Seed an empty .env so the profile owns a credentials file from day one. Without it,
  33. # profile-scoped env writes (dashboard Channels/Keys pages, `hermes -p <name> auth add`)
  34. # had no file until first write and the profile silently inherited shell API keys —
  35. # read by users as "the new profile reads the root .env". Skipped when a clone copied one.
  36. _seed_file_if_missing(profile_dir / ".env", _PLACEHOLDER_ENV, 0o600)
  37. # Default SOUL.md to customize immediately (skipped when a clone already provided one).
  38. with contextlib.suppress(Exception): # best-effort — don't fail profile creation over this
  39. from hermes_cli.default_soul import DEFAULT_SOUL_MD
  40. _seed_file_if_missing(profile_dir / "SOUL.md", DEFAULT_SOUL_MD)
  41. # Opt-out marker read by seed_profile_skills() and `hermes update`'s all-profile sync
  42. # (the feature still works via the empty skills/ dir if this fails).
  43. if no_skills:
  44. _seed_file_if_missing(
  45. profile_dir / NO_BUNDLED_SKILLS_MARKER,
  46. "This profile opted out of bundled-skill seeding (`hermes profile create --no-skills`).\n"
  47. "Delete this file to re-enable sync on the next `hermes update`.\n",
  48. )
  49. # Migrate config-only clones now so desktop/status don't warn that a just-created
  50. # profile is v0/outdated; --clone-all snapshots stay byte-for-byte apart from the
  51. # explicit runtime/history stripping above.
  52. if not clone_all:
  53. _migrate_profile_config_if_outdated(profile_dir)
  54. # Description last, so a partial-create failure doesn't strand a description file.
  55. if description and description.strip():
  56. with contextlib.suppress(Exception): # non-fatal — `hermes profile describe` works later
  57. write_profile_meta(profile_dir, description=description.strip(), description_auto=False)