@lostgradient/weft 0.8.0 → 0.9.0

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 (103) hide show
  1. package/README.md +7 -3
  2. package/dist/cli/generated/operation-client.generated.d.ts +1 -0
  3. package/dist/cli-main.js +78 -78
  4. package/dist/core/context/durable-operations.js +1 -1
  5. package/dist/core/context/index.d.ts +1 -0
  6. package/dist/core/context/index.js +2 -0
  7. package/dist/core/context/operation-request.d.ts +2 -0
  8. package/dist/core/context/run-operation.js +3 -2
  9. package/dist/core/context/speculative-child.js +1 -0
  10. package/dist/core/context/types.d.ts +1 -0
  11. package/dist/core/engine/activity-heartbeat-tracking.d.ts +1 -1
  12. package/dist/core/engine/activity-heartbeat-tracking.js +5 -1
  13. package/dist/core/engine/anonymous-signal-sequence.js +0 -2
  14. package/dist/core/engine/bulk-operations.js +10 -29
  15. package/dist/core/engine/callback-creators-bundles.js +1 -1
  16. package/dist/core/engine/disposal.js +1 -0
  17. package/dist/core/engine/engine-internal-types.d.ts +1 -0
  18. package/dist/core/engine/index.js +13 -9
  19. package/dist/core/engine/inline-launch-queue.js +1 -1
  20. package/dist/core/engine/internals.d.ts +23 -1
  21. package/dist/core/engine/lifecycle/fork-helpers.js +1 -0
  22. package/dist/core/engine/lifecycle/resume.js +6 -0
  23. package/dist/core/engine/lifecycle/start-exec.d.ts +2 -2
  24. package/dist/core/engine/lifecycle/start-exec.js +6 -4
  25. package/dist/core/engine/lifecycle/start.js +1 -0
  26. package/dist/core/engine/lifecycle/transition.js +6 -0
  27. package/dist/core/engine/memo-durable-activity.js +1 -1
  28. package/dist/core/engine/operations-activity.d.ts +1 -1
  29. package/dist/core/engine/operations-activity.js +7 -5
  30. package/dist/core/engine/operations-time.d.ts +2 -2
  31. package/dist/core/engine/operations-time.js +40 -6
  32. package/dist/core/engine/schedules.js +3 -0
  33. package/dist/core/engine/termination/cleanup.js +15 -13
  34. package/dist/core/engine/termination/finalizer-activity.d.ts +1 -1
  35. package/dist/core/engine/termination/finalizer-activity.js +5 -1
  36. package/dist/core/engine/termination/finalizer.js +1 -1
  37. package/dist/core/engine/validation/schedule-options.d.ts +9 -0
  38. package/dist/core/engine/validation/schedule-options.js +59 -0
  39. package/dist/core/engine/validation/schedule.d.ts +3 -8
  40. package/dist/core/engine/validation/schedule.js +40 -56
  41. package/dist/core/engine/validation.js +1 -0
  42. package/dist/core/execution-strategy.d.ts +1 -0
  43. package/dist/core/inline-execution-strategy.context-options.d.ts +1 -0
  44. package/dist/core/inline-execution-strategy.context-options.js +3 -0
  45. package/dist/core/types/activity.d.ts +11 -0
  46. package/dist/core/types/checkpoint.d.ts +1 -0
  47. package/dist/core/types/schedules.d.ts +10 -0
  48. package/dist/core/types/state.d.ts +6 -0
  49. package/dist/core/types/workflow-context.d.ts +13 -0
  50. package/dist/core/worker-execution-strategy.d.ts +1 -0
  51. package/dist/core/worker-inbound-message.d.ts +1 -0
  52. package/dist/core/worker-inbound-message.js +3 -0
  53. package/dist/json-schema.js +2 -2
  54. package/dist/mcp/cli.js +32 -32
  55. package/dist/observability/index.js +2 -2
  56. package/dist/runtime/portable.d.ts +1 -1
  57. package/dist/server/handler.js +27 -27
  58. package/dist/server/index.d.ts +2 -0
  59. package/dist/server/index.js +30 -30
  60. package/dist/server/operations/create-schedule.d.ts +2 -0
  61. package/dist/server/operations/create-schedule.js +13 -2
  62. package/dist/server/rest-binding.d.ts +1 -1
  63. package/dist/server/runtime/task-dispatch.js +6 -0
  64. package/dist/server/runtime/task-polling.js +9 -1
  65. package/dist/server/runtime/task-reconciliation.js +2 -1
  66. package/dist/server/task-queue-types.d.ts +2 -0
  67. package/dist/server/task-state.d.ts +4 -0
  68. package/dist/service-worker/index.d.ts +15 -14
  69. package/dist/service-worker/index.js +28 -28
  70. package/dist/service-worker/scheduler.d.ts +41 -1
  71. package/dist/storage/auto.d.ts +36 -18
  72. package/dist/storage/auto.js +1 -1
  73. package/dist/storage/compressed-storage.js +1 -1
  74. package/dist/storage/indexeddb.d.ts +6 -1
  75. package/dist/storage/indexeddb.js +1 -1
  76. package/dist/storage/interface.js +1 -1
  77. package/dist/storage/lmdb.js +1 -1
  78. package/dist/storage/memory.js +1 -1
  79. package/dist/storage/neon.js +1 -1
  80. package/dist/storage/resolve.js +1 -1
  81. package/dist/storage/scoped-storage.js +1 -1
  82. package/dist/storage/testing.js +1 -1
  83. package/dist/storage/turso.js +1 -1
  84. package/dist/storage/typed-storage.js +1 -1
  85. package/dist/storage/web-extension.d.ts +1 -1
  86. package/dist/storage/web-extension.js +1 -1
  87. package/dist/testing/index.js +33 -33
  88. package/dist/version.d.ts +1 -1
  89. package/dist/version.js +1 -1
  90. package/dist/worker/execute-with-interceptors.d.ts +9 -3
  91. package/dist/worker/execute-with-interceptors.js +14 -2
  92. package/dist/worker/long-poll.d.ts +2 -1
  93. package/dist/worker/long-poll.js +1 -1
  94. package/dist/worker/protocol-messages.d.ts +2 -0
  95. package/dist/worker/protocol-schemas.d.ts +8 -0
  96. package/dist/worker/protocol-schemas.js +1 -0
  97. package/dist/worker/protocol.js +1 -1
  98. package/dist/worker/remote-activity-context.d.ts +2 -0
  99. package/dist/workers/activity-runner.d.ts +2 -0
  100. package/dist/workers/activity-runner.js +9 -1
  101. package/dist/workers/workflow-runner.d.ts +2 -1
  102. package/dist/workers/workflow-runner.js +1 -0
  103. package/package.json +1 -1
@@ -6,6 +6,7 @@ declare const createScheduleInput: z.ZodObject<{
6
6
  every: z.ZodOptional<z.ZodUnknown>;
7
7
  input: z.ZodOptional<z.ZodUnknown>;
8
8
  id: z.ZodOptional<z.ZodUnknown>;
9
+ description: z.ZodOptional<z.ZodUnknown>;
9
10
  overlap: z.ZodOptional<z.ZodUnknown>;
10
11
  backfill: z.ZodOptional<z.ZodUnknown>;
11
12
  jitter: z.ZodOptional<z.ZodUnknown>;
@@ -21,6 +22,7 @@ export declare const createScheduleOperation: import("../operation-catalog.ts").
21
22
  every?: unknown;
22
23
  input?: unknown;
23
24
  id?: unknown;
25
+ description?: unknown;
24
26
  overlap?: unknown;
25
27
  backfill?: unknown;
26
28
  jitter?: unknown;
@@ -14,6 +14,7 @@ const VALID_SCHEDULE_OVERLAP_POLICIES = new Set([
14
14
  every: z.unknown().optional().describe("Interval period (duration string or milliseconds). Supply exactly one of cronExpression or every."),
15
15
  input: z.unknown().optional(),
16
16
  id: z.unknown().optional(),
17
+ description: z.unknown().optional().describe("Operator-facing schedule description. Runtime validation requires a string."),
17
18
  overlap: z.unknown().optional(),
18
19
  backfill: z.unknown().optional(),
19
20
  jitter: z.unknown().optional()
@@ -32,6 +33,12 @@ function validateOptionalScheduleFields(input) {
32
33
  throw invalidParamsFault('Field "id" must be a non-empty string');
33
34
  validatedId = input.id;
34
35
  }
36
+ let validatedDescription;
37
+ if (input.description !== void 0) {
38
+ if (typeof input.description !== "string")
39
+ throw invalidParamsFault('Field "description" must be a string');
40
+ validatedDescription = input.description;
41
+ }
35
42
  let validatedOverlap;
36
43
  if (input.overlap !== void 0) {
37
44
  if (typeof input.overlap !== "string" || !isScheduleOverlapPolicy(input.overlap))
@@ -52,14 +59,15 @@ function validateOptionalScheduleFields(input) {
52
59
  }
53
60
  return {
54
61
  id: validatedId,
62
+ description: validatedDescription,
55
63
  overlap: validatedOverlap,
56
64
  backfill: validatedBackfill,
57
65
  jitter: validatedJitter
58
66
  };
59
67
  }
60
68
  function validateCreateScheduleInput(input) {
61
- const { type, spec } = validateRequiredScheduleFields(input), { id, overlap, backfill, jitter } = validateOptionalScheduleFields(input);
62
- return { type, spec, id, overlap, backfill, jitter };
69
+ const { type, spec } = validateRequiredScheduleFields(input), { id, description, overlap, backfill, jitter } = validateOptionalScheduleFields(input);
70
+ return { type, spec, id, description, overlap, backfill, jitter };
63
71
  }
64
72
  export const createScheduleOperation = defineOperation({
65
73
  name: "weft.schedules.create",
@@ -77,6 +85,7 @@ export const createScheduleOperation = defineOperation({
77
85
  invoke: async ({ input, engine }) => {
78
86
  const typedEngine = engine, validated = validateCreateScheduleInput(input), options = {
79
87
  ...validated.id !== void 0 ? { id: validated.id } : {},
88
+ ...validated.description !== void 0 ? { description: validated.description } : {},
80
89
  ...validated.overlap !== void 0 ? { overlap: validated.overlap } : {},
81
90
  ...validated.backfill !== void 0 ? { backfill: validated.backfill } : {},
82
91
  ...validated.jitter !== void 0 ? { jitter: validated.jitter } : {}
@@ -102,6 +111,7 @@ export const createScheduleRestBinding = {
102
111
  every: { kind: "body-field", bodyField: "every" },
103
112
  input: { kind: "body-field", bodyField: "input" },
104
113
  id: { kind: "body-field", bodyField: "id" },
114
+ description: { kind: "body-field", bodyField: "description" },
105
115
  overlap: { kind: "body-field", bodyField: "overlap" },
106
116
  backfill: { kind: "body-field", bodyField: "backfill" },
107
117
  jitter: { kind: "body-field", bodyField: "jitter" }
@@ -124,6 +134,7 @@ export const createScheduleRestBinding = {
124
134
  every: Object.hasOwn(record, "every") ? record.every : void 0,
125
135
  input: record.input,
126
136
  id: record.id,
137
+ description: record.description,
127
138
  overlap: record.overlap,
128
139
  backfill: record.backfill,
129
140
  jitter: record.jitter
@@ -131,7 +131,7 @@ export type RestBinding<Input, Output> = {
131
131
  * detail (for example, `get-workflow-result` returns the custom message
132
132
  * `"Timeout waiting for workflow result"` on a `Timeout`, and `get-stream-chunks`
133
133
  * handles `InvalidParams` inline). This per-operation hook is the current
134
- * contract, not a transitional shim.
134
+ * contract.
135
135
  */
136
136
  readonly shapeFault?: (fault: OperationFault) => Response;
137
137
  };
@@ -74,6 +74,7 @@ async function selectAndReserveWorker(context, options, task, queue, visibilityT
74
74
  visibilityTimeout,
75
75
  retryPolicy: task.retryPolicy,
76
76
  workflowId: task.workflowId,
77
+ workflowExecutionToken: task.workflowExecutionToken,
77
78
  attemptToken
78
79
  }, normalizedInflightRecord = await transitionQueuedToInflight(options.engine.storage, task.operationId, inflightRecord, {
79
80
  queuedRecord: existingQueuedRecord,
@@ -86,6 +87,9 @@ async function selectAndReserveWorker(context, options, task, queue, visibilityT
86
87
  input: task.input === void 0 ? null : task.input,
87
88
  attempt: task.attempt ?? 1,
88
89
  attemptToken,
90
+ ...task.workflowExecutionToken !== void 0 && {
91
+ workflowExecutionToken: task.workflowExecutionToken
92
+ },
89
93
  ...task.headers ? { headers: task.headers } : {}
90
94
  }));
91
95
  recordTaskQueueLatencyMetric(context.metricsCollector, normalizedInflightRecord);
@@ -116,6 +120,7 @@ async function enqueueTaskForLongPoll(context, options, task, queue, visibilityT
116
120
  retryPolicy: task.retryPolicy,
117
121
  queuedAt: Date.now(),
118
122
  workflowId: task.workflowId,
123
+ workflowExecutionToken: task.workflowExecutionToken,
119
124
  retryCount: Math.max(0, attempt - 1),
120
125
  requeueCount: 0
121
126
  }, existingQueuedRecord = await readQueuedRecord(options.engine.storage, task.operationId), normalizedQueuedRecord = await markQueued(options.engine.storage, mergeQueuedRecordLifecycle(freshRecord, existingQueuedRecord)), enqueued = context.taskQueue.enqueue(queue, {
@@ -126,6 +131,7 @@ async function enqueueTaskForLongPoll(context, options, task, queue, visibilityT
126
131
  retryPolicy: task.retryPolicy,
127
132
  visibilityTimeout,
128
133
  workflowId: task.workflowId,
134
+ workflowExecutionToken: task.workflowExecutionToken,
129
135
  firstQueuedAt: normalizedQueuedRecord.firstQueuedAt,
130
136
  lastQueuedAt: normalizedQueuedRecord.lastQueuedAt,
131
137
  lastDispatchedAt: normalizedQueuedRecord.lastDispatchedAt,
@@ -98,6 +98,7 @@ export function createLongPollInflightRecord(queue, task) {
98
98
  visibilityTimeout,
99
99
  retryPolicy: task.retryPolicy,
100
100
  workflowId: task.workflowId,
101
+ workflowExecutionToken: task.workflowExecutionToken,
101
102
  attemptToken: crypto.randomUUID(),
102
103
  firstQueuedAt: task.firstQueuedAt ?? task.enqueuedAt ?? now,
103
104
  lastQueuedAt: task.lastQueuedAt ?? task.enqueuedAt ?? now,
@@ -139,7 +140,14 @@ export async function handleTaskPollRequest(context, options, request, url, prin
139
140
  const rawTimeout = url.searchParams.get("timeout"), timeout = rawTimeout !== null ? Math.min(Math.max(0, Number(rawTimeout)), MAX_POLL_TIMEOUT) : DEFAULT_POLL_TIMEOUT, task = await context.taskQueue.poll(queue, activities, timeout, request.signal);
140
141
  if (task !== null) {
141
142
  const claim = await markTaskClaimedByLongPollWorker(context, options, queue, task);
142
- return Response.json({ ...task, workerId: claim.workerId, attemptToken: claim.attemptToken });
143
+ return Response.json({
144
+ ...task,
145
+ workerId: claim.workerId,
146
+ attemptToken: claim.attemptToken,
147
+ ...task.workflowExecutionToken !== void 0 && {
148
+ workflowExecutionToken: task.workflowExecutionToken
149
+ }
150
+ });
143
151
  }
144
152
  return new Response(null, { status: 204 });
145
153
  }
@@ -68,7 +68,8 @@ function createRequeuedTaskDispatch(record, nextAttempt) {
68
68
  queue: record.queue,
69
69
  attempt: nextAttempt,
70
70
  visibilityTimeout: record.visibilityTimeout,
71
- workflowId: record.workflowId
71
+ workflowId: record.workflowId,
72
+ workflowExecutionToken: record.workflowExecutionToken
72
73
  };
73
74
  if (record.retryPolicy !== void 0)
74
75
  taskDispatch.retryPolicy = record.retryPolicy;
@@ -13,6 +13,8 @@ export interface PendingTask extends TaskLifecycleFields {
13
13
  headers?: Record<string, string> | undefined;
14
14
  /** Workflow that dispatched this activity. Present when the dispatch included a workflowId. */
15
15
  workflowId?: string | undefined;
16
+ /** Durable token for the workflow run that dispatched this activity, when known. */
17
+ workflowExecutionToken?: string | undefined;
16
18
  /**
17
19
  * Task priority. Higher values are dequeued first. Tasks with equal priority
18
20
  * maintain FIFO order. Default: 0. Agent workflow tasks use priority 10.
@@ -55,6 +55,8 @@ export interface QueuedRecord extends TaskLifecycleFields {
55
55
  queuedAt: number;
56
56
  /** Workflow that dispatched this activity. Present when the dispatch included a workflowId. */
57
57
  workflowId?: string | undefined;
58
+ /** Durable token for the workflow run that dispatched this activity, when known. */
59
+ workflowExecutionToken?: string | undefined;
58
60
  }
59
61
  /** Persisted record for a task in the inflight state. */
60
62
  export interface InflightRecord extends TaskLifecycleFields {
@@ -69,6 +71,8 @@ export interface InflightRecord extends TaskLifecycleFields {
69
71
  retryPolicy?: RetryPolicy | undefined;
70
72
  /** Workflow that dispatched this activity. Present when the dispatch included a workflowId. */
71
73
  workflowId?: string | undefined;
74
+ /** Durable token for the workflow run that dispatched this activity, when known. */
75
+ workflowExecutionToken?: string | undefined;
72
76
  /**
73
77
  * Unique, unguessable token identifying this dispatch attempt. Rotated on every
74
78
  * (re-)dispatch because each dispatch writes a fresh InflightRecord. The
@@ -7,8 +7,10 @@
7
7
  * @module service-worker
8
8
  */
9
9
  import type { Engine } from '../core/engine';
10
- import type { ServiceWorkerScheduler } from './scheduler';
10
+ import type { ServiceWorkerScheduler } from './scheduler.ts';
11
11
  import type { MinimalExtendableEvent, MinimalFetchEvent, MinimalPeriodicSyncEvent } from './shared.ts';
12
+ export { ServiceWorkerScheduler } from './scheduler.ts';
13
+ export type { ServiceWorkerSchedulerOptions } from './scheduler.ts';
12
14
  export { buildDelegatedRequest, DEFAULT_PERIODIC_SYNC_TAG, normalizePathPrefix } from './shared.ts';
13
15
  export type { MinimalExtendableEvent, MinimalFetchEvent, MinimalPeriodicSyncEvent, } from './shared.ts';
14
16
  /**
@@ -73,21 +75,20 @@ export declare function createFetchHandler(options: ServiceWorkerOptions): (even
73
75
  * `event.waitUntil(...)` so the sync extends until the tick promise
74
76
  * resolves.
75
77
  *
76
- * Note: `ServiceWorkerScheduler` is the parameter type but is not currently
77
- * re-exported from `@lostgradient/weft/service-worker` — construct the scheduler in the
78
- * module that imports it directly (e.g. the dashboard runtime) and pass
79
- * the instance to this factory.
80
- *
81
78
  * @example
82
79
  * ```ts
83
- * import { createPeriodicSyncHandler } from '@lostgradient/weft/service-worker';
84
- *
85
- * // In your Service Worker (with a scheduler from the runtime that imports it):
86
- * declare const scheduler: { tick(): Promise<void> };
87
- * const handler = createPeriodicSyncHandler(
88
- * scheduler as Parameters<typeof createPeriodicSyncHandler>[0],
89
- * 'weft-timers',
90
- * );
80
+ * import { ServiceWorkerScheduler, createPeriodicSyncHandler } from '@lostgradient/weft/service-worker';
81
+ * import { MemoryStorage } from '@lostgradient/weft';
82
+ *
83
+ * const storage = new MemoryStorage();
84
+ *
85
+ * const scheduler = new ServiceWorkerScheduler({
86
+ * storage,
87
+ * onTimerFired: (entry) => {
88
+ * console.log(`Timer ${entry.id} fired.`);
89
+ * },
90
+ * });
91
+ * const handler = createPeriodicSyncHandler(scheduler, 'weft-timers');
91
92
  * // self.addEventListener('periodicsync', handler);
92
93
  * console.log(typeof handler); // 'function'
93
94
  * ```