← Back to PRs

#16960: perf: skip cache-busting for bundled hooks, use mtime for workspace hooks

by mudrii open 2026-02-15 08:39 View on GitHub →
size: S
## Summary Bundled hooks in `dist/` are immutable between `npm install` runs — there is no reason to append `?t=Date.now()` to their import URLs on every gateway restart. This forced V8 to re-parse and re-compile ~30 transitive module chunks per hook (×4 hooks = ~120 redundant compilations per restart). ## Changes - **New:** `src/hooks/import-url.ts` — `buildImportUrl(path, source)` helper that decides cache-busting strategy based on hook source: - `openclaw-bundled`: bare URL (no query string) → V8 module cache effective across restarts - `openclaw-workspace` / `openclaw-managed` / `openclaw-plugin`: `?t=<mtime>` → only re-parsed when file actually changes - Falls back to `?t=Date.now()` if `stat()` fails (safety net) - **Updated:** `src/hooks/loader.ts` — use `buildImportUrl()` for both directory-based and legacy handler imports - **Updated:** `src/hooks/plugin-hooks.ts` — use `buildImportUrl()` for plugin hook imports - **New:** `src/hooks/import-url.test.ts` — 7 unit tests covering all source types, cacheability, and fallback behavior ## Observed impact Cold restart (first of day) — hooks take ~1.3s to load as V8 compiles all modules fresh: ``` 07:49:39 → boot-md registered 07:49:40 → bootstrap-extra-files (1.3s gap) ``` With this fix, bundled hook imports will hit V8 module cache on subsequent restarts, eliminating redundant parse+compile cycles for ~30 immutable dist chunks. ## Testing - `pnpm build` ✅ - `pnpm lint` ✅ (0 warnings, 0 errors) - `pnpm test -- src/hooks/` ✅ (90 tests, 11 files, all passing) Fixes #16953 <!-- greptile_comment --> <h3>Greptile Summary</h3> Eliminates redundant V8 module compilation on gateway restarts by using smart cache-busting strategies: bundled hooks skip cache-busting entirely (immutable between installs), while workspace/managed/plugin hooks use mtime-based invalidation (only re-parsed when files actually change). - Adds `buildImportUrl()` helper that selects cache strategy based on hook source type - Updates `loader.ts` and `plugin-hooks.ts` to use the new helper for all hook imports - Includes 7 unit tests covering all source types, cacheability guarantees, and fallback behavior - Addresses performance issue where ~120 module chunks were redundantly re-compiled on every restart <h3>Confidence Score: 5/5</h3> - Safe to merge - focused performance optimization with proper fallbacks - Clean implementation with comprehensive test coverage (7 tests covering all source types and edge cases), proper error handling (falls back to Date.now() if stat fails), and minimal surface area for issues. The logic correctly distinguishes between immutable bundled hooks and mutable workspace/plugin hooks. - No files require special attention <sub>Last reviewed commit: 10805c4</sub> <!-- greptile_other_comments_section --> <!-- /greptile_comment -->

Most Similar PRs