Skip to main content

Plugin System

Lua-based plugins extend Mahresources with custom actions, hooks, pages, JSON API endpoints, and menu items. Plugins run in sandboxed VMs, are discovered automatically from a configurable directory, and can be enabled or disabled at runtime.

Configuration​

FlagEnv VariableDefaultDescription
-plugin-pathPLUGIN_PATH./pluginsDirectory to scan for plugin subdirectories
-plugins-disabledPLUGINS_DISABLED=1falseDisable the plugin system entirely
-plugin-schedule-tickPLUGIN_SCHEDULE_TICK30sHow often the plugin scheduler looks for due work; bounds the resolution of every plugin schedule
-plugin-command-pathPLUGIN_COMMAND_PATHstartup PATH snapshotTrusted absolute directories searched for declared command executables
-plugin-command-staging-pathPLUGIN_COMMAND_STAGING_PATHdata directory or private temporary rootOS-backed command exchange/import root, exclusively leased by one active server process
-plugin-command-run-quotaPLUGIN_COMMAND_RUN_QUOTA8589934592 (8 GiB)Sampled per-run exchange and import-temp quota
-plugin-command-staging-quotaPLUGIN_COMMAND_STAGING_QUOTA53687091200 (50 GiB)Sampled deployment-wide staging quota
-plugin-command-exchange-retentionPLUGIN_COMMAND_EXCHANGE_RETENTION168hRetention for terminal, unleased exchange folders
-plugin-command-output-retentionPLUGIN_COMMAND_OUTPUT_RETENTION720hRetention for output tails; durable runs and import maps remain

Plugin Discovery​

At startup, the plugin manager scans the plugin directory for subdirectories containing a plugin.lua file. Discovery is sorted alphabetically for deterministic load order.

plugins/
+-- my-plugin/
| +-- plugin.lua
+-- another-plugin/
+-- plugin.lua

During discovery, a temporary Lua VM executes only the top-level code of plugin.lua (not init()) to read the plugin global table for metadata and settings. The temporary VM is then closed.

Plugin Metadata​

Every plugin declares a global plugin table:

plugin = {
name = "image-processor",
version = "1.0.0",
description = "Processes images using external APIs"
}
FieldRequiredDescription
nameYesPlugin identifier (displayed in management UI). Must match ^[a-z][a-z0-9_-]{0,49}$. block and manage are reserved (they are route words under /v1/plugins/), and a plugin using one fails to load
versionNoVersion string
descriptionNoShort description
settingsNoArray of setting definitions
api_versionNoDeclares a permission manifest. See Plugin Permissions
capabilitiesNoThe mah modules to install. Requires api_version
networkNoOutbound host allowlist. Requires api_version
commandsNoFixed server-command declarations. Requires api_version and the commands capability
download_limitsNoPer-domain pacing for this plugin's own mah.download.submit jobs. Requires api_version
allow_private_hostsNoPermission to reach private addresses. Requires api_version
dependenciesNoPlugin names that must be enabled first. Requires api_version
min_app_versionNoRecorded and displayed, never enforced. Requires api_version

The name is validated at discovery: lower case, starting with a letter, up to 50 characters of a-z, 0-9, - and _. It is a URL segment in every menu href and the prefix of every shortcode the plugin registers, so a name outside that grammar is skipped with a warning rather than loaded. Two directories declaring the same name are both skipped, because the name is what a plugin's enabled state, settings and KV namespace belong to.

A plugin that declares api_version receives only the capabilities it lists -- plus what those imply, and the handful of modules every plugin gets. If it also declares network, its outbound requests are confined to those hosts; declaring no network means any public host, which is the broadest policy rather than the narrowest. It may also declare download_limits to throttle only its own mah.download.submit jobs by domain; those limits grant no new capability and do not affect other users or plugins. A plugin that declares no api_version at all is legacy: it keeps the full mah surface, with a warning. Legacy is not an exemption from the network rules. See Plugin Permissions for the capability list, the consent model, and the three network layers.

Declared server commands​

A command-bearing plugin declares a fixed argv template. This is an argv list, not a command-line string: Mahresources launches the executable directly and never invokes a shell.

plugin = {
name = "media-fetcher",
version = "1.0.0",
api_version = 1,
capabilities = { "commands", "db:write" },
commands = {
{
name = "download",
argv = {
"yt-dlp",
"--ignore-config",
"--paths", "{{exchange_dir}}",
"-o", "{{output_template}}",
"--format", "{{format}}",
"--no-playlist",
"--no-directories",
"--cookies", "cookies.txt",
"--", "{{url}}",
},
inputs = { "cookies.txt" },
timeout = 7200,
sensitive_params = { "url" },
},
},
}

Each declaration has these fields:

FieldRequiredContract
nameYesUnique command slug within the plugin
argvYesNonempty list of literal arguments and whole-element placeholders
timeoutNoSeconds after spawn; defaults to 3600 and is capped at 86400
sensitive_paramsNoParameter names redacted from stored params and argv; order does not affect identity
inputsNoAt most 4 file names the command may be given contents for; a leading dot is refused, and order does not affect identity

The first argv element (the executable) must be a literal basename: no slash, backslash, .., leading dash or placeholder. It is resolved only from the operator's PLUGIN_COMMAND_PATH. A placeholder must occupy its whole argv element; "--output={{name}}" is invalid. Every plugin-filled placeholder must have a nonempty string value at submission. {{exchange_dir}} is host-filled and reserved, so supplying that key is an error. A run accepts at most 32 parameters, 8 KiB per value and 64 KiB in aggregate.

Changing a command's name, positional argv, timeout, sensitive parameter set or declared input file names changes manifest identity and requires fresh operator consent. Put variable data after a fixed option, and terminate option parsing with a literal -- where the program supports it. The yt-dlp example also uses --ignore-config so an operator config file cannot silently add behavior, pins output with --paths, forbids playlists and directories, and names the one file it may be handed the contents of. The separate plugin must still reject absolute paths, separators and .. in its operator-supplied output template.

Supplied input files​

A declaration that lists inputs may be given those files' contents for one run; the host writes them into the run's private exchange folder just before the process starts, and the program reads them from its working directory. The names are consented to at enable time; the contents are not shown to the operator and are not reviewable, because the operator consents to the names only. Supplied contents are written as regular files owned by the service account, at mode 0600, and are never stored in any record: the run row, the admin history and mah.fs.runs() show the names and byte counts only.

A supplied file can be read back with mah.fs.read and removed with mah.fs.discard or mah.fs.discard_run, which is what a program that rewrites its input in place (yt-dlp refreshing a cookie jar) makes useful. It otherwise stays in the exchange folder until that folder is swept, which is 7 days by default unless the plugin discards it earlier — so a credential handed to a run this way lives in the staging root for up to that window, including after a plugin disable that dropped the completion callback. Cancelling a run while it is still queued leaves no file, because the runner observes that cancellation before it writes anything — but cancelling is not a way to remove a supplied credential: once the host has begun preparing the run, the files it staged stay until the plugin discards them or the folder is swept.

The name rules are the exchange file-name rules plus one: a name may not begin with a dot, so the files common tools read without being asked (.netrc, .gitconfig, .env) cannot be supplied at all. Names and limits are declared and enforced; a name the declaration does not list is refused before anything durable exists. The host cannot verify that a declared input is actually referenced by the program's argv, so that stays part of the review.

Commands are trusted service-account processes, not extensions of the Lua sandbox. Read Plugin Permissions before enabling one and mah.commands / mah.fs before authoring one.

Plugin Lifecycle​

  1. Discovery -- Plugin directory is scanned at startup. Metadata and settings are read from each plugin.lua.
  2. State check -- The database is queried for previously enabled plugins. Those plugins are enabled automatically.
  3. Enable -- A full Lua VM is created with safe libraries. plugin.lua is executed, then init() is called (if defined). Hooks, actions, injections, pages, menus, and API endpoints registered during init() become active.
  4. Run -- The plugin responds to hooks, serves pages, and executes actions.
  5. Disable -- All hooks, injections, block types, pages, menus, actions, and API endpoints are removed. A running async handler is stopped at its next step and its job ends interrupted with the reason that the plugin was disabled. In-flight async work is waited for, bounded at 5 seconds, after which the VM is closed once that work stops. Disabling is refused while another enabled plugin depends on this one.

Plugin Settings​

Settings are defined in the plugin.settings table and appear in the management UI when the plugin is selected.

plugin = {
name = "my-plugin",
settings = {
{ name = "api_key", type = "password", label = "API Key", required = true },
{ name = "model", type = "select", label = "Model", options = {"fast", "quality"}, default = "fast" },
{ name = "max_size", type = "number", label = "Max Size", default = 1024 },
{ name = "enabled", type = "boolean", label = "Feature Enabled", default = true },
{ name = "prefix", type = "string", label = "Output Prefix", default = "processed_" }
}
}

Setting Types​

TypeValidationUI Element
stringRequired check onlyText input
passwordRequired check onlyPassword input
booleanMust be booleanCheckbox
numberMust be numericNumber input
selectMust match one of optionsDropdown

Required settings must be configured before the plugin can be enabled.

A password setting is a secret the plugin's code receives. Every occurrence of its value is replaced with [redacted] in what the server itself shows or stores from the plugin's work: a job's title, progress, failure reason, result and timeline, the jobs panel, an action's answer, error messages, mah.log entries, and the server's log lines about a plugin's errors, failed loads and refused requests. It is not removed from what the plugin itself sends to the person using it, such as its pages, blocks, shortcodes or API responses: a plugin is trusted code, and what it chooses to show is its own. An action's or schedule's id is not redacted either: it is part of the plugin's code, and naming one after the plugin's own key is the plugin's choice. The redaction knows the setting's current value; after changing a key, revoke the old one where it was issued, because work that started with the old key is not redacted of it.

Reading Settings at Runtime​

local api_key = mah.get_setting("api_key")
local max_size = mah.get_setting("max_size")

Returns the setting value with the correct Lua type (string, number, boolean), or nil if not set.

State Persistence​

Plugin enabled/disabled state and settings are stored in the database (PluginState table). This means:

  • Plugins that were enabled before a restart are re-enabled automatically
  • Settings survive server restarts
  • The plugin directory itself only needs the Lua source files

Management UI​

Plugin management page

Navigate to the plugin management page to see all discovered plugins with their name, version, description, and current state (enabled/disabled). From this page:

  • Enable or disable individual plugins
  • Configure plugin settings
  • Review the capabilities, network allowlist, and dependencies the plugin declares
  • Open or close the plugin to group-limited accounts
  • Inspect its schedules and run one now
  • Inspect one-shot deferred downloads submitted by mah.download.submit
  • Purge its stored data
  • Open its generated documentation page

Management API​

MethodPathDescription
GET/v1/plugins/manageList all discovered plugins with state
POST/v1/plugin/enableEnable a plugin (form: name)
POST/v1/plugin/disableDisable a plugin (form: name)
POST/v1/plugin/settingsSave settings (query: name, JSON body: key-value pairs)
POST/v1/plugin/purge-dataPurge all KV store data for a disabled plugin (form: name)
POST/v1/plugin/scopedAccessAllow or refuse group-limited accounts per plugin (form: name, allowed). See Plugin Permissions
GET/v1/plugin/schedulesList recorded plugin schedules. See mah.schedule
POST/v1/plugin/schedule/runRun one schedule now; answers once the run has started, or 409 when it could not start within 10 seconds or the account it runs as may not run it
GET/v1/plugin/scheduled-downloadsList one-shot deferred downloads for a plugin
POST/v1/plugin/scheduled-downloads/cancelCancel a pending deferred download

Enable a Plugin​

curl -X POST http://localhost:8181/v1/plugin/enable \
-d "name=image-processor"

Required settings must be saved before enabling. If required settings are missing, the enable request fails with 400 Bad Request and a single-message JSON body, {"error": "missing required settings: [...]"} (not the structured multi-error {"errors": [...]} format used by entity validation).

Save Settings​

curl -X POST "http://localhost:8181/v1/plugin/settings?name=image-processor" \
-H "Content-Type: application/json" \
-d '{
"api_key": "sk-abc123",
"model": "quality"
}'

Only keys declared in plugin.settings are persisted; unknown keys are ignored.

Key-Value Storage​

Plugins have access to a persistent key-value store via the mah.kv module. Each plugin's data is scoped by plugin name -- plugins cannot read or write another plugin's keys.

mah.kv.set("last_run", "completed")
local last = mah.kv.get("last_run")
mah.kv.delete("last_run")
local keys = mah.kv.list("prefix_")

Values are JSON-serialized before storage and deserialized on read, up to 8 MB per key. For a key that a later call into the plugin, or the same plugin in another process, may have written since it was read, mah.kv.compare_and_set writes only while the stored value is still the one that was read. See the mah.kv reference.

Purging Plugin Data​

To purge all KV data for a plugin, disable the plugin first, then call the purge endpoint:

curl -X POST http://localhost:8181/v1/plugin/purge-data \
-d "name=image-processor"

The plugin must be disabled before purging. The management UI also has a Purge Data button on the plugin detail view for disabled plugins.

Lua VM Sandboxing​

Each enabled plugin runs in an isolated Lua VM with restricted libraries.

Allowed: base, table, string, math, coroutine

Blocked: os, io, debug, package

Removed base functions: dofile, loadfile, load, loadstring

Every VM that executes a plugin.lua opens the same libraries and removes the same base functions: the discovery-time one that reads metadata at startup, the enable-time one, and the one that parses settings. That is deliberate, so a plugin cannot tell which run it is in and declare a different manifest to each. Discovery still executes the top-level code of every plugin.lua on disk, enabled or not, so only place trusted files in the plugin directory.

Each VM has a mutex ensuring single-threaded access. All calls into the VM (hooks, actions, page handlers) acquire this lock.