# troll: agent guide
troll manages TCP port claims and leases on one machine (Linux, macOS, and Windows). It also shows live listeners and their owners, and skips OS-excluded port ranges (such as Windows Hyper-V/WinNAT blocks, refusing explicit claims inside them with `OsExcluded`).
- **Ephemeral ports.** Tests and any throwaway server that can report its assigned port bind port 0, read back the port the OS chose, and register nothing.
- **Leases.** Use a lease (or `troll with`) for a temporary process that needs its port number before it starts and must be reachable by a person or another process.
- **Permanent claims.** Make a claim as a project decision for a long-lived app or shared service.
For a new project that needs several permanent ports, choose an unused app name from `troll app list`, such as `agent-demo-stack`. Use `troll app reserve agent-demo-stack --components frontend,backend,db:service`, then `troll env agent-demo-stack` for `PORT_<COMPONENT>` assignments. Use `troll with --group frontend,backend -- your-dev-stack` for a temporary stack.
If unsure between a lease and a claim, use a lease. If unsure whether you need the port number in advance, bind port 0.
Use `troll with -- your-dev-server` to lease a port and set the child's `PORT` and `TROLL_PORT`. Pass `--for DURATION` to set lease length, `--pool NAME` to allocate from a named `[pools]` sub-range in `config.toml`, and `--heartbeat` to periodically renew the lease while the child runs (strictly capped at `lease_max` from the original `leased_at`, disabled by default). After the child exits, Troll attempts to release the lease or stack that invocation acquired, preserving other allocations and unrelated processes. The child owns standard output. With `--json`, troll writes its diagnostics to standard error. Never hand-write a registry or claim file. Mark shared backend services with `--role service` and record their users with `--used-by`. Ordinary apps use the default `app` role.
For manual leases, keep the `port` and `leased_at` from your own `troll lease --json` or MCP lease reply. Pass both when renewing or releasing. A port lookup can show someone else's replacement lease. If you no longer have your lease's timestamp, let it expire without renewing or releasing it. Release leaves any process on the port running.
Edit, never release and re-claim. Editing keeps the port allocated and preserves the original claim or lease history and omitted fields. Read the content tag with `troll check` and pass it as `--tag` to `troll edit` or `troll lease-edit` to guard against ordinary concurrent edits. If the tag is stale, read again and retry only if the edit still makes sense. To clear a container, first verify your claim and its current container with `troll check PORT --json` or `troll_check`. Send the reviewed tag with `--clear-container` or `container: null` in Model Context Protocol (MCP) edit changes. The claim stays allocated and its process keeps running.
Set `--web /path` and optionally `--web-scheme http|https` on a permanent claim. Troll builds the local URL shown by `check` and `list` from the scheme, path, port and bind. The registry stores the web scheme and path.
Run `troll zone list` to see excluded ports and ranges. Enabled red zones block new claims, app allocations and leases. Existing conflicts stay allocated and appear in the report. Zone labels allow `[A-Za-z0-9._-]` (1–64 chars, rejecting `.` and `..`), preserve display case, and match case-insensitively on their lowercase canonical key.
Use `troll explain <port> [--json]` for a combined port diagnosis: claims, leases, live listeners, passively detected service identity and directory files, matching red zones, 30-day removal history for that port, and a recommended next step.
Removed permanent claims, components, apps, leases, stacks, and swept leases are kept for 30 days in `history.toml` under `registry.lock`. Use `troll history [list | show <ID> | restore <ID>] [--json]` or MCP `troll_history` to inspect or atomically restore a removed entry when all of its ports are free and outside enabled red zones.
MCP tools `troll_edit` and `troll_lease_edit` require the tag from `troll_check`. Use `troll_app` to reserve and manage app components, `troll_zone` to manage red zones, `troll_history` to inspect or restore 30-day history, and `pool` or `group` on `troll_lease` for named pools or temporary stacks. Keep the returned group ID and review its members with `troll_list` before using it as `group` with `troll_renew` or `troll_release`, or as `--group` on the command line. Group actions use the holder that originally took the stack. Group release leaves every process running.
Before releasing a permanent claim you manage, confirm it and its dependents with `troll check PORT --json` or `troll_check`. Pass its tag with `--if-tag` to `troll release` or send `claim: {port, tag}` to `troll_release`. Review any dependent projects before using `--force-shared` or `claim.force_shared`. Release leaves any process on the port running.
For app release and component removal, run `troll_app` with `action: "show"` or `troll app show`. Confirm the component you manage, or every permanent claim under the project for whole-app release. Always send the component tag for removal or the app tag for release (`tag` in MCP, `--if-tag` in the CLI). Repeat the exact app name in `confirm` for app release. These releases leave all processes running.
Before changing a zone you manage, confirm its label, ranges and state with `troll_zone` and `action: "list"`, or `troll zone list --json`. Pass the current tag for MCP edits and for CLI or MCP removal. Enable and disable take the label alone on both the CLI and MCP; a CLI edit does too. Use those in a registry where you control changes to that label, and otherwise use the console or the HTTP API, which carry the tag on an edit and on either toggle.
Use `--json` for machine-readable results. For the fixed-port example, run
`troll check 8123` and `troll zone list` first. Use a port and project you
manage. Choose a port free of claims, leases and live listeners and outside
every enabled zone, the configured lease range and the operating system's
ephemeral range. The [runbook port check](../RUNBOOK.md#check-a-fixed-port)
explains how to check the ranges.
```sh
troll claim 8123 --project demo --service web --json
troll lease --project demo --purpose preview --json
```
Set `LEASE_PORT` and `LEASED_AT` to the values from your own lease reply.
The guarded release below leaves any process on that port running.
```sh
troll with --project demo --purpose preview --json -- sh -c 'printf "%s\n" "$PORT"'
troll renew "$LEASE_PORT" --leased-at "$LEASED_AT" --for 1h --json
troll release "$LEASE_PORT" --leased-at "$LEASED_AT" --json
troll check 8123 --json
troll explain 8123 --json
troll history list --json
troll list --drift --json
troll doctor --json
troll sweep --dry-run --json
```
When piped, under `NO_COLOR`, `TERM=dumb`, `--no-ansi-colors`, `TROLL_OUTPUT=plain`, `--json`, or detected AI/CI environments, troll emits raw plain text with no ANSI color codes.
The dry run leaves the registry entries and processes in place. For a real
sweep, use the console or send the reviewed lease identities through the
HTTP API. Approve only leases you manage. Use a bare `troll sweep` only after
confirming you administer the entire selected registry and reviewing the dry
run. The CLI reevaluates every lease in that registry at execution. Both
forms preserve permanent claims, active, overdue and unknown leases, and all
processes.
Registry command exit codes are 0 for success, 1 for failure, 2 for usage errors, 3 for refusal, and 4 for a registry lock timeout. After spawning, `troll with` normally returns the child's code. Run `troll --llm-guide --full` for the manual and see `docs/guides/json-output.md` in the troll repository for every response field.
`troll import-legacy <dir> --json` lists legacy claims with a TCP listener in
the configured lease range under `live_lease_range_claims`. It lists in-range
claims whose listener check failed under `unknown_lease_range_claims` with
`listener_check_error`.
If `troll init` or `troll init --yes` finds a service with an unknown startup registry at a candidate port, it exits 3 (`ServiceIdentityUnknown`). The registry can be unknown for a service of any version, not only an older one: the status file omits `registry_home` whenever troll could not resolve or represent that path. It may create an empty registry lock file. Report the port and PID to the operator and leave the service running. The diagnostic verifies a listener. The operator must establish ownership and the startup registry from the original launch details before following the service recovery procedure in `RUNBOOK.md`. `init --yes` keeps an existing configuration. Init refuses the listening port when the service reports a different registry.
On Unix, a SIGINT, SIGTERM or SIGHUP observed at the final startup check cancels `troll with` before spawn. It attempts to release any lease it acquired and exits with 128 plus the signal number. If release fails, use the port and `leased_at` reported for your own `troll with` invocation in `troll release <port> --leased-at <leased_at>`, or let the lease expire. The guarded release leaves any process on the port running.