|
| 1 | +/** |
| 2 | + * Real behavior proof for PR #92351 (issue #92241). |
| 3 | + * |
| 4 | + * Demonstrates the dist-rotation guard: |
| 5 | + * 1. isDistRotationError correctly identifies stale-dist ERR_MODULE_NOT_FOUND |
| 6 | + * 2. guardedLoad wrapper catches, clears cache, logs operator warning, re-throws |
| 7 | + * 3. Non-rotation errors pass through untouched |
| 8 | + */ |
| 9 | +import { createLazyPromiseLoader, isDistRotationError } from "../src/shared/lazy-promise.js"; |
| 10 | +import { formatErrorMessage } from "../src/infra/errors.js"; |
| 11 | + |
| 12 | +const PASS = "✓"; |
| 13 | +const FAIL = "✗"; |
| 14 | + |
| 15 | +let passed = 0; |
| 16 | +let failed = 0; |
| 17 | + |
| 18 | +function assert(label: string, condition: boolean) { |
| 19 | + if (condition) { |
| 20 | + console.log(` ${PASS} ${label}`); |
| 21 | + passed += 1; |
| 22 | + } else { |
| 23 | + console.log(` ${FAIL} ${label}`); |
| 24 | + failed += 1; |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +console.log("=== isDistRotationError: positive cases ==="); |
| 29 | + |
| 30 | +// Case 1: Linux ERR_MODULE_NOT_FOUND in openclaw/dist |
| 31 | +const err1 = Object.assign( |
| 32 | + new Error("Cannot find module '/usr/lib/node_modules/openclaw/dist/cleanup-DlVQZQex.js' imported from ..."), |
| 33 | + { code: "ERR_MODULE_NOT_FOUND" }, |
| 34 | +); |
| 35 | +assert( |
| 36 | + "Linux ERR_MODULE_NOT_FOUND with openclaw/dist path → true", |
| 37 | + isDistRotationError(err1), |
| 38 | +); |
| 39 | + |
| 40 | +// Case 2: Windows path with backslashes |
| 41 | +const err2 = Object.assign( |
| 42 | + new Error("Cannot find module 'C:\\Users\\app\\AppData\\Roaming\\npm\\node_modules\\openclaw\\dist\\chunk-abc123.js'"), |
| 43 | + { code: "MODULE_NOT_FOUND" }, |
| 44 | +); |
| 45 | +assert( |
| 46 | + "Windows MODULE_NOT_FOUND with openclaw\\dist path → true", |
| 47 | + isDistRotationError(err2), |
| 48 | +); |
| 49 | + |
| 50 | +// Case 3: ERR_MODULE_NOT_FOUND with hashed chunk (the actual symptom) |
| 51 | +const err3 = Object.assign( |
| 52 | + new Error("Cannot find module '/usr/lib/node_modules/openclaw/dist/chunks/cleanup-DbGY5-v-.js'"), |
| 53 | + { code: "ERR_MODULE_NOT_FOUND" }, |
| 54 | +); |
| 55 | +assert( |
| 56 | + "Hashed chunk ERR_MODULE_NOT_FOUND in dist → true", |
| 57 | + isDistRotationError(err3), |
| 58 | +); |
| 59 | + |
| 60 | +console.log("\n=== isDistRotationError: negative cases ==="); |
| 61 | + |
| 62 | +// Case 4: ERR_MODULE_NOT_FOUND outside openclaw |
| 63 | +const err4 = Object.assign( |
| 64 | + new Error("Cannot find module '/usr/lib/node_modules/lodash/index.js'"), |
| 65 | + { code: "ERR_MODULE_NOT_FOUND" }, |
| 66 | +); |
| 67 | +assert( |
| 68 | + "ERR_MODULE_NOT_FOUND outside openclaw → false", |
| 69 | + !isDistRotationError(err4), |
| 70 | +); |
| 71 | + |
| 72 | +// Case 5: Third-party package missing, importer under openclaw/dist/ (not rotation) |
| 73 | +const err5 = Object.assign( |
| 74 | + new Error( |
| 75 | + "Cannot find package 'optional-dep'" + |
| 76 | + " imported from /usr/lib/node_modules/openclaw/dist/chunks/get-reply.js", |
| 77 | + ), |
| 78 | + { code: "ERR_MODULE_NOT_FOUND" }, |
| 79 | +); |
| 80 | +assert( |
| 81 | + "third-party dep missing (importer in dist) → false", |
| 82 | + !isDistRotationError(err5), |
| 83 | +); |
| 84 | + |
| 85 | +// Case 6: Other error code |
| 86 | +const err6 = Object.assign(new Error("ENOENT: no such file"), { code: "ENOENT" }); |
| 87 | +assert( |
| 88 | + "ENOENT error → false", |
| 89 | + !isDistRotationError(err6), |
| 90 | +); |
| 91 | + |
| 92 | +// Case 7: No code property |
| 93 | +assert( |
| 94 | + "Error without code → false", |
| 95 | + !isDistRotationError(new Error("plain")), |
| 96 | +); |
| 97 | + |
| 98 | +// Case 8: Non-object inputs |
| 99 | +assert("null → false", !isDistRotationError(null)); |
| 100 | +assert("undefined → false", !isDistRotationError(undefined)); |
| 101 | +assert("string → false", !isDistRotationError("ERR_MODULE_NOT_FOUND")); |
| 102 | + |
| 103 | +console.log("\n=== guardedLoad: behavior verification ==="); |
| 104 | + |
| 105 | +// Simulate the guardedLoad pattern from get-reply.ts |
| 106 | +async function guardedLoad<T>( |
| 107 | + loader: { load(): Promise<T>; clear(): void }, |
| 108 | + label: string, |
| 109 | +): Promise<T> { |
| 110 | + try { |
| 111 | + return await loader.load(); |
| 112 | + } catch (err) { |
| 113 | + if (isDistRotationError(err)) { |
| 114 | + loader.clear(); |
| 115 | + console.log( |
| 116 | + `[ERROR] auto-reply/reply-loader: bundled module changed under running gateway ` + |
| 117 | + `after update/rollback — restart required (lazy module "${label}" failed: ${formatErrorMessage(err)})`, |
| 118 | + ); |
| 119 | + console.log( |
| 120 | + `[WARN] auto-reply/reply-loader: run "systemctl --user restart openclaw-gateway.service" ` + |
| 121 | + `(or equivalent) to reload dist modules`, |
| 122 | + ); |
| 123 | + } |
| 124 | + throw err; |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +// Test: normal load succeeds |
| 129 | +let loadCalls = 0; |
| 130 | +const goodLoader = createLazyPromiseLoader(async () => { |
| 131 | + loadCalls += 1; |
| 132 | + return { key: `value-${loadCalls}` }; |
| 133 | +}); |
| 134 | + |
| 135 | +const result1 = await guardedLoad(goodLoader, "test-module"); |
| 136 | +assert( |
| 137 | + "guardedLoad: normal load returns value", |
| 138 | + result1.key === "value-1", |
| 139 | +); |
| 140 | +assert( |
| 141 | + "guardedLoad: normal load dedupes (single call)", |
| 142 | + loadCalls === 1, |
| 143 | +); |
| 144 | + |
| 145 | +// Test: dist rotation error → clear + re-throw |
| 146 | +let clearCalled = false; |
| 147 | +const distErrLoader = { |
| 148 | + load: async () => { |
| 149 | + throw Object.assign( |
| 150 | + new Error("Cannot find module '/usr/lib/node_modules/openclaw/dist/chunks/stale-hash.js'"), |
| 151 | + { code: "ERR_MODULE_NOT_FOUND" }, |
| 152 | + ); |
| 153 | + }, |
| 154 | + clear: () => { clearCalled = true; }, |
| 155 | +}; |
| 156 | + |
| 157 | +console.log("\n--- dist rotation guard triggers (expected error output below) ---"); |
| 158 | +let threw = false; |
| 159 | +try { |
| 160 | + await guardedLoad(distErrLoader, "stale-module"); |
| 161 | +} catch { |
| 162 | + threw = true; |
| 163 | +} |
| 164 | +assert("guardedLoad: dist rotation error re-thrown", threw); |
| 165 | +assert("guardedLoad: dist rotation clears stale cache", clearCalled); |
| 166 | +assert("guardedLoad: dist rotation propagated to caller", threw); |
| 167 | + |
| 168 | +// Test: non-rotation error → no clear, re-throw |
| 169 | +let clearCalled2 = false; |
| 170 | +const plainErrLoader = { |
| 171 | + load: async () => { |
| 172 | + throw Object.assign(new Error("ENOENT: /tmp/missing"), { code: "ENOENT" }); |
| 173 | + }, |
| 174 | + clear: () => { clearCalled2 = true; }, |
| 175 | +}; |
| 176 | + |
| 177 | +console.log("--- non-rotation error (expected pass-through, no clear) ---"); |
| 178 | +let threw2 = false; |
| 179 | +try { |
| 180 | + await guardedLoad(plainErrLoader, "other-module"); |
| 181 | +} catch { |
| 182 | + threw2 = true; |
| 183 | +} |
| 184 | +assert("guardedLoad: non-rotation error does NOT clear cache", !clearCalled2); |
| 185 | +assert("guardedLoad: non-rotation error still re-thrown", threw2); |
| 186 | + |
| 187 | +console.log(`\n=== Results: ${passed} passed, ${failed} failed ===`); |
| 188 | +process.exit(failed > 0 ? 1 : 0); |
0 commit comments