Skip to content

The agent that grows with you

fix(docker): rebootstrap re-seed temp is randomly named so a stale temp never blocks recovery

teknium1b2688440a9c3
diff --git a/scripts/docker_rebootstrap_nous_session.py b/scripts/docker_rebootstrap_nous_session.py
index a6130076418..8fd7323df2f 100644
--- a/scripts/docker_rebootstrap_nous_session.py
+++ b/scripts/docker_rebootstrap_nous_session.py
@@ -39,6 +39,7 @@ from __future__ import annotations
import json
import os
import sys
+import tempfile
from datetime import datetime, timezone
from typing import Any, Optional
@@ -189,8 +190,10 @@ def reseed_if_terminal(auth_path: str, seed_raw: str) -> str:
# 0600 from creation: the seed holds a refresh token and must never sit at umask, even briefly.
# (stdlib only by design — see module docstring — so this mirrors utils.atomic_json_write by hand.)
- tmp_path = f"{auth_path}.rebootstrap.{os.getpid()}.tmp"
- fd = os.open(tmp_path, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o600)
+ # Randomly named: boot-hook PIDs inside a container repeat, so a PID-named temp left by a
+ # SIGKILL'd run would collide with O_EXCL forever and main() would swallow the FileExistsError.
+ fd, tmp_path = tempfile.mkstemp(
+ dir=os.path.dirname(auth_path) or ".", prefix=os.path.basename(auth_path) + ".rebootstrap.", suffix=".tmp")
try:
with os.fdopen(fd, "w", encoding="utf-8") as fh:
json.dump(store, fh)
diff --git a/tests/tools/test_docker_rebootstrap_nous_session.py b/tests/tools/test_docker_rebootstrap_nous_session.py
index abd9468d9b0..04ffb860273 100644
--- a/tests/tools/test_docker_rebootstrap_nous_session.py
+++ b/tests/tools/test_docker_rebootstrap_nous_session.py
@@ -9,6 +9,7 @@ These are pure-stdlib tmp_path tests (no container build).
from __future__ import annotations
import importlib.util
+import os
import json
from pathlib import Path
@@ -113,3 +114,18 @@ def test_terminal_entry_missing_marker_is_not_terminal(tmp_path):
entry) → not terminal, no re-seed."""
auth = _write_auth(tmp_path, {"nous": {"client_id": "hermes-cli-vps"}})
assert mod.reseed_if_terminal(auth, _FRESH_SEED) == "not_terminal"
+
+
+def test_stale_temp_from_a_killed_prior_run_does_not_block_reseed(tmp_path):
+ """Boot-hook PIDs repeat inside a container: a temp left by a SIGKILL'd run must never make
+ the next re-seed fail (main() swallows the exception, so the recovery path would be dead)."""
+ home = tmp_path / "home"
+ home.mkdir()
+ auth = _write_auth(home, {"nous": _terminal_nous_state()})
+ stale = Path(f"{auth}.rebootstrap.{os.getpid()}.tmp")
+ stale.write_text("{torn", encoding="utf-8")
+
+ assert mod.reseed_if_terminal(auth, _FRESH_SEED) == "reseeded"
+ store = json.loads(Path(auth).read_text())
+ assert store["providers"]["nous"]["refresh_token"] == "FRESH-rt"
+ assert sorted(p.name for p in home.iterdir()) == sorted(["auth.json", stale.name]), "no new temp survives"