# The worker

Mago runs extension plugins in PHP child processes called workers. This page describes the bundled worker entry points, PHP runtime flags, protocol handling, and startup diagnostics.

## Worker entry points

The package ships two equivalent entry points:

- `vendor/friendsofmago/mago-symfony/resources/worker.php`
- `vendor/bin/mago-symfony-worker`, a thin wrapper that requires `resources/worker.php`

Register `resources/worker.php` under `[extension-hosts.symfony]` in `mago.toml`:

```
[extension-hosts.symfony]
command = ["php", "-d", "xdebug.mode=off", "-d", "pcov.enabled=0", "-d", "memory_limit=512M", "vendor/friendsofmago/mago-symfony/resources/worker.php"]
workers = 1
```

## PHP runtime flags

- `-d xdebug.mode=off` disables Xdebug in the worker. PHP ignores the flag when Xdebug is not installed.
- `-d pcov.enabled=0` disables PCOV in the worker. PHP ignores the flag when PCOV is not installed.
- `-d memory_limit=512M` raises the worker memory limit for large codebases and large compiled container XML dumps. Small and medium applications run within PHP's default `128M` limit.
- `workers = 1` runs one PHP worker process for this extension host.

To debug a provider with Xdebug, override `XDEBUG_MODE` in the shell because the environment variable takes precedence over `-d xdebug.mode=off`:

```
XDEBUG_MODE=debug vendor/bin/mago analyze
```

## Protocol and stream separation

Mago communicates with the worker over standard input and standard output using binary frames. Any stray text written to standard output breaks framing.

`resources/worker.php` protects the stream before loading Composer's autoloader:

1. Sets `display_errors` to `stderr`.
2. Starts an output buffer callback that redirects printed output to standard error.
3. Catches any `Throwable` raised during autoloading or worker execution, writes the message to standard error, flushes the stream, and exits with code `1`.

## Autoloader discovery

`resources/worker.php` searches these candidate paths for Composer's `autoload.php`:

1. `$GLOBALS['_composer_autoload_path']`, set when invoked through Composer's bin proxy.
2. `dirname(__DIR__, 3) . '/autoload.php'`, when installed under `vendor/friendsofmago/mago-symfony/`.
3. `dirname($_SERVER['SCRIPT_FILENAME'], 2) . '/autoload.php'` and `dirname($_SERVER['SCRIPT_FILENAME'], 4) . '/autoload.php'`.
4. `getcwd() . '/vendor/autoload.php'`.
5. `dirname(__DIR__) . '/vendor/autoload.php'`, in a package checkout root.

The worker accepts an autoloader only when its `composer/autoload_psr4.php` maps `MagoSymfony\` to the same `realpath` as the running package's `src/` directory. That guard prevents a symlinked path repository from loading a stale copy of the package.

## Configuration errors and warnings

- **Missing auto-discovered paths.** If `.mago/symfony-container.json`, `var/cache/dev/*Container.xml`, and `var/cache/test/*Container.xml` do not exist, startup stays silent and succeeds.
- **Explicit container errors.** If `MAGO_SYMFONY_CONTAINER_XML` or `MAGO_SYMFONY_CONTAINER_MAP` points to a missing, unreadable, or malformed file, plugin registration throws a `RuntimeException`. Missing `ext-dom`, `ext-libxml`, or `ext-xmlreader` during XML loading throws the same exception. The worker writes the error to standard error and exits without corrupting standard output framing.
- **Invalid `MAGO_SYMFONY_USER_CLASS`.** If the configured class does not exist in Mago's codebase or does not implement `Symfony\Component\Security\Core\User\UserInterface`, the worker writes a warning to standard error once. It leaves `getUser()` unnarrowed and continues analysis.
