@nanobpm/urban 0.96.3 → 0.97.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 +32 -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/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/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/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
package/README.md
CHANGED
|
@@ -210,6 +210,38 @@ wiring; `WorkflowClient` deploys and starts, `Worker` hosts your `run` steps.
|
|
|
210
210
|
inspectable in a modeller/Operate — `@nanobpm/urban` bundles `bpmn-auto-layout`
|
|
211
211
|
so this works out of the box.
|
|
212
212
|
|
|
213
|
+
For a long-running step (transcription, an agent call, a multi-GB upload),
|
|
214
|
+
declare a job lock timeout so the engine's lease cannot expire mid-handler and
|
|
215
|
+
the job run twice — per step with `w.run(name, handler, { timeoutMs })`, or
|
|
216
|
+
worker-wide with `new Worker({ jobTimeoutMs })`:
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
const flow = defineFlow("uploads", (w) => {
|
|
220
|
+
w.run("transcode", transcode, { timeoutMs: 3_600_000 }); // 1h lock for this step
|
|
221
|
+
w.run("notify", notify); // worker-wide default (30s) applies
|
|
222
|
+
});
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
Manifest-declared workers get the same knob as `timeoutMs` on the `workers[]`
|
|
226
|
+
entry (validated as a positive integer of ms):
|
|
227
|
+
|
|
228
|
+
```jsonc
|
|
229
|
+
// nano.app.json
|
|
230
|
+
{
|
|
231
|
+
"workers": [
|
|
232
|
+
{ "taskType": "media.transcode", "handler": "workers/transcode.ts", "timeoutMs": 3600000 }
|
|
233
|
+
]
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
Connector-backed workers resolve the lock timeout from three sources, first
|
|
238
|
+
declared wins: the pack's `defineWorker({ timeoutMs })` (authoritative runtime
|
|
239
|
+
value), then the pack's `nano-ide.ext.json` worker `timeoutMs` (design-time
|
|
240
|
+
default), then the app manifest's `workers[].timeoutMs`. Pack-supplied values
|
|
241
|
+
are validated at registration (a non-positive-integer fails the mount, naming
|
|
242
|
+
the source); the manifest value is validated at author time like any other
|
|
243
|
+
worker.
|
|
244
|
+
|
|
213
245
|
### Deploy by convention (`resources/`)
|
|
214
246
|
|
|
215
247
|
Deployables are discovered by convention: with **no `models`** block in the
|
|
@@ -28,6 +28,8 @@ export interface ConnectorWorkerOptions {
|
|
|
28
28
|
export interface DefinedConnectorWorker {
|
|
29
29
|
type: string;
|
|
30
30
|
maxParallelJobs?: number;
|
|
31
|
+
/** Job lock timeout, ms (issue #596) — carried from `defineWorker({ timeoutMs })`. */
|
|
32
|
+
timeoutMs?: number;
|
|
31
33
|
handle: ConnectorWorkerOptions["handle"];
|
|
32
34
|
}
|
|
33
35
|
/** The `@nanobpm/worker` entrypoint the pack calls. Records the handler; does not
|
|
@@ -29,7 +29,12 @@ export function defineWorker(opts) {
|
|
|
29
29
|
if (typeof opts.handle !== "function") {
|
|
30
30
|
throw new Error(`defineWorker("${opts.type}"): handle must be a function`);
|
|
31
31
|
}
|
|
32
|
-
registered.push({
|
|
32
|
+
registered.push({
|
|
33
|
+
type: opts.type,
|
|
34
|
+
maxParallelJobs: opts.maxParallelJobs,
|
|
35
|
+
timeoutMs: opts.timeoutMs,
|
|
36
|
+
handle: opts.handle,
|
|
37
|
+
});
|
|
33
38
|
}
|
|
34
39
|
/** Return every worker registered since the last drain, and clear the registry.
|
|
35
40
|
* The runtime calls this immediately after importing one pack entry, so the
|
|
@@ -201,6 +201,20 @@ export interface WorkerSubscription {
|
|
|
201
201
|
readonly jobType: string;
|
|
202
202
|
unsubscribe(): Promise<void>;
|
|
203
203
|
}
|
|
204
|
+
/** Options for {@link EngineClient.registerWorker}. */
|
|
205
|
+
export interface RegisterWorkerOptions {
|
|
206
|
+
/** Worker name reported to the engine. The adapter supplies a default. */
|
|
207
|
+
workerName?: string;
|
|
208
|
+
/** Max jobs the worker handles in parallel. The adapter supplies a default. */
|
|
209
|
+
maxParallelJobs?: number;
|
|
210
|
+
/** Job lock timeout, ms (issue #596): how long the engine leases an activated job to
|
|
211
|
+
* this worker before re-activating it elsewhere. Set it above the handler's worst-case
|
|
212
|
+
* runtime for long steps (transcription, an agent call, a multi-GB upload) so the lock
|
|
213
|
+
* cannot expire mid-handler and the job run twice. Absent ⇒ the engine/SDK default. */
|
|
214
|
+
timeoutMs?: number;
|
|
215
|
+
/** Restrict the variables surfaced to the handler to this subset (engine-side projection). */
|
|
216
|
+
fetchVariables?: string[];
|
|
217
|
+
}
|
|
204
218
|
/**
|
|
205
219
|
* A process instance's terminal-relevant lifecycle state, as the engine reports it.
|
|
206
220
|
* `ACTIVE` (still running), `COMPLETED` (ended normally at an end event), or
|
|
@@ -910,11 +924,7 @@ export interface EngineClient {
|
|
|
910
924
|
local?: boolean;
|
|
911
925
|
}): Promise<void>;
|
|
912
926
|
/** Register a push worker for a job type. Draining is handled by the adapter. */
|
|
913
|
-
registerWorker(jobType: string, handler: JobHandler, options?:
|
|
914
|
-
workerName?: string;
|
|
915
|
-
maxParallelJobs?: number;
|
|
916
|
-
fetchVariables?: string[];
|
|
917
|
-
}): Promise<WorkerSubscription>;
|
|
927
|
+
registerWorker(jobType: string, handler: JobHandler, options?: RegisterWorkerOptions): Promise<WorkerSubscription>;
|
|
918
928
|
/** Tear down all connections. */
|
|
919
929
|
close(): Promise<void>;
|
|
920
930
|
}
|
|
@@ -84,8 +84,31 @@ export type InstanceTracking = SchemaInstanceTracking & {
|
|
|
84
84
|
* characters, or `activeStatuses.map(...)` crash on a non-array).
|
|
85
85
|
*/
|
|
86
86
|
export declare function isConfiguredStatusSelector(v: unknown): v is readonly string[];
|
|
87
|
+
/**
|
|
88
|
+
* A worker declaration (`workers[]`) as the runtime consumes it: the schema-owned
|
|
89
|
+
* {@link Worker} plus the pending-schema `timeoutMs` field (issue #596) — the job lock
|
|
90
|
+
* timeout in ms, passed through `registerWorker` to `createJobWorker({ jobTimeoutMs })`.
|
|
91
|
+
* For a long-running step (transcription, an agent call, a multi-GB upload) the SDK's
|
|
92
|
+
* default lock expires mid-handler: the engine re-activates the job and a second worker
|
|
93
|
+
* runs it again, and the first worker's completion is rejected. Set `timeoutMs` above the
|
|
94
|
+
* handler's worst-case runtime to keep the lock for the whole step.
|
|
95
|
+
*
|
|
96
|
+
* Bridged locally — like {@link NetworkConfig} — until `@nanobpm/nano-app-schema` folds
|
|
97
|
+
* `timeoutMs` into the `worker` definition (the schema shape is owned by that package,
|
|
98
|
+
* ADR 0027, and CI bans `declare module` augmentation). Drop this augmentation and
|
|
99
|
+
* re-export `Worker` plainly once the dep bump lands (No Drift Surfaces).
|
|
100
|
+
*/
|
|
101
|
+
export type WorkerDecl = Worker & {
|
|
102
|
+
/** Job lock timeout, ms. Must be a positive integer (the manifest validator enforces it). */
|
|
103
|
+
readonly timeoutMs?: number;
|
|
104
|
+
};
|
|
87
105
|
/** The job type a worker subscribes to (schema key: `taskType`). */
|
|
88
106
|
export declare function workerJobType(w: Worker): string | undefined;
|
|
107
|
+
/** A worker declaration's job lock timeout, or `undefined` when absent/invalid — the
|
|
108
|
+
* validator flags an invalid value, so a consumer treats it as unset rather than
|
|
109
|
+
* propagating a malformed lock to the engine. Read reflectively (like `network.bind`)
|
|
110
|
+
* because `timeoutMs` is pending-schema and not on the static {@link Worker} type. */
|
|
111
|
+
export declare function workerTimeoutMs(w: Worker): number | undefined;
|
|
89
112
|
/**
|
|
90
113
|
* The valid HTTP bind modes, in declaration order. This is the single runtime source of
|
|
91
114
|
* truth for the set: {@link BindMode}, {@link isBindMode}, and the manifest validator
|
|
@@ -25,6 +25,14 @@ export function isConfiguredStatusSelector(v) {
|
|
|
25
25
|
export function workerJobType(w) {
|
|
26
26
|
return w.taskType;
|
|
27
27
|
}
|
|
28
|
+
/** A worker declaration's job lock timeout, or `undefined` when absent/invalid — the
|
|
29
|
+
* validator flags an invalid value, so a consumer treats it as unset rather than
|
|
30
|
+
* propagating a malformed lock to the engine. Read reflectively (like `network.bind`)
|
|
31
|
+
* because `timeoutMs` is pending-schema and not on the static {@link Worker} type. */
|
|
32
|
+
export function workerTimeoutMs(w) {
|
|
33
|
+
const v = Reflect.get(w, "timeoutMs");
|
|
34
|
+
return typeof v === "number" && Number.isInteger(v) && v > 0 ? v : undefined;
|
|
35
|
+
}
|
|
28
36
|
/**
|
|
29
37
|
* The valid HTTP bind modes, in declaration order. This is the single runtime source of
|
|
30
38
|
* truth for the set: {@link BindMode}, {@link isBindMode}, and the manifest validator
|
|
@@ -13,6 +13,9 @@ interface PackWorkerSpec {
|
|
|
13
13
|
type: string;
|
|
14
14
|
entry: string;
|
|
15
15
|
maxParallelJobs?: number;
|
|
16
|
+
/** Job lock timeout, ms (issue #596) — a design-time default the pack's
|
|
17
|
+
* `defineWorker({ timeoutMs })` overrides when both are set. */
|
|
18
|
+
timeoutMs?: number;
|
|
16
19
|
configFields?: PackConfigField[];
|
|
17
20
|
}
|
|
18
21
|
/** The pack manifest (`nano-ide.ext.json`) fields the runtime reads. */
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
// the pack's `@nanobpm/worker` import live behind `host.importConnectorModule`.
|
|
17
17
|
import { errorMessage, isRecord } from "../guards.js";
|
|
18
18
|
import { BpmnError } from "../host.js";
|
|
19
|
-
import { workerJobType } from "../manifest.js";
|
|
19
|
+
import { workerJobType, workerTimeoutMs } from "../manifest.js";
|
|
20
20
|
import { drainDefinedWorkers, } from "../../connector-worker-sdk.js";
|
|
21
21
|
import { runInJobContext } from "../execContext.js";
|
|
22
22
|
function joinRoot(root, p) {
|
|
@@ -121,6 +121,24 @@ export function adaptConnectorHandler(worker) {
|
|
|
121
121
|
return isRecord(ret) ? ret : {};
|
|
122
122
|
};
|
|
123
123
|
}
|
|
124
|
+
/**
|
|
125
|
+
* Validate a connector worker's job lock timeout (issue #596) before it reaches the
|
|
126
|
+
* engine. Unlike a manifest `workers[].timeoutMs` (author-time validated by
|
|
127
|
+
* `collectManifestIssues`) or a `w.run({ timeoutMs })` (validated at flow build), a
|
|
128
|
+
* connector timeout arrives from a THIRD-PARTY pack — `defineWorker({ timeoutMs })`
|
|
129
|
+
* in pack code or the `timeoutMs` field of its `nano-ide.ext.json` — with no
|
|
130
|
+
* validating gate in between. A zero/negative lock would expire instantly (the job
|
|
131
|
+
* re-activated mid-handler ⇒ duplicate execution) and a non-integer is a pack typo;
|
|
132
|
+
* fail closed at registration, naming the source, rather than propagate either to
|
|
133
|
+
* the engine.
|
|
134
|
+
*/
|
|
135
|
+
function validateConnectorTimeout(value, source, jobType, packId) {
|
|
136
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
137
|
+
throw new Error(`connector worker "${jobType}" (pack "${packId}"): ${source} timeoutMs must be a ` +
|
|
138
|
+
`positive integer (ms), got ${JSON.stringify(value)}`);
|
|
139
|
+
}
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
124
142
|
/**
|
|
125
143
|
* Mount every enabled connector worker (`workers[]` entries carrying a
|
|
126
144
|
* `connector` field). Fails closed on a seam mismatch (an enabled connector whose
|
|
@@ -192,11 +210,24 @@ export async function mountConnectors(ctx, app) {
|
|
|
192
210
|
}
|
|
193
211
|
const inner = adaptConnectorHandler(worker);
|
|
194
212
|
const wrapped = (job) => runInJobContext({ instanceKey: job.processInstanceKey, elementId: job.elementId, jobType }, () => inner(job));
|
|
213
|
+
// Job lock timeout (issue #596) precedence, first declared wins:
|
|
214
|
+
// 1. the pack's `defineWorker({ timeoutMs })` — the authoritative runtime value
|
|
215
|
+
// 2. the pack's `nano-ide.ext.json` spec — the pack's design-time default
|
|
216
|
+
// 3. the app manifest's `workers[].timeoutMs` — the app author's declaration
|
|
217
|
+
// (mirrors the maxParallelJobs precedence below). The manifest value is already
|
|
218
|
+
// author-time validated (`collectManifestIssues`); the two pack-supplied values
|
|
219
|
+
// are validated here before either reaches the engine as a lock horizon.
|
|
220
|
+
const timeoutMs = worker.timeoutMs !== undefined
|
|
221
|
+
? validateConnectorTimeout(worker.timeoutMs, "defineWorker", jobType, packId)
|
|
222
|
+
: spec.timeoutMs !== undefined
|
|
223
|
+
? validateConnectorTimeout(spec.timeoutMs, "nano-ide.ext.json", jobType, packId)
|
|
224
|
+
: workerTimeoutMs(decl);
|
|
195
225
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
196
226
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
197
227
|
// The pack's `defineWorker({ maxParallelJobs })` is the authoritative runtime
|
|
198
228
|
// value; the ext.json spec is a design-time default when the code omits it.
|
|
199
229
|
maxParallelJobs: worker.maxParallelJobs ?? spec.maxParallelJobs,
|
|
230
|
+
timeoutMs,
|
|
200
231
|
});
|
|
201
232
|
subs.push(sub);
|
|
202
233
|
jobTypes.push(jobType);
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// on the engine. Handlers are resolved by a small, documented contract and are injected
|
|
3
3
|
// with the app's runtime API (datasource + engine + host utils).
|
|
4
4
|
import { isRecord } from "../guards.js";
|
|
5
|
-
import { workerJobType } from "../manifest.js";
|
|
5
|
+
import { workerJobType, workerTimeoutMs } from "../manifest.js";
|
|
6
6
|
import { runLlmJob } from "./llm.js";
|
|
7
7
|
import { runInJobContext } from "../execContext.js";
|
|
8
8
|
import { readLineage } from "../lineage.js";
|
|
@@ -274,6 +274,8 @@ export async function mountWorkers(ctx, app, sched = defaultScheduler()) {
|
|
|
274
274
|
};
|
|
275
275
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
276
276
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
277
|
+
// The declared job lock timeout (issue #596) — absent ⇒ the engine/SDK default.
|
|
278
|
+
timeoutMs: workerTimeoutMs(decl),
|
|
277
279
|
});
|
|
278
280
|
subs.push(sub);
|
|
279
281
|
jobTypes.push(jobType);
|
|
@@ -293,6 +295,8 @@ export async function mountWorkers(ctx, app, sched = defaultScheduler()) {
|
|
|
293
295
|
};
|
|
294
296
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
295
297
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
298
|
+
// The declared job lock timeout (issue #596) — absent ⇒ the engine/SDK default.
|
|
299
|
+
timeoutMs: workerTimeoutMs(decl),
|
|
296
300
|
});
|
|
297
301
|
subs.push(sub);
|
|
298
302
|
jobTypes.push(jobType);
|
|
@@ -125,6 +125,18 @@ export function collectManifestIssues(m) {
|
|
|
125
125
|
});
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
|
+
// The pending-schema job lock timeout (issue #596): when present it must be a
|
|
129
|
+
// positive integer of ms — a zero/negative lock would expire instantly (the job
|
|
130
|
+
// re-activated mid-handler), and a non-integer is a manifest typo to fail fast on.
|
|
131
|
+
if (worker !== undefined && "timeoutMs" in worker) {
|
|
132
|
+
const t = worker.timeoutMs;
|
|
133
|
+
if (typeof t !== "number" || !Number.isInteger(t) || t <= 0) {
|
|
134
|
+
issues.push({
|
|
135
|
+
path: `workers[${i}].timeoutMs`,
|
|
136
|
+
message: "timeoutMs must be a positive integer (ms)",
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
}
|
|
128
140
|
});
|
|
129
141
|
}
|
|
130
142
|
const data = isRecord(obj.data) ? obj.data : undefined;
|
|
@@ -286,6 +286,7 @@ export declare class SdkEngineClient implements EngineClient {
|
|
|
286
286
|
registerWorker(jobType: string, handler: JobHandler, options?: {
|
|
287
287
|
workerName?: string;
|
|
288
288
|
maxParallelJobs?: number;
|
|
289
|
+
timeoutMs?: number;
|
|
289
290
|
fetchVariables?: string[];
|
|
290
291
|
}): Promise<WorkerSubscription>;
|
|
291
292
|
/** Stop one worker, tolerating a Falcon worker whose transport never bound
|
|
@@ -1074,6 +1074,9 @@ export class SdkEngineClient {
|
|
|
1074
1074
|
jobType,
|
|
1075
1075
|
workerName: options?.workerName ?? `urban:${jobType}`,
|
|
1076
1076
|
maxParallelJobs: options?.maxParallelJobs ?? 8,
|
|
1077
|
+
// The job lock timeout (issue #596): pass it through only when declared, so an
|
|
1078
|
+
// undeclared worker keeps the SDK's default lock rather than an explicit value.
|
|
1079
|
+
...(options?.timeoutMs !== undefined ? { jobTimeoutMs: options.timeoutMs } : {}),
|
|
1077
1080
|
fetchVariables: options?.fetchVariables,
|
|
1078
1081
|
jobHandler: async (job) => {
|
|
1079
1082
|
const rawKey = job.jobKey;
|
package/dist/runtime/index.d.ts
CHANGED
|
@@ -4,11 +4,11 @@ export { EventBus } from "./core/events.ts";
|
|
|
4
4
|
export type { DispatchMode, Disposer, EmitChannel, ErrorSink, EventBusOptions, Listener, Middleware, ParallelChannel, SerialChannel, WaterfallChannel, } from "./core/events.ts";
|
|
5
5
|
export { URBAN_EVENT_MODES, createUrbanEvents, mountExtensions, urbanEventMode } from "./core/extensions.ts";
|
|
6
6
|
export type { DispatchRequest, DispatchResponse, ExtensionHost, ExtensionRegistered, ExtensionSetupContext, GateDecision, GateRequest, LifecycleEvent, MountExtensionsOptions, ReconcileEvent, UrbanEventName, UrbanEvents, UrbanExtension, } from "./core/extensions.ts";
|
|
7
|
-
export { bindModeToHost, expandEnv, expandEnvString, isBindMode, loadManifest, parseManifest, resolveBindHost, resolveBindMode, workerJobType, ALL_INTERFACES_HOST, BIND_ENV_VAR, LOOPBACK_HOST, } from "./core/manifest.ts";
|
|
8
|
-
export type { ActionDecl, AppManifest, BindMode, ChatSurface, DataSource, DomainField, DomainType, InstanceTracking, LlmBinding, NetworkConfig, PagesSurface, Surfaces, TaskInboxSurface, Trigger, Worker, } from "./core/manifest.ts";
|
|
7
|
+
export { bindModeToHost, expandEnv, expandEnvString, isBindMode, loadManifest, parseManifest, resolveBindHost, resolveBindMode, workerJobType, workerTimeoutMs, ALL_INTERFACES_HOST, BIND_ENV_VAR, LOOPBACK_HOST, } from "./core/manifest.ts";
|
|
8
|
+
export type { ActionDecl, AppManifest, BindMode, ChatSurface, DataSource, DomainField, DomainType, InstanceTracking, LlmBinding, NetworkConfig, PagesSurface, Surfaces, TaskInboxSurface, Trigger, Worker, WorkerDecl, } from "./core/manifest.ts";
|
|
9
9
|
export { collectManifestIssues, ManifestValidationError, validateManifest, } from "./core/validate.ts";
|
|
10
10
|
export type { ValidationIssue } from "./core/validate.ts";
|
|
11
|
-
export type { EngineClient, AgentHistoryFilter, AgentHistoryRecord, AgentInstanceFilter, AgentInstanceMetrics, AgentInstanceSummary, ElementInstanceState, ElementInstanceSummary, ElementInstanceFilter, ElementInstanceWaitState, ElementInstanceWaitStateFilter, WaitStateType, EngineJob, FormSchema, HostContext, HttpHandler, HttpRequest, HttpResponse, HttpServer, IncidentState, EngineIncidentState, IncidentFilter, IncidentSummary, JobFilter, JobHandler, JobSummary, CreatedProcessInstance, ProcessDefinitionIdentity, ProcessInstanceSnapshot, ProcessInstanceState, SqliteDb, UserTaskState, UserTaskSummary, UserTaskFilter, VariableFilter, VariableSummary, WatchHandle, WorkerSubscription, } from "./core/host.ts";
|
|
11
|
+
export type { EngineClient, AgentHistoryFilter, AgentHistoryRecord, AgentInstanceFilter, AgentInstanceMetrics, AgentInstanceSummary, ElementInstanceState, ElementInstanceSummary, ElementInstanceFilter, ElementInstanceWaitState, ElementInstanceWaitStateFilter, WaitStateType, EngineJob, FormSchema, HostContext, HttpHandler, HttpRequest, HttpResponse, HttpServer, IncidentState, EngineIncidentState, IncidentFilter, IncidentSummary, JobFilter, JobHandler, JobSummary, CreatedProcessInstance, ProcessDefinitionIdentity, ProcessInstanceSnapshot, ProcessInstanceState, RegisterWorkerOptions, SqliteDb, UserTaskState, UserTaskSummary, UserTaskFilter, VariableFilter, VariableSummary, WatchHandle, WorkerSubscription, } from "./core/host.ts";
|
|
12
12
|
export type { TranscriptContentBlock, TranscriptContentType, TranscriptToolCall, TranscriptTurnMetrics, TranscriptTurnRole, } from "@nanobpm/agentic/transcript";
|
|
13
13
|
export { buildFormSchema, parseFormSchema, pickFormLinkage, presentFormIdentifier, resolveFormIdentifier, } from "./core/form-contract.ts";
|
|
14
14
|
export { pickProcessDefinitionIdentity, presentEngineKey, presentString, } from "./core/process-identity.ts";
|
package/dist/runtime/index.js
CHANGED
|
@@ -5,7 +5,7 @@ export { createUrbanApp } from "./core/runtime.js";
|
|
|
5
5
|
export { EventBus } from "./core/events.js";
|
|
6
6
|
export { URBAN_EVENT_MODES, createUrbanEvents, mountExtensions, urbanEventMode } from "./core/extensions.js";
|
|
7
7
|
// Manifest
|
|
8
|
-
export { bindModeToHost, expandEnv, expandEnvString, isBindMode, loadManifest, parseManifest, resolveBindHost, resolveBindMode, workerJobType, ALL_INTERFACES_HOST, BIND_ENV_VAR, LOOPBACK_HOST, } from "./core/manifest.js";
|
|
8
|
+
export { bindModeToHost, expandEnv, expandEnvString, isBindMode, loadManifest, parseManifest, resolveBindHost, resolveBindMode, workerJobType, workerTimeoutMs, ALL_INTERFACES_HOST, BIND_ENV_VAR, LOOPBACK_HOST, } from "./core/manifest.js";
|
|
9
9
|
// Validation
|
|
10
10
|
export { collectManifestIssues, ManifestValidationError, validateManifest, } from "./core/validate.js";
|
|
11
11
|
// Shared form/user-task contract normalization (issue #252): the single source of truth
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nanobpm/urban",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.97.0",
|
|
4
4
|
"description": "Urban: build and run code-first apps on Nano — runtime, derivation toolkit, and CLI in one. Author durable processes with @nanobpm/workflow (defineFlow), a typed datasource, forms, triggers and surfaces from a nano.app.json manifest, on Node or Deno.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"type": "module",
|
|
@@ -106,7 +106,7 @@
|
|
|
106
106
|
"@nanobpm/agentic": "^0.15.0",
|
|
107
107
|
"@nanobpm/nano-app-schema": "^0.11.0",
|
|
108
108
|
"@nanobpm/nano-sdk": "^1.2.5",
|
|
109
|
-
"@nanobpm/workflow": "^0.
|
|
109
|
+
"@nanobpm/workflow": "^0.16.0",
|
|
110
110
|
"bpmn-auto-layout": "^2.0.0-alpha.2",
|
|
111
111
|
"create-urban-app": "^0.15.3",
|
|
112
112
|
"yaml": "^2.9.0"
|
|
@@ -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);
|
|
@@ -23,6 +23,11 @@ import { join } from "node:path";
|
|
|
23
23
|
/** A tiny engine that records registrations and can deliver a job to a handler. */
|
|
24
24
|
class MiniEngine implements EngineClient {
|
|
25
25
|
workers = new Map<string, JobHandler>();
|
|
26
|
+
/** The options each `registerWorker` call carried, keyed by job type. */
|
|
27
|
+
workerOptions = new Map<
|
|
28
|
+
string,
|
|
29
|
+
{ workerName?: string; maxParallelJobs?: number; timeoutMs?: number; fetchVariables?: string[] } | undefined
|
|
30
|
+
>();
|
|
26
31
|
async deployResources(): Promise<{ deployed: number }> { return { deployed: 0 }; }
|
|
27
32
|
async createInstance(): Promise<{ processInstanceKey: string }> { return { processInstanceKey: "pi" }; }
|
|
28
33
|
async cancelInstance(): Promise<void> {}
|
|
@@ -46,8 +51,13 @@ class MiniEngine implements EngineClient {
|
|
|
46
51
|
async updateJobRetries(): Promise<void> {}
|
|
47
52
|
async setVariables(): Promise<void> {}
|
|
48
53
|
async close(): Promise<void> {}
|
|
49
|
-
async registerWorker(
|
|
54
|
+
async registerWorker(
|
|
55
|
+
jobType: string,
|
|
56
|
+
handler: JobHandler,
|
|
57
|
+
options?: { workerName?: string; maxParallelJobs?: number; timeoutMs?: number; fetchVariables?: string[] },
|
|
58
|
+
): Promise<WorkerSubscription> {
|
|
50
59
|
this.workers.set(jobType, handler);
|
|
60
|
+
this.workerOptions.set(jobType, options);
|
|
51
61
|
return { jobType, unsubscribe: async () => void this.workers.delete(jobType) };
|
|
52
62
|
}
|
|
53
63
|
deliver(jobType: string, job: EngineJob) {
|
|
@@ -226,6 +236,40 @@ test("mountWorkers silently skips a connector-backed worker (mountConnectors own
|
|
|
226
236
|
assert.ok(!logs.some((l) => l.msg.includes("neither a handler nor an llm")));
|
|
227
237
|
});
|
|
228
238
|
|
|
239
|
+
test("mountWorkers passes a worker's declared job lock timeout (timeoutMs) to registerWorker (issue #596)", async () => {
|
|
240
|
+
const engine = new MiniEngine();
|
|
241
|
+
const { ctx } = makeCtx(
|
|
242
|
+
{
|
|
243
|
+
workers: [
|
|
244
|
+
{ taskType: "transcribe", handler: "workers/transcribe.ts", timeoutMs: 600_000 },
|
|
245
|
+
{ taskType: "quick", handler: "workers/quick.ts" },
|
|
246
|
+
],
|
|
247
|
+
llm: { fast: { provider: "env", model: "${NANO_APP_LLM_MODEL}" } },
|
|
248
|
+
},
|
|
249
|
+
engine,
|
|
250
|
+
);
|
|
251
|
+
ctx.host.importModule = async () => ({ default: () => ({}) });
|
|
252
|
+
await mountWorkers(ctx, makeApp());
|
|
253
|
+
assert.equal(engine.workerOptions.get("transcribe")?.timeoutMs, 600_000);
|
|
254
|
+
// A worker with no declared timeout leaves the engine/SDK default lock in place.
|
|
255
|
+
assert.equal(engine.workerOptions.get("quick")?.timeoutMs, undefined);
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
test("mountWorkers passes an llm worker's declared timeoutMs to registerWorker (issue #596)", async () => {
|
|
259
|
+
const engine = new MiniEngine();
|
|
260
|
+
const { ctx } = makeCtx(
|
|
261
|
+
{
|
|
262
|
+
workers: [{ taskType: "summarize", llm: "fast", timeoutMs: 300_000 }],
|
|
263
|
+
llm: { fast: { provider: "env", model: "${NANO_APP_LLM_MODEL}" } },
|
|
264
|
+
},
|
|
265
|
+
engine,
|
|
266
|
+
);
|
|
267
|
+
await withFetch("{}", async () => {
|
|
268
|
+
await mountWorkers(ctx, makeApp());
|
|
269
|
+
});
|
|
270
|
+
assert.equal(engine.workerOptions.get("summarize")?.timeoutMs, 300_000);
|
|
271
|
+
});
|
|
272
|
+
|
|
229
273
|
test("mountWorkers binds the job's correlation context onto the handler's app.log", async () => {
|
|
230
274
|
const engine = new MiniEngine();
|
|
231
275
|
const { ctx } = makeCtx({ workers: [{ taskType: "charge", handler: "workers/charge.ts" }] }, engine);
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import type { AppApi, Mounted, RuntimeContext } from "../context.ts";
|
|
6
6
|
import { isRecord } from "../guards.ts";
|
|
7
7
|
import type { EngineJob, JobHandler, WorkerSubscription } from "../host.ts";
|
|
8
|
-
import { workerJobType, type Worker } from "../manifest.ts";
|
|
8
|
+
import { workerJobType, workerTimeoutMs, type Worker } from "../manifest.ts";
|
|
9
9
|
import { type DecisionEvaluator, type LlmRuntime, runLlmJob } from "./llm.ts";
|
|
10
10
|
import { runInJobContext } from "../execContext.ts";
|
|
11
11
|
import type { JobExecContext } from "../execContext.ts";
|
|
@@ -326,6 +326,8 @@ export async function mountWorkers(
|
|
|
326
326
|
};
|
|
327
327
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
328
328
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
329
|
+
// The declared job lock timeout (issue #596) — absent ⇒ the engine/SDK default.
|
|
330
|
+
timeoutMs: workerTimeoutMs(decl),
|
|
329
331
|
});
|
|
330
332
|
subs.push(sub);
|
|
331
333
|
jobTypes.push(jobType);
|
|
@@ -349,6 +351,8 @@ export async function mountWorkers(
|
|
|
349
351
|
};
|
|
350
352
|
const sub = await ctx.engine.registerWorker(jobType, wrapped, {
|
|
351
353
|
workerName: `${ctx.manifest.id}:${jobType}`,
|
|
354
|
+
// The declared job lock timeout (issue #596) — absent ⇒ the engine/SDK default.
|
|
355
|
+
timeoutMs: workerTimeoutMs(decl),
|
|
352
356
|
});
|
|
353
357
|
subs.push(sub);
|
|
354
358
|
jobTypes.push(jobType);
|
|
@@ -100,6 +100,25 @@ test("an llm-backed worker (no handler) is accepted", () => {
|
|
|
100
100
|
assert.ok(!issues.some((i) => i.path.startsWith("workers[0]")));
|
|
101
101
|
});
|
|
102
102
|
|
|
103
|
+
test("a worker job lock timeout (timeoutMs) must be a positive integer (issue #596)", () => {
|
|
104
|
+
const ok = collectManifestIssues({
|
|
105
|
+
...valid,
|
|
106
|
+
workers: [{ taskType: "a.b", handler: "workers/x.ts", timeoutMs: 600_000 }],
|
|
107
|
+
});
|
|
108
|
+
assert.ok(!ok.some((i) => i.path.startsWith("workers[0]")));
|
|
109
|
+
|
|
110
|
+
for (const bad of [0, -5, 1.5, Number.NaN, "600000"]) {
|
|
111
|
+
const issues = collectManifestIssues({
|
|
112
|
+
...valid,
|
|
113
|
+
workers: [{ taskType: "a.b", handler: "workers/x.ts", timeoutMs: bad }],
|
|
114
|
+
});
|
|
115
|
+
assert.ok(
|
|
116
|
+
issues.some((i) => i.path === "workers[0].timeoutMs"),
|
|
117
|
+
`timeoutMs=${String(bad)} must be rejected`,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
103
122
|
test("data.default must reference an existing source", () => {
|
|
104
123
|
const issues = collectManifestIssues({
|
|
105
124
|
...valid,
|
|
@@ -144,6 +144,18 @@ export function collectManifestIssues(m: unknown): ValidationIssue[] {
|
|
|
144
144
|
});
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
+
// The pending-schema job lock timeout (issue #596): when present it must be a
|
|
148
|
+
// positive integer of ms — a zero/negative lock would expire instantly (the job
|
|
149
|
+
// re-activated mid-handler), and a non-integer is a manifest typo to fail fast on.
|
|
150
|
+
if (worker !== undefined && "timeoutMs" in worker) {
|
|
151
|
+
const t = worker.timeoutMs;
|
|
152
|
+
if (typeof t !== "number" || !Number.isInteger(t) || t <= 0) {
|
|
153
|
+
issues.push({
|
|
154
|
+
path: `workers[${i}].timeoutMs`,
|
|
155
|
+
message: "timeoutMs must be a positive integer (ms)",
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
}
|
|
147
159
|
});
|
|
148
160
|
}
|
|
149
161
|
|
|
@@ -1211,6 +1211,23 @@ test("registerWorker creates a worker the SDK auto-starts, and dispatches + comp
|
|
|
1211
1211
|
assert.equal(rec.stopped, 1);
|
|
1212
1212
|
});
|
|
1213
1213
|
|
|
1214
|
+
test("registerWorker passes the job lock timeout through to createJobWorker (issue #596)", async () => {
|
|
1215
|
+
const client = fakeSdkClient();
|
|
1216
|
+
const engine = new SdkEngineClient(client);
|
|
1217
|
+
await engine.registerWorker("svc", () => ({}), { timeoutMs: 600_000 });
|
|
1218
|
+
const rec = client.workers[0];
|
|
1219
|
+
assert.equal(rec.cfg.jobTimeoutMs, 600_000);
|
|
1220
|
+
});
|
|
1221
|
+
|
|
1222
|
+
test("registerWorker leaves jobTimeoutMs unset (SDK default lock) when no timeoutMs is given", async () => {
|
|
1223
|
+
const client = fakeSdkClient();
|
|
1224
|
+
const engine = new SdkEngineClient(client);
|
|
1225
|
+
await engine.registerWorker("svc", () => ({}));
|
|
1226
|
+
const rec = client.workers[0];
|
|
1227
|
+
assert.equal(rec.cfg.jobTimeoutMs, undefined);
|
|
1228
|
+
assert.equal("jobTimeoutMs" in rec.cfg, false);
|
|
1229
|
+
});
|
|
1230
|
+
|
|
1214
1231
|
test("registerWorker fails the job when the handler throws", async () => {
|
|
1215
1232
|
const client = fakeSdkClient();
|
|
1216
1233
|
const engine = new SdkEngineClient(client);
|
|
@@ -1366,12 +1366,15 @@ export class SdkEngineClient implements EngineClient {
|
|
|
1366
1366
|
async registerWorker(
|
|
1367
1367
|
jobType: string,
|
|
1368
1368
|
handler: JobHandler,
|
|
1369
|
-
options?: { workerName?: string; maxParallelJobs?: number; fetchVariables?: string[] },
|
|
1369
|
+
options?: { workerName?: string; maxParallelJobs?: number; timeoutMs?: number; fetchVariables?: string[] },
|
|
1370
1370
|
): Promise<WorkerSubscription> {
|
|
1371
1371
|
const worker = this.client.createJobWorker({
|
|
1372
1372
|
jobType,
|
|
1373
1373
|
workerName: options?.workerName ?? `urban:${jobType}`,
|
|
1374
1374
|
maxParallelJobs: options?.maxParallelJobs ?? 8,
|
|
1375
|
+
// The job lock timeout (issue #596): pass it through only when declared, so an
|
|
1376
|
+
// undeclared worker keeps the SDK's default lock rather than an explicit value.
|
|
1377
|
+
...(options?.timeoutMs !== undefined ? { jobTimeoutMs: options.timeoutMs } : {}),
|
|
1375
1378
|
fetchVariables: options?.fetchVariables,
|
|
1376
1379
|
jobHandler: async (job) => {
|
|
1377
1380
|
const rawKey = job.jobKey;
|
package/src/runtime/index.ts
CHANGED
|
@@ -50,6 +50,7 @@ export {
|
|
|
50
50
|
resolveBindHost,
|
|
51
51
|
resolveBindMode,
|
|
52
52
|
workerJobType,
|
|
53
|
+
workerTimeoutMs,
|
|
53
54
|
ALL_INTERFACES_HOST,
|
|
54
55
|
BIND_ENV_VAR,
|
|
55
56
|
LOOPBACK_HOST,
|
|
@@ -70,6 +71,7 @@ export type {
|
|
|
70
71
|
TaskInboxSurface,
|
|
71
72
|
Trigger,
|
|
72
73
|
Worker,
|
|
74
|
+
WorkerDecl,
|
|
73
75
|
} from "./core/manifest.ts";
|
|
74
76
|
|
|
75
77
|
// Validation
|
|
@@ -112,6 +114,7 @@ export type {
|
|
|
112
114
|
ProcessDefinitionIdentity,
|
|
113
115
|
ProcessInstanceSnapshot,
|
|
114
116
|
ProcessInstanceState,
|
|
117
|
+
RegisterWorkerOptions,
|
|
115
118
|
SqliteDb,
|
|
116
119
|
UserTaskState,
|
|
117
120
|
UserTaskSummary,
|