@nanobpm/engine-wasm 0.8.7 → 0.9.1

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.
package/README.md CHANGED
@@ -44,19 +44,69 @@ import init, { TestEngine } from "@nanobpm/engine-wasm";
44
44
  await init(); // instantiate the wasm module
45
45
  const engine = new TestEngine();
46
46
 
47
- engine.deploy(bpmnXml); // deploy a BPMN diagram
47
+ engine.deploy(bpmnXml); // deploy a BPMN diagram (or a DMN decision)
48
48
  const handle = engine.createInstance("my-process", "{}");
49
49
  const snap = JSON.parse(engine.snapshot()); // live primary state
50
50
  const trace = JSON.parse(engine.events()); // the event log
51
51
  engine.reset(); // back to a clean engine
52
52
  ```
53
53
 
54
- The lean surface covers primary state and execution: `deploy`, `createInstance`,
55
- `snapshot`, `events`, job ops (`activateJobs`, `completeJob`, `failJob`,
56
- `updateRetries`, `throwError`), user-task ops (`completeUserTask`,
54
+ The lean surface covers primary state and execution: `deploy`, `deployDecision`,
55
+ `evaluateDecision`, `createInstance`,
56
+ `snapshot`, `events`, `replayEvents`, job ops (`activateJobs`, `completeJob`, `failJob`,
57
+ `updateRetries`, `updateTimeout`, `throwError`), user-task ops (`completeUserTask`,
57
58
  `assignUserTask`, …), messaging/signals, the virtual clock (`advanceTime`,
58
59
  `tickNow`), and `reset`. It links **no** SQLite / read-model code.
59
60
 
61
+ `deploy` accepts **either** a BPMN process **or** a DMN decision resource: a
62
+ document with no BPMN `<process>` is routed to decision deployment (equivalent to
63
+ calling `deployDecision`), so a `businessRuleTask` carrying a
64
+ `zeebe:calledDecision` can resolve its decision. `evaluateDecision(decisionId,
65
+ variablesJson)` evaluates a deployed decision standalone.
66
+
67
+ ### Optional job leasing and agent history
68
+
69
+ `activateJobs(type, maxJobs, timeoutMs, worker, withLease?)` defaults to nonleasing
70
+ for **every** job kind and agent marker. Activated jobs always contain
71
+ `leaseToken`: `null` without a lease, otherwise an opaque string. Never parse it
72
+ as a number. Once a job is leased, subsequent activations must also opt in;
73
+ nonleasing workers skip it after failures and timeouts.
74
+
75
+ Pass the token as the final optional argument to `completeJob`, `completeAgentJob`,
76
+ `failJob`, or `throwError`. Leased lifecycle commands require the current token.
77
+ `updateRetries` and `updateTimeout` also accept a final token; it is optional, but
78
+ a supplied stale value is rejected. Timeout updates accept signed millisecond
79
+ durations; zero and negative updates make the activation immediately eligible for
80
+ expiry on the next clock tick without discarding its current lease.
81
+
82
+ Agent APIs follow the vendored stable/8.10 contract:
83
+
84
+ - `createAgentInstance(JSON.stringify({elementInstanceKey, jobKey, jobLease, history}))`
85
+ requires nonempty history establishing CONFIGURATION. `jobLease` is the activated
86
+ job's `leaseToken`, unchanged. Definition, limits, tools, and metrics are history
87
+ fields, not top-level request properties.
88
+ - `updateAgentInstance(agentInstanceKey, JSON.stringify({elementInstanceKey, jobKey,
89
+ jobLease, status?, history?}))` requires all three attribution fields even without
90
+ history.
91
+ - Both return canonical results containing positionally correlated `createdHistory`;
92
+ CREATE also returns `agentInstanceKey`. Duplicate CREATE rejects rather than upserting.
93
+ - Failure and timeout leave history PENDING. Job completion commits the winning
94
+ attempt and discards superseded attempts; cancellation or a business error
95
+ discards pending history. Searches default to COMMITTED. Timestamps are RFC-3339
96
+ strings and loop iterations start at 1.
97
+ - System prompts are typed content-block arrays. Persisted historical text remains
98
+ one TEXT block, even when the text resembles JSON.
99
+ - Metrics preserve the difference between an omitted object and an object with
100
+ nullable counters. Zero and signed values remain observations, never sentinels.
101
+
102
+ Agent searches currently support exact scalar filters and `$eq`, plus `$in` for
103
+ history commit status. Other advanced operators are rejected; pagination and sorting
104
+ are not implemented by the simulation API.
105
+
106
+ `replayEvents(engine.events())` restores a complete exported trace through core
107
+ replay, including opaque lease state. It rejects unknown event types and missing
108
+ prefixes rather than silently dropping records.
109
+
60
110
  ## Usage — read-model
61
111
 
62
112
  ```js
@@ -13,7 +13,7 @@ export class TestEngine {
13
13
  * (key, type, instance/element, retries, variables) for the dispatch loop to
14
14
  * hand to worker handlers. The host owns the wall clock via `tickNow`.
15
15
  */
16
- activateJobs(job_type: string, max_jobs: number, timeout_ms: number, worker: string): string;
16
+ activateJobs(job_type: string, max_jobs: number, timeout_ms: number, worker: string, with_lease?: boolean | null): string;
17
17
  /**
18
18
  * Advance the virtual clock by `by_ms` milliseconds, firing any timers that
19
19
  * become due and expiring any lapsed job locks.
@@ -39,8 +39,9 @@ export class TestEngine {
39
39
  */
40
40
  cancelInstance(instance_key: string): string;
41
41
  /**
42
- * Complete an AgentInstance by its dedicated key, driving it to the terminal
43
- * `COMPLETED` status. Returns the snapshot.
42
+ * Legacy embedded-only completion extension. It neither commits history nor
43
+ * advances BPMN; canonical workers complete their owning job instead.
44
+ * Returns the snapshot.
44
45
  */
45
46
  completeAgentInstance(agent_instance_key: string): string;
46
47
  /**
@@ -64,13 +65,13 @@ export class TestEngine {
64
65
  * like `completeJob` — e.g. the agent's final
65
66
  * decision when it signals `completionConditionFulfilled`.
66
67
  */
67
- completeAgentJob(job_key: string, variables_json: string, agent_result_json: string): string;
68
+ completeAgentJob(job_key: string, variables_json: string, agent_result_json: string, lease_token?: string | null): string;
68
69
  /**
69
70
  * Complete a waiting job by key, merging `variables_json` (a JSON object
70
71
  * string) into the instance. The job is activated first if it has not been
71
72
  * already, so the UI can complete a freshly-created job directly.
72
73
  */
73
- completeJob(job_key: string, variables_json: string): string;
74
+ completeJob(job_key: string, variables_json: string, lease_token?: string | null): string;
74
75
  /**
75
76
  * Complete a waiting user task by key, merging `variables_json` into the
76
77
  * instance before the parked token resumes. The task must be in the
@@ -87,17 +88,9 @@ export class TestEngine {
87
88
  */
88
89
  correlateMessage(message_name: string, correlation_key: string, variables_json: string): string;
89
90
  /**
90
- * Reconcile / create an AgentInstance for an already-activated agent task.
91
- * `request_json` is `{ elementInstanceKey, jobKey?, jobLease?, definition?,
92
- * limits?, history? }` where `definition` is `{ model?, provider?,
93
- * systemPrompt? }`, `limits` is `{ maxTokens?, maxModelCalls?, maxToolCalls? }`
94
- * (omitted limits default to unlimited), and `history` is an initial batch of
95
- * turns (see the turn shape on `updateAgentInstance`). `jobKey`/`jobLease`
96
- * are the activation's job attribution: **required** for an `external`
97
- * (job-backed) agent element, where the CREATE is rejected unless they
98
- * reference that element's ACTIVATED job with a matching lease token and
99
- * `elementInstanceKey` (#1099); unused for the engine-native
100
- * `aiAgentTask`/`aiAgentSubProcess` variants. Returns the snapshot.
91
+ * Create an agent using `{ elementInstanceKey, jobKey, jobLease, history }`.
92
+ * History must establish its CONFIGURATION. Returns the canonical creation
93
+ * result with `agentInstanceKey` and positionally correlated `createdHistory`.
101
94
  */
102
95
  createAgentInstance(request_json: string): string;
103
96
  /**
@@ -149,11 +142,26 @@ export class TestEngine {
149
142
  */
150
143
  debugStep(): string;
151
144
  /**
152
- * Parse and deploy a BPMN resource. Returns a JSON object
153
- * `{ "processIds": [...], "snapshot": {...} }` on success, or throws a
154
- * JS error carrying the parse/deploy failure message.
145
+ * Parse and deploy a BPMN **or** DMN resource — the one entry point accepts
146
+ * both (issue #1158). A BPMN process resource deploys as before and returns
147
+ * `{ "processIds": [...], "snapshot": {...} }`. A DMN decision resource (no
148
+ * `<process>` element) is routed to decision deployment and returns the same
149
+ * shape as the `deployDecision` method
150
+ * (`{ "decisionRequirementsId": ..., "decisionRequirementsKey": ..., "version": N, "decisions": [{ "decisionId", "decisionName", "decisionKey", "version" }], "snapshot": {...} }`),
151
+ * so a `zeebe:calledDecision` on a business rule task can finally resolve.
152
+ * On failure it throws a JS error carrying the parse/deploy message for the
153
+ * format the document most resembles.
155
154
  */
156
155
  deploy(xml: string): string;
156
+ /**
157
+ * Parse and deploy a DMN decision-requirements resource explicitly. Registers
158
+ * every `<decision>` it contains (by id) so a business rule task's
159
+ * `zeebe:calledDecision` resolves and `evaluateDecision` can run it. Returns a
160
+ * JSON object
161
+ * `{ "decisionRequirementsId": ..., "decisionRequirementsKey": ..., "version": N, "decisions": [{ "decisionId", "decisionName", "decisionKey", "version" }], "snapshot": {...} }`
162
+ * on success, or throws a JS error carrying the parse/deploy failure message.
163
+ */
164
+ deployDecision(xml: string): string;
157
165
  /**
158
166
  * Deploy a single `form-js` `.form` resource (the verbatim form-js JSON
159
167
  * document). Mirrors the native deploy decomposition, which registers a
@@ -182,6 +190,18 @@ export class TestEngine {
182
190
  * on success, or throws a JS error carrying the deploy failure message.
183
191
  */
184
192
  deployResource(resource_name: string, content: string): string;
193
+ /**
194
+ * Evaluate a deployed decision by id against the given variables — the
195
+ * standalone counterpart to a business rule task's in-line evaluation. The
196
+ * decision must already be deployed (via `deploy`/`deployDecision`). Read-only:
197
+ * it evaluates and returns the result without mutating engine state or
198
+ * recording a decision instance. `variables_json` is a JSON object string
199
+ * (`"{}"` / `""` for none). Returns
200
+ * `{ "decisionId": ..., "decisionKey": ..., "output": <value> }` on success,
201
+ * or throws a JS error carrying an "unknown decision" or evaluation-failure
202
+ * message.
203
+ */
204
+ evaluateDecision(decision_id: string, variables_json: string): string;
185
205
  /**
186
206
  * The full ordered event log emitted so far, as a JSON array of
187
207
  * `{ seq, now, type, ...payload }`. Useful for a step-through / trace view.
@@ -191,7 +211,7 @@ export class TestEngine {
191
211
  * Fail a waiting job by key with the given remaining `retries` and message.
192
212
  * With no retries left this raises an incident (visible in the snapshot).
193
213
  */
194
- failJob(job_key: string, retries: number, message: string): string;
214
+ failJob(job_key: string, retries: number, message: string, lease_token?: string | null): string;
195
215
  /**
196
216
  * Migrate a running process instance to a target process definition (Zeebe
197
217
  * "migrate process instance"): re-point every active element instance at the
@@ -216,6 +236,11 @@ export class TestEngine {
216
236
  * Create a fresh, empty simulated engine. The virtual clock starts at 0.
217
237
  */
218
238
  constructor();
239
+ /**
240
+ * Restore a complete trace returned by `events()`, using the core journal
241
+ * decoder and replay engine. Replaces this simulation only after decoding succeeds.
242
+ */
243
+ replayEvents(events_json: string): string;
219
244
  /**
220
245
  * Discard all engine state (deployed definitions, instances, jobs, timers,
221
246
  * the event log and the virtual clock), returning the engine to the same
@@ -249,7 +274,7 @@ export class TestEngine {
249
274
  * raised. The job is activated first if needed, so the UI can throw an
250
275
  * error directly from a freshly-created job. Returns the snapshot.
251
276
  */
252
- throwError(job_key: string, error_code: string, error_message: string): string;
277
+ throwError(job_key: string, error_code: string, error_message: string, lease_token?: string | null): string;
253
278
  /**
254
279
  * Set the engine clock to a wall-clock instant (ms), then trigger due timers
255
280
  * and expire lapsed job locks. The embedded host calls this with `Date.now()`
@@ -263,29 +288,21 @@ export class TestEngine {
263
288
  */
264
289
  unassignUserTask(user_task_key: string): string;
265
290
  /**
266
- * Advance an AgentInstance: set its `status` (a REST spelling other than
267
- * `COMPLETED`, which is reachable only through `completeAgentInstance` and is
268
- * rejected here with a targeted error), accumulate `metrics`, optionally
269
- * replace `tools`, and append a `history` batch. `request_json` is
270
- * `{ agentInstanceKey, elementInstanceKey, elementId, processInstanceKey,
271
- * status?, metrics?, tools?, jobKey?, jobLease?, history? }`. `tools` is a
272
- * nullable changeset: omit it to leave the stored set unchanged, pass `null`
273
- * to clear it, or an array to replace it. `jobKey`/`jobLease` are the
274
- * activation's job attribution, stamped onto every appended turn (as the
275
- * gateway does). A turn is `{ loopIteration?, producedAt?, role?, content?,
276
- * systemPrompt?, historyItemId?, model?, provider? }`, where `producedAt` is
277
- * an RFC-3339 `date-time` string (the REST spelling; a bare epoch-millis
278
- * number is also accepted); `content` items are `{ contentType?, text?,
279
- * documentReference?, object? }`, where `object` is arbitrary JSON (the REST
280
- * wire shape). Returns the snapshot.
281
- */
282
- updateAgentInstance(request_json: string): string;
291
+ * Update an agent with `{ elementInstanceKey, jobKey, jobLease, status?, history? }`.
292
+ * Configuration and metrics are submitted through history, never top-level fields.
293
+ * Returns `createdHistory`; the engine owns pending/commit/discard semantics.
294
+ */
295
+ updateAgentInstance(agent_instance_key: string, request_json: string): string;
283
296
  /**
284
297
  * Set a job's remaining retries by key. Used to recover a job parked on a
285
298
  * no-retries incident before resolving that incident; does not by itself
286
299
  * unblock the job. Returns the snapshot.
287
300
  */
288
- updateRetries(job_key: string, retries: number): string;
301
+ updateRetries(job_key: string, retries: number, lease_token?: string | null): string;
302
+ /**
303
+ * Update an activated job's timeout, optionally checking its opaque lease.
304
+ */
305
+ updateTimeout(job_key: string, timeout_ms: number, lease_token?: string | null): string;
289
306
  /**
290
307
  * Update a user task's attributes from a JSON changeset object. Recognised
291
308
  * keys (all optional): `candidateGroups` / `candidateUsers` (string arrays),
@@ -309,14 +326,14 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
309
326
  export interface InitOutput {
310
327
  readonly memory: WebAssembly.Memory;
311
328
  readonly __wbg_testengine_free: (a: number, b: number) => void;
312
- readonly testengine_activateJobs: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
329
+ readonly testengine_activateJobs: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
313
330
  readonly testengine_advanceTime: (a: number, b: number, c: number) => void;
314
331
  readonly testengine_assignUserTask: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
315
332
  readonly testengine_broadcastSignal: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
316
333
  readonly testengine_cancelInstance: (a: number, b: number, c: number, d: number) => void;
317
334
  readonly testengine_completeAgentInstance: (a: number, b: number, c: number, d: number) => void;
318
- readonly testengine_completeAgentJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
319
- readonly testengine_completeJob: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
335
+ readonly testengine_completeAgentJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
336
+ readonly testengine_completeJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
320
337
  readonly testengine_completeUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
321
338
  readonly testengine_correlateMessage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
322
339
  readonly testengine_createAgentInstance: (a: number, b: number, c: number, d: number) => void;
@@ -327,23 +344,27 @@ export interface InitOutput {
327
344
  readonly testengine_debugResume: (a: number, b: number) => void;
328
345
  readonly testengine_debugStep: (a: number, b: number) => void;
329
346
  readonly testengine_deploy: (a: number, b: number, c: number, d: number) => void;
347
+ readonly testengine_deployDecision: (a: number, b: number, c: number, d: number) => void;
330
348
  readonly testengine_deployForm: (a: number, b: number, c: number, d: number) => void;
331
349
  readonly testengine_deployResource: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
350
+ readonly testengine_evaluateDecision: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
332
351
  readonly testengine_events: (a: number, b: number) => void;
333
- readonly testengine_failJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
352
+ readonly testengine_failJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
334
353
  readonly testengine_migrate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
335
354
  readonly testengine_modify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
336
355
  readonly testengine_new: () => number;
337
356
  readonly testengine_now: (a: number) => number;
357
+ readonly testengine_replayEvents: (a: number, b: number, c: number, d: number) => void;
338
358
  readonly testengine_reset: (a: number) => void;
339
359
  readonly testengine_resolveIncident: (a: number, b: number, c: number, d: number) => void;
340
360
  readonly testengine_setVariables: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
341
361
  readonly testengine_snapshot: (a: number, b: number) => void;
342
- readonly testengine_throwError: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
362
+ readonly testengine_throwError: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
343
363
  readonly testengine_tickNow: (a: number, b: number, c: number) => void;
344
364
  readonly testengine_unassignUserTask: (a: number, b: number, c: number, d: number) => void;
345
- readonly testengine_updateAgentInstance: (a: number, b: number, c: number, d: number) => void;
346
- readonly testengine_updateRetries: (a: number, b: number, c: number, d: number, e: number) => void;
365
+ readonly testengine_updateAgentInstance: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
366
+ readonly testengine_updateRetries: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
367
+ readonly testengine_updateTimeout: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
347
368
  readonly testengine_updateUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
348
369
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
349
370
  readonly __wbindgen_export: (a: number, b: number) => number;