Skip to main content
0.0.x

@netscript/plugin-workers-core

Job, task, workflow, runtime, config, and testing primitives for NetScript workers plugins. This page is written against the package's public surface reported by deno doc. For the full index of packages and plugins return to the reference overview.

Background-job definitions fail in two places: at definition time, when a job is missing an execution target and nobody notices until production; and at runtime, when the executor and its storage are welded together and untestable. This package attacks both. defineJob, defineTask, and defineWorkflow are typestate-gated builders — build() only exists once an entrypoint or handler is set, so an incomplete definition is a compile error rather than a runtime surprise. The runtime composes from injected registry, worker, and storage ports with memory-backed defaults, so the same definitions run in production and in permission-free tests.

This is the core that the deployable @netscript/plugin-workers plugin binds to a NetScript host. Use it directly for custom hosts, libraries, and tests.

Job context cancellation

JobHandlerContext on the root surface and JobContext on the runtime surface carry a required signal: AbortSignal and optional deadlineAt: number (epoch milliseconds). JobDispatchContext accepts an optional caller signal; the InProcessJobRunner supplies a separate owned signal to the handler.

Input or event In-process runner behavior
job.timeout Deadline is runner clock time at dispatch plus timeout in milliseconds.
Caller deadlineAt Uses the earlier of this deadline and the job timeout deadline; absent when neither exists.
Deadline elapsed Aborts with a DOMException named TimeoutError.
runner.stop() Stops admission, aborts active handlers with ShutdownError, and awaits bounded drain.
Caller signal abort Preserves an Error named TimeoutError, ShutdownError, or AbortError; otherwise normalizes to AbortError.
Later abort Does not replace the first abort reason.
Cleanup exceeds abortGracePeriodMs Dispatch rejects with the abort reason; default budget 1,000 ms, without physical JavaScript termination.

Pass the signal into downstream abort-aware APIs and check it at yielding checkpoints. Cleanup belongs in finally. See handler guidance and cleanup tuning. The Workers API runs router does not expose an operator cancel operation; caller signal cancellation here describes custom in-process dispatch, not an HTTP cancel endpoint. Other execution modes require their own propagation verification.

Exports

The package publishes eighteen entrypoints. The root path carries the authoring surface; the subpaths expose the runtime layers a host composes.

Export specifier Module Exports Purpose
@netscript/plugin-workers-core/integration/commands ./commands.ts 14 Thin checked command sink and its supplied worker boundary.
@netscript/plugin-workers-core ./mod.ts 32 The authoring surface — the three typestate builders, handler results and tools, schedule and permission presets, inspection, and the runtime entry points (documented below).
@netscript/plugin-workers-core/builders ./src/builders/mod.ts 28 The builder layer behind the DSL, including the builder-state types tooling needs.
@netscript/plugin-workers-core/runtime ./src/runtime/mod.ts 132 The full runtime: createWorkersRuntime, the in-process dispatcher and runner, execution records, and resolveWorkerIdempotencyKey.
@netscript/plugin-workers-core/presets ./src/presets/mod.ts 28 startWorkers and the runtime port shapes its default composition fills in.
@netscript/plugin-workers-core/executor ./src/executor/mod.ts 33 The multi-runtime task executor and its per-runtime adapters (Deno, .NET, cmd, generic executable) plus runProcess.
@netscript/plugin-workers-core/workflow ./src/workflow/mod.ts 27 The workflow builder, workflow events, workflow clock, and an in-memory workflow state store.
@netscript/plugin-workers-core/registry ./src/registry/mod.ts 22 KV-backed job and task registries with their filter and selector types.
@netscript/plugin-workers-core/state ./src/state/mod.ts 9 Execution state: records, statuses, trigger types, and the KV execution state store.
@netscript/plugin-workers-core/stores ./src/stores/mod.ts 14 The KV-backed worker idempotency store and its atomic KV primitives.
@netscript/plugin-workers-core/streams ./src/streams/mod.ts 20 Durable stream projection of executions and jobs (workersStreamSchema, createWorkersStreamProducer, the mutation hook).
@netscript/plugin-workers-core/shutdown ./src/shutdown/mod.ts 5 ShutdownManager and the resource/report types behind graceful drain.
@netscript/plugin-workers-core/schemas ./src/domain/public-schema.ts 13 Public structural schemas for worker definitions.
@netscript/plugin-workers-core/contracts/v1 ./src/contracts/v1/mod.ts 27 Version 1 workers API schemas and contract route types (workersContract, workersContractV1).
@netscript/plugin-workers-core/config ./src/config/mod.ts 32 defineWorkers, defineJobs, and the queue-provider configuration schemas.
@netscript/plugin-workers-core/telemetry ./src/telemetry/mod.ts 26 Worker instrumentation abstractions and applyWorkerInstrumentations.
@netscript/plugin-workers-core/abstracts ./src/abstracts/mod.ts 31 Abstract runtime contracts and reserved extension-point base classes.
@netscript/plugin-workers-core/testing ./src/testing/mod.ts 46 createTestWorkersRuntime plus job, result, and execution-record fixtures.

Export counts are the symbol counts deno doc reports for each entrypoint; the layered subpaths re-export shared vocabulary, so the counts overlap rather than sum.

Root surface (@netscript/plugin-workers-core)

Definition builders

Each builder is typestate-gated: build() becomes available only after the definition is complete.

Symbol Kind Description
defineJob function Start a worker job definition chain.
JobBuilder interface Root-surface job builder typestate API.
JobDefinition type alias Root-surface job definition derived from the thin public schema.
defineJobHandler function Define a worker job handler.
JobHandlerContext type alias Context passed to root-surface job handlers.
JobResult type alias Result returned by worker job handlers.
JobId type alias Branded worker job identifier.
defineTask function Start a worker task definition chain.
TaskBuilder interface Root-surface task builder typestate API.
TaskDefinition type alias Root-surface task definition derived from the thin public schema.
TaskId type alias Branded worker task identifier.
defineWorkflow function Start a worker workflow definition chain.
WorkflowBuilder interface Root-surface workflow builder typestate API.
WorkflowDefinition type alias Root-surface workflow definition derived from the thin public schema.

Jobs and tasks both accept either an in-process handler or a runtime-specific entrypoint; workflows sequence their definitions. The three builders share the same typestate discipline.

Handler results and tools

Symbol Kind Description
createSuccessResult function Create a successful job result.
createFailureResult function Create a failed job result.
createJobTools function Create handler tools backed by the active worker telemetry context.
JobTools type alias Runtime tools exposed to worker job handlers.
JobToolSpan type alias Span operations exposed to worker job handlers.

JobTools is how a handler reaches telemetry without importing an SDK: the span operations it exposes are bound to the active worker instrumentation context.

Schedules and permissions

Symbol Kind Description
cron variable Cron schedule helpers for worker jobs.
CronHelpers type alias Cron schedule helper surface for worker jobs.
permissions variable Permission presets for worker jobs and tasks.
PermissionPresets type alias Worker permission preset surface for common job execution modes.

Runtime

Symbol Kind Description
startWorkers function Create and start a workers runtime using default composition.
createWorkersRuntime function Create an unstarted workers runtime with the root surface's minimal options.

Two entry styles for one runtime: root startWorkers() creates and starts the memory-backed preset, while root createWorkersRuntime() creates the same runtime without starting it. Import createWorkersRuntime from /runtime when registries, executors, storage, or other runtime ports must be injected explicitly.

Inspection

Symbol Kind Description
inspectJob function Inspect a job definition without starting a runtime.
inspectTask function Inspect a task definition without starting a runtime.
inspectWorkflow function Inspect a workflow definition without starting a runtime.

These are what CLI listings and doctor checks call: they read a definition and report on it without starting a runtime or touching storage.

Idempotency

Symbol Kind Description
WorkerIdempotencyPort interface At-least-once applied-key guard window; downstream persistence must be independently idempotent.
WorkerIdempotencyInput type alias Input used to resolve and claim an applied key for one worker delivery.
WorkerIdempotencyClaim type alias Result returned when a worker delivery attempts to claim an applied key.
WorkerIdempotencySource type alias How a worker delivery idempotency key was resolved.

Delivery is at-least-once with an applied-key guard window. A crash after an external effect and before marking it applied can repeat that effect; one effective application requires independently idempotent downstream persistence. A key is resolved from a caller-supplied key, the message id, or a payload hash — WorkerIdempotencySource records which — and then claimed against the port before the effect runs.


Back to the reference overview.

Checked command sink

Register branded job/task targets with createWorkerCommandOutboxSink; delivery topic selects the copied allowlist entry. The existing supplied trigger receives the stable dedupe key, correlation and W3C. A receipt is checked for the selected kind/id, nonempty run identity and valid acceptance time before normalization. Missing, malformed, mismatched and bare status responses cannot settle. No queue or progress protocol is added. Cancellation is cooperative. The relay publication-to-mark window and worker effect-to-applied-marker window both permit redelivery; downstream work must be independently idempotent. Construction needs no permissions; supplied clients own theirs.

Symbol Kind Description
createWorkerCommandOutboxSink function Compose a checked existing-worker boundary sink.
WorkerCommandTarget type alias Registered branded job/task target.
WorkerCommandMetadata type alias Stable dedupe, correlation and W3C propagation.
WorkerJobCommandRequest type alias Selected branded job and decoded payload.
WorkerTaskCommandRequest type alias Selected branded task and decoded payload.
WorkerCommandClientPort interface Existing explicit trigger boundary returning raw acceptance for checking.
WorkerCommandSinkOptions type alias Copied topic target registry and supplied client.
CommandJson type alias Service-owned canonical decoded data.
CommandTraceContext type alias Service-owned validated W3C fields.
CommandOutboxDelivery type alias Service-owned decoded delivery.
CommandOutboxSink interface Service-owned documented acceptance boundary.
CommandOutboxAcceptance type alias Service-owned normalized checked identity/time.

Runtime-schema tasks

defineTask(id).payload(selectedSchema).handler(...).build() preserves the selected schema's payload type and validates input before the task handler. The legacy .payload<T>() overload keeps ordinary task compatibility but provides no runtime schema; durable saga worker-task effects refuse it. These task effects use the same selected-definition payload validation as job effects.

KV retention

Each job's retention.kvRetentionDays (default 3) bounds terminal execution records under workers/executions. The execution state store and the registry execution-write port both set KV expireIn from completedAt; rewriting a result or progress record does not extend that deadline. Pending, queued, and running executions remain durable while they are open. An explicit archiveToDb: false never causes a database archive in these KV write or cleanup paths.

DELETE /cleanup now deletes expired terminal execution records instead of returning a stubbed success. It inspects at most 1000 entries per call and advances to the next page on later calls. Atomic version checks prevent it from deleting a record updated after inspection. Fresh terminal records and open executions are preserved. This endpoint also removes legacy execution records written without TTL; normal new-record expiry is performed by the KV backend and needs no app session. Job/task definitions remain configuration rather than expiring execution history.

Exports: discovery metadata

The @netscript/plugin-workers-core/discovery entrypoint (./src/builders/discovery.ts) exports NETSCRIPT_CONTRIBUTION_BUILDERS. Pass these immutable patterns through the plugin SDK additionalBuilders option. The CLI composes first-party metadata before walking the project, so single-plugin updates succeed regardless of workers/streams order.

Export specifier Module
@netscript/plugin-workers-core/discovery ./src/builders/discovery.ts