Skip to content

The agent that grows with you

fix(cli): keep newly created SOUL.md and distribution.yaml at 0644

briandevans4541d3018181
diff --git a/hermes_cli/profile_distribution.py b/hermes_cli/profile_distribution.py
index ce269fad513..c7298e25b84 100644
--- a/hermes_cli/profile_distribution.py
+++ b/hermes_cli/profile_distribution.py
@@ -61,6 +61,7 @@ Update semantics:
from __future__ import annotations
+import os
import re
import shutil
import subprocess
@@ -273,12 +274,26 @@ def write_manifest(profile_dir: Path, manifest: DistributionManifest) -> Path:
# tracking and env_requires with no error surfaced anywhere.
from utils import atomic_yaml_write
+ # atomic_yaml_write preserves an existing file's mode, but a file it
+ # *creates* keeps mkstemp's 0600. _materialize() reaches this line with no
+ # manifest on disk whenever a distribution declares an explicit
+ # `distribution_owned` allowlist that does not list distribution.yaml
+ # itself, so the file is never copied out of the staged tree. The manifest
+ # is a shareable descriptor rather than a secret and used to land at the
+ # umask default, so restore 0644 on the create path only.
+ existed = mf_path.exists()
+
atomic_yaml_write(
mf_path,
manifest.to_dict(),
sort_keys=False,
default_flow_style=False,
)
+ if not existed:
+ try:
+ os.chmod(mf_path, 0o644)
+ except OSError:
+ pass
return mf_path
diff --git a/hermes_cli/web_routers/profiles.py b/hermes_cli/web_routers/profiles.py
index 4b519731705..56bcea631e9 100644
--- a/hermes_cli/web_routers/profiles.py
+++ b/hermes_cli/web_routers/profiles.py
@@ -625,8 +625,16 @@ async def update_profile_soul(name: str, body: ProfileSoulUpdate):
# persists that empty document over it.
try:
prior_mode = stat.S_IMODE(soul_path.stat().st_mode)
+ except FileNotFoundError:
+ # First save for this profile -- there is no prior file to match,
+ # so fall back to the mode profile creation itself produces:
+ # hermes_cli.profiles seeds SOUL.md with a bare write_text() and
+ # deliberately chmods only .env to 0600. Without this the create
+ # path keeps mkstemp's 0600 and the atomicity fix would silently
+ # tighten the persona document.
+ prior_mode = 0o644
except OSError:
- # First save for this profile -- there is no prior file to match.
+ # stat() failed for some other reason -- do not guess a mode.
prior_mode = None
atomic_write_text(soul_path, body.content)
diff --git a/tests/hermes_cli/test_profile_distribution.py b/tests/hermes_cli/test_profile_distribution.py
index b0282efe854..43b4dbad627 100644
--- a/tests/hermes_cli/test_profile_distribution.py
+++ b/tests/hermes_cli/test_profile_distribution.py
@@ -737,3 +737,25 @@ class TestManifestCrashDurability:
mode = stat.S_IMODE(mf.stat().st_mode)
assert mode == 0o644, f"mode changed to {oct(mode)}"
+ @pytest.mark.skipif(
+ sys.platform == "win32", reason="POSIX permission bits"
+ )
+ def test_created_file_mode_is_not_tightened(self, tmp_path):
+ """A manifest this function *creates* must not land owner-only.
+
+ ``atomic_yaml_write`` only re-applies a mode it captured from an
+ existing file, so a fresh distribution.yaml would otherwise keep
+ ``tempfile.mkstemp``'s 0600. ``_materialize`` hits that path whenever a
+ distribution's explicit ``distribution_owned`` allowlist omits
+ distribution.yaml, so the staged copy never lands in the profile.
+ """
+ import stat
+
+ mf = tmp_path / "distribution.yaml"
+ assert not mf.exists()
+
+ write_manifest(tmp_path, DistributionManifest(name="fresh", version="1.0.0"))
+
+ mode = stat.S_IMODE(mf.stat().st_mode)
+ assert mode == 0o644, f"new manifest created as {oct(mode)}"
+
diff --git a/tests/hermes_cli/test_web_profile_soul_writes.py b/tests/hermes_cli/test_web_profile_soul_writes.py
index 752529666fd..06f209d7f36 100644
--- a/tests/hermes_cli/test_web_profile_soul_writes.py
+++ b/tests/hermes_cli/test_web_profile_soul_writes.py
@@ -110,3 +110,22 @@ class TestSoulWriteDurability:
assert r.status_code == 200, r.text
mode = stat.S_IMODE(soul.stat().st_mode)
assert mode == 0o644, f"mode changed to {oct(mode)}"
+
+ @pytest.mark.skipif(sys.platform == "win32", reason="POSIX permission bits")
+ def test_created_file_mode_is_not_tightened(self, client, profile_dir: Path):
+ """The first-ever Save must not leave SOUL.md owner-only.
+
+ There is no prior file to copy permissions from, and
+ ``atomic_write_text`` swaps in a ``tempfile.mkstemp`` file (0600).
+ Profile creation seeds SOUL.md with a plain ``write_text()`` and
+ chmods only ``.env`` to 0600, so routing this endpoint through the
+ atomic writer must not tighten the persona document as a side effect.
+ """
+ soul = profile_dir / "SOUL.md"
+ assert not soul.exists()
+
+ r = client.put("/api/profiles/demo/soul", json={"content": SOUL})
+
+ assert r.status_code == 200, r.text
+ mode = stat.S_IMODE(soul.stat().st_mode)
+ assert mode == 0o644, f"first save created SOUL.md as {oct(mode)}"