Plugin system
This page explains how a NetScript plugin works: the core package behind it, the typed manifest of contributions it declares, the generated registries that wire it into a host, and why its HTTP API and background work run as separate processes.
To add a plugin to a workspace, follow the
add-a-plugin how-to ; to write one, see
author-a-plugin . The exact symbols are in the @netscript/plugin reference .
A plugin is a thin layer over a core package
A NetScript plugin is usually two packages. The logic lives in a core package; the plugin is a thin integration layer that contributes that logic to a host.
| Public plugin | Sibling core package | What lives in core |
|---|---|---|
@netscript/plugin-workers | @netscript/plugin-workers-core | Job/scheduler runtime, ports, contracts. |
@netscript/plugin-sagas | @netscript/plugin-sagas-core | Saga DSL, durable runtime, durability tiers. |
@netscript/plugin-triggers | @netscript/plugin-triggers-core | Trigger definition builders, ingress runtime. |
@netscript/plugin-streams | @netscript/plugin-streams-core | Durable stream producer runtime. |
@netscript/plugin-auth | @netscript/plugin-auth-core (+ backends) | AuthBackendPort seam, domain, contracts. |
The split separates behavior from integration:
- The core package implements the capability: the authoring DSL (for example the saga and trigger definition builders), the runtime, the ports it depends on, the adapters, telemetry helpers, and the versioned contract types. Long-running behavior and state live here.
- The plugin package is the integration layer. Its
mod.tsis small: it re-exports the userland surface from its core sibling, and adds the one thing the core cannot supply, a manifest that tells a host what the plugin contributes and how to wire it.
Because application authors reach core symbols through the public plugin, each plugin's reference page lists its core package in an Internals subsection rather than as a separate top-level entry.
Streams follow the same split. Runtime producers use defineStreamSchema and createDurableStream
from @netscript/plugin-streams-core, and consumers read the stream over HTTP/SSE. The
@netscript/plugin-streams root carries the service manifest and typed topic definitions. See
durable streams for the producer contract.
The two-tier shape also applies to custom plugins. When you scaffold a capability that NetScript
does not ship, using netscript plugin new <name>, the generator emits the same pair: a JSR-publishable
core engine package under packages/plugin-<name>-core/ (its domain, ports, application
orchestration, contracts/v1, and testing doubles) and a thin connector under plugins/<name>/
(its manifest, adapter, Aspire contribution, CLI and scaffold entrypoints, and service), whose
contracts subpath re-exports the core's contracts/v1. A custom plugin starts with the same
behavior-versus-integration split, thin and independently publishable. See
author-a-plugin for the walkthrough.
The manifest
A plugin describes itself with a typed manifest, built with definePlugin() from
@netscript/plugin
. The manifest is a
declaration, not executable wiring: definePlugin(name, version) returns a builder, and each
chained .with*() call adds a contribution along an extension axis.
// plugins/workers/src/public/mod.ts (abridged)
import { definePlugin } from "@netscript/plugin";
// definePlugin(name, version) returns a builder. No behavior runs here —
// each .with*() call only NAMES a contribution against a fixed axis.
export const workersManifest = definePlugin("@netscript/plugin-workers", "0.0.9")
.withType("background-processor")
.withService({ name: "workers-api", entrypoint: "./services/src/main.ts" })
.withBackgroundProcessor({ name: "workers-combined", entrypoint: "./bin/combined.ts", concurrency: 2 })
.withBackgroundProcessor({ name: "workers-worker", entrypoint: "./bin/worker.ts", concurrency: 2 })
.withBackgroundProcessor({ name: "workers-scheduler", entrypoint: "./bin/scheduler.ts" })
.withDbSchemas([{ path: "./database/workers.prisma", engine: "postgres" }])
.withContractVersions([{ version: "v1", loader: "./contracts/v1/mod.ts" }])
.withRuntimeConfigTopics([{ name: "workers", schemaPath: "./runtime/workers.schema.json" }])
.withAspire("./src/aspire/mod.ts")
.build();The builder exposes a fixed set of contribution axes, and any number of plugins can contribute against them. That asymmetry lets capabilities compose without the host growing new code for each plugin. The most used axes:
| Builder method | Contributes | Used for |
|---|---|---|
.withService(…) | An HTTP service: name, entrypoint, port. | The plugin's oRPC API resource. |
.withBackgroundProcessor(…) | A long-running processor: name, entrypoint, concurrency. | Workers, runners, processors — runs apart from the API. |
.withDbSchemas(…) | A Prisma schema fragment + engine. | Owned tables merged into the workspace schema. |
.withStreamTopics(…) | Typed stream topic definitions. | Durable change-data channels. |
.withRuntimeConfigTopics(…) | A named, schema-validated config topic. | Per-plugin runtime configuration. |
.withContractVersions(…) | A versioned contract loader. | Forward/backward contract compatibility. |
.withTelemetry(…) | Telemetry registration. | Spans, metrics, structured logs. |
.withAspire(…) | An Aspire contribution module path. | How the AppHost materializes the plugin's resources. |
A workers plugin contributes job definitions and processors; a sagas plugin contributes saga definitions; a streams plugin contributes stream topics and a service; an auth plugin contributes its oRPC service, contract versions, and a runtime-config topic. The host never needs a plugin's internals, only these contribution shapes.
This is registration over inheritance: a plugin registers named contributions against open extension axes instead of subclassing host internals. The host can validate each registration at composition time, log it, and reject conflicts; duplicate plugin names, for example, fail with a structured error that names both contributors.
Generated registries
NetScript does not auto-discover plugins from node_modules, and the host does not
hand-import each plugin. Plugins live under plugins/, and a generated registry links
declaring a plugin to the runtime using it.
The codegen step emits static registry modules, TypeScript the runtime imports directly, so adding a plugin is a regeneration step, not a code edit. Two properties of this loader matter:
- Load order is deterministic. Plugins must not depend on the order in which other plugins load; the framework asserts against order dependence.
- Each load contributes named registrations against one or more extension axes, and duplicate names across plugins are rejected with a structured error.
The add-a-plugin how-to and the
CLI cheat sheet
cover the sequence:
netscript plugin install … links enabled web apps and compiles declared runtime registries.
Use --app <name> to select one app. Links are stored in
NetScript.Apps.<name>.PluginReferences and the installed manifest's consumer list. On AppHost
restart, linked endpoints reach the app through existing service discovery. Run
netscript generate plugins again after adding jobs or other registrable source files.
Process isolation
"One plugin" is a packaging unit, not a runtime unit. The AppHost materializes a plugin's HTTP API and its background work as distinct Aspire resources, each with its own entrypoint and permission set. The workers plugin's Aspire contribution registers the API as a Deno service and one combined worker/scheduler as a Deno background resource. The manifest also advertises worker-only and scheduler-only entrypoints; listing them does not mean all three start by default:
// plugins/workers/src/aspire/workers-contribution.ts (abridged)
// The API is a service resource; each processor is a SEPARATE background resource.
const api = builder.addDenoService("workers-api", {
entrypoint: "plugins/workers/services/src/main.ts",
port: ctx.port("workers-api"), // allocated per project
permissions: WORKERS_SERVICE_PERMISSIONS, // narrow: serve HTTP
});
const combined = builder.addDenoBackground("workers-combined", {
entrypoint: "plugins/workers/bin/combined.ts",
permissions: WORKERS_BACKGROUND_PERMISSIONS, // wider: queues, db
});
// The default graph has one scheduler owner, inside combined.
return [api, combined];Every scaffolded plugin ships its background entrypoints:
| Plugin | Background entrypoint | AppHost resource |
|---|---|---|
| workers | bin/combined.ts | Deno background workers-combined, separate from workers-api. |
| sagas | src/runtime/saga-runner.ts | Deno background saga-runner. |
| triggers | src/runtime/trigger-processor.ts | Deno background trigger-processor. |
These are the packaged plugin entrypoints. A generated project's AppHost instead starts the
application wrappers workers/runtime.ts, sagas/runtime.ts and triggers/runtime.ts, under
resources named workers, sagas and triggers. The workers wrapper calls startCombinedProcess,
which starts both the scheduler and the queue consumer. Keep one scheduler owner per job set;
starting scheduler.ts alongside a combined runner registers those schedules twice. See
the deployment choices.
The split is structural for three reasons:
- Crash containment. A processor that hits an unrecoverable error takes down only its own resource. The HTTP API keeps serving, and other processors keep running.
- Independent scaling. The API and the processors have different load profiles, request
latency versus queue depth, so they are tuned and scaled separately:
WORKERS_CONCURRENCYon the background resource never touches the API. - Least privilege. The API gets a narrow permission set (serve HTTP); background resources get the wider set they need (queues, database, file watching).
See orchestration with Aspire for how the AppHost assembles these resources from the manifest and the plugin's Aspire contribution.
Guarding a plugin's API
createPluginService from @netscript/plugin/service requires an explicit auth posture and
installs the guard before any RPC, REST, or raw route; /health stays anonymous. This is the guarded
form netscript plugin new billing generates for a plugin other than the auth plugin:
export const billingService = createPluginService(billingRouter, {
name: 'billing',
auth: {
authn: {
authenticator: createAuthServiceAuthenticator({ serviceName: 'auth', timeoutMs: 10_000 }),
},
authz: {
authorizer: createContractAuthorizer(
mountPluginContract(billingContractDefinition, billingContractMount),
),
},
},
});
createAuthServiceAuthenticator (@netscript/plugin-auth/authenticator) verifies each bearer
session through the auth service, so billing embeds no backend, KV handle, or provider secret. A
public API records auth: { public: true, reason: '…' } instead. Background callers run under a
service identity, never an app session; see
verifying sessions from another plugin.
Auth as the worked example
The auth plugin applies every idea on this page and stretches the core/plugin split across five units:
@netscript/plugin-auth-coredefines the port,AuthBackendPort, and nothing provider-specific.- Three backend adapters implement that port:
@netscript/auth-kv-oauth(the only interactive backend, with a full OAuth/OIDC redirect flow),@netscript/auth-workos, and@netscript/auth-better-auth(both non-interactive). @netscript/plugin-authis the composing plugin: it carries the manifest, runs theauth-apioRPC service at its allocated endpoint, and selects exactly one active backend throughNETSCRIPT_AUTH_BACKEND(defaultkv-oauth).
Behavior lives in the core and the adapters, the plugin only composes and declares, and the host
sees one service contribution whichever backend is active. When a backend cannot satisfy a
capability, such as an interactive sign-in on a non-interactive backend, the seam throws a typed
AuthBackendOperationUnsupportedError. The
auth model
walks the port and its adapters, and Observability model covers the auth audit surface.
Design rationale
The shape keeps three concerns apart:
- Capability authors work in a core package and think only about behavior, ports, and contracts, never about how a host discovers them.
- Plugin packages stay small: they translate a capability into a manifest of contributions, the only thing a host has to understand.
- Hosts depend on a stable, finite set of contribution shapes and a deterministic registry.
As a result, a host can add workers, sagas, triggers, streams, and auth in any combination, and each plugin contributes its services, background processors, schemas, and topics through the same vocabulary, each running as its own isolated resource.
Next: install one with Add a plugin.