opschain agent
packs:agents
Runs an OpsChain agent in the foreground against a server: on the first run
it registers with the --token registration token, then it holds the
agent-initiated channel open and serves the built-in five-surface minimum
spec (healthz, readyz, info, metrics, logs) through the tunnel. The server
address resolves through the global target precedence — --server <url>,
OPSCHAIN_SERVER, or the active context — the same way every other
server-facing command finds its server. On start it names the home it
resolved and its state directory: Home: <home> (from OPSCHAIN_HOME) — state dir <home>/agent.
This is the production daemon-side entry point. For a one-command local
development loop (embedded server, auto-enrolment, no ceremony) use the
dev subcommand; init writes a new agent repository carrying the SDK
this binary embeds, and sdk exports, versions and verifies that SDK.
The native SDK is Python; an agent written in any other language runs as a
separate process this command fronts with --target <url>:
| Subcommand | Page |
|---|---|
agent dev | opschain docs cli/agent-dev |
agent init | opschain docs cli/agent-init |
agent sdk export | opschain docs cli/agent-sdk-export |
agent sdk version | opschain docs cli/agent-sdk-version |
Flags
| Flag | Meaning |
|---|---|
--server <url> | Server base URL (global flag; default: OPSCHAIN_SERVER or the active context). A trailing / is stripped. |
--token <token> | Agent registration token — required on the first run; afterwards the saved identity in --dir is used. |
--name <name> | Agent display name (default: hostname). |
--code <code> | Per-project-unique agent code (default: the name; a code fixed on the token wins). |
--dir <path> | Agent state directory (default: <opschain home>/agent). |
--transport auto|stream|poll | Channel transport (default auto). |
--target <target> | The process declared capabilities forward to: unix:///path/agent.sock, a signed http://127.0.0.1:<port>, or an https://<host> that is signed or presented a client certificate (see Target forms). A target URL never carries credentials (user:password@). |
--target-secret-file <path> | File holding the hop secret every forwarded request is signed with. The file must be readable by its owner only (chmod 600) and hold at least 32 characters; surrounding whitespace is not part of the secret. Required for an http:// target. |
--target-insecure | Forward to a loopback http:// target unsigned. Accepted only by opschain agent dev; opschain agent refuses it. |
--target-ca-cert <path> | PEM CA bundle that verifies an https:// target (default: the system roots). |
--target-client-cert <path> | PEM client certificate presented to an https:// target. Give it with --target-client-key. |
--target-client-key <path> | PEM private key of --target-client-cert. Give it with --target-client-cert. The file must be readable by its owner only (chmod 600). |
--capability name[=json] | Declare an optional capability (repeatable), e.g. chat or x-doctor. An empty config takes the capability's default. |
--release <name>@<version> | The signed agent release this instance runs (default: $OPSCHAIN_AGENT_RELEASE); verified at enrolment and bound to the instance. A start that declares a different release than the state directory enrolled with re-enrols the instance (the restart-from-release contract) and needs --token. |
--lane <lane> | The lane this instance serves: stable (default), shadow, canary or a label (default: $OPSCHAIN_AGENT_LANE). |
--restart-mode exec|exit | How a platform-instructed restart from a release is performed: exec re-executes this process from the artifact fetched and verified against the release; exit ends the process with code 75 for the process manager or container platform to restart it on the image the release names. Default: exit inside a container, exec on a host ($OPSCHAIN_AGENT_RESTART_MODE). |
--artifact-store <dir|url> | Where exec-mode restarts fetch release artifacts by their content-addressed reference — a directory, or an http(s) base URL (GET <store>/<reference>); default $OPSCHAIN_AGENT_ARTIFACT_STORE. |
--dir defaults under the opschain home, so the global --home moves it
too.
Target forms
A declared capability with no in-process handler is forwarded to the
process --target names. Four forms are accepted. Anything else is
refused at start (exit 2):
| Form | When to use it | What protects the hop |
|---|---|---|
unix:///path/agent.sock | The process runs on the same Unix host. This is the default for a process agent dev supervises. | Only the socket's owner can connect. At start and again on every connection the runtime refuses a socket that group or others can reach, a symlink in place of the socket, a socket another user owns, and a socket in a directory group or others can write unless the directory carries the sticky bit (as /tmp does). Bind it 0600 inside a directory only you can enter. |
http://127.0.0.1:<port> with --target-secret-file | No socket is available (Windows, or a process that only listens on TCP). | Every request is signed with the hop secret. The process verifies the signature and refuses anything unsigned, wrongly signed or stale. A signed loopback port proves the runtime to the process, not who is listening on the port: wherever another local user could bind the port, prefer the socket, or https:// with a verified certificate. |
http://127.0.0.1:<port> with --target-insecure | A quick local experiment under agent dev only. | Nothing: any local process can call that port. agent dev prints a warning at start. |
https://<host> with --target-secret-file, or with --target-client-cert and --target-client-key | The process runs on another host. | TLS, verified against --target-ca-cert or the system roots. The process authenticates the runtime by the signature, the client certificate, or both. An https:// target with neither is refused (target_unsigned). |
Loopback means 127.0.0.1 (any 127.x address written in full), [::1]
or localhost. Plain http:// to any other host is refused, and so are
0.0.0.0, [::], a zone id, shorthand or numeric forms such as 127.1
or 2130706433, and localhost.. The runtime dials only loopback
addresses for an http:// target: if localhost resolves to any other
address, the connection is refused (target_plain_http_remote, a 502 to
the caller).
The signed hop
With a hop secret, every forwarded request carries:
| Header | Value |
|---|---|
X-OpsChain-Hop-Timestamp | Signing time, Unix seconds. |
X-OpsChain-Hop-Signature | Lowercase hex HMAC-SHA256 of the canonical string, keyed with the secret. |
X-OpsChain-Principal-Id | The calling user or service account id; not sent for any other caller (signed as empty). |
X-Opschain-Correlation-Id | The correlation id of the request. |
The agent is told the calling principal and the correlation id for its own records; authorisation is decided by the platform, so no tier is sent.
The canonical string is these lines, joined by a newline with no newline at the end:
OPSCHAIN-HOP-V1
<METHOD>
<request URI: path and query exactly as sent>
<timestamp>
<lowercase hex SHA-256 of the body>
x-opschain-principal-id:<value, or empty>
x-opschain-correlation-id:<value, or empty>
The platform and the runtime remove any copy of these four headers a
caller sent before the runtime signs, so a caller cannot forge one. Every
header whose name, in any case and with _ read as -, matches one of
the two context headers or starts with X-OpsChain-Hop- is removed, so a
CGI or WSGI server that folds X_OpsChain_Principal_Id into the
principal header never sees a forged copy. A caller's
X-OpsChain-Agent-Tier header is removed under any spelling too, so a
caller cannot make your process believe a tier. The principal id is the
calling user or service account id (a UUID), which the platform hands
the runtime with the exchange, never in a header; it is empty for any
other caller (a static administration token, a step token, an anonymous
call). The runtime sets the context headers on every forwarded request,
signed or not. Treat only these two context headers as set by the
platform (the platform also adds X-Opschain-Exchange-Id, the exchange's
id, which is not signed): every other X-OpsChain- header a caller sends is removed
before the request reaches your process, and any other header is the
caller's own. The request URI is signed byte for byte as it goes on the
wire. The runtime sends the query exactly as the caller gave it, with
nothing dropped or reordered, except that any byte outside printable
ASCII is percent-encoded first, so what your process receives and what
was signed are always the same ASCII bytes. In Python, verify with the shipped stub rather than writing your
own: opschain_sidecar.verify and opschain_sidecar.serve. It refuses a
timestamp more than 300 seconds from the process's clock, answers 400 to
a negative, non-numeric, conflicting or longer than 19 digits
Content-Length before it reads the body, 413 to a body larger than
8 MiB, 411 to a Transfer-Encoding body, and 401 malformed to a
timestamp that is not ASCII digits or a signature that is not ASCII; it
drops a connection that sends nothing for 30 seconds or closes before the
whole body arrives, and answers 500 with a fixed body (the traceback goes
to the process's standard error) when your handler raises. In any other
language, rebuild the canonical string above and check your code against
this test vector:
| Input | Value |
|---|---|
| Secret | 0123456789abcdef0123456789abcdef |
| Method | POST |
| Request URI | /chat?q=é (the é sent as the UTF-8 bytes C3 A9) |
| Timestamp | 1700000000 |
| Body | {"message":"hello"} (SHA-256 9b2d43affbf49a367028df2e1414f84c0e099ac98c3d54a8a80157fd7771af25) |
| Principal id | 0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a5b |
| Correlation id | 0190a1b2-c3d4-7e5f-8a9b-0c1d2e3f4a60 |
| Signature | 1e2c8795c3006ad8425d058e6cf4901d99a21916280c4270e83b8e00c13cc98e |
With the request URI /chat?lane=stable and every other input the same,
the signature is
02302a3de94cb9db7839a71ec49b75233c53751a94da8c94c4cbc4cf852e7b85.
The secret is the file's
content, or the OPSCHAIN_AGENT_HOP_SECRET value a supervised process
receives, used as given. The signed body is capped at 8 MiB; a larger
request answers 413.
Instances and the platform-instructed restart
Every opschain agent process is one instance of the agent named by
its code: a second state directory started with the same code enrols a
second instance (the server answers already_exists only when a fresh
keypair presents the instance id of a process that is still live). The
state directory remembers the instance id, its lane and the release it
enrolled with.
A release rollover (opschain admin agent rollover) drains the instance
and sends it restart_from_release over its own channel. The runtime
acknowledges, finishes what it holds, says goodbye and restarts: in
exec mode it fetches the artifact from --artifact-store by the
release's content-addressed reference, verifies its SHA-256 against the
release's digest (a reference that is not a sha256: digest refuses,
naming exit mode as the way), and re-executes itself with
OPSCHAIN_AGENT_RELEASE set; in exit mode — a container cannot
replace its own image — it exits with code 75 and the container
platform's restart policy brings it back on the image the release names.
Either way the restarted process re-enrols the same instance (same id,
same keypair) from the new release, verified at enrolment, and dispatch
resumes. The log line is platform: restart from release <name@version> instructed (<mode> mode); draining, then restarting.
Signals
SIGTERM or SIGINT drains gracefully: the agent says goodbye to the server
and the process prints stopping: saying goodbye to the server (signal again to stop hard). A second signal stops hard.
Exit codes
| Code | Meaning |
|---|---|
| 0 | Clean shutdown. |
| 1 | Registration or the channel loop failed. |
| 2 | Usage error (no server target, bad --transport, malformed --capability, bad --release or --restart-mode, or a refused --target). |
| 75 | A platform-instructed restart in exit mode: restart this process on the image the release names. |
Named errors
--transport must be auto, stream or poll--capability needs a name (name or name=<json-config>)--release is named <name>@<version>--restart-mode must be exec or exitagent target refused (<reason>): <detail>, where<reason>is one of:target_malformed: the target is not a base URL, or names no host.target_userinfo: the target URL carries userinfo (user:password@). The detail never repeats it.target_scheme_unsupported: the scheme is notunix,httporhttps.target_plain_http_remote: plainhttp://to a host other than loopback.target_unsigned: a loopbackhttp://target with neither--target-secret-filenor (agent devonly)--target-insecure, or anhttps://target with neither--target-secret-filenor a client certificate pair.target_insecure_not_dev:--target-insecureoutsideopschain agent dev.target_socket_path: the socket path is not absolute, names a symlink, or names something that is not a socket.target_socket_exposed: group or others can reach the socket.target_socket_owner: another user owns the socket.target_socket_dir_exposed: group or others can write the socket's directory and it does not carry the sticky bit, so another user could replace the socket. Keep the whole path to the socket in directories only you (or root) can change: the check covers the socket's own directory, not the directories above it.target_socket_unsupported: aunix://target on Windows.target_secret_file: the secret file is missing, unreadable or not a regular file.target_secret_file_exposed: group or others can read the secret file.target_secret_short: the secret holds fewer than 32 characters.target_tls_without_https: a--target-ca-cert,--target-client-certor--target-client-keygiven without anhttps://target.target_options_without_target: a--target-insecure,--target-secret-file,--target-ca-cert,--target-client-certor--target-client-keygiven without--target(underagent dev, also beside a supervised command, which gets its own secret).target_ca_cert: the CA bundle is unreadable or holds no PEM certificate.target_client_cert: the client certificate and key are not given together, or do not load.target_client_key_exposed: group or others can read the client key file.
A socket refusal found when a connection is made (target_socket_path,
target_socket_exposed, target_socket_owner,
target_socket_dir_exposed), and a loopback target
that resolved elsewhere (target_plain_http_remote), answer the API
caller 502 with the body agent target refused (<reason>) only; the
detail (path, mode, owner) goes to the agent's log.
All exit 2.
Examples
First run, registering with a minted token:
opschain agent --server https://opschain.example.com --token <registration-token> --name build-agent
Fronting a local process on its owner-only socket, with the chat capability:
opschain agent --server https://opschain.example.com --target unix:///run/my-agent/agent.sock --capability chat
Fronting a process that only listens on TCP, signing every request:
opschain agent --server https://opschain.example.com --target http://127.0.0.1:9000 --target-secret-file /etc/my-agent/hop.secret --capability chat
Fronting a process on another host over mutual TLS:
opschain agent --server https://opschain.example.com --target https://sidecar.internal:8443 \
--target-ca-cert /etc/my-agent/ca.pem --target-client-cert /etc/my-agent/client.pem --target-client-key /etc/my-agent/client.key
See also
opschain docs cli/agent-dev— the local development loop.opschain docs cli/runner— the work-executing daemon (runners run steps; agents serve capabilities).