Plugin Hook System
Relevant source files
The following files were used as context for generating this wiki page:
The Plugin Hook System is the core execution engine of abx-dl, shared with the ArchiveBox ecosystem. It provides a language-agnostic framework for running specialized downloading and scraping tools through a series of distinct lifecycle phases. Each plugin is a directory containing a config.json definition and various hook scripts—executable files that react to specific bus events to perform tasks such as dependency installation, environment setup, and content extraction.
Sources: README.md:42-45, abx_dl/models.py:87-95
Hook Architecture and Naming Convention
Hooks are treated as black-box executables that must follow a strict naming convention to be discovered and categorized by the system. This convention allows the orchestrator to determine the event family, execution order, and runtime behavior (background vs. foreground) without introspecting the file content.
Filename Pattern
The standard format for a hook filename is:
on_{Event}__[{order}_]{description}[.finite][.daemon][.bg].{ext}
- {Event}: The exact bus event family (e.g.,
Install,BinaryRequest,CrawlSetup,Snapshot). - {order}: An optional execution order prefix (defaults to 0).
- .bg: Marks the hook as a background process.
- .finite / .daemon: Sub-types for background hooks; finite hooks exit on their own, while daemons run until killed.
The diagram above illustrates how the parse_hook_filename function extracts metadata from a hook's filesystem name. Sources: abx_dl/models.py:339-354
Execution Phases and Lifecycle
abx-dl executes hooks in four primary phases. Each phase has a specific purpose and a contract regarding what type of data (JSONL records) it is allowed to emit to stdout.
Phase Definitions
| Phase | Event | Purpose | Emitted Records |
|---|---|---|---|
| Install | InstallEvent | Reads required_binaries and triggers resolution. | N/A |
| BinaryRequest | BinaryRequestEvent | Provider hooks resolve or install required binaries. | Binary |
| CrawlSetup | CrawlSetupEvent | Launches daemons (like Chrome) or shared runtime state. | Machine, Process |
| Snapshot | SnapshotEvent | Per-URL execution to extract content. | ArchiveResult, Snapshot, Tag |
Sources: README.md:44-51, TODOs.md:14-23, TODOs.md:31-33, skills/abx-dl/SKILL.md:103-107
Lifecycle Flow
The execution flow ensures that dependencies are resolved before any crawling or snapshots begin.
This flow represents the standard runtime sequence after dependency preflight. Sources: README.md:144-152
Hook Discovery and Filtering
The system discovers plugins from the directory defined by ABX_PLUGINS_DIR or the default abx-plugins package. The discovery process involves iterating through subdirectories and identifying valid hook files and configuration schemas.
Key Data Structures
HookClass: Stores the path, event type, order, and background status of a script.PluginClass: Groups hooks together with aPluginConfigloaded fromconfig.json.PluginConfig: Defines metadata,output_mimetypes,required_binaries, andrequired_plugins.
Sources: abx_dl/models.py:64-84, abx_dl/models.py:107-118
Dependency Resolution
When specific plugins are requested via --plugins, the system uses filter_plugins to transitively resolve dependencies. This includes:
- Plugins listed in
required_plugins. - Provider plugins named in
required_binaries[].binproviders(e.g.,apt,brew,npm).
Sources: abx_dl/models.py:404-442, tests/test_plugins.py:39-65
Background Hooks and Daemons
Background hooks (.bg suffix) allow plugins to maintain state across multiple snapshot attempts or provide services to other hooks. A common example is the chrome plugin, which launches a browser instance during the CrawlSetup phase and keeps it running for the duration of the crawl.
- PID Management: Background hooks are tracked via PID files and cleaned up at the end of the run.
- Communication: Hooks often communicate via the shared
ProcessandBinaryrecords emitted to the bus. - Binary Discovery: Hooks can output
{"type": "Binary", "name": "...", "abspath": "..."}to register binary paths for the current environment.
Sources: CLAUDE.md:73-78, TODOs.md:42-46, abx_dl/models.py:236-240
Configuration and Runtime Environment
Every hook execution is provided with a flat runtime configuration model (PluginEnv). This environment is assembled from global defaults, user configuration (config.env), and runtime-derived caches (derived.env).
Environment Variables
Hooks receive configuration as environment variables. Key variables include:
{PLUGIN}_BINARY: Path to the required executable.{PLUGIN}_TIMEOUT: Per-plugin timeout override.DATA_DIR,CRAWL_DIR,SNAP_DIR: Output destination paths.
Sources: README.md:65-90, abx_dl/models.py:143-161
The relationship between Plugins, their configurations, and their associated Hook scripts. Sources: abx_dl/models.py:87-133
Summary
The Plugin Hook System's strict adherence to naming conventions and execution phases allows abx-dl to remain extensible and robust. By separating binary installation from snapshot extraction and supporting background daemons, the system can handle complex scraping tasks that require managed browser states and diverse external dependencies.
Sources: README.md:42-53, TODOs.md:1-12