@lostgradient/weft 0.2.0 → 0.3.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 (207) hide show
  1. package/README.md +47 -22
  2. package/dist/cli/generated/operation-client.generated.d.ts +28 -1
  3. package/dist/cli/generated/operation-client.generated.js +2 -0
  4. package/dist/cli-main.js +79 -79
  5. package/dist/client/handle-delegation.d.ts +4 -0
  6. package/dist/client/handle-delegation.js +6 -0
  7. package/dist/client/http-client-requests.d.ts +2 -0
  8. package/dist/client/http-client-requests.js +3 -0
  9. package/dist/client/http-client.d.ts +4 -1
  10. package/dist/client/http-client.js +9 -1
  11. package/dist/client/interface.d.ts +57 -2
  12. package/dist/client/local.d.ts +4 -1
  13. package/dist/client/local.js +7 -0
  14. package/dist/client/start-body.d.ts +7 -1
  15. package/dist/client/start-body.js +13 -4
  16. package/dist/core/codec/extension-codec.js +4 -2
  17. package/dist/core/codec/index.d.ts +1 -0
  18. package/dist/core/codec/index.js +1 -0
  19. package/dist/core/codec/serializer-registry.d.ts +122 -0
  20. package/dist/core/codec/serializer-registry.js +51 -0
  21. package/dist/core/context/index.d.ts +10 -0
  22. package/dist/core/context/index.js +3 -0
  23. package/dist/core/context/internals.d.ts +10 -0
  24. package/dist/core/context/internals.js +5 -1
  25. package/dist/core/context/run-operation.d.ts +16 -3
  26. package/dist/core/context/run-operation.js +16 -7
  27. package/dist/core/context/speculative-child.js +2 -0
  28. package/dist/core/context/types.d.ts +6 -0
  29. package/dist/core/engine/bulk-operations-purge.js +1 -0
  30. package/dist/core/engine/bulk-operations.js +1 -1
  31. package/dist/core/engine/callback-creators-bundles.js +2 -1
  32. package/dist/core/engine/callback-creators-core.js +2 -1
  33. package/dist/core/engine/construction.d.ts +1 -1
  34. package/dist/core/engine/construction.js +15 -3
  35. package/dist/core/engine/disposal.js +12 -0
  36. package/dist/core/engine/engine-create-types.d.ts +0 -14
  37. package/dist/core/engine/engine-internal-types.d.ts +17 -0
  38. package/dist/core/engine/engine-leak-warnings.d.ts +6 -0
  39. package/dist/core/engine/engine-leak-warnings.js +4 -0
  40. package/dist/core/engine/engine-runtime-helpers.d.ts +17 -0
  41. package/dist/core/engine/engine-runtime-helpers.js +26 -5
  42. package/dist/core/engine/errors.d.ts +74 -0
  43. package/dist/core/engine/errors.js +25 -1
  44. package/dist/core/engine/handle-result.js +1 -1
  45. package/dist/core/engine/handles.d.ts +89 -40
  46. package/dist/core/engine/handles.js +25 -27
  47. package/dist/core/engine/index.d.ts +122 -4
  48. package/dist/core/engine/index.js +82 -5
  49. package/dist/core/engine/inline-launch-queue.d.ts +14 -0
  50. package/dist/core/engine/inline-launch-queue.js +32 -7
  51. package/dist/core/engine/internals.d.ts +26 -10
  52. package/dist/core/engine/lifecycle/fork-helpers.js +1 -7
  53. package/dist/core/engine/lifecycle/persist.js +5 -20
  54. package/dist/core/engine/lifecycle/recovered-services.d.ts +45 -0
  55. package/dist/core/engine/lifecycle/recovered-services.js +34 -0
  56. package/dist/core/engine/lifecycle/resume.js +33 -5
  57. package/dist/core/engine/lifecycle/shared.d.ts +8 -0
  58. package/dist/core/engine/lifecycle/start-batch.js +23 -12
  59. package/dist/core/engine/lifecycle/start-commit.d.ts +47 -0
  60. package/dist/core/engine/lifecycle/start-commit.js +27 -0
  61. package/dist/core/engine/lifecycle/start-exec.d.ts +30 -2
  62. package/dist/core/engine/lifecycle/start-exec.js +38 -0
  63. package/dist/core/engine/lifecycle/start-or-signal-resolution.d.ts +79 -0
  64. package/dist/core/engine/lifecycle/start-or-signal-resolution.js +60 -0
  65. package/dist/core/engine/lifecycle/start-or-signal.d.ts +45 -0
  66. package/dist/core/engine/lifecycle/start-or-signal.js +141 -0
  67. package/dist/core/engine/lifecycle/start.d.ts +3 -3
  68. package/dist/core/engine/lifecycle/start.js +42 -37
  69. package/dist/core/engine/lifecycle.d.ts +3 -2
  70. package/dist/core/engine/lifecycle.js +9 -2
  71. package/dist/core/engine/listing.js +1 -1
  72. package/dist/core/engine/operations-data.d.ts +16 -0
  73. package/dist/core/engine/operations-data.js +6 -0
  74. package/dist/core/engine/operations-time.d.ts +3 -2
  75. package/dist/core/engine/operations-time.js +6 -1
  76. package/dist/core/engine/persisted-data-version.d.ts +5 -9
  77. package/dist/core/engine/persisted-data-version.js +4 -5
  78. package/dist/core/engine/schedule-handle.d.ts +45 -0
  79. package/dist/core/engine/schedule-handle.js +26 -0
  80. package/dist/core/engine/schedules.d.ts +1 -1
  81. package/dist/core/engine/schedules.js +7 -3
  82. package/dist/core/engine/second-instance-detector.d.ts +96 -0
  83. package/dist/core/engine/second-instance-detector.js +108 -0
  84. package/dist/core/engine/signals.d.ts +22 -0
  85. package/dist/core/engine/signals.js +15 -0
  86. package/dist/core/engine/termination/cleanup.d.ts +25 -0
  87. package/dist/core/engine/termination/cleanup.js +21 -1
  88. package/dist/core/engine/termination/complete.js +4 -3
  89. package/dist/core/engine/termination/suspend.d.ts +68 -0
  90. package/dist/core/engine/termination/suspend.js +41 -0
  91. package/dist/core/engine/termination.d.ts +4 -2
  92. package/dist/core/engine/termination.js +2 -0
  93. package/dist/core/engine/validation.js +25 -1
  94. package/dist/core/engine/workflow-feed.d.ts +5 -3
  95. package/dist/core/events/event-map.d.ts +2 -1
  96. package/dist/core/events/workflow-events.d.ts +23 -0
  97. package/dist/core/events/workflow-events.js +9 -0
  98. package/dist/core/inline-execution-strategy.d.ts +5 -0
  99. package/dist/core/inline-execution-strategy.js +2 -1
  100. package/dist/core/list-filter-validation.js +2 -1
  101. package/dist/core/start-workflow-validation.d.ts +22 -0
  102. package/dist/core/start-workflow-validation.js +11 -1
  103. package/dist/core/step-context.d.ts +10 -6
  104. package/dist/core/step-context.js +7 -15
  105. package/dist/core/types/activity.d.ts +6 -3
  106. package/dist/core/types/identity.d.ts +8 -1
  107. package/dist/core/types/launch-metadata.d.ts +33 -0
  108. package/dist/core/types/launch-metadata.js +0 -0
  109. package/dist/core/types/message-handles.d.ts +25 -0
  110. package/dist/core/types/options.d.ts +90 -7
  111. package/dist/core/types/reviews.d.ts +2 -1
  112. package/dist/core/types/services-resolution.d.ts +47 -0
  113. package/dist/core/types/services-resolution.js +0 -0
  114. package/dist/core/types/state.d.ts +11 -11
  115. package/dist/core/types/workflow-builder.d.ts +5 -4
  116. package/dist/core/types/workflow-context.d.ts +25 -0
  117. package/dist/core/types/workflow-function.d.ts +17 -0
  118. package/dist/core/types/workflow-snapshot.d.ts +29 -0
  119. package/dist/core/types/workflow-snapshot.js +0 -0
  120. package/dist/core/types.d.ts +3 -0
  121. package/dist/core/types.js +3 -0
  122. package/dist/core/weft-error.d.ts +46 -14
  123. package/dist/core/weft-error.js +12 -1
  124. package/dist/diagnostics/doctor.js +6 -3
  125. package/dist/diagnostics/format.js +2 -2
  126. package/dist/diagnostics/types.d.ts +1 -0
  127. package/dist/diagnostics/version-check.js +6 -4
  128. package/dist/index.d.ts +10 -5
  129. package/dist/index.js +11 -2
  130. package/dist/json-schema.js +3 -3
  131. package/dist/mcp/cli.js +35 -35
  132. package/dist/mcp/list-filter.js +2 -1
  133. package/dist/mcp/session.js +1 -0
  134. package/dist/observability/index.js +2 -2
  135. package/dist/server/handler.js +30 -30
  136. package/dist/server/index.js +33 -33
  137. package/dist/server/interactive-operations.js +1 -0
  138. package/dist/server/operations/resume-workflow.js +2 -2
  139. package/dist/server/operations/start-or-signal-workflow.d.ts +39 -0
  140. package/dist/server/operations/start-or-signal-workflow.js +140 -0
  141. package/dist/server/operations/start-workflow-options.d.ts +32 -0
  142. package/dist/server/operations/start-workflow-options.js +63 -0
  143. package/dist/server/operations/start-workflow.js +7 -69
  144. package/dist/server/operations/suspend-workflow.d.ts +13 -0
  145. package/dist/server/operations/suspend-workflow.js +36 -0
  146. package/dist/server/rest-binding.d.ts +18 -7
  147. package/dist/server/rest-bindings.js +12 -0
  148. package/dist/server/runtime/task-dispatch.js +5 -3
  149. package/dist/server/runtime/task-polling.d.ts +16 -2
  150. package/dist/server/runtime/task-polling.js +20 -5
  151. package/dist/server/runtime/websocket-worker.js +8 -0
  152. package/dist/server/serve-internals.d.ts +8 -0
  153. package/dist/server/serve-internals.js +4 -2
  154. package/dist/server/task-state.d.ts +8 -0
  155. package/dist/service-worker/index.js +28 -28
  156. package/dist/storage/capabilities.d.ts +10 -2
  157. package/dist/storage/capabilities.js +2 -2
  158. package/dist/storage/http.js +2 -2
  159. package/dist/storage/index.d.ts +7 -1
  160. package/dist/storage/indexeddb.js +1 -1
  161. package/dist/storage/interface.d.ts +40 -0
  162. package/dist/storage/interface.js +1 -1
  163. package/dist/storage/key-prefixes.d.ts +1 -1
  164. package/dist/storage/key-prefixes.js +3 -0
  165. package/dist/storage/lmdb.js +1 -1
  166. package/dist/storage/memory.js +1 -1
  167. package/dist/storage/neon-value-mapping.d.ts +47 -0
  168. package/dist/storage/neon-value-mapping.js +11 -0
  169. package/dist/storage/neon.d.ts +108 -0
  170. package/dist/storage/neon.js +10 -0
  171. package/dist/storage/node-sqlite-loader.d.ts +71 -0
  172. package/dist/storage/node-sqlite-loader.js +41 -0
  173. package/dist/storage/node-sqlite.d.ts +1 -19
  174. package/dist/storage/node-sqlite.js +38 -32
  175. package/dist/storage/postgres-key-value-queries.d.ts +79 -0
  176. package/dist/storage/postgres-key-value-queries.js +63 -0
  177. package/dist/storage/resolve.d.ts +2 -165
  178. package/dist/storage/resolve.js +1 -1
  179. package/dist/storage/scoped-storage.js +1 -1
  180. package/dist/storage/storage-configuration.d.ts +209 -0
  181. package/dist/storage/storage-configuration.js +0 -0
  182. package/dist/storage/text-value-store.d.ts +13 -10
  183. package/dist/storage/turso.js +2 -2
  184. package/dist/storage/typed-storage.js +1 -1
  185. package/dist/storage/web-extension.js +1 -1
  186. package/dist/testing/event-loop.d.ts +36 -2
  187. package/dist/testing/index.d.ts +31 -1
  188. package/dist/testing/index.js +33 -33
  189. package/dist/version.d.ts +1 -1
  190. package/dist/version.js +1 -1
  191. package/dist/worker/index.js +9 -5
  192. package/dist/worker/long-poll.js +4 -0
  193. package/dist/worker/protocol-messages.d.ts +20 -0
  194. package/dist/worker/protocol-schemas.d.ts +32 -0
  195. package/dist/worker/protocol-schemas.js +8 -4
  196. package/dist/worker/protocol-task-result.d.ts +28 -0
  197. package/dist/worker/protocol-task-result.js +76 -0
  198. package/dist/worker/protocol.d.ts +4 -15
  199. package/dist/worker/protocol.js +1 -1
  200. package/dist/worker/registry/fair-share.d.ts +29 -0
  201. package/dist/worker/registry/fair-share.js +30 -0
  202. package/dist/worker/registry/routing.d.ts +18 -0
  203. package/dist/worker/registry/routing.js +14 -0
  204. package/dist/worker/registry/types.d.ts +7 -0
  205. package/dist/worker/registry.d.ts +16 -1
  206. package/dist/worker/registry.js +24 -36
  207. package/package.json +17 -4
@@ -1,5 +1,10 @@
1
- import { compareScores, scoreWorker } from "./registry/fair-share.js";
2
- import { matchesWorkerCapabilities, pickLeastLoaded } from "./registry/routing.js";
1
+ import { FairShareCounters } from "./registry/fair-share.js";
2
+ import {
3
+ matchesWorkerCapabilities,
4
+ pickFairShare,
5
+ pickLeastLoaded,
6
+ pickRoundRobin
7
+ } from "./registry/routing.js";
3
8
  import {
4
9
  deploymentHealth,
5
10
  projectDeploymentSummaries,
@@ -19,7 +24,7 @@ export class WorkerRegistry {
19
24
  this.#deploymentDrainStates = new Map;
20
25
  this.#policy = options?.policy ?? "least-loaded";
21
26
  this.#roundRobinCursor = new Map;
22
- this.#fairShareCounts = new Map;
27
+ this.#fairShareCounts = new FairShareCounters;
23
28
  }
24
29
  get policy() {
25
30
  return this.#policy;
@@ -51,7 +56,7 @@ export class WorkerRegistry {
51
56
  if (info === void 0)
52
57
  return;
53
58
  this.#workers.delete(workerId);
54
- this.#fairShareCounts.delete(workerId);
59
+ this.#fairShareCounts.purge(workerId);
55
60
  for (const [operationId, task] of this.#inFlightTasks)
56
61
  if (task.workerId === workerId)
57
62
  this.#inFlightTasks.delete(operationId);
@@ -99,39 +104,23 @@ export class WorkerRegistry {
99
104
  }
100
105
  #selectByPolicy(eligible, queue, activityName, fairShareKey) {
101
106
  if (this.#policy === "round-robin")
102
- return this.#pickRoundRobin(eligible, queue, activityName);
107
+ return pickRoundRobin(eligible, this.#roundRobinCursor, queue, activityName);
103
108
  if (this.#policy === "fair-share" && fairShareKey !== void 0)
104
- return this.#pickFairShare(eligible, fairShareKey);
109
+ return pickFairShare(eligible, this.#fairShareCounts, fairShareKey);
105
110
  return pickLeastLoaded(eligible);
106
111
  }
107
- #pickRoundRobin(eligible, queue, activityName) {
108
- const key = `${queue ?? "__default__"}::${activityName}`, cursor = this.#roundRobinCursor.get(key) ?? 0, pick = eligible[cursor % eligible.length];
109
- this.#roundRobinCursor.set(key, cursor + 1);
110
- return pick;
111
- }
112
- #pickFairShare(eligible, fairShareKey) {
113
- const winner = eligible.map((worker) => scoreWorker({
114
- id: worker.id,
115
- inFlight: worker.inFlight,
116
- keyLoad: this.#fairShareCounts.get(worker.id)?.get(fairShareKey) ?? 0
117
- })).reduce((best, candidate) => compareScores(candidate, best) < 0 ? candidate : best);
118
- return this.#workers.get(winner.id);
119
- }
120
- assignTask(workerId, operationId, visibilityTimeout, fairShareKey) {
112
+ assignTask(workerId, operationId, visibilityTimeout, fairShareKey, attemptToken) {
121
113
  const deadline = Date.now() + visibilityTimeout, task = {
122
114
  operationId,
123
115
  workerId,
124
116
  deadline,
125
117
  visibilityTimeout
126
118
  };
119
+ if (attemptToken !== void 0)
120
+ task.attemptToken = attemptToken;
127
121
  if (fairShareKey !== void 0) {
128
122
  task.fairShareKey = fairShareKey;
129
- let workerCounts = this.#fairShareCounts.get(workerId);
130
- if (workerCounts === void 0) {
131
- workerCounts = new Map;
132
- this.#fairShareCounts.set(workerId, workerCounts);
133
- }
134
- workerCounts.set(fairShareKey, (workerCounts.get(fairShareKey) ?? 0) + 1);
123
+ this.#fairShareCounts.increment(workerId, fairShareKey);
135
124
  }
136
125
  this.#inFlightTasks.set(operationId, task);
137
126
  this.taskAssigned(workerId);
@@ -164,6 +153,14 @@ export class WorkerRegistry {
164
153
  isAssignedToWorker(operationId, workerId) {
165
154
  return this.#inFlightTasks.get(operationId)?.workerId === workerId;
166
155
  }
156
+ isAssignedToAttempt(operationId, workerId, attemptToken) {
157
+ const task = this.#inFlightTasks.get(operationId);
158
+ if (task === void 0 || task.workerId !== workerId)
159
+ return !1;
160
+ if (task.attemptToken === void 0 || attemptToken === void 0)
161
+ return !0;
162
+ return task.attemptToken === attemptToken;
163
+ }
167
164
  isAssigned(operationId) {
168
165
  return this.#inFlightTasks.has(operationId);
169
166
  }
@@ -285,16 +282,7 @@ export class WorkerRegistry {
285
282
  #releaseFairShare(task) {
286
283
  if (task.fairShareKey === void 0)
287
284
  return;
288
- const workerCounts = this.#fairShareCounts.get(task.workerId);
289
- if (workerCounts === void 0)
290
- return;
291
- const current = workerCounts.get(task.fairShareKey) ?? 0, next = Math.max(0, current - 1);
292
- if (next === 0) {
293
- workerCounts.delete(task.fairShareKey);
294
- if (workerCounts.size === 0)
295
- this.#fairShareCounts.delete(task.workerId);
296
- } else
297
- workerCounts.set(task.fairShareKey, next);
285
+ this.#fairShareCounts.release(task.workerId, task.fairShareKey);
298
286
  }
299
287
  }
300
288
  function createDrainRecord(options) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lostgradient/weft",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A Bun-native durable execution engine with pluggable key-value storage.",
5
5
  "keywords": [
6
6
  "bun",
@@ -107,6 +107,12 @@
107
107
  "import": "./dist/storage/turso.js",
108
108
  "default": "./dist/storage/turso.js"
109
109
  },
110
+ "./storage/neon": {
111
+ "types": "./dist/storage/neon.d.ts",
112
+ "bun": "./dist/storage/neon.js",
113
+ "import": "./dist/storage/neon.js",
114
+ "default": "./dist/storage/neon.js"
115
+ },
110
116
  "./storage/indexeddb": {
111
117
  "types": "./dist/storage/indexeddb.d.ts",
112
118
  "import": "./dist/storage/indexeddb.js",
@@ -202,18 +208,19 @@
202
208
  "lint": "oxlint --type-aware --tsconfig ./tsconfig.json --report-unused-disable-directives-severity=error && bun run scripts/check-lint-disables.ts && bun run scripts/check-import-cycles.ts && bun run scripts/check-internal-imports.ts && bun run scripts/check-engine-internals-field-access.ts && bun run scripts/check-definition-vocabulary.ts && bun run scripts/check-catalog-completeness.ts && bun run scripts/check-catalog-drift.ts && bun run scripts/check-type-ergonomics.ts",
203
209
  "lint:fix": "oxlint --fix --type-aware --tsconfig ./tsconfig.json",
204
210
  "markdown-doctests:inventory": "bun run scripts/extract-markdown-doctests.ts inventory",
205
- "prepack": "bun run build && bun run verify:exports && bun run verify:portability && bun run verify:markdown-doctests && bun run verify:jsdoc && bun run verify:jsdoc:doctests && bun run verify:jsdoc:declarations && bun run check:package-contents && bun run validate:package-consumers",
211
+ "prepack": "bun run scripts/run-gates.ts prepack",
206
212
  "pr:title": "bun run scripts/pr-title.ts",
207
213
  "prepare": "husky",
208
214
  "prepublishOnly": "bun run validate",
209
215
  "start": "bun run src/index.ts",
210
- "test": "bun test --timeout 15000 --path-ignore-patterns='src/benchmarks/**' --path-ignore-patterns='examples/checkout/**' --path-ignore-patterns='src/service-worker/service-worker-browser.test.ts' --path-ignore-patterns='src/storage/indexeddb-browser.test.ts' --path-ignore-patterns='src/storage/web-extension-browser.test.ts' && bun test src/benchmarks --timeout 15000 --parallel=1",
216
+ "test": "bun test --timeout 15000 --path-ignore-patterns='src/benchmarks/**' --path-ignore-patterns='examples/checkout/**' --path-ignore-patterns='src/service-worker/service-worker-browser.test.ts' --path-ignore-patterns='src/storage/indexeddb-browser.test.ts' --path-ignore-patterns='src/storage/web-extension-browser.test.ts'",
217
+ "test:benchmarks": "bun test src/benchmarks --timeout 15000 --parallel=1",
211
218
  "test:browser-smoke": "WEFT_BROWSER_SMOKE=1 bun test --timeout 20000 src/service-worker/service-worker-browser.test.ts src/storage/indexeddb-browser.test.ts src/storage/web-extension-browser.test.ts",
212
219
  "test:coverage": "bun run scripts/check-coverage.ts",
213
220
  "typecheck": "tsc --noEmit",
214
221
  "typecheck:package-root": "bun run build && tsc --noEmit -p tsconfig.package-test-d.json",
215
222
  "typecheck:tests": "tsc --noEmit -p tsconfig.test-d.json && bun run typecheck:package-root",
216
- "validate": "bun run lint && bun run typecheck && bun run typecheck:tests && bun run verify:documentation && bun run verify:no-test-sleeps && bun run verify:public-api-jsdoc && bun run test",
223
+ "validate": "bun run scripts/run-gates.ts validate",
217
224
  "validate:package-consumers": "bun run scripts/validate-package-consumers.ts",
218
225
  "verify:documentation": "bun run scripts/verify-documentation.ts",
219
226
  "verify:exports": "bun run scripts/verify-tree-shaking.ts",
@@ -248,7 +255,9 @@
248
255
  "zod": "^4.3.6"
249
256
  },
250
257
  "devDependencies": {
258
+ "@electric-sql/pglite": "^0.5.1",
251
259
  "@libsql/client": "^0.17.2",
260
+ "@neondatabase/serverless": "^1.1.0",
252
261
  "@types/bun": "^1.3.13",
253
262
  "better-sqlite3": "^12.8.0",
254
263
  "chalk": "^5.6.2",
@@ -267,6 +276,7 @@
267
276
  },
268
277
  "peerDependencies": {
269
278
  "@libsql/client": "^0.17.2",
279
+ "@neondatabase/serverless": "^1.1.0",
270
280
  "@opentelemetry/api": "^1.0.0",
271
281
  "better-sqlite3": "^12.8.0",
272
282
  "lmdb": "^3.5.2"
@@ -275,6 +285,9 @@
275
285
  "@libsql/client": {
276
286
  "optional": true
277
287
  },
288
+ "@neondatabase/serverless": {
289
+ "optional": true
290
+ },
278
291
  "@opentelemetry/api": {
279
292
  "optional": true
280
293
  },