The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
refactor(tools): _crash helper for tirith operational failures; inline sync-state path
Teknium4de0842e19b8
diff --git a/tools/skills_sync_client.py b/tools/skills_sync_client.pyindex 070b94971c0..086b9e4503b 100644--- a/tools/skills_sync_client.py+++ b/tools/skills_sync_client.py@@ -216,8 +216,7 @@ def _adopt_manifest_opt_ins(remote_manifest: Optional[Dict[str, bool]]) -> List[# Device label (commit ``author.device``; advisory, never an auth input)def _default_device_label() -> str:"""Short hostname + random suffix (two machines can share a hostname); bare uuid if unusable."""- import socket- import uuid+ import socket, uuidtry:host = socket.gethostname() or ""except OSError:@@ -246,7 +245,7 @@ def _device_id_path() -> Path:def _write_device_id(val: str) -> None:- _device_id_path().parent.mkdir(parents=True, exist_ok=True)+ _skills_dir().mkdir(parents=True, exist_ok=True)_device_id_path().write_text(val, encoding="utf-8")@@ -264,10 +263,6 @@ def set_device_name(name: str) -> str:_EMPTY_STATE: Dict[str, Any] = {"head": None, "skills": {}}-def _sync_state_path() -> Path:- return _skills_dir() / ".sync_state"--def _load_state_file(path: Path, what: str = "sync state read") -> Optional[Dict[str, Any]]:"""Parse a state file; None if missing / corrupt / not a dict."""try:@@ -281,7 +276,7 @@ def _load_state_file(path: Path, what: str = "sync state read") -> Optional[Dictdef read_sync_state() -> Dict[str, Any]:"""``{"head": "sha256:...|null", "skills": {...}}``; a default on missing/corrupt. A legacy``.sync_manifest`` is migrated to ``.sync_state`` on read so no head record is lost."""- path = _sync_state_path()+ path = _skills_dir() / ".sync_state"if path.exists():return _load_state_file(path) or dict(_EMPTY_STATE)legacy = _skills_dir() / ".sync_manifest"@@ -297,7 +292,7 @@ def write_sync_state(data: Dict[str, Any]) -> None:"""Write the local sync state atomically. Best-effort."""try:from tools.skill_usage import _atomic_write- _atomic_write(_sync_state_path(), ".sync_state_",+ _atomic_write(_skills_dir() / ".sync_state", ".sync_state_",lambda f: json.dump(data, f, indent=2, sort_keys=True, ensure_ascii=False))except Exception as e:logger.debug("skills_sync_client: sync state write failed: %s", e)diff --git a/tools/tirith_security.py b/tools/tirith_security.pyindex 4f245dd032a..cea68148dca 100644--- a/tools/tirith_security.py+++ b/tools/tirith_security.py@@ -470,6 +470,12 @@ def _fail(fail_open: bool, open_summary: str, closed_summary: str) -> dict:return _verdict("allow", open_summary) if fail_open else _verdict("block", closed_summary)+def _crash(fail_open: bool, open_summary: str, closed_summary: str) -> dict:+ """An operational failure: count it toward the circuit breaker, then fail open/closed."""+ _record_tirith_crash()+ return _fail(fail_open, open_summary, closed_summary)++def check_command_security(command: str) -> dict:"""Run the tirith scan on a command -> ``{"action": allow|warn|block, "findings", "summary"}``.Exit code determines the action; JSON enriches. Spawn failures/timeouts respect fail_open."""@@ -495,22 +501,19 @@ def check_command_security(command: str) -> dict:except OSError as exc:# FileNotFoundError / PermissionError / exec format error: dedupe by (class, errno)# so each failure mode surfaces once, not per command.- spawn_key = f"tirith_spawn_failed:{type(exc).__name__}:{getattr(exc, 'errno', '')}"- _warn_once(spawn_key, "tirith spawn failed: %s", exc)- _record_tirith_crash()- return _fail(fail_open, f"tirith unavailable: {exc}", f"tirith spawn failed (fail-closed): {exc}")+ _warn_once(f"tirith_spawn_failed:{type(exc).__name__}:{getattr(exc, 'errno', '')}",+ "tirith spawn failed: %s", exc)+ return _crash(fail_open, f"tirith unavailable: {exc}", f"tirith spawn failed (fail-closed): {exc}")except subprocess.TimeoutExpired:_warn_once(f"tirith_timeout:{timeout}", "tirith timed out after %ds", timeout)- _record_tirith_crash()- return _fail(fail_open, f"tirith timed out ({timeout}s)", "tirith timed out (fail-closed)")+ return _crash(fail_open, f"tirith timed out ({timeout}s)", "tirith timed out (fail-closed)")exit_code = result.returncodeif (action := _EXIT_ACTIONS.get(exit_code)) is None:# Unknown exit code (includes signal-killed, e.g. -11): respect fail_open.logger.warning("tirith returned unexpected exit code %d", exit_code)- _record_tirith_crash()- return _fail(fail_open, f"tirith exit code {exit_code} (fail-open)",- f"tirith exit code {exit_code} (fail-closed)")+ return _crash(fail_open, f"tirith exit code {exit_code} (fail-open)",+ f"tirith exit code {exit_code} (fail-closed)")if action == "allow":_crash_count = 0 # successful execution resets the circuit breaker# JSON enriches findings/summary; a parse failure never changes the verdict.