Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/specify_cli/presets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ def _content_sha256(content: bytes) -> str:
return hashlib.sha256(content).hexdigest()


def _atomic_write_text(path: Path, content: str) -> None:
"""Write *content* to *path* atomically via mkstemp + os.replace."""
fd, tmp = tempfile.mkstemp(
dir=str(path.parent), prefix=f".{path.name}.", suffix=".tmp"
)
try:
with os.fdopen(fd, "w", encoding="utf-8") as f:
f.write(content)
os.replace(tmp, path)
except BaseException:
try:
os.unlink(tmp)
except OSError:
pass
raise


def _constitution_is_generated(
project_root: Path,
memory_constitution: Path,
Expand Down Expand Up @@ -848,7 +865,7 @@ def _register_commands(
composed_dir = preset_dir / ".composed"
composed_dir.mkdir(parents=True, exist_ok=True)
composed_file = composed_dir / f"{cmd['name']}.md"
composed_file.write_text(composed, encoding="utf-8")
_atomic_write_text(composed_file, composed)
commands_to_register.append({
**cmd,
"file": f".composed/{cmd['name']}.md",
Expand Down Expand Up @@ -1838,7 +1855,7 @@ def record_written(written: Dict[str, List[str]]) -> None:
composed_dir = pack_dir / ".composed"
composed_dir.mkdir(parents=True, exist_ok=True)
composed_file = composed_dir / f"{cmd_name}.md"
composed_file.write_text(composed, encoding="utf-8")
_atomic_write_text(composed_file, composed)
written = self._register_for_non_skill_agents(
registrar,
[{**tmpl, "file": f".composed/{cmd_name}.md"}],
Expand All @@ -1858,7 +1875,7 @@ def record_written(written: Dict[str, List[str]]) -> None:
shared_composed = self.presets_dir / ".composed"
shared_composed.mkdir(parents=True, exist_ok=True)
composed_file = shared_composed / f"{cmd_name}.md"
composed_file.write_text(composed, encoding="utf-8")
_atomic_write_text(composed_file, composed)
source = layers[0]["source"]
if source.startswith("extension:"):
source_id = source.split(":", 1)[1].split(" ", 1)[0]
Expand Down