PotionUI Docs v0.3.0

Initializing your plugin: boot vs enable

Four lifecycle hooks fire around a plugin's own life. They are ordinary manifest hooks — declare a handler the same way you would for any other hook point:

On this page
yaml
hooks:
  backend:
    - hook: "plugin.lifecycle.boot"
      handler: "backend.hooks.lifecycle_hooks.on_boot"
    - hook: "plugin.lifecycle.ready"
      handler: "backend.hooks.lifecycle_hooks.on_ready"
    - hook: "plugin.lifecycle.enable"
      handler: "backend.hooks.lifecycle_hooks.on_enable"
    - hook: "plugin.lifecycle.disable"
      handler: "backend.hooks.lifecycle_hooks.on_disable"
HookFires
plugin.lifecycle.bootOnce per process, for every enabled plugin — at startup, and right after enable when a plugin is enabled at runtime
plugin.lifecycle.readyOnce per process, for every enabled plugin, once the event loop is running and generations a previous process left unfinished have been marked failed — and right after boot when a plugin is enabled at runtime
plugin.lifecycle.enableOnly on the disabled → enabled transition, never at boot
plugin.lifecycle.disableExactly once whenever an enabled plugin is torn down: admin disable, uninstall, reload, a rescan that replaces its changed manifest or finds its folder gone, or a mount failure. Fires before the plugin's hooks are unregistered, and never for a plugin that never reached enabled

A backend hook entry also accepts remote: true (default false). Set it on a hook whose handler must run inside a Remote Native worker's execution of the pipeline (for example a prompt.transform handler), as opposed to a hook that only runs core-side before dispatch. Plugins that contribute no pipe but do declare a remote: true hook are included in the worker compatibility handshake the same way a pipe-contributing plugin is (compute_remote_plugin_bundle_fingerprint, src/pipelines/remote_fingerprint.py); hooks left at the default are core-only and invisible to that handshake.

Per-process initialization belongs in boot. enable fires exactly once in the lifetime of an installation: the moment an admin turns the plugin on. It does not fire again when the server restarts — the startup resync re-registers an already-enabled plugin's handlers without replaying its enable transition, because a restart is not a transition. A plugin that creates its tables or warms a cache from enable alone works until the first restart and then silently stops being initialized.

python
def on_boot(context):
    """Runs on every process start; make it idempotent."""
    MyPluginTables().create_all()
    return context

boot is dispatched only to the subject plugin's own handler, so context.get("plugin_id") is always your plugin. enable and disable, by contrast, broadcast to every enabled plugin's handler — those are for reacting to another plugin's state changing, so a handler there must check plugin_id before assuming the event is about itself.

A handler that raises is logged and skipped: one plugin's failing boot cannot abort startup, block another plugin's boot, or fail the enable that triggered it. None of the hooks can block the transition they report.

Because the registry owns the teardown signal, a plugin that starts background work in ready can cancel it in disable and rely on it: the same teardown fires disable on a reload or a rescan that swaps in a changed manifest, and the replacement copy receives boot and ready again. The handler runs while the registry lock is held (reentrant), so it may call back into the registry.

ready is for work that needs the running application rather than just the process: reconciling the plugin's own records against generation state after a restart, or starting a background coroutine. Like boot it is dispatched only to the plugin's own handler. Handlers are synchronous; src.plugin_api.hooks.run_on_app_loop(coro) schedules a coroutine on the application's event loop from any thread and returns a concurrent.futures.Future (get_app_loop() returns the loop itself). Both raise RuntimeError before the application has started, and ready is the first point at which they work. Do not block a ready handler on that future: the handler runs on the loop it is waiting for.

python
from src.plugin_api.hooks import run_on_app_loop

def on_ready(context):
    run_on_app_loop(reconcile_my_records())
    return context

Plugin code across updates#

Each time PotionUI enables a plugin, it first checks the modules it already imported from the plugin's folder in this process. They are kept when the folder and the bytes of manifest.yml are the same as when they were imported, and each of their files is still there with the size and modification time it had at the end of the last enable; a module first imported after that enable is kept when its file is not newer than that enable. Only the size and the modification time are compared: a change of mode or owner alone keeps the modules, and a file put in place with the same size and modification time as the one it replaces is not noticed. Otherwise, PotionUI unloads every module it imported from that folder, and from the plugin's previous folder if the plugin moved. This covers an update from a catalog, new files followed by a scan, a Python file edited by hand followed by a disable and an enable, the folder put back after a failed update, and a plugin that left the plugin folders and came back. The plugin then runs from its files as they are now: packages, __init__.py files, the modules its entry points import and its pipes are imported again, as after a restart. Two kinds of module are left in place: a module that belongs to another registered plugin, and a module that another thread is still importing at that moment, which is unloaded at the next enable instead. The modules a plugin had loaded at its last enable from a folder it moved out of are unloaded even when another plugin now holds that folder. The bytecode Python cached for each module that is unloaded is removed as well, where its folder allows it, so that module is compiled again from its file. By default, Python checks cached bytecode only against the size of its file and the second of its modification time, at a restart too, so a module first imported after the change can run its previous version when its file was rewritten at the same size within the same second.

An enabled plugin updated from a catalog needs no restart. A scan only notices a changed manifest.yml, so after editing Python files by hand, disable and enable the plugin, or restart PotionUI. A scan that changes or removes a plugin also reloads presets, pipes, recipes and model layouts, as an enable does, so it takes about as long as an enable.

The entry points named in the manifest run again at every enable, even when nothing changed, so their module-level state starts again then. The modules they import keep their state across a disable and an enable when nothing changed, and start again when the plugin's code is unloaded. Code a module runs while it is imported runs again each time the module is imported again. Whatever it registers outside itself at that point (a logging handler, an atexit function, an entry in a registry shared by the whole process) is registered once more, and the previous version's entry stays unless something removes it. Make such a registration idempotent, or do it in boot and undo it in disable.

Work the previous version started keeps running on that version's modules after the update. An import it makes inside a function, of a module that is not loaded at that moment, reads the files on disk, which may already be the new version's. After the update that is true of every module of the plugin. While the previous version's disable handler runs, the new files are already in place, so it is true of any module that version had not imported yet. State that such work and the new version must share (a count of running jobs, a reservation) has to live outside the plugin's modules, in the database or in a file, or disable has to wait for that work or cancel it.

Two cases are not reloaded. A compiled extension module (.pyd, .so) shipped in a plugin folder cannot be unloaded, so a new version of one takes effect at the next restart. A module of the plugin's folder that the core or another plugin imported is unloaded with the plugin, and the importer keeps the object it already has.