@nanobpm/engine-wasm 0.8.7 → 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.
package/README.md CHANGED
@@ -52,11 +52,54 @@ engine.reset(); // back to a clean engine
52
52
  ```
53
53
 
54
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`,
55
+ `snapshot`, `events`, `replayEvents`, job ops (`activateJobs`, `completeJob`, `failJob`,
56
+ `updateRetries`, `updateTimeout`, `throwError`), user-task ops (`completeUserTask`,
57
57
  `assignUserTask`, …), messaging/signals, the virtual clock (`advanceTime`,
58
58
  `tickNow`), and `reset`. It links **no** SQLite / read-model code.
59
59
 
60
+ ### Optional job leasing and agent history
61
+
62
+ `activateJobs(type, maxJobs, timeoutMs, worker, withLease?)` defaults to nonleasing
63
+ for **every** job kind and agent marker. Activated jobs always contain
64
+ `leaseToken`: `null` without a lease, otherwise an opaque string. Never parse it
65
+ as a number. Once a job is leased, subsequent activations must also opt in;
66
+ nonleasing workers skip it after failures and timeouts.
67
+
68
+ Pass the token as the final optional argument to `completeJob`, `completeAgentJob`,
69
+ `failJob`, or `throwError`. Leased lifecycle commands require the current token.
70
+ `updateRetries` and `updateTimeout` also accept a final token; it is optional, but
71
+ a supplied stale value is rejected. Timeout updates accept signed millisecond
72
+ durations; zero and negative updates make the activation immediately eligible for
73
+ expiry on the next clock tick without discarding its current lease.
74
+
75
+ Agent APIs follow the vendored stable/8.10 contract:
76
+
77
+ - `createAgentInstance(JSON.stringify({elementInstanceKey, jobKey, jobLease, history}))`
78
+ requires nonempty history establishing CONFIGURATION. `jobLease` is the activated
79
+ job's `leaseToken`, unchanged. Definition, limits, tools, and metrics are history
80
+ fields, not top-level request properties.
81
+ - `updateAgentInstance(agentInstanceKey, JSON.stringify({elementInstanceKey, jobKey,
82
+ jobLease, status?, history?}))` requires all three attribution fields even without
83
+ history.
84
+ - Both return canonical results containing positionally correlated `createdHistory`;
85
+ CREATE also returns `agentInstanceKey`. Duplicate CREATE rejects rather than upserting.
86
+ - Failure and timeout leave history PENDING. Job completion commits the winning
87
+ attempt and discards superseded attempts; cancellation or a business error
88
+ discards pending history. Searches default to COMMITTED. Timestamps are RFC-3339
89
+ strings and loop iterations start at 1.
90
+ - System prompts are typed content-block arrays. Persisted historical text remains
91
+ one TEXT block, even when the text resembles JSON.
92
+ - Metrics preserve the difference between an omitted object and an object with
93
+ nullable counters. Zero and signed values remain observations, never sentinels.
94
+
95
+ Agent searches currently support exact scalar filters and `$eq`, plus `$in` for
96
+ history commit status. Other advanced operators are rejected; pagination and sorting
97
+ are not implemented by the simulation API.
98
+
99
+ `replayEvents(engine.events())` restores a complete exported trace through core
100
+ replay, including opaque lease state. It rejects unknown event types and missing
101
+ prefixes rather than silently dropping records.
102
+
60
103
  ## Usage — read-model
61
104
 
62
105
  ```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
  /**
@@ -191,7 +184,7 @@ export class TestEngine {
191
184
  * Fail a waiting job by key with the given remaining `retries` and message.
192
185
  * With no retries left this raises an incident (visible in the snapshot).
193
186
  */
194
- failJob(job_key: string, retries: number, message: string): string;
187
+ failJob(job_key: string, retries: number, message: string, lease_token?: string | null): string;
195
188
  /**
196
189
  * Migrate a running process instance to a target process definition (Zeebe
197
190
  * "migrate process instance"): re-point every active element instance at the
@@ -216,6 +209,11 @@ export class TestEngine {
216
209
  * Create a fresh, empty simulated engine. The virtual clock starts at 0.
217
210
  */
218
211
  constructor();
212
+ /**
213
+ * Restore a complete trace returned by `events()`, using the core journal
214
+ * decoder and replay engine. Replaces this simulation only after decoding succeeds.
215
+ */
216
+ replayEvents(events_json: string): string;
219
217
  /**
220
218
  * Discard all engine state (deployed definitions, instances, jobs, timers,
221
219
  * the event log and the virtual clock), returning the engine to the same
@@ -249,7 +247,7 @@ export class TestEngine {
249
247
  * raised. The job is activated first if needed, so the UI can throw an
250
248
  * error directly from a freshly-created job. Returns the snapshot.
251
249
  */
252
- throwError(job_key: string, error_code: string, error_message: string): string;
250
+ throwError(job_key: string, error_code: string, error_message: string, lease_token?: string | null): string;
253
251
  /**
254
252
  * Set the engine clock to a wall-clock instant (ms), then trigger due timers
255
253
  * and expire lapsed job locks. The embedded host calls this with `Date.now()`
@@ -263,29 +261,21 @@ export class TestEngine {
263
261
  */
264
262
  unassignUserTask(user_task_key: string): string;
265
263
  /**
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;
264
+ * Update an agent with `{ elementInstanceKey, jobKey, jobLease, status?, history? }`.
265
+ * Configuration and metrics are submitted through history, never top-level fields.
266
+ * Returns `createdHistory`; the engine owns pending/commit/discard semantics.
267
+ */
268
+ updateAgentInstance(agent_instance_key: string, request_json: string): string;
283
269
  /**
284
270
  * Set a job's remaining retries by key. Used to recover a job parked on a
285
271
  * no-retries incident before resolving that incident; does not by itself
286
272
  * unblock the job. Returns the snapshot.
287
273
  */
288
- updateRetries(job_key: string, retries: number): string;
274
+ updateRetries(job_key: string, retries: number, lease_token?: string | null): string;
275
+ /**
276
+ * Update an activated job's timeout, optionally checking its opaque lease.
277
+ */
278
+ updateTimeout(job_key: string, timeout_ms: number, lease_token?: string | null): string;
289
279
  /**
290
280
  * Update a user task's attributes from a JSON changeset object. Recognised
291
281
  * keys (all optional): `candidateGroups` / `candidateUsers` (string arrays),
@@ -309,14 +299,14 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
309
299
  export interface InitOutput {
310
300
  readonly memory: WebAssembly.Memory;
311
301
  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;
302
+ readonly testengine_activateJobs: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
313
303
  readonly testengine_advanceTime: (a: number, b: number, c: number) => void;
314
304
  readonly testengine_assignUserTask: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
315
305
  readonly testengine_broadcastSignal: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
316
306
  readonly testengine_cancelInstance: (a: number, b: number, c: number, d: number) => void;
317
307
  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;
308
+ readonly testengine_completeAgentJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number, j: number) => void;
309
+ readonly testengine_completeJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
320
310
  readonly testengine_completeUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
321
311
  readonly testengine_correlateMessage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
322
312
  readonly testengine_createAgentInstance: (a: number, b: number, c: number, d: number) => void;
@@ -330,20 +320,22 @@ export interface InitOutput {
330
320
  readonly testengine_deployForm: (a: number, b: number, c: number, d: number) => void;
331
321
  readonly testengine_deployResource: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
332
322
  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;
323
+ readonly testengine_failJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number, i: number) => void;
334
324
  readonly testengine_migrate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
335
325
  readonly testengine_modify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
336
326
  readonly testengine_new: () => number;
337
327
  readonly testengine_now: (a: number) => number;
328
+ readonly testengine_replayEvents: (a: number, b: number, c: number, d: number) => void;
338
329
  readonly testengine_reset: (a: number) => void;
339
330
  readonly testengine_resolveIncident: (a: number, b: number, c: number, d: number) => void;
340
331
  readonly testengine_setVariables: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
341
332
  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;
333
+ 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
334
  readonly testengine_tickNow: (a: number, b: number, c: number) => void;
344
335
  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;
336
+ readonly testengine_updateAgentInstance: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
337
+ readonly testengine_updateRetries: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
338
+ readonly testengine_updateTimeout: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
347
339
  readonly testengine_updateUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
348
340
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
349
341
  readonly __wbindgen_export: (a: number, b: number) => number;