Load and cache hook definitions from the config file. Returns: An empty list when the file is missing or malformed so that normal execution is never interrupted.
()
| 36 | |
| 37 | |
| 38 | def _load_hooks() -> list[dict[str, Any]]: |
| 39 | """Load and cache hook definitions from the config file. |
| 40 | |
| 41 | Returns: |
| 42 | An empty list when the file is missing or malformed so that normal |
| 43 | execution is never interrupted. |
| 44 | """ |
| 45 | global _hooks_config # noqa: PLW0603 |
| 46 | if _hooks_config is not None: |
| 47 | return _hooks_config |
| 48 | |
| 49 | from deepagents_code.model_config import DEFAULT_CONFIG_DIR |
| 50 | |
| 51 | hooks_path = DEFAULT_CONFIG_DIR / "hooks.json" |
| 52 | |
| 53 | if not hooks_path.is_file(): |
| 54 | _hooks_config = [] |
| 55 | return _hooks_config |
| 56 | |
| 57 | try: |
| 58 | data = json.loads(hooks_path.read_text()) |
| 59 | if not isinstance(data, dict): |
| 60 | logger.warning( |
| 61 | "Hooks config at %s must be a JSON object, got %s", |
| 62 | hooks_path, |
| 63 | type(data).__name__, |
| 64 | ) |
| 65 | _hooks_config = [] |
| 66 | return _hooks_config |
| 67 | hooks = data.get("hooks", []) |
| 68 | if not isinstance(hooks, list): |
| 69 | logger.warning( |
| 70 | "Hooks config 'hooks' key at %s must be a list, got %s", |
| 71 | hooks_path, |
| 72 | type(hooks).__name__, |
| 73 | ) |
| 74 | _hooks_config = [] |
| 75 | return _hooks_config |
| 76 | _hooks_config = hooks |
| 77 | except (json.JSONDecodeError, OSError) as exc: |
| 78 | logger.warning("Failed to load hooks config from %s: %s", hooks_path, exc) |
| 79 | _hooks_config = [] |
| 80 | |
| 81 | return _hooks_config |
| 82 | |
| 83 | |
| 84 | def _run_single_hook(command: list[str], event: str, payload_bytes: bytes) -> None: |
no test coverage detected
searching dependent graphs…