@workflow/world-local 5.0.0-beta.10 → 5.0.0-beta.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/dist/config.d.ts +17 -5
  2. package/dist/config.d.ts.map +1 -1
  3. package/dist/config.js +18 -13
  4. package/dist/config.js.map +1 -1
  5. package/dist/fs.d.ts +58 -2
  6. package/dist/fs.d.ts.map +1 -1
  7. package/dist/fs.js +203 -24
  8. package/dist/fs.js.map +1 -1
  9. package/dist/index.d.ts +15 -2
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +82 -5
  12. package/dist/index.js.map +1 -1
  13. package/dist/init.d.ts +91 -0
  14. package/dist/init.d.ts.map +1 -0
  15. package/dist/init.js +268 -0
  16. package/dist/init.js.map +1 -0
  17. package/dist/instrumentObject.d.ts +8 -0
  18. package/dist/instrumentObject.d.ts.map +1 -0
  19. package/dist/instrumentObject.js +66 -0
  20. package/dist/instrumentObject.js.map +1 -0
  21. package/dist/queue.d.ts +8 -1
  22. package/dist/queue.d.ts.map +1 -1
  23. package/dist/queue.js +164 -53
  24. package/dist/queue.js.map +1 -1
  25. package/dist/storage/events-storage.d.ts +7 -0
  26. package/dist/storage/events-storage.d.ts.map +1 -0
  27. package/dist/storage/events-storage.js +744 -0
  28. package/dist/storage/events-storage.js.map +1 -0
  29. package/dist/storage/filters.d.ts +22 -0
  30. package/dist/storage/filters.d.ts.map +1 -0
  31. package/dist/storage/filters.js +33 -0
  32. package/dist/storage/filters.js.map +1 -0
  33. package/dist/storage/helpers.d.ts +18 -0
  34. package/dist/storage/helpers.d.ts.map +1 -0
  35. package/dist/storage/helpers.js +44 -0
  36. package/dist/storage/helpers.js.map +1 -0
  37. package/dist/storage/hooks-storage.d.ts +12 -0
  38. package/dist/storage/hooks-storage.d.ts.map +1 -0
  39. package/dist/storage/hooks-storage.js +92 -0
  40. package/dist/storage/hooks-storage.js.map +1 -0
  41. package/dist/storage/index.d.ts +12 -0
  42. package/dist/storage/index.d.ts.map +1 -0
  43. package/dist/storage/index.js +32 -0
  44. package/dist/storage/index.js.map +1 -0
  45. package/dist/storage/legacy.d.ts +13 -0
  46. package/dist/storage/legacy.d.ts.map +1 -0
  47. package/dist/storage/legacy.js +73 -0
  48. package/dist/storage/legacy.js.map +1 -0
  49. package/dist/storage/runs-storage.d.ts +7 -0
  50. package/dist/storage/runs-storage.d.ts.map +1 -0
  51. package/dist/storage/runs-storage.js +58 -0
  52. package/dist/storage/runs-storage.js.map +1 -0
  53. package/dist/storage/steps-storage.d.ts +7 -0
  54. package/dist/storage/steps-storage.d.ts.map +1 -0
  55. package/dist/storage/steps-storage.js +49 -0
  56. package/dist/storage/steps-storage.js.map +1 -0
  57. package/dist/storage.d.ts +9 -2
  58. package/dist/storage.d.ts.map +1 -1
  59. package/dist/storage.js +8 -455
  60. package/dist/storage.js.map +1 -1
  61. package/dist/streamer.d.ts +4 -2
  62. package/dist/streamer.d.ts.map +1 -1
  63. package/dist/streamer.js +451 -104
  64. package/dist/streamer.js.map +1 -1
  65. package/dist/telemetry.d.ts +28 -0
  66. package/dist/telemetry.d.ts.map +1 -0
  67. package/dist/telemetry.js +71 -0
  68. package/dist/telemetry.js.map +1 -0
  69. package/dist/test-helpers.d.ts +54 -0
  70. package/dist/test-helpers.d.ts.map +1 -0
  71. package/dist/test-helpers.js +118 -0
  72. package/dist/test-helpers.js.map +1 -0
  73. package/dist/util.js.map +1 -1
  74. package/package.json +12 -11
package/dist/queue.js CHANGED
@@ -1,26 +1,80 @@
1
1
  import { setTimeout } from 'node:timers/promises';
2
- import { JsonTransport } from '@vercel/queue';
3
2
  import { MessageId, ValidQueueName } from '@workflow/world';
3
+ import { Sema } from 'async-sema';
4
4
  import { monotonicFactory } from 'ulid';
5
5
  import { Agent } from 'undici';
6
- import z from 'zod';
6
+ import { z } from 'zod/v4';
7
7
  import { resolveBaseUrl } from './config.js';
8
+ import { jsonReplacer, jsonReviver } from './fs.js';
9
+ import { getPackageInfo } from './init.js';
10
+ /**
11
+ * JSON transport that preserves Uint8Array values using the same
12
+ * replacer/reviver that world-local uses for filesystem storage.
13
+ * Uint8Array → { __type: 'Uint8Array', data: '<base64>' } in JSON.
14
+ */
15
+ class TypedJsonTransport {
16
+ contentType = 'application/json';
17
+ serialize(value) {
18
+ return Buffer.from(JSON.stringify(value, jsonReplacer));
19
+ }
20
+ async deserialize(stream) {
21
+ const chunks = [];
22
+ const reader = stream.getReader();
23
+ while (true) {
24
+ const { done, value } = await reader.read();
25
+ if (done)
26
+ break;
27
+ if (value)
28
+ chunks.push(value);
29
+ }
30
+ const text = Buffer.concat(chunks).toString();
31
+ return JSON.parse(text, jsonReviver);
32
+ }
33
+ }
8
34
  // For local queue, there is no technical limit on the message visibility lifespan,
9
35
  // but the environment variable can be used for testing purposes to set a max visibility limit.
10
36
  const LOCAL_QUEUE_MAX_VISIBILITY = parseInt(process.env.WORKFLOW_LOCAL_QUEUE_MAX_VISIBILITY ?? '0', 10) ||
11
37
  Infinity;
12
- // Create a custom agent with unlimited headers timeout for long-running steps
13
- const httpAgent = new Agent({
14
- headersTimeout: 0,
15
- });
38
+ // Maximum safe delay for setTimeout in Node.js (2^31 - 1 milliseconds ≈ 24.85 days)
39
+ // Larger values cause "TimeoutOverflowWarning: X does not fit into a 32-bit signed integer"
40
+ // When the clamped timeout fires, the handler will recalculate remaining time from
41
+ // persistent state and return another timeoutSeconds if needed.
42
+ const MAX_SAFE_TIMEOUT_MS = 2147483647;
43
+ // The local workers share the same Node.js process and event loop,
44
+ // so we need to limit concurrency to avoid overwhelming the system.
45
+ const DEFAULT_CONCURRENCY_LIMIT = 1000;
46
+ const WORKFLOW_LOCAL_QUEUE_CONCURRENCY = parseInt(process.env.WORKFLOW_LOCAL_QUEUE_CONCURRENCY ?? '0', 10) ||
47
+ DEFAULT_CONCURRENCY_LIMIT;
48
+ function getQueueRoute(queueName) {
49
+ if (queueName.startsWith('__wkf_step_')) {
50
+ return { pathname: 'step', prefix: '__wkf_step_' };
51
+ }
52
+ if (queueName.startsWith('__wkf_workflow_')) {
53
+ return { pathname: 'flow', prefix: '__wkf_workflow_' };
54
+ }
55
+ throw new Error('Unknown queue name prefix');
56
+ }
16
57
  export function createQueue(config) {
17
- const transport = new JsonTransport();
58
+ // Create a custom agent optimized for high-concurrency local workflows:
59
+ // - headersTimeout: 0 allows long-running steps
60
+ // - connections: 1000 allows many parallel connections to the same host
61
+ // - pipelining: 1 (default) for HTTP/1.1 compatibility
62
+ // - keepAliveTimeout: 30s keeps connections warm for rapid step execution
63
+ const httpAgent = new Agent({
64
+ headersTimeout: 0,
65
+ connections: 1000,
66
+ keepAliveTimeout: 30_000,
67
+ });
68
+ const transport = new TypedJsonTransport();
18
69
  const generateId = monotonicFactory();
70
+ const semaphore = new Sema(WORKFLOW_LOCAL_QUEUE_CONCURRENCY);
19
71
  /**
20
72
  * holds inflight messages by idempotency key to ensure
21
73
  * that we don't queue the same message multiple times
22
74
  */
23
75
  const inflightMessages = new Map();
76
+ /** Direct in-process handlers by queue prefix, bypassing HTTP when set. */
77
+ const directHandlers = new Map();
24
78
  const queue = async (queueName, message, opts) => {
25
79
  const cleanup = [];
26
80
  if (opts?.idempotencyKey) {
@@ -30,17 +84,13 @@ export function createQueue(config) {
30
84
  }
31
85
  }
32
86
  const body = transport.serialize(message);
33
- let pathname;
34
- if (queueName.startsWith('__wkf_step_')) {
35
- pathname = `step`;
36
- }
37
- else if (queueName.startsWith('__wkf_workflow_')) {
38
- pathname = `flow`;
39
- }
40
- else {
41
- throw new Error('Unknown queue name prefix');
42
- }
87
+ const { pathname, prefix } = getQueueRoute(queueName);
43
88
  const messageId = MessageId.parse(`msg_${generateId()}`);
89
+ // Extract identifiers from the message for structured logging.
90
+ // Workflow messages have `runId`, step messages have `workflowRunId` + `stepId`.
91
+ const msg = message;
92
+ const runId = (msg.runId ?? msg.workflowRunId ?? undefined);
93
+ const stepId = (msg.stepId ?? undefined);
44
94
  if (opts?.idempotencyKey) {
45
95
  const key = opts.idempotencyKey;
46
96
  inflightMessages.set(key, messageId);
@@ -49,46 +99,96 @@ export function createQueue(config) {
49
99
  });
50
100
  }
51
101
  (async () => {
52
- let defaultRetriesLeft = 3;
53
- const baseUrl = await resolveBaseUrl(config);
54
- for (let attempt = 0; defaultRetriesLeft > 0; attempt++) {
55
- defaultRetriesLeft--;
56
- const response = await fetch(`${baseUrl}/.well-known/workflow/v1/${pathname}`, {
57
- method: 'POST',
58
- duplex: 'half',
59
- // @ts-expect-error undici type differences
60
- dispatcher: httpAgent,
61
- headers: {
102
+ const token = semaphore.tryAcquire();
103
+ if (!token) {
104
+ console.warn(`[world-local]: concurrency limit (${WORKFLOW_LOCAL_QUEUE_CONCURRENCY}) reached, waiting for queue to free up`);
105
+ await semaphore.acquire();
106
+ }
107
+ // Safety limit to prevent infinite loops in the local queue.
108
+ // The actual max delivery enforcement happens in the workflow/step handlers
109
+ // (at MAX_QUEUE_DELIVERIES = 48), so this just needs to be comfortably higher.
110
+ const MAX_LOCAL_SAFETY_LIMIT = 256;
111
+ try {
112
+ for (let attempt = 0; attempt < MAX_LOCAL_SAFETY_LIMIT; attempt++) {
113
+ const headers = {
114
+ ...opts?.headers,
62
115
  'content-type': 'application/json',
63
116
  'x-vqs-queue-name': queueName,
64
117
  'x-vqs-message-id': messageId,
65
118
  'x-vqs-message-attempt': String(attempt + 1),
66
- },
67
- body,
68
- });
69
- if (response.ok) {
70
- return;
71
- }
72
- const text = await response.text();
73
- if (response.status === 503) {
74
- try {
75
- const timeoutSeconds = Number(JSON.parse(text).timeoutSeconds);
76
- await setTimeout(timeoutSeconds * 1000);
77
- defaultRetriesLeft++;
78
- continue;
119
+ };
120
+ const directHandler = directHandlers.get(prefix);
121
+ let response;
122
+ if (directHandler) {
123
+ const req = new Request(`http://localhost/.well-known/workflow/v1/${pathname}`, {
124
+ method: 'POST',
125
+ headers,
126
+ body,
127
+ });
128
+ response = await directHandler(req);
129
+ }
130
+ else {
131
+ const baseUrl = await resolveBaseUrl(config);
132
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any -- undici v7 dispatcher types don't match @types/node's RequestInit
133
+ response = await fetch(`${baseUrl}/.well-known/workflow/v1/${pathname}`, {
134
+ method: 'POST',
135
+ duplex: 'half',
136
+ dispatcher: httpAgent,
137
+ headers,
138
+ body,
139
+ });
79
140
  }
80
- catch { }
141
+ const text = await response.text();
142
+ if (response.ok) {
143
+ try {
144
+ const timeoutSeconds = Number(JSON.parse(text).timeoutSeconds);
145
+ if (Number.isFinite(timeoutSeconds) && timeoutSeconds >= 0) {
146
+ // Clamp to MAX_SAFE_TIMEOUT_MS to avoid Node.js setTimeout overflow warning.
147
+ // When this fires early, the handler recalculates remaining time from
148
+ // persistent state and returns another timeoutSeconds if needed.
149
+ if (timeoutSeconds > 0) {
150
+ const timeoutMs = Math.min(timeoutSeconds * 1000, MAX_SAFE_TIMEOUT_MS);
151
+ await setTimeout(timeoutMs);
152
+ }
153
+ continue;
154
+ }
155
+ }
156
+ catch { }
157
+ return;
158
+ }
159
+ console.error(`[world-local] Queue message failed (attempt ${attempt + 1}, HTTP ${response.status})`, {
160
+ queueName,
161
+ messageId,
162
+ ...(runId && { runId }),
163
+ ...(stepId && { stepId }),
164
+ handlerError: text,
165
+ });
166
+ // 5s linear backoff to approximate VQS retry timing in local dev.
167
+ // VQS uses 5s linear for attempts 1–32, then exponential, but for
168
+ // local dev linear 5s is sufficient — the handler enforces the real
169
+ // cap at MAX_QUEUE_DELIVERIES (48) which keeps total time under ~4min.
170
+ await setTimeout(5000);
81
171
  }
82
- console.error(`[embedded world] Failed to queue message`, {
172
+ console.error(`[world-local] Queue message exhausted safety limit (${MAX_LOCAL_SAFETY_LIMIT} attempts)`, {
83
173
  queueName,
84
- text,
85
- status: response.status,
86
- headers: Object.fromEntries(response.headers.entries()),
87
- body: body.toString(),
174
+ messageId,
175
+ ...(runId && { runId }),
176
+ ...(stepId && { stepId }),
88
177
  });
89
178
  }
90
- console.error(`[embedded world] Reached max retries of embedded world queue implementation`);
91
- })().finally(() => {
179
+ finally {
180
+ semaphore.release();
181
+ }
182
+ })()
183
+ .catch((err) => {
184
+ // Silently ignore client disconnect errors (e.g., browser refresh during streaming)
185
+ // These are expected and should not cause unhandled rejection warnings
186
+ const isAbortError = err?.name === 'AbortError' || err?.name === 'ResponseAborted';
187
+ if (!isAbortError) {
188
+ console.error('[local world] Queue operation failed:', err);
189
+ }
190
+ })
191
+ .finally(() => {
92
192
  for (const fn of cleanup) {
93
193
  fn();
94
194
  }
@@ -116,15 +216,15 @@ export function createQueue(config) {
116
216
  if (!queueName.startsWith(prefix)) {
117
217
  return Response.json({ error: 'Unhandled queue' }, { status: 400 });
118
218
  }
119
- const body = await new JsonTransport().deserialize(req.body);
219
+ const body = await new TypedJsonTransport().deserialize(req.body);
120
220
  try {
121
221
  const result = await handler(body, { attempt, queueName, messageId });
122
222
  let timeoutSeconds = null;
123
223
  if (typeof result?.timeoutSeconds === 'number') {
124
224
  timeoutSeconds = Math.min(result.timeoutSeconds, LOCAL_QUEUE_MAX_VISIBILITY);
125
225
  }
126
- if (timeoutSeconds) {
127
- return Response.json({ timeoutSeconds }, { status: 503 });
226
+ if (timeoutSeconds != null) {
227
+ return Response.json({ timeoutSeconds });
128
228
  }
129
229
  return Response.json({ ok: true });
130
230
  }
@@ -134,8 +234,19 @@ export function createQueue(config) {
134
234
  };
135
235
  };
136
236
  const getDeploymentId = async () => {
137
- return 'dpl_embedded';
237
+ const packageInfo = await getPackageInfo();
238
+ return `dpl_local@${packageInfo.version}`;
239
+ };
240
+ return {
241
+ queue,
242
+ createQueueHandler,
243
+ getDeploymentId,
244
+ registerHandler(prefix, handler) {
245
+ directHandlers.set(prefix, handler);
246
+ },
247
+ async close() {
248
+ await httpAgent.close();
249
+ },
138
250
  };
139
- return { queue, createQueueHandler, getDeploymentId };
140
251
  }
141
252
  //# sourceMappingURL=queue.js.map
package/dist/queue.js.map CHANGED
@@ -1 +1 @@
1
- {"version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C1B,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { setTimeout } from 'node:timers/promises';\nimport type { Transport } from '@vercel/queue';\nimport { MessageId, type Queue, ValidQueueName } from '@workflow/world';\nimport { Sema } from 'async-sema';\nimport { monotonicFactory } from 'ulid';\nimport { Agent } from 'undici';\nimport { z } from 'zod/v4';\nimport type { Config } from './config.js';\nimport { resolveBaseUrl } from './config.js';\nimport { jsonReplacer, jsonReviver } from './fs.js';\nimport { getPackageInfo } from './init.js';\n\n/**\n * JSON transport that preserves Uint8Array values using the same\n * replacer/reviver that world-local uses for filesystem storage.\n * Uint8Array → { __type: 'Uint8Array', data: '<base64>' } in JSON.\n */\nclass TypedJsonTransport implements Transport<unknown> {\n readonly contentType = 'application/json';\n\n serialize(value: unknown): Buffer {\n return Buffer.from(JSON.stringify(value, jsonReplacer));\n }\n\n async deserialize(stream: ReadableStream<Uint8Array>): Promise<unknown> {\n const chunks: Uint8Array[] = [];\n const reader = stream.getReader();\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n if (value) chunks.push(value);\n }\n const text = Buffer.concat(chunks).toString();\n return JSON.parse(text, jsonReviver);\n }\n}\n\n// For local queue, there is no technical limit on the message visibility lifespan,\n// but the environment variable can be used for testing purposes to set a max visibility limit.\nconst LOCAL_QUEUE_MAX_VISIBILITY =\n parseInt(process.env.WORKFLOW_LOCAL_QUEUE_MAX_VISIBILITY ?? '0', 10) ||\n Infinity;\n\n// Maximum safe delay for setTimeout in Node.js (2^31 - 1 milliseconds ≈ 24.85 days)\n// Larger values cause \"TimeoutOverflowWarning: X does not fit into a 32-bit signed integer\"\n// When the clamped timeout fires, the handler will recalculate remaining time from\n// persistent state and return another timeoutSeconds if needed.\nconst MAX_SAFE_TIMEOUT_MS = 2147483647;\n\n// The local workers share the same Node.js process and event loop,\n// so we need to limit concurrency to avoid overwhelming the system.\nconst DEFAULT_CONCURRENCY_LIMIT = 1000;\nconst WORKFLOW_LOCAL_QUEUE_CONCURRENCY =\n parseInt(process.env.WORKFLOW_LOCAL_QUEUE_CONCURRENCY ?? '0', 10) ||\n DEFAULT_CONCURRENCY_LIMIT;\n\nexport type DirectHandler = (req: Request) => Promise<Response>;\n\nexport type LocalQueue = Queue & {\n /** Close the HTTP agent and release resources. */\n close(): Promise<void>;\n /** Register a direct in-process handler for a queue prefix, bypassing HTTP. */\n registerHandler(\n prefix: '__wkf_step_' | '__wkf_workflow_',\n handler: DirectHandler\n ): void;\n};\n\nfunction getQueueRoute(queueName: ValidQueueName): {\n pathname: 'flow' | 'step';\n prefix: '__wkf_step_' | '__wkf_workflow_';\n} {\n if (queueName.startsWith('__wkf_step_')) {\n return { pathname: 'step', prefix: '__wkf_step_' };\n }\n if (queueName.startsWith('__wkf_workflow_')) {\n return { pathname: 'flow', prefix: '__wkf_workflow_' };\n }\n throw new Error('Unknown queue name prefix');\n}\n\nexport function createQueue(config: Partial<Config>): LocalQueue {\n // Create a custom agent optimized for high-concurrency local workflows:\n // - headersTimeout: 0 allows long-running steps\n // - connections: 1000 allows many parallel connections to the same host\n // - pipelining: 1 (default) for HTTP/1.1 compatibility\n // - keepAliveTimeout: 30s keeps connections warm for rapid step execution\n const httpAgent = new Agent({\n headersTimeout: 0,\n connections: 1000,\n keepAliveTimeout: 30_000,\n });\n const transport = new TypedJsonTransport();\n const generateId = monotonicFactory();\n const semaphore = new Sema(WORKFLOW_LOCAL_QUEUE_CONCURRENCY);\n\n /**\n * holds inflight messages by idempotency key to ensure\n * that we don't queue the same message multiple times\n */\n const inflightMessages = new Map<string, MessageId>();\n /** Direct in-process handlers by queue prefix, bypassing HTTP when set. */\n const directHandlers = new Map<string, DirectHandler>();\n\n const queue: Queue['queue'] = async (queueName, message, opts) => {\n const cleanup = [] as (() => void)[];\n\n if (opts?.idempotencyKey) {\n const existing = inflightMessages.get(opts.idempotencyKey);\n if (existing) {\n return { messageId: existing };\n }\n }\n\n const body = transport.serialize(message);\n const { pathname, prefix } = getQueueRoute(queueName);\n const messageId = MessageId.parse(`msg_${generateId()}`);\n\n // Extract identifiers from the message for structured logging.\n // Workflow messages have `runId`, step messages have `workflowRunId` + `stepId`.\n const msg = message as Record<string, unknown>;\n const runId = (msg.runId ?? msg.workflowRunId ?? undefined) as\n | string\n | undefined;\n const stepId = (msg.stepId ?? undefined) as string | undefined;\n\n if (opts?.idempotencyKey) {\n const key = opts.idempotencyKey;\n inflightMessages.set(key, messageId);\n cleanup.push(() => {\n inflightMessages.delete(key);\n });\n }\n\n (async () => {\n const token = semaphore.tryAcquire();\n if (!token) {\n console.warn(\n `[world-local]: concurrency limit (${WORKFLOW_LOCAL_QUEUE_CONCURRENCY}) reached, waiting for queue to free up`\n );\n await semaphore.acquire();\n }\n // Safety limit to prevent infinite loops in the local queue.\n // The actual max delivery enforcement happens in the workflow/step handlers\n // (at MAX_QUEUE_DELIVERIES = 48), so this just needs to be comfortably higher.\n const MAX_LOCAL_SAFETY_LIMIT = 256;\n try {\n for (let attempt = 0; attempt < MAX_LOCAL_SAFETY_LIMIT; attempt++) {\n const headers: Record<string, string> = {\n ...opts?.headers,\n 'content-type': 'application/json',\n 'x-vqs-queue-name': queueName,\n 'x-vqs-message-id': messageId,\n 'x-vqs-message-attempt': String(attempt + 1),\n };\n const directHandler = directHandlers.get(prefix);\n let response: Response;\n\n if (directHandler) {\n const req = new Request(\n `http://localhost/.well-known/workflow/v1/${pathname}`,\n {\n method: 'POST',\n headers,\n body,\n }\n );\n response = await directHandler(req);\n } else {\n const baseUrl = await resolveBaseUrl(config);\n // eslint-disable-next-line @typescript-eslint/no-explicit-any -- undici v7 dispatcher types don't match @types/node's RequestInit\n response = await fetch(\n `${baseUrl}/.well-known/workflow/v1/${pathname}`,\n {\n method: 'POST',\n duplex: 'half',\n dispatcher: httpAgent,\n headers,\n body,\n } as any\n );\n }\n\n const text = await response.text();\n\n if (response.ok) {\n try {\n const timeoutSeconds = Number(JSON.parse(text).timeoutSeconds);\n if (Number.isFinite(timeoutSeconds) && timeoutSeconds >= 0) {\n // Clamp to MAX_SAFE_TIMEOUT_MS to avoid Node.js setTimeout overflow warning.\n // When this fires early, the handler recalculates remaining time from\n // persistent state and returns another timeoutSeconds if needed.\n if (timeoutSeconds > 0) {\n const timeoutMs = Math.min(\n timeoutSeconds * 1000,\n MAX_SAFE_TIMEOUT_MS\n );\n await setTimeout(timeoutMs);\n }\n continue;\n }\n } catch {}\n return;\n }\n\n console.error(\n `[world-local] Queue message failed (attempt ${attempt + 1}, HTTP ${response.status})`,\n {\n queueName,\n messageId,\n ...(runId && { runId }),\n ...(stepId && { stepId }),\n handlerError: text,\n }\n );\n\n // 5s linear backoff to approximate VQS retry timing in local dev.\n // VQS uses 5s linear for attempts 1–32, then exponential, but for\n // local dev linear 5s is sufficient — the handler enforces the real\n // cap at MAX_QUEUE_DELIVERIES (48) which keeps total time under ~4min.\n await setTimeout(5000);\n }\n\n console.error(\n `[world-local] Queue message exhausted safety limit (${MAX_LOCAL_SAFETY_LIMIT} attempts)`,\n {\n queueName,\n messageId,\n ...(runId && { runId }),\n ...(stepId && { stepId }),\n }\n );\n } finally {\n semaphore.release();\n }\n })()\n .catch((err) => {\n // Silently ignore client disconnect errors (e.g., browser refresh during streaming)\n // These are expected and should not cause unhandled rejection warnings\n const isAbortError =\n err?.name === 'AbortError' || err?.name === 'ResponseAborted';\n if (!isAbortError) {\n console.error('[local world] Queue operation failed:', err);\n }\n })\n .finally(() => {\n for (const fn of cleanup) {\n fn();\n }\n });\n\n return { messageId };\n };\n\n const HeaderParser = z.object({\n 'x-vqs-queue-name': ValidQueueName,\n 'x-vqs-message-id': MessageId,\n 'x-vqs-message-attempt': z.coerce.number(),\n });\n\n const createQueueHandler: Queue['createQueueHandler'] = (prefix, handler) => {\n return async (req) => {\n const headers = HeaderParser.safeParse(Object.fromEntries(req.headers));\n\n if (!headers.success || !req.body) {\n return Response.json(\n {\n error: !req.body\n ? 'Missing request body'\n : 'Missing required headers',\n },\n { status: 400 }\n );\n }\n\n const queueName = headers.data['x-vqs-queue-name'];\n const messageId = headers.data['x-vqs-message-id'];\n const attempt = headers.data['x-vqs-message-attempt'];\n\n if (!queueName.startsWith(prefix)) {\n return Response.json({ error: 'Unhandled queue' }, { status: 400 });\n }\n\n const body = await new TypedJsonTransport().deserialize(req.body);\n try {\n const result = await handler(body, { attempt, queueName, messageId });\n\n let timeoutSeconds: number | null = null;\n if (typeof result?.timeoutSeconds === 'number') {\n timeoutSeconds = Math.min(\n result.timeoutSeconds,\n LOCAL_QUEUE_MAX_VISIBILITY\n );\n }\n\n if (timeoutSeconds != null) {\n return Response.json({ timeoutSeconds });\n }\n\n return Response.json({ ok: true });\n } catch (error) {\n return Response.json(String(error), { status: 500 });\n }\n };\n };\n\n const getDeploymentId: Queue['getDeploymentId'] = async () => {\n const packageInfo = await getPackageInfo();\n return `dpl_local@${packageInfo.version}`;\n };\n\n return {\n queue,\n createQueueHandler,\n getDeploymentId,\n registerHandler(\n prefix: '__wkf_step_' | '__wkf_workflow_',\n handler: DirectHandler\n ) {\n directHandlers.set(prefix, handler);\n },\n async close() {\n await httpAgent.close();\n },\n };\n}\n"]}
@@ -0,0 +1,7 @@
1
+ import type { Storage } from '@workflow/world';
2
+ /**
3
+ * Creates the events storage implementation using the filesystem.
4
+ * Implements the Storage['events'] interface with create, list, and listByCorrelationId operations.
5
+ */
6
+ export declare function createEventsStorage(basedir: string, tag?: string): Storage['events'];
7
+ //# sourceMappingURL=events-storage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"events-storage.d.ts","sourceRoot":"","sources":["../../src/storage/events-storage.ts"],"names":[],"mappings":"AAUA,OAAO,KAAK,EAMV,OAAO,EAGR,MAAM,iBAAiB,CAAC;AAiDzB;;;GAGG;AACH,wBAAgB,mBAAmB,CACjC,OAAO,EAAE,MAAM,EACf,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,QAAQ,CAAC,CAi9BnB"}