@nanobpm/engine-wasm 0.8.6 → 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,12 +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, definition?, limits?, history? }`
92
- * where `definition` is `{ model?, provider?, systemPrompt? }`, `limits` is
93
- * `{ maxTokens?, maxModelCalls?, maxToolCalls? }` (omitted limits default to
94
- * unlimited), and `history` is an initial batch of turns (see the turn shape
95
- * on `updateAgentInstance`). 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`.
96
94
  */
97
95
  createAgentInstance(request_json: string): string;
98
96
  /**
@@ -186,7 +184,7 @@ export class TestEngine {
186
184
  * Fail a waiting job by key with the given remaining `retries` and message.
187
185
  * With no retries left this raises an incident (visible in the snapshot).
188
186
  */
189
- failJob(job_key: string, retries: number, message: string): string;
187
+ failJob(job_key: string, retries: number, message: string, lease_token?: string | null): string;
190
188
  /**
191
189
  * Migrate a running process instance to a target process definition (Zeebe
192
190
  * "migrate process instance"): re-point every active element instance at the
@@ -211,6 +209,11 @@ export class TestEngine {
211
209
  * Create a fresh, empty simulated engine. The virtual clock starts at 0.
212
210
  */
213
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;
214
217
  /**
215
218
  * Discard all engine state (deployed definitions, instances, jobs, timers,
216
219
  * the event log and the virtual clock), returning the engine to the same
@@ -244,7 +247,7 @@ export class TestEngine {
244
247
  * raised. The job is activated first if needed, so the UI can throw an
245
248
  * error directly from a freshly-created job. Returns the snapshot.
246
249
  */
247
- 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;
248
251
  /**
249
252
  * Set the engine clock to a wall-clock instant (ms), then trigger due timers
250
253
  * and expire lapsed job locks. The embedded host calls this with `Date.now()`
@@ -258,29 +261,21 @@ export class TestEngine {
258
261
  */
259
262
  unassignUserTask(user_task_key: string): string;
260
263
  /**
261
- * Advance an AgentInstance: set its `status` (a REST spelling other than
262
- * `COMPLETED`, which is reachable only through `completeAgentInstance` and is
263
- * rejected here with a targeted error), accumulate `metrics`, optionally
264
- * replace `tools`, and append a `history` batch. `request_json` is
265
- * `{ agentInstanceKey, elementInstanceKey, elementId, processInstanceKey,
266
- * status?, metrics?, tools?, jobKey?, jobLease?, history? }`. `tools` is a
267
- * nullable changeset: omit it to leave the stored set unchanged, pass `null`
268
- * to clear it, or an array to replace it. `jobKey`/`jobLease` are the
269
- * activation's job attribution, stamped onto every appended turn (as the
270
- * gateway does). A turn is `{ loopIteration?, producedAt?, role?, content?,
271
- * systemPrompt?, historyItemId?, model?, provider? }`, where `producedAt` is
272
- * an RFC-3339 `date-time` string (the REST spelling; a bare epoch-millis
273
- * number is also accepted); `content` items are `{ contentType?, text?,
274
- * documentReference?, object? }`, where `object` is arbitrary JSON (the REST
275
- * wire shape). Returns the snapshot.
276
- */
277
- 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;
278
269
  /**
279
270
  * Set a job's remaining retries by key. Used to recover a job parked on a
280
271
  * no-retries incident before resolving that incident; does not by itself
281
272
  * unblock the job. Returns the snapshot.
282
273
  */
283
- 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;
284
279
  /**
285
280
  * Update a user task's attributes from a JSON changeset object. Recognised
286
281
  * keys (all optional): `candidateGroups` / `candidateUsers` (string arrays),
@@ -304,14 +299,14 @@ export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembl
304
299
  export interface InitOutput {
305
300
  readonly memory: WebAssembly.Memory;
306
301
  readonly __wbg_testengine_free: (a: number, b: number) => void;
307
- 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;
308
303
  readonly testengine_advanceTime: (a: number, b: number, c: number) => void;
309
304
  readonly testengine_assignUserTask: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
310
305
  readonly testengine_broadcastSignal: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
311
306
  readonly testengine_cancelInstance: (a: number, b: number, c: number, d: number) => void;
312
307
  readonly testengine_completeAgentInstance: (a: number, b: number, c: number, d: number) => void;
313
- readonly testengine_completeAgentJob: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
314
- 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;
315
310
  readonly testengine_completeUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
316
311
  readonly testengine_correlateMessage: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
317
312
  readonly testengine_createAgentInstance: (a: number, b: number, c: number, d: number) => void;
@@ -325,20 +320,22 @@ export interface InitOutput {
325
320
  readonly testengine_deployForm: (a: number, b: number, c: number, d: number) => void;
326
321
  readonly testengine_deployResource: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
327
322
  readonly testengine_events: (a: number, b: number) => void;
328
- 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;
329
324
  readonly testengine_migrate: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
330
325
  readonly testengine_modify: (a: number, b: number, c: number, d: number, e: number, f: number, g: number, h: number) => void;
331
326
  readonly testengine_new: () => number;
332
327
  readonly testengine_now: (a: number) => number;
328
+ readonly testengine_replayEvents: (a: number, b: number, c: number, d: number) => void;
333
329
  readonly testengine_reset: (a: number) => void;
334
330
  readonly testengine_resolveIncident: (a: number, b: number, c: number, d: number) => void;
335
331
  readonly testengine_setVariables: (a: number, b: number, c: number, d: number, e: number, f: number, g: number) => void;
336
332
  readonly testengine_snapshot: (a: number, b: number) => void;
337
- 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;
338
334
  readonly testengine_tickNow: (a: number, b: number, c: number) => void;
339
335
  readonly testengine_unassignUserTask: (a: number, b: number, c: number, d: number) => void;
340
- readonly testengine_updateAgentInstance: (a: number, b: number, c: number, d: number) => void;
341
- 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;
342
339
  readonly testengine_updateUserTask: (a: number, b: number, c: number, d: number, e: number, f: number) => void;
343
340
  readonly __wbindgen_add_to_stack_pointer: (a: number) => number;
344
341
  readonly __wbindgen_export: (a: number, b: number) => number;