@nanobpm/urban 0.96.3 → 0.98.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 +60 -0
- package/dist/runtime/connector-worker-sdk.d.ts +2 -0
- package/dist/runtime/connector-worker-sdk.js +6 -1
- package/dist/runtime/core/host.d.ts +15 -5
- package/dist/runtime/core/manifest.d.ts +23 -0
- package/dist/runtime/core/manifest.js +8 -0
- package/dist/runtime/core/modules/connectors.d.ts +3 -0
- package/dist/runtime/core/modules/connectors.js +32 -1
- package/dist/runtime/core/modules/formjs.gen.js +1 -1
- package/dist/runtime/core/modules/forms.d.ts +20 -0
- package/dist/runtime/core/modules/forms.js +47 -0
- package/dist/runtime/core/modules/pages.js +12 -1
- package/dist/runtime/core/modules/runtime.gen.js +1 -1
- package/dist/runtime/core/modules/surfaces.js +57 -4
- package/dist/runtime/core/modules/workers.js +5 -1
- package/dist/runtime/core/validate.js +12 -0
- package/dist/runtime/engine/nanosdk.d.ts +1 -0
- package/dist/runtime/engine/nanosdk.js +3 -0
- package/dist/runtime/index.d.ts +3 -3
- package/dist/runtime/index.js +1 -1
- package/package.json +2 -2
- package/src/runtime/browser/fake-dom.test-utils.ts +16 -1
- package/src/runtime/browser/formjs.browser.js +258 -7
- package/src/runtime/browser/formjs.browser.test.ts +690 -0
- package/src/runtime/browser/runtime.browser.js +25 -3
- package/src/runtime/browser/runtime.engineform.test.ts +203 -0
- package/src/runtime/connector-worker-sdk.test.ts +9 -0
- package/src/runtime/connector-worker-sdk.ts +8 -1
- package/src/runtime/core/host.ts +16 -1
- package/src/runtime/core/manifest.ts +28 -0
- package/src/runtime/core/modules/connectors.test.ts +158 -1
- package/src/runtime/core/modules/connectors.ts +44 -1
- package/src/runtime/core/modules/formjs.gen.ts +1 -1
- package/src/runtime/core/modules/forms.ts +47 -0
- package/src/runtime/core/modules/pages.test.ts +21 -0
- package/src/runtime/core/modules/pages.ts +13 -1
- package/src/runtime/core/modules/runtime.gen.ts +1 -1
- package/src/runtime/core/modules/surfaces.test.ts +308 -0
- package/src/runtime/core/modules/surfaces.ts +58 -4
- package/src/runtime/core/modules/workers.test.ts +45 -1
- package/src/runtime/core/modules/workers.ts +5 -1
- package/src/runtime/core/validate.test.ts +19 -0
- package/src/runtime/core/validate.ts +12 -0
- package/src/runtime/engine/nanosdk.test.ts +17 -0
- package/src/runtime/engine/nanosdk.ts +4 -1
- package/src/runtime/index.ts +3 -0
|
@@ -1939,7 +1939,10 @@ function buildDetailForm(f, row, onSuccess) {
|
|
|
1939
1939
|
//
|
|
1940
1940
|
// `cfg.formKeyField` (default "form_key") names the row column carrying the engine
|
|
1941
1941
|
// formKey; `cfg.userTaskKeyField` (default "user_task_key") names the column
|
|
1942
|
-
// carrying the user-task key to complete.
|
|
1942
|
+
// carrying the user-task key to complete; `cfg.processInstanceKeyField` (default
|
|
1943
|
+
// "process_instance_key", falling back to a camelCase `processInstanceKey` column)
|
|
1944
|
+
// names the column carrying the process-instance key whose variables become the
|
|
1945
|
+
// renderer's context (#603). A row with no formKey degrades to a bare
|
|
1943
1946
|
// "Complete" button (empty variables), matching taskInbox's no-form fallback.
|
|
1944
1947
|
/** @param {any} cfg
|
|
1945
1948
|
* @param {Record<string, any>} row
|
|
@@ -1994,8 +1997,22 @@ function buildEngineForm(cfg, row, onSuccess) {
|
|
|
1994
1997
|
return box;
|
|
1995
1998
|
}
|
|
1996
1999
|
box.append(el("p", { class: "njf-text" }, "loading form\u2026"));
|
|
1997
|
-
|
|
1998
|
-
|
|
2000
|
+
// Load the row's task variables alongside the form (#603): they are the
|
|
2001
|
+
// renderer's context — `{{name}}` text templating, `=varName` image sources,
|
|
2002
|
+
// and keyed-input prefill — so the drawer shows the reviewer what they are
|
|
2003
|
+
// reviewing, exactly like the taskInbox surface. The row column naming the
|
|
2004
|
+
// process-instance key is `cfg.processInstanceKeyField` (default
|
|
2005
|
+
// "process_instance_key", falling back to a camelCase `processInstanceKey`
|
|
2006
|
+
// column); a row without one (or a failed fetch) degrades to an empty context
|
|
2007
|
+
// rather than blocking the form.
|
|
2008
|
+
const pikField = cfg.processInstanceKeyField || "process_instance_key";
|
|
2009
|
+
const pik = row[pikField] != null ? row[pikField] : row.processInstanceKey;
|
|
2010
|
+
const varsP =
|
|
2011
|
+
pik != null && pik !== ""
|
|
2012
|
+
? getJSON("/app/actions/variables?processInstanceKey=" + encodeURIComponent(String(pik))).catch(() => ({}))
|
|
2013
|
+
: Promise.resolve({});
|
|
2014
|
+
Promise.all([getJSON("/app/actions/form?formKey=" + encodeURIComponent(String(formKey))), varsP])
|
|
2015
|
+
.then(([f, context]) => {
|
|
1999
2016
|
box.replaceChildren();
|
|
2000
2017
|
const renderer = /** @type {any} */ (globalThis).NanoFormJs;
|
|
2001
2018
|
if (!f || !f.schema || !renderer) {
|
|
@@ -2005,6 +2022,11 @@ function buildEngineForm(cfg, row, onSuccess) {
|
|
|
2005
2022
|
box.append(
|
|
2006
2023
|
renderer.renderForm(f.schema, {
|
|
2007
2024
|
submitLabel: "Complete",
|
|
2025
|
+
context: context || {},
|
|
2026
|
+
// Rebase root-relative image sources ("/app/…") onto the app mount root,
|
|
2027
|
+
// exactly like every page fetch — otherwise they escape the console's
|
|
2028
|
+
// path-prefixed proxy (/console/app-view/<name>/) and 404.
|
|
2029
|
+
resolveUrl: apiUrl,
|
|
2008
2030
|
/** @param {Record<string, any>} variables */
|
|
2009
2031
|
onSubmit: (variables) => {
|
|
2010
2032
|
msg.className = "njf-msg";
|
|
@@ -256,3 +256,206 @@ test("buildEngineForm treats a whitespace-only completePath as absent", async (t
|
|
|
256
256
|
assert.ok(completeReq, "a whitespace-only completePath still targets the default /app/actions/complete seam");
|
|
257
257
|
assert.equal(completeReq!.body.userTaskKey, "ut-e");
|
|
258
258
|
});
|
|
259
|
+
|
|
260
|
+
test("buildEngineForm loads the row's task variables and renders {{title}} + an =img1 image with them (#603)", async (t) => {
|
|
261
|
+
// The acceptance scenario end to end through the pages drawer: a task whose
|
|
262
|
+
// variables include `title` and `img1`, with a form using {{title}} and an
|
|
263
|
+
// image with source "=img1", renders both — and prefills the keyed input.
|
|
264
|
+
const created: FakeElement[] = [];
|
|
265
|
+
const calls: FetchCall[] = [];
|
|
266
|
+
t.after(installFakeDom(created));
|
|
267
|
+
t.after(
|
|
268
|
+
installFakeFetch((url) => {
|
|
269
|
+
if (url.includes("/app/actions/form")) {
|
|
270
|
+
return {
|
|
271
|
+
status: 200,
|
|
272
|
+
json: {
|
|
273
|
+
schema: {
|
|
274
|
+
components: [
|
|
275
|
+
{ type: "text", text: "Reviewing {{title}}" },
|
|
276
|
+
{ type: "image", source: "=img1", alt: "Proposed thumbnail" },
|
|
277
|
+
{ type: "textfield", key: "title" },
|
|
278
|
+
],
|
|
279
|
+
},
|
|
280
|
+
},
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
if (url.includes("/app/actions/variables")) {
|
|
284
|
+
return { status: 200, json: { title: "Draft v2", img1: "https://cdn.example.com/t1.png" } };
|
|
285
|
+
}
|
|
286
|
+
return { status: 200, json: { ok: true } };
|
|
287
|
+
}, calls),
|
|
288
|
+
);
|
|
289
|
+
const box = buildEngineForm(
|
|
290
|
+
CFG,
|
|
291
|
+
{ form_key: "form-v", user_task_key: "ut-v", process_instance_key: "pi-v" },
|
|
292
|
+
() => {},
|
|
293
|
+
);
|
|
294
|
+
assert.ok(box);
|
|
295
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
296
|
+
const varsReq = calls.find((c) => c.url.includes("/app/actions/variables"));
|
|
297
|
+
assert.ok(varsReq, "the row's variables are fetched alongside the form");
|
|
298
|
+
assert.match(varsReq!.url, /processInstanceKey=pi-v/, "fetched by the row's process-instance key");
|
|
299
|
+
const p = created.find((n) => n.tagName === "P" && n.className.includes("njf-text") && n.textContent.includes("Reviewing"));
|
|
300
|
+
assert.ok(p, "the text component rendered");
|
|
301
|
+
assert.equal(p!.textContent, "Reviewing Draft v2", "{{title}} templated from the task's variables");
|
|
302
|
+
const img = created.find((n) => n.tagName === "IMG");
|
|
303
|
+
assert.ok(img, "the image component rendered");
|
|
304
|
+
assert.equal(img!.getAttribute("src"), "https://cdn.example.com/t1.png", "=img1 resolved from the task's variables");
|
|
305
|
+
assert.equal(img!.getAttribute("alt"), "Proposed thumbnail");
|
|
306
|
+
const input = created.find((n) => n.tagName === "INPUT");
|
|
307
|
+
assert.equal(input!.value, "Draft v2", "the keyed input is prefilled from the same context");
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
test("buildEngineForm rebases a root-relative image source through the pages resolver — revalidated, fail-closed off http(s)", async (t) => {
|
|
311
|
+
// Behind the console proxy the app is mounted at /console/app-view/<name>/, so a
|
|
312
|
+
// form image source of /app/thumb.png is rebased through the same mount-aware
|
|
313
|
+
// resolver apiUrl uses — otherwise the <img> requests the console's
|
|
314
|
+
// /app/thumb.png and app-served thumbnails 404. The renderer REVALIDATES the
|
|
315
|
+
// resolver's output (round 5): it must come back an http(s)/protocol-relative
|
|
316
|
+
// URL or a same-origin path. Under this Node runner the module base is file://,
|
|
317
|
+
// so apiUrl's rebase yields file://… — refused (fail closed: a non-http(s)
|
|
318
|
+
// origin is not a fetchable image). In a browser the same code yields
|
|
319
|
+
// https://… and renders (the http(s) pass side is pinned in
|
|
320
|
+
// formjs.browser.test.ts with an https-mount resolver).
|
|
321
|
+
const created: FakeElement[] = [];
|
|
322
|
+
const calls: FetchCall[] = [];
|
|
323
|
+
t.after(installFakeDom(created));
|
|
324
|
+
t.after(
|
|
325
|
+
installFakeFetch((url) => {
|
|
326
|
+
if (url.includes("/app/actions/form")) {
|
|
327
|
+
return {
|
|
328
|
+
status: 200,
|
|
329
|
+
json: {
|
|
330
|
+
schema: {
|
|
331
|
+
components: [
|
|
332
|
+
{ type: "image", source: "/app/thumb.png", alt: "literal" },
|
|
333
|
+
{ type: "image", source: "=img1", alt: "from variable" },
|
|
334
|
+
],
|
|
335
|
+
},
|
|
336
|
+
},
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
if (url.includes("/app/actions/variables")) {
|
|
340
|
+
return { status: 200, json: { img1: "/app/var-thumb.png" } };
|
|
341
|
+
}
|
|
342
|
+
return { status: 200, json: { ok: true } };
|
|
343
|
+
}, calls),
|
|
344
|
+
);
|
|
345
|
+
const box = buildEngineForm(
|
|
346
|
+
CFG,
|
|
347
|
+
{ form_key: "form-v", user_task_key: "ut-v", process_instance_key: "pi-v" },
|
|
348
|
+
() => {},
|
|
349
|
+
);
|
|
350
|
+
assert.ok(box);
|
|
351
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
352
|
+
const srcs = created.filter((n) => n.tagName === "IMG").map((n) => n.getAttribute("src"));
|
|
353
|
+
assert.equal(srcs.length, 0, "file://-rebased sources are refused under Node; https:// ones render in a browser");
|
|
354
|
+
for (const src of srcs) {
|
|
355
|
+
assert.ok(!src!.startsWith("/app/"), "never the root-relative form that would hit the console's /app/");
|
|
356
|
+
}
|
|
357
|
+
});
|
|
358
|
+
|
|
359
|
+
test("buildEngineForm still renders the form when the variables fetch fails (#603 degradation)", async (t) => {
|
|
360
|
+
const created: FakeElement[] = [];
|
|
361
|
+
t.after(installFakeDom(created));
|
|
362
|
+
t.after(
|
|
363
|
+
installFakeFetch((url) => {
|
|
364
|
+
if (url.includes("/app/actions/form")) {
|
|
365
|
+
return { status: 200, json: { schema: { components: [{ type: "textfield", key: "note" }] } } };
|
|
366
|
+
}
|
|
367
|
+
return { status: 500 }; // variables fetch fails
|
|
368
|
+
}, []),
|
|
369
|
+
);
|
|
370
|
+
const box = buildEngineForm(CFG, { form_key: "form-x", user_task_key: "ut-x", process_instance_key: "pi-x" }, () => {});
|
|
371
|
+
assert.ok(box);
|
|
372
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
373
|
+
const form = created.find((n) => n.tagName === "FORM");
|
|
374
|
+
assert.ok(form, "a variables-fetch failure does not block the form");
|
|
375
|
+
const input = created.find((n) => n.tagName === "INPUT");
|
|
376
|
+
assert.equal(input!.value, "", "no context → no prefill");
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
test("buildEngineForm honours a camelCase processInstanceKey column and a custom processInstanceKeyField", async (t) => {
|
|
380
|
+
const created: FakeElement[] = [];
|
|
381
|
+
const calls: FetchCall[] = [];
|
|
382
|
+
t.after(installFakeDom(created));
|
|
383
|
+
t.after(
|
|
384
|
+
installFakeFetch((url) => {
|
|
385
|
+
if (url.includes("/app/actions/form")) {
|
|
386
|
+
return { status: 200, json: { schema: { components: [] } } };
|
|
387
|
+
}
|
|
388
|
+
return { status: 200, json: {} };
|
|
389
|
+
}, calls),
|
|
390
|
+
);
|
|
391
|
+
buildEngineForm(CFG, { form_key: "f", user_task_key: "u", processInstanceKey: "pi-camel" }, () => {});
|
|
392
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
393
|
+
assert.ok(
|
|
394
|
+
calls.some((c) => c.url.includes("processInstanceKey=pi-camel")),
|
|
395
|
+
"a camelCase processInstanceKey column is picked up when process_instance_key is absent",
|
|
396
|
+
);
|
|
397
|
+
calls.length = 0;
|
|
398
|
+
buildEngineForm(
|
|
399
|
+
{ ...CFG, processInstanceKeyField: "pi" },
|
|
400
|
+
{ form_key: "f", user_task_key: "u", pi: "pi-custom" },
|
|
401
|
+
() => {},
|
|
402
|
+
);
|
|
403
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
404
|
+
assert.ok(
|
|
405
|
+
calls.some((c) => c.url.includes("processInstanceKey=pi-custom")),
|
|
406
|
+
"cfg.processInstanceKeyField names a custom column",
|
|
407
|
+
);
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
// ── PR #604 round 5: the pages resolver (apiUrl) cannot launder a forbidden scheme ──
|
|
411
|
+
|
|
412
|
+
test("buildEngineForm rejects a root-prefixed forbidden-scheme image source through the pages resolver", async (t) => {
|
|
413
|
+
// apiUrl strips the leading slash before `new URL(u.slice(1), APP_BASE)`, so a
|
|
414
|
+
// source of "/javascript:alert(1)" — accepted by the allowlist as an
|
|
415
|
+
// app-relative path — resolves to the forbidden-scheme URL
|
|
416
|
+
// "javascript:alert(1)". The renderer must revalidate the RESOLVED URL: no
|
|
417
|
+
// <img> with a non-http(s)/data-image src may be set, for a literal source or
|
|
418
|
+
// a variable-backed one.
|
|
419
|
+
const created: FakeElement[] = [];
|
|
420
|
+
const calls: FetchCall[] = [];
|
|
421
|
+
t.after(installFakeDom(created));
|
|
422
|
+
t.after(
|
|
423
|
+
installFakeFetch((url) => {
|
|
424
|
+
if (url.includes("/app/actions/form")) {
|
|
425
|
+
return {
|
|
426
|
+
status: 200,
|
|
427
|
+
json: {
|
|
428
|
+
schema: {
|
|
429
|
+
components: [
|
|
430
|
+
{ type: "image", source: "/javascript:alert(1)", alt: "literal" },
|
|
431
|
+
{ type: "image", source: "=evil", alt: "from variable" },
|
|
432
|
+
{ type: "image", source: "/app/legit.png", alt: "legit" },
|
|
433
|
+
],
|
|
434
|
+
},
|
|
435
|
+
},
|
|
436
|
+
};
|
|
437
|
+
}
|
|
438
|
+
if (url.includes("/app/actions/variables")) {
|
|
439
|
+
return { status: 200, json: { evil: "/data:text/html;base64,PGI+" } };
|
|
440
|
+
}
|
|
441
|
+
return { status: 200, json: { ok: true } };
|
|
442
|
+
}, calls),
|
|
443
|
+
);
|
|
444
|
+
const box = buildEngineForm(
|
|
445
|
+
CFG,
|
|
446
|
+
{ form_key: "form-v", user_task_key: "ut-v", process_instance_key: "pi-v" },
|
|
447
|
+
() => {},
|
|
448
|
+
);
|
|
449
|
+
assert.ok(box);
|
|
450
|
+
await new Promise((r) => setTimeout(r, 0));
|
|
451
|
+
const srcs = created.filter((n) => n.tagName === "IMG").map((n) => n.getAttribute("src"));
|
|
452
|
+
// Under this Node runner apiUrl's rebase lands on file://, so the renderer's
|
|
453
|
+
// fail-closed revalidation refuses even the legitimate source — in a browser
|
|
454
|
+
// the same resolver yields https:// and the legitimate image renders (the
|
|
455
|
+
// pass side is pinned in formjs.browser.test.ts with an https-mount resolver).
|
|
456
|
+
// The security property this test pins: no laundered forbidden-scheme src.
|
|
457
|
+
assert.equal(srcs.length, 0, "no laundered (or file://-rebased) source rendered");
|
|
458
|
+
for (const src of srcs) {
|
|
459
|
+
assert.ok(!/^(javascript|data|vbscript):/i.test(src!), "no forbidden-scheme src was set");
|
|
460
|
+
}
|
|
461
|
+
});
|
|
@@ -20,6 +20,15 @@ test("defineWorker records handlers that drainDefinedWorkers returns then clears
|
|
|
20
20
|
assert.deepEqual(drainDefinedWorkers(), []);
|
|
21
21
|
});
|
|
22
22
|
|
|
23
|
+
test("defineWorker carries the declared job lock timeout (timeoutMs) through the drain (issue #596)", () => {
|
|
24
|
+
drainDefinedWorkers(); // start clean
|
|
25
|
+
defineWorker({ type: "a:slow", timeoutMs: 900_000, handle: async () => ({}) });
|
|
26
|
+
defineWorker({ type: "a:fast", handle: async () => ({}) });
|
|
27
|
+
const [slow, fast] = drainDefinedWorkers();
|
|
28
|
+
assert.equal(slow.timeoutMs, 900_000);
|
|
29
|
+
assert.equal(fast.timeoutMs, undefined);
|
|
30
|
+
});
|
|
31
|
+
|
|
23
32
|
test("defineWorker rejects a missing type or non-function handle", () => {
|
|
24
33
|
assert.throws(() => defineWorker({ type: "", handle: async () => ({}) }), /type` is required/);
|
|
25
34
|
const missingHandle: Parameters<typeof defineWorker>[0] = JSON.parse('{"type":"x"}');
|
|
@@ -49,6 +49,8 @@ export interface ConnectorWorkerOptions {
|
|
|
49
49
|
export interface DefinedConnectorWorker {
|
|
50
50
|
type: string;
|
|
51
51
|
maxParallelJobs?: number;
|
|
52
|
+
/** Job lock timeout, ms (issue #596) — carried from `defineWorker({ timeoutMs })`. */
|
|
53
|
+
timeoutMs?: number;
|
|
52
54
|
handle: ConnectorWorkerOptions["handle"];
|
|
53
55
|
}
|
|
54
56
|
|
|
@@ -66,7 +68,12 @@ export function defineWorker(opts: ConnectorWorkerOptions): void {
|
|
|
66
68
|
if (typeof opts.handle !== "function") {
|
|
67
69
|
throw new Error(`defineWorker("${opts.type}"): handle must be a function`);
|
|
68
70
|
}
|
|
69
|
-
registered.push({
|
|
71
|
+
registered.push({
|
|
72
|
+
type: opts.type,
|
|
73
|
+
maxParallelJobs: opts.maxParallelJobs,
|
|
74
|
+
timeoutMs: opts.timeoutMs,
|
|
75
|
+
handle: opts.handle,
|
|
76
|
+
});
|
|
70
77
|
}
|
|
71
78
|
|
|
72
79
|
/** Return every worker registered since the last drain, and clear the registry.
|
package/src/runtime/core/host.ts
CHANGED
|
@@ -240,6 +240,21 @@ export interface WorkerSubscription {
|
|
|
240
240
|
unsubscribe(): Promise<void>;
|
|
241
241
|
}
|
|
242
242
|
|
|
243
|
+
/** Options for {@link EngineClient.registerWorker}. */
|
|
244
|
+
export interface RegisterWorkerOptions {
|
|
245
|
+
/** Worker name reported to the engine. The adapter supplies a default. */
|
|
246
|
+
workerName?: string;
|
|
247
|
+
/** Max jobs the worker handles in parallel. The adapter supplies a default. */
|
|
248
|
+
maxParallelJobs?: number;
|
|
249
|
+
/** Job lock timeout, ms (issue #596): how long the engine leases an activated job to
|
|
250
|
+
* this worker before re-activating it elsewhere. Set it above the handler's worst-case
|
|
251
|
+
* runtime for long steps (transcription, an agent call, a multi-GB upload) so the lock
|
|
252
|
+
* cannot expire mid-handler and the job run twice. Absent ⇒ the engine/SDK default. */
|
|
253
|
+
timeoutMs?: number;
|
|
254
|
+
/** Restrict the variables surfaced to the handler to this subset (engine-side projection). */
|
|
255
|
+
fetchVariables?: string[];
|
|
256
|
+
}
|
|
257
|
+
|
|
243
258
|
/**
|
|
244
259
|
* A process instance's terminal-relevant lifecycle state, as the engine reports it.
|
|
245
260
|
* `ACTIVE` (still running), `COMPLETED` (ended normally at an end event), or
|
|
@@ -991,7 +1006,7 @@ export interface EngineClient {
|
|
|
991
1006
|
registerWorker(
|
|
992
1007
|
jobType: string,
|
|
993
1008
|
handler: JobHandler,
|
|
994
|
-
options?:
|
|
1009
|
+
options?: RegisterWorkerOptions,
|
|
995
1010
|
): Promise<WorkerSubscription>;
|
|
996
1011
|
/** Tear down all connections. */
|
|
997
1012
|
close(): Promise<void>;
|
|
@@ -123,11 +123,39 @@ export function isConfiguredStatusSelector(v: unknown): v is readonly string[] {
|
|
|
123
123
|
return Array.isArray(v) && v.length > 0 && v.every((s) => typeof s === "string" && s.length > 0);
|
|
124
124
|
}
|
|
125
125
|
|
|
126
|
+
/**
|
|
127
|
+
* A worker declaration (`workers[]`) as the runtime consumes it: the schema-owned
|
|
128
|
+
* {@link Worker} plus the pending-schema `timeoutMs` field (issue #596) — the job lock
|
|
129
|
+
* timeout in ms, passed through `registerWorker` to `createJobWorker({ jobTimeoutMs })`.
|
|
130
|
+
* For a long-running step (transcription, an agent call, a multi-GB upload) the SDK's
|
|
131
|
+
* default lock expires mid-handler: the engine re-activates the job and a second worker
|
|
132
|
+
* runs it again, and the first worker's completion is rejected. Set `timeoutMs` above the
|
|
133
|
+
* handler's worst-case runtime to keep the lock for the whole step.
|
|
134
|
+
*
|
|
135
|
+
* Bridged locally — like {@link NetworkConfig} — until `@nanobpm/nano-app-schema` folds
|
|
136
|
+
* `timeoutMs` into the `worker` definition (the schema shape is owned by that package,
|
|
137
|
+
* ADR 0027, and CI bans `declare module` augmentation). Drop this augmentation and
|
|
138
|
+
* re-export `Worker` plainly once the dep bump lands (No Drift Surfaces).
|
|
139
|
+
*/
|
|
140
|
+
export type WorkerDecl = Worker & {
|
|
141
|
+
/** Job lock timeout, ms. Must be a positive integer (the manifest validator enforces it). */
|
|
142
|
+
readonly timeoutMs?: number;
|
|
143
|
+
};
|
|
144
|
+
|
|
126
145
|
/** The job type a worker subscribes to (schema key: `taskType`). */
|
|
127
146
|
export function workerJobType(w: Worker): string | undefined {
|
|
128
147
|
return w.taskType;
|
|
129
148
|
}
|
|
130
149
|
|
|
150
|
+
/** A worker declaration's job lock timeout, or `undefined` when absent/invalid — the
|
|
151
|
+
* validator flags an invalid value, so a consumer treats it as unset rather than
|
|
152
|
+
* propagating a malformed lock to the engine. Read reflectively (like `network.bind`)
|
|
153
|
+
* because `timeoutMs` is pending-schema and not on the static {@link Worker} type. */
|
|
154
|
+
export function workerTimeoutMs(w: Worker): number | undefined {
|
|
155
|
+
const v: unknown = Reflect.get(w, "timeoutMs");
|
|
156
|
+
return typeof v === "number" && Number.isInteger(v) && v > 0 ? v : undefined;
|
|
157
|
+
}
|
|
158
|
+
|
|
131
159
|
/**
|
|
132
160
|
* The valid HTTP bind modes, in declaration order. This is the single runtime source of
|
|
133
161
|
* truth for the set: {@link BindMode}, {@link isBindMode}, and the manifest validator
|
|
@@ -12,6 +12,11 @@ import { DataLayer } from "./datasource.ts";
|
|
|
12
12
|
/** A tiny engine that records registrations and can deliver a job to a handler. */
|
|
13
13
|
class MiniEngine implements EngineClient {
|
|
14
14
|
workers = new Map<string, JobHandler>();
|
|
15
|
+
/** The options each `registerWorker` call carried, keyed by job type. */
|
|
16
|
+
workerOptions = new Map<
|
|
17
|
+
string,
|
|
18
|
+
{ workerName?: string; maxParallelJobs?: number; timeoutMs?: number; fetchVariables?: string[] } | undefined
|
|
19
|
+
>();
|
|
15
20
|
async deployResources(): Promise<{ deployed: number }> { return { deployed: 0 }; }
|
|
16
21
|
async createInstance(): Promise<{ processInstanceKey: string }> { return { processInstanceKey: "pi" }; }
|
|
17
22
|
async cancelInstance(): Promise<void> {}
|
|
@@ -34,8 +39,13 @@ class MiniEngine implements EngineClient {
|
|
|
34
39
|
async resolveIncident(): Promise<void> {}
|
|
35
40
|
async updateJobRetries(): Promise<void> {}
|
|
36
41
|
async setVariables(): Promise<void> {}
|
|
37
|
-
async registerWorker(
|
|
42
|
+
async registerWorker(
|
|
43
|
+
jobType: string,
|
|
44
|
+
handler: JobHandler,
|
|
45
|
+
options?: { workerName?: string; maxParallelJobs?: number; timeoutMs?: number; fetchVariables?: string[] },
|
|
46
|
+
): Promise<WorkerSubscription> {
|
|
38
47
|
this.workers.set(jobType, handler);
|
|
48
|
+
this.workerOptions.set(jobType, options);
|
|
39
49
|
return { jobType, unsubscribe: async () => void this.workers.delete(jobType) };
|
|
40
50
|
}
|
|
41
51
|
deliver(jobType: string, job: EngineJob) {
|
|
@@ -165,6 +175,153 @@ test("mountConnectors imports the pack entry and registers the drained worker",
|
|
|
165
175
|
assert.deepEqual(out, { echoed: "hi" });
|
|
166
176
|
});
|
|
167
177
|
|
|
178
|
+
test("mountConnectors carries a pack worker's defineWorker timeoutMs to registerWorker (issue #596)", async () => {
|
|
179
|
+
const engine = new MiniEngine();
|
|
180
|
+
const { ctx } = makeCtx(
|
|
181
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID }] },
|
|
182
|
+
engine,
|
|
183
|
+
{
|
|
184
|
+
files: slackFiles(),
|
|
185
|
+
onImport: () => {
|
|
186
|
+
defineWorker({
|
|
187
|
+
type: "slack:send-message",
|
|
188
|
+
timeoutMs: 900_000,
|
|
189
|
+
async handle(job) {
|
|
190
|
+
await job.complete({});
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
},
|
|
194
|
+
},
|
|
195
|
+
);
|
|
196
|
+
await mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" }));
|
|
197
|
+
assert.equal(engine.workerOptions.get("slack:send-message")?.timeoutMs, 900_000);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test("mountConnectors falls back to the ext.json spec timeoutMs when defineWorker omits it (issue #596)", async () => {
|
|
201
|
+
const engine = new MiniEngine();
|
|
202
|
+
const files = slackFiles();
|
|
203
|
+
files[`/app/node_modules/${PKG}/nano-ide.ext.json`] = JSON.stringify({
|
|
204
|
+
id: PACK_ID,
|
|
205
|
+
workers: [
|
|
206
|
+
{
|
|
207
|
+
type: "slack:send-message",
|
|
208
|
+
entry: "worker.ts",
|
|
209
|
+
timeoutMs: 600_000,
|
|
210
|
+
configFields: [{ key: "botToken", env: "SLACK_BOT_TOKEN" }],
|
|
211
|
+
},
|
|
212
|
+
],
|
|
213
|
+
});
|
|
214
|
+
const { ctx } = makeCtx(
|
|
215
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID }] },
|
|
216
|
+
engine,
|
|
217
|
+
{
|
|
218
|
+
files,
|
|
219
|
+
onImport: () => {
|
|
220
|
+
defineWorker({ type: "slack:send-message", async handle(job) { await job.complete({}); } });
|
|
221
|
+
},
|
|
222
|
+
},
|
|
223
|
+
);
|
|
224
|
+
await mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" }));
|
|
225
|
+
assert.equal(engine.workerOptions.get("slack:send-message")?.timeoutMs, 600_000);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
test("mountConnectors honours the manifest-declared timeoutMs for a connector worker (issue #596)", async () => {
|
|
229
|
+
const engine = new MiniEngine();
|
|
230
|
+
const { ctx } = makeCtx(
|
|
231
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID, timeoutMs: 3_600_000 }] },
|
|
232
|
+
engine,
|
|
233
|
+
{
|
|
234
|
+
files: slackFiles(),
|
|
235
|
+
onImport: () => {
|
|
236
|
+
defineWorker({ type: "slack:send-message", async handle(job) { await job.complete({}); } });
|
|
237
|
+
},
|
|
238
|
+
},
|
|
239
|
+
);
|
|
240
|
+
await mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" }));
|
|
241
|
+
assert.equal(engine.workerOptions.get("slack:send-message")?.timeoutMs, 3_600_000);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
test("mountConnectors connector timeout precedence: defineWorker > ext.json spec > manifest (issue #596)", async () => {
|
|
245
|
+
// Pack code wins over the ext.json spec, which wins over the manifest declaration.
|
|
246
|
+
const engine = new MiniEngine();
|
|
247
|
+
const files = slackFiles();
|
|
248
|
+
files[`/app/node_modules/${PKG}/nano-ide.ext.json`] = JSON.stringify({
|
|
249
|
+
id: PACK_ID,
|
|
250
|
+
workers: [
|
|
251
|
+
{
|
|
252
|
+
type: "slack:send-message",
|
|
253
|
+
entry: "worker.ts",
|
|
254
|
+
timeoutMs: 600_000,
|
|
255
|
+
configFields: [{ key: "botToken", env: "SLACK_BOT_TOKEN" }],
|
|
256
|
+
},
|
|
257
|
+
],
|
|
258
|
+
});
|
|
259
|
+
const { ctx } = makeCtx(
|
|
260
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID, timeoutMs: 3_600_000 }] },
|
|
261
|
+
engine,
|
|
262
|
+
{
|
|
263
|
+
files,
|
|
264
|
+
onImport: () => {
|
|
265
|
+
defineWorker({
|
|
266
|
+
type: "slack:send-message",
|
|
267
|
+
timeoutMs: 900_000,
|
|
268
|
+
async handle(job) { await job.complete({}); },
|
|
269
|
+
});
|
|
270
|
+
},
|
|
271
|
+
},
|
|
272
|
+
);
|
|
273
|
+
await mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" }));
|
|
274
|
+
assert.equal(engine.workerOptions.get("slack:send-message")?.timeoutMs, 900_000);
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
test("mountConnectors rejects a non-positive-integer defineWorker timeoutMs (issue #596)", async () => {
|
|
278
|
+
const engine = new MiniEngine();
|
|
279
|
+
const { ctx } = makeCtx(
|
|
280
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID }] },
|
|
281
|
+
engine,
|
|
282
|
+
{
|
|
283
|
+
files: slackFiles(),
|
|
284
|
+
onImport: () => {
|
|
285
|
+
defineWorker({ type: "slack:send-message", timeoutMs: 0, async handle(job) { await job.complete({}); } });
|
|
286
|
+
},
|
|
287
|
+
},
|
|
288
|
+
);
|
|
289
|
+
await assert.rejects(
|
|
290
|
+
() => mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" })),
|
|
291
|
+
/defineWorker timeoutMs must be a positive integer/,
|
|
292
|
+
);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
test("mountConnectors rejects a malformed ext.json spec timeoutMs (issue #596)", async () => {
|
|
296
|
+
const engine = new MiniEngine();
|
|
297
|
+
const files = slackFiles();
|
|
298
|
+
files[`/app/node_modules/${PKG}/nano-ide.ext.json`] = JSON.stringify({
|
|
299
|
+
id: PACK_ID,
|
|
300
|
+
workers: [
|
|
301
|
+
{
|
|
302
|
+
type: "slack:send-message",
|
|
303
|
+
entry: "worker.ts",
|
|
304
|
+
timeoutMs: -5,
|
|
305
|
+
configFields: [{ key: "botToken", env: "SLACK_BOT_TOKEN" }],
|
|
306
|
+
},
|
|
307
|
+
],
|
|
308
|
+
});
|
|
309
|
+
const { ctx } = makeCtx(
|
|
310
|
+
{ workers: [{ taskType: "slack:send-message", connector: PACK_ID }] },
|
|
311
|
+
engine,
|
|
312
|
+
{
|
|
313
|
+
files,
|
|
314
|
+
onImport: () => {
|
|
315
|
+
defineWorker({ type: "slack:send-message", async handle(job) { await job.complete({}); } });
|
|
316
|
+
},
|
|
317
|
+
},
|
|
318
|
+
);
|
|
319
|
+
await assert.rejects(
|
|
320
|
+
() => mountConnectors(ctx, makeApp({ SLACK_BOT_TOKEN: "xoxb-1" })),
|
|
321
|
+
/nano-ide\.ext\.json timeoutMs must be a positive integer/,
|
|
322
|
+
);
|
|
323
|
+
});
|
|
324
|
+
|
|
168
325
|
test("mountConnectors skips a worker whose required env is unset", async () => {
|
|
169
326
|
const engine = new MiniEngine();
|
|
170
327
|
const { ctx, logs } = makeCtx(
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
import type { AppApi, Mounted, RuntimeContext } from "../context.ts";
|
|
19
19
|
import { errorMessage, isRecord } from "../guards.ts";
|
|
20
20
|
import { BpmnError, type EngineJob, type JobHandler, type WorkerSubscription } from "../host.ts";
|
|
21
|
-
import { workerJobType, type Worker } from "../manifest.ts";
|
|
21
|
+
import { workerJobType, workerTimeoutMs, type Worker } from "../manifest.ts";
|
|
22
22
|
import {
|
|
23
23
|
drainDefinedWorkers,
|
|
24
24
|
type ConnectorWorkerJob,
|
|
@@ -39,6 +39,9 @@ interface PackWorkerSpec {
|
|
|
39
39
|
type: string;
|
|
40
40
|
entry: string;
|
|
41
41
|
maxParallelJobs?: number;
|
|
42
|
+
/** Job lock timeout, ms (issue #596) — a design-time default the pack's
|
|
43
|
+
* `defineWorker({ timeoutMs })` overrides when both are set. */
|
|
44
|
+
timeoutMs?: number;
|
|
42
45
|
configFields?: PackConfigField[];
|
|
43
46
|
}
|
|
44
47
|
|
|
@@ -172,6 +175,32 @@ export function adaptConnectorHandler(worker: DefinedConnectorWorker): JobHandle
|
|
|
172
175
|
};
|
|
173
176
|
}
|
|
174
177
|
|
|
178
|
+
/**
|
|
179
|
+
* Validate a connector worker's job lock timeout (issue #596) before it reaches the
|
|
180
|
+
* engine. Unlike a manifest `workers[].timeoutMs` (author-time validated by
|
|
181
|
+
* `collectManifestIssues`) or a `w.run({ timeoutMs })` (validated at flow build), a
|
|
182
|
+
* connector timeout arrives from a THIRD-PARTY pack — `defineWorker({ timeoutMs })`
|
|
183
|
+
* in pack code or the `timeoutMs` field of its `nano-ide.ext.json` — with no
|
|
184
|
+
* validating gate in between. A zero/negative lock would expire instantly (the job
|
|
185
|
+
* re-activated mid-handler ⇒ duplicate execution) and a non-integer is a pack typo;
|
|
186
|
+
* fail closed at registration, naming the source, rather than propagate either to
|
|
187
|
+
* the engine.
|
|
188
|
+
*/
|
|
189
|
+
function validateConnectorTimeout(
|
|
190
|
+
value: number,
|
|
191
|
+
source: string,
|
|
192
|
+
jobType: string,
|
|
193
|
+
packId: string,
|
|
194
|
+
): number {
|
|
195
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
196
|
+
throw new Error(
|
|
197
|
+
`connector worker "${jobType}" (pack "${packId}"): ${source} timeoutMs must be a ` +
|
|
198
|
+
`positive integer (ms), got ${JSON.stringify(value)}`,
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
return value;
|
|
202
|
+
}
|
|
203
|
+
|
|
175
204
|
/**
|
|
176
205
|
* Mount every enabled connector worker (`workers[]` entries carrying a
|
|
177
206
|
* `connector` field). Fails closed on a seam mismatch (an enabled connector whose
|
|
@@ -259,11 +288,25 @@ export async function mountConnectors(ctx: RuntimeContext, app: AppApi): Promise
|
|
|
259
288
|
{ instanceKey: job.processInstanceKey, elementId: job.elementId, jobType },
|
|
260
289
|
() => inner(job),
|
|
261
290
|
);
|
|
291
|
+
// Job lock timeout (issue #596) precedence, first declared wins:
|
|
292
|
+
// 1. the pack's `defineWorker({ timeoutMs })` — the authoritative runtime value
|
|
293
|
+
// 2. the pack's `nano-ide.ext.json` spec — the pack's design-time default
|
|
294
|
+
// 3. the app manifest's `workers[].timeoutMs` — the app author's declaration
|
|
295
|
+
// (mirrors the maxParallelJobs precedence below). The manifest value is already
|
|
296
|
+
// author-time validated (`collectManifestIssues`); the two pack-supplied values
|
|
297
|
+
// are validated here before either reaches the engine as a lock horizon.
|
|
298
|
+
const timeoutMs =
|
|
299
|
+
worker.timeoutMs !== undefined
|
|
300
|
+
? validateConnectorTimeout(worker.timeoutMs, "defineWorker", jobType, packId)
|
|
301
|
+
: spec.timeoutMs !== undefined
|
|
302
|
+
? validateConnectorTimeout(spec.timeoutMs, "nano-ide.ext.json", jobType, packId)
|
|
303
|
+
: workerTimeoutMs(decl);
|
|
262
304
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
263
305
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
264
306
|
// The pack's `defineWorker({ maxParallelJobs })` is the authoritative runtime
|
|
265
307
|
// value; the ext.json spec is a design-time default when the code omits it.
|
|
266
308
|
maxParallelJobs: worker.maxParallelJobs ?? spec.maxParallelJobs,
|
|
309
|
+
timeoutMs,
|
|
267
310
|
});
|
|
268
311
|
subs.push(sub);
|
|
269
312
|
jobTypes.push(jobType);
|