|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import { |
| 3 | + NO_FILE_CONTEXT, |
| 4 | + StandardResourceCatalog, |
| 5 | +} from "../src/v3/resource-catalog/standardResourceCatalog.js"; |
| 6 | + |
| 7 | +// Regression tests for COULD_NOT_FIND_EXECUTOR on warm worker processes when |
| 8 | +// a task's `task()` / `schemaTask()` call is evaluated during another task's |
| 9 | +// execution (e.g. as a side effect of `await import(...)` of a module that |
| 10 | +// contains a task definition). |
| 11 | +// |
| 12 | +// Production throw site: |
| 13 | +// - managed-run-worker.ts:566 (post-wrap) |
| 14 | +// - dev-run-worker.ts:578 (post-wrap) |
| 15 | +// Pre-fix symptom: `resourceCatalog.getTask(execution.task.id)` returned |
| 16 | +// undefined even after the worker re-imported the task entrypoint. |
| 17 | +// |
| 18 | +// Pre-fix mechanism: `registerTaskMetadata` silently returned when |
| 19 | +// `_currentFileContext` was unset. Any `task()` call firing during a |
| 20 | +// running task's run() / lifecycle hooks (directly, or transitively via a |
| 21 | +// dynamic import) hit the silent guard. Node's ESM module cache then |
| 22 | +// prevented recovery — the worker's setContext + re-import fallback didn't |
| 23 | +// re-evaluate the module body, so the `task()` call never fired again. |
| 24 | +// |
| 25 | +// Post-fix: the runtime workers wrap their `executor.execute(...)` call with |
| 26 | +// `setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT)` so any `task()` |
| 27 | +// call firing during execution registers normally with sentinel file |
| 28 | +// metadata. The catalog detects the sentinel and emits a one-time warning |
| 29 | +// per task id to keep the bundle-shape pattern visible. The indexer never |
| 30 | +// sets this sentinel context — its behavior is unchanged. |
| 31 | + |
| 32 | +describe("StandardResourceCatalog — runtime registration via sentinel context", () => { |
| 33 | + afterEach(() => { |
| 34 | + delete (globalThis as { __catalogRegisterTaskMetadata?: unknown }) |
| 35 | + .__catalogRegisterTaskMetadata; |
| 36 | + vi.restoreAllMocks(); |
| 37 | + }); |
| 38 | + |
| 39 | + it("silently drops registration when no context is set (indexer's invariant)", () => { |
| 40 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 41 | + const catalog = new StandardResourceCatalog(); |
| 42 | + |
| 43 | + catalog.registerTaskMetadata({ |
| 44 | + id: "no-context-task", |
| 45 | + fns: { run: async () => "ok" }, |
| 46 | + }); |
| 47 | + |
| 48 | + expect(catalog.getTask("no-context-task")).toBeUndefined(); |
| 49 | + expect(warn).not.toHaveBeenCalled(); |
| 50 | + }); |
| 51 | + |
| 52 | + it( |
| 53 | + "registers normally and warns once when the sentinel context is set " + |
| 54 | + "(simulates the worker's executor wrap)", |
| 55 | + () => { |
| 56 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 57 | + const catalog = new StandardResourceCatalog(); |
| 58 | + |
| 59 | + catalog.setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT); |
| 60 | + catalog.registerTaskMetadata({ |
| 61 | + id: "lazy-task", |
| 62 | + fns: { run: async () => "ok" }, |
| 63 | + }); |
| 64 | + catalog.clearCurrentFileContext(); |
| 65 | + |
| 66 | + const registered = catalog.getTask("lazy-task"); |
| 67 | + expect(registered).toBeDefined(); |
| 68 | + expect(registered?.id).toBe("lazy-task"); |
| 69 | + expect(registered?.filePath).toBe(NO_FILE_CONTEXT); |
| 70 | + expect(registered?.entryPoint).toBe(NO_FILE_CONTEXT); |
| 71 | + expect(warn).toHaveBeenCalledTimes(1); |
| 72 | + expect(warn.mock.calls[0]?.[0]).toContain("lazy-task"); |
| 73 | + } |
| 74 | + ); |
| 75 | + |
| 76 | + it( |
| 77 | + "warm-start path: a task whose top-level definition fires during a " + |
| 78 | + "dynamic import inside the sentinel wrap remains findable; the " + |
| 79 | + "worker's setContext + re-import fallback (managed-run-worker.ts:482) " + |
| 80 | + "is not needed", |
| 81 | + async () => { |
| 82 | + vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 83 | + const catalog = new StandardResourceCatalog(); |
| 84 | + |
| 85 | + (globalThis as { __catalogRegisterTaskMetadata?: unknown }) |
| 86 | + .__catalogRegisterTaskMetadata = ( |
| 87 | + task: Parameters<StandardResourceCatalog["registerTaskMetadata"]>[0] |
| 88 | + ) => { |
| 89 | + catalog.registerTaskMetadata(task); |
| 90 | + }; |
| 91 | + |
| 92 | + // Simulate the worker wrap: setContext(NO_FILE_CONTEXT) → run user code |
| 93 | + // (which does a dynamic import) → clearContext. |
| 94 | + catalog.setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT); |
| 95 | + await import("./fixtures/dynamic-task-module.mjs"); |
| 96 | + catalog.clearCurrentFileContext(); |
| 97 | + |
| 98 | + const registered = catalog.getTask("lazy-task"); |
| 99 | + expect(registered).toBeDefined(); |
| 100 | + expect(registered?.filePath).toBe(NO_FILE_CONTEXT); |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + it("warns at most once per task id under the sentinel context", () => { |
| 105 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 106 | + const catalog = new StandardResourceCatalog(); |
| 107 | + |
| 108 | + catalog.setCurrentFileContext(NO_FILE_CONTEXT, NO_FILE_CONTEXT); |
| 109 | + |
| 110 | + const register = (id: string) => |
| 111 | + catalog.registerTaskMetadata({ |
| 112 | + id, |
| 113 | + fns: { run: async () => "ok" }, |
| 114 | + }); |
| 115 | + |
| 116 | + register("task-a"); |
| 117 | + register("task-a"); |
| 118 | + register("task-a"); |
| 119 | + expect(warn).toHaveBeenCalledTimes(1); |
| 120 | + |
| 121 | + register("task-b"); |
| 122 | + expect(warn).toHaveBeenCalledTimes(2); |
| 123 | + |
| 124 | + catalog.clearCurrentFileContext(); |
| 125 | + }); |
| 126 | + |
| 127 | + it( |
| 128 | + "control: real file context registers without firing the sentinel warning", |
| 129 | + async () => { |
| 130 | + const warn = vi.spyOn(console, "warn").mockImplementation(() => {}); |
| 131 | + const catalog = new StandardResourceCatalog(); |
| 132 | + |
| 133 | + (globalThis as { __catalogRegisterTaskMetadata?: unknown }) |
| 134 | + .__catalogRegisterTaskMetadata = ( |
| 135 | + task: Parameters<StandardResourceCatalog["registerTaskMetadata"]>[0] |
| 136 | + ) => { |
| 137 | + catalog.registerTaskMetadata(task); |
| 138 | + }; |
| 139 | + |
| 140 | + catalog.setCurrentFileContext( |
| 141 | + "/app/dist/lazy-task.entry.mjs", |
| 142 | + "src/tasks/lazy-task.ts" |
| 143 | + ); |
| 144 | + await import("./fixtures/dynamic-task-module.mjs?control"); |
| 145 | + catalog.clearCurrentFileContext(); |
| 146 | + |
| 147 | + const task = catalog.getTask("lazy-task"); |
| 148 | + expect(task).toBeDefined(); |
| 149 | + expect(task?.filePath).toBe("/app/dist/lazy-task.entry.mjs"); |
| 150 | + expect(task?.entryPoint).toBe("src/tasks/lazy-task.ts"); |
| 151 | + expect(warn).not.toHaveBeenCalled(); |
| 152 | + } |
| 153 | + ); |
| 154 | +}); |
0 commit comments