---
title: "First run"
description: "What a Dex Agents CLI command asks and writes on its first run, how it finds or starts the local Dex server, and how it connects to a remote Dex server."
canonical_url: "https://docs.superdurable.io/zh-Hans/reference/agents/cli-first-run/"
source: "docs/reference/agents/cli-first-run.mdx"
locale: "en"
site_locale: "zh-Hans"
---

> 供 AI Agent 使用的全部页面索引：https://docs.superdurable.io/zh-Hans/llms.txt

> 本页内容是英文原文，中文译文还在准备中。

<a id="first-run"></a>

# First run

Without a configuration file, the command asks the **init** questions and
writes **\~/.dex/agents/HARNESS.yaml**, such as
**\~/.dex/agents/claude-sdk.yaml**:

- the model, with the command's default;
- the system prompt, empty for the harness's default instructions;
- the connectors to enable, comma-separated, empty for every authorized
  connection;
- the workspace, **\~/dex-agent-workspace** by default, or for
  **dex-agents-pi** the directory the command starts in;
- whether to allow the file tools, shell commands, web fetching, and the
  browser;
- when shell commands are allowed, the commands that run without approval.

**--yes** writes and uses the defaults without asking. Without a terminal, the
command runs on the defaults, writes nothing, and says to run **init**.

Run **init** again at any time to update the file. It replaces only the fields
it asks about: **model**, **system_prompt**, **connectors.enabled**,
**workspace**, the switches of **local_tools**, and
**local_tools.shell.allow**. It keeps every other field
of a valid file as written, such as **dex.server**, **dex.auth**,
**dex.tls**, **connectors.mcp**, **mcp_servers**, **approval**, **media**,
**sessions**, and the harness sections. At a terminal, each question offers
the file's current value as its default. **--yes** takes those same
defaults, so it keeps the file's values; without a file, or after replacing
an invalid one, it takes the command's defaults.

- A new system prompt removes **system_prompt_file**. An empty answer keeps
  either field.
- **local_tools.shell.allow** stays unless you enter a new list when shell
  commands are allowed.
- A section missing from the file gets its default.
- **init** writes the file again from its values, so comments in the old file
  are not kept.
- **init** writes a temporary file beside the configuration and renames it
  over the old one. A crash or a full disk leaves the old file or the new one,
  never a partial file. When the configuration path is a symbolic link,
  **init** writes the file it points at and keeps the link. A new file is
  readable only by its owner; an existing file keeps its permissions.
- A file that is not YAML or fails the configuration rules is replaced as if
  there were none. The old file is copied to **HARNESS.yaml.bak**, which
  replaces an earlier copy, and **init** prints one line that names it. A
  symbolic link at that path is replaced by the copy; the file it pointed at
  stays unchanged.
- A file that exists but cannot be read stops **init** with an error and stays
  unchanged.

Changed in 0.6.9: **init** keeps the fields it does not ask about. Before, it
wrote the whole file from its answers, so a configured Dex server and the
other settings were lost.

```text
dex-agents-NAME [run|init|connectors|sessions] [options]

  run          Chat with the agent in this terminal (the default).
  init         Create or replace the configuration file.
  connectors   List the connector tools and the Dex Web connections.
  sessions     List this command's sessions.

  -c, --config <file>   Configuration file (default ~/.dex/agents/NAME.yaml).
  -y, --yes             Write and use the defaults instead of asking.
  -r, --resume <id>     Continue a session.
  -m, --model <id>      Override the configured model.
      --approval-mode <m>   Start new sessions in ask, low_risk, or all.
      --plain           Chat in plain text, without the terminal UI.
  -h, --help            Show this help.
```

The exit code is 0 on success. It is 1 for a failure at run time, such as a
Dex server that does not start, an explicit **connectors.mcp** URL that does
not answer, or a turn or session that failed during the chat. It is 2 for a
usage or configuration error. After SIGTERM or SIGHUP, it is 128 plus the
signal number.

Ctrl-C at an **init** question, or at a first-run question, stops the command
with exit code 130. Ctrl-D there stops it with exit code 1. Either way the
command prints **Cancelled; no configuration written.** and writes no file.

<a id="dex-server"></a>

## The local Dex server

With **dex.server: auto**, the default, the command uses a **dexcli dev** on
this machine. It looks at 127.0.0.1:8801 for Dex and 127.0.0.1:8802 for Dex
Web:

- When nothing answers on the Dex Web port, the command starts **dexcli dev**
  in the background on those ports. Its output goes to
  **\~/.dex/agents/dexcli-dev.log**, and its process and ports to
  **\~/.dex/agents/dexcli-dev.json**.
- When a **dexcli dev** answers, the command reuses it. It checks the Dex Web
  readiness answer and the Dex port first. While Dex Web reports that Dex is
  starting, it waits up to a minute.
- When anything else answers, the command stops with exit code 1.

Changed in 0.6.0: a **dexcli dev** that the command started stops when the
command exits, and Dex Web stops with it. This happens on **/exit**, at the end
of input, on the Ctrl-C that exits, on a failure, and on SIGTERM or SIGHUP. The
command stops the whole process group of **dexcli dev**, which includes
**dex-server-zd**. It removes **\~/.dex/agents/dexcli-dev.json**, sends
SIGTERM, and sends SIGKILL after 20 seconds. Another SIGTERM, SIGHUP, or
Ctrl-C during those 20 seconds sends SIGKILL at once. The command then prints
"Stopped the dexcli dev this command started." It signals the group only
while **dexcli dev** itself still runs. If **dexcli dev** exits but its process
group seems to have members left, the command says that processes may still
run and how to stop them. On Windows, the TypeScript commands stop the process
tree with **taskkill /T**, and add **/F** after 20 seconds. The Python commands
first send **dexcli dev** CTRL_BREAK_EVENT, and then use **taskkill /T /F**.
Before 0.6.0, the started **dexcli dev** kept running after the command
exited.

A **dexcli dev** that the command reused keeps running. This includes one you
started yourself and one that another command started. Sessions and their
state stay in **\~/.dex/zd/\<Dex port>**, so a later command that starts
**dexcli dev** again can **--resume** them.

To share one Dex server across commands, run **dexcli dev** yourself before
you start them, or set **dex.server** to a URL. Two commands that both use
**auto** share the **dexcli dev** that the first one started. It stops when
the first command exits. The second command then fails with exit code 1 and a
short error that names the stopped **dexcli dev**. In the TypeScript commands,
the Worker's reconnect messages, such as for a lost Dex server, go only to the
log file **\~/.dex/agents/HARNESS.log**. Other library warnings show their first
line on the terminal and their full text in that file.

A command killed with SIGKILL, or one that crashes, cannot stop the
**dexcli dev** it started. That **dexcli dev** keeps running, and its record in
**\~/.dex/agents/dexcli-dev.json** stays. A later command reuses it like any
other running **dexcli dev** and never stops it. Stop it with **kill** and the
pid in the record.

These checks tell a **dexcli dev** from an unrelated service. They do not
authenticate it: another local user's process that binds the ports first and
answers the same way is trusted. Run on a machine you do not share, or set
**dex.server** and **connectors.mcp** to URLs you trust.

The command finds **dexcli** in this order: **DEXCLI**; the copy it downloaded
earlier to **\~/.dex/bin**, checked against its recorded SHA-256; the first
**dexcli** on PATH of at least the pinned version. Otherwise it downloads the
pinned **dexcli** release for the platform from GitHub, checks it against the
release's checksums, and extracts only **dexcli** and **dex-server-zd**. A
downloaded copy that changed later stops the command; delete its directory to
download it again. The same **dexcli** draws the [terminal UI](https://docs.superdurable.io/zh-Hans/reference/agents/cli-chat.md#terminal-ui),
which needs **dexcli** 1.46.0 or later. A **DEXCLI** that points at an older
**dexcli** keeps the plain chat. Changed in 0.6.19: the command looks for
`dexcli` 1.48.2 or later on `PATH` and downloads 1.48.2 when needed. That
release also draws the terminal UI with encryption; see
[Payload encryption](https://docs.superdurable.io/zh-Hans/reference/agents/cli-payload-encryption.md#payload-encryption-effects).

| Environment variable                             | Meaning                                                                                                                                                                                                               |
| ------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **DEXCLI**                                       | The **dexcli** executable to run. A value that is not an executable file stops the command with an error that says to unset **DEXCLI** or point it at **dexcli**. Changed in 0.6.0: the Python commands check it too. |
| **DEX_AGENTS_DEX_PORT**, **DEX_AGENTS_WEB_PORT** | The ports of the **dexcli dev** that **auto** reuses or starts.                                                                                                                                                       |
| **DEX_AGENTS_DEXCLI_DOWNLOAD_URL**               | The base URL of **dexcli** release downloads, such as a mirror. It must be https, or http on 127.0.0.1, localhost, or \[::1].                                                                                         |
| **DEX_AGENTS_OPEN_BROWSER**                      | **0** or **false** keeps the command from opening a web browser, such as the Dex Web Connectors page at a terminal. Since 0.6.0.                                                                                      |

Any other **dex.server** value is the URL of a Dex server. The command
connects to it and starts nothing. See [dexcli dev](https://docs.superdurable.io/zh-Hans/reference/cli.md#start-locally)
for the local server itself, and
[Run against a remote Dex server](https://docs.superdurable.io/zh-Hans/reference/agents/cli-first-run.md#remote-server) for a server that requires
TLS or a credential.

<a id="remote-server"></a>

## Run against a remote Dex server

Since 0.6.2. A command can keep its sessions on a shared Dex server that
requires TLS and a credential. Set **dex.server** to the server's URL and add
**dex.auth**:

```yaml
dex:
  server: https://dex.example.com
  auth:
    mode: api_key
    token_url: https://auth.example.com/oauth/token
    server_id: dex-prod
  tls:
    ca_file: ~/certs/dex-ca.pem
connectors:
  enabled: []
```

```bash
export DEX_API_KEY=...   # never in the configuration file
dex-agents-claude-sdk
```

The Worker, every Client of the command, and the [terminal UI](https://docs.superdurable.io/zh-Hans/reference/agents/cli-chat.md#terminal-ui)
use these settings. For a server that Dex Cloud runs, see
[Connect Workers and Clients](https://docs.superdurable.io/zh-Hans/production-operations/use-dex-cloud.md#connect).

| **dex.auth.mode**                | The secret              | The other settings                                                                                                                                    |
| -------------------------------- | ----------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- |
| **none**, the default            | None                    | None                                                                                                                                                  |
| **api_key**                      | **DEX_API_KEY**         | **token_url** and **server_id**, both required. The command exchanges the key at **token_url** for short-lived access tokens scoped to **server_id**. |
| **bearer** with **source: env**  | **DEX_CREDENTIAL**      | None                                                                                                                                                  |
| **bearer** with **source: file** | The content of **file** | **file**, required. The command reads the file again for each call, so a token that another process rotates needs no restart.                         |

The secret comes only from the environment. The schema rejects
**api_key**, **token**, **credential**, and **access_token** under
**dex.auth**.

**dex.auth** alone turns authentication on. Without it, or with
**mode: none**, the command reads none of **DEX_API_KEY**, **DEX_TOKEN_URL**,
**DEX_SERVER_ID**, **DEX_CREDENTIAL**, and **DEX_CREDENTIAL_FILE**. So these
variables, exported for **dexcli**, never change a configuration that does not
ask for them. Any mode other than **none** needs a **dex.server** URL: the
local **dexcli dev** takes no credential, and **auto** with **dex.auth** stops
the command with **auth_needs_server_url**.

Environment variables use the names that [dexcli](https://docs.superdurable.io/zh-Hans/reference/cli.md#authentication)
uses. Each one is trimmed, and one that is empty after trimming is not set. A
variable that is set overrides the matching field of the file:

| Variable                | Overrides                                                     |
| ----------------------- | ------------------------------------------------------------- |
| **DEX_TOKEN_URL**       | **dex.auth.token_url**, with **mode: api_key**                |
| **DEX_SERVER_ID**       | **dex.auth.server_id**, with **mode: api_key**                |
| **DEX_CREDENTIAL_FILE** | **dex.auth.file**, with **mode: bearer** and **source: file** |
| **DEX_TLS_CA_FILE**     | **dex.tls.ca_file**, with an **https** **dex.server**         |
| **DEX_TLS_SERVER_NAME** | **dex.tls.server_name**, with an **https** **dex.server**     |

The token URL must be **https**, or **http** on **localhost**, **127.0.0.0/8**,
or **\[::1]**. The command rejects any other with **insecure_token_url**. A
credential may also go to an **http** **dex.server**; it then crosses the
network in clear text, so use one only on a trusted network.

An **https** **dex.server** turns TLS on. The command verifies the server
against the default trust store and the URL's host. **dex.tls.ca_file** adds
the root CAs of a PEM file, such as a private CA. **dex.tls.server_name** sets
the name the certificate must match, when it differs from the host. Both need
an **https** **dex.server**; otherwise the command stops with
**tls_needs_https_server**. The URL scheme decides TLS, so the command never
reads **DEX_TLS** or **DEX_FLOW_SERVICE_ADDRESS**.

In the file, a leading **\~** in **dex.auth.file** and **dex.tls.ca_file** is
the home directory, and a relative path is relative to the file's directory.
In a variable, a relative path is relative to the working directory.

A missing or invalid setting stops the command at startup with exit code 2. The
message names the setting, such as
**DEX_API_KEY is not set; dex.auth mode api_key needs it.** A rejected
credential stops the command with exit code 1 and one line that says which part
refused it:

- the token endpoint did not exchange **DEX_API_KEY**: the line starts with
  **could not exchange DEX_API_KEY for an access token** and names the token
  URL and the HTTP status;
- the server answered **UNAUTHENTICATED** or **PERMISSION_DENIED**: the line
  names the server, the credential, and the status code.

Messages name settings and variables, never credential values. The token URL
they show is a value of your own configuration, not a secret. They leave out
the error text that the token endpoint or the server sent, which may repeat the
credential.

The command keeps **DEX_API_KEY**, **DEX_CREDENTIAL**, and
**DEX_CREDENTIAL_FILE** to itself, whether or not **dex.auth** uses them.
Shell commands, the browser, downloads, and **dexcli dev** never see them, so
an agent cannot read them with a tool. Only the command's own Worker process
and the terminal UI get the one variable that **dex.auth** needs.

With **dex.server** set to a URL, **connectors.mcp: auto** serves the
Connector tools authorized on this machine; see [Connectors](https://docs.superdurable.io/zh-Hans/reference/agents/cli-tools.md#connectors).
**sessions.summary: auto** is off. Sessions on a shared server resume only
from the host, user, configuration file, and workspace that started them; see
[Resume a session](https://docs.superdurable.io/zh-Hans/reference/agents/cli-chat.md#resume). See
[Authentication](https://docs.superdurable.io/zh-Hans/reference/server/authentication.md) for how the server checks
credentials.
